Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions frontend/app/users/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { beforeEach, describe, expect, test, vi } from "vitest"

vi.mock("next/cache", () => ({
revalidatePath: vi.fn(),
}))

vi.mock("@/lib/users/api", () => ({
updateUserSettings: vi.fn(),
}))

import { revalidatePath } from "next/cache"
import { updateUserSettings } from "@/lib/users/api"
import { updateUserSettingsAction } from "./actions"

describe("updateUserSettingsAction", () => {
beforeEach(() => {
vi.clearAllMocks()
})

test("returns data and revalidates on success", async () => {
vi.mocked(updateUserSettings).mockResolvedValueOnce({ ok: true, data: undefined })

const result = await updateUserSettingsAction({ shareNotificationEnabled: true })

expect(result).toEqual({ data: {} })
expect(revalidatePath).toHaveBeenCalledWith("/users/settings")
})

test("returns error without revalidating on failure", async () => {
vi.mocked(updateUserSettings).mockResolvedValueOnce({
ok: false,
message: "shareNotificationEnabled is required.",
code: "bad_request",
})

const result = await updateUserSettingsAction({ shareNotificationEnabled: true })

expect(result).toEqual({ error: "shareNotificationEnabled is required." })
expect(revalidatePath).not.toHaveBeenCalled()
})
})
10 changes: 10 additions & 0 deletions frontend/app/users/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server"

import { revalidatePath } from "next/cache"
import {
createUserFolder,
deleteUserFolder,
Expand All @@ -10,6 +11,7 @@ import {
toggleUserFavorite,
updateUserFavoriteFolderAssignment,
updateUserFolder,
updateUserSettings,
} from "@/lib/users/api"
import type {
CreateUserFavoriteFolderRequest,
Expand All @@ -18,6 +20,7 @@ import type {
ToggleUserFavoriteRequest,
UpdateUserFavoriteFolderAssignmentRequest,
UpdateUserFavoriteFolderRequest,
UpdateUserSettingsRequest,
} from "@/lib/users/types"

export async function createUserFolderAction(
Expand Down Expand Up @@ -84,3 +87,10 @@ export async function removeUserSharedObjectAction(id: number) {
export async function toggleAdminModeAction() {
return toggleAdminMode()
}

export async function updateUserSettingsAction(body: UpdateUserSettingsRequest) {
const result = await updateUserSettings(body)
if (!result.ok) return { error: result.message }
revalidatePath("/users/settings")
return { data: {} }
}
133 changes: 132 additions & 1 deletion frontend/lib/users/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,34 @@ vi.mock("@/lib/api-base", () => ({
getServerApiBase: vi.fn(() => "https://api.example.test"),
}))

import { createUserFolder } from "./api"
vi.mock("@/lib/http", () => ({
apiFetchJson: vi.fn(),
}))

import { getServerApiBase } from "@/lib/api-base"
import { getToken } from "@/lib/auth"
import { apiFetchJson } from "@/lib/http"
import { createUserFolder, getUserSettings, updateUserSettings } from "./api"

const BASE = "https://api.example.test"

function mockFetch(status: number, body: unknown = {}) {
global.fetch = vi.fn(async () => {
if (status === 204) {
return new Response(null, { status: 204 })
}
return new Response(typeof body === "string" ? body : JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json" },
})
}) as typeof fetch
}

describe("createUserFolder", () => {
beforeEach(() => {
vi.restoreAllMocks()
vi.mocked(getToken).mockResolvedValue("test-token")
vi.mocked(getServerApiBase).mockReturnValue(BASE)
global.fetch = vi.fn(
async () =>
new Response(JSON.stringify({ id: 1, name: "Favorites" }), {
Expand Down Expand Up @@ -40,3 +63,111 @@ describe("createUserFolder", () => {
)
})
})

describe("user settings", () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(getToken).mockResolvedValue("test-token")
vi.mocked(getServerApiBase).mockReturnValue(BASE)
})

describe("getUserSettings", () => {
test("returns default-enabled state from GET", async () => {
vi.mocked(apiFetchJson).mockResolvedValueOnce({
ok: true,
data: { shareNotificationEnabled: true },
})

const result = await getUserSettings()

expect(result).toEqual({
data: { shareNotificationEnabled: true },
error: null,
})
expect(apiFetchJson).toHaveBeenCalledWith(`${BASE}/api/users/me/settings`, {
headers: { Authorization: "Bearer test-token" },
cache: "no-store",
})
})

test("returns disabled state from GET", async () => {
vi.mocked(apiFetchJson).mockResolvedValueOnce({
ok: true,
data: { shareNotificationEnabled: false },
})

const result = await getUserSettings()

expect(result.data?.shareNotificationEnabled).toBe(false)
})

test("returns auth_required when token is missing", async () => {
vi.mocked(getToken).mockResolvedValue(null)

const result = await getUserSettings()

expect(result).toEqual({ data: null, error: "auth_required" })
expect(apiFetchJson).not.toHaveBeenCalled()
})
})

describe("updateUserSettings", () => {
test("enables share notifications via PUT", async () => {
mockFetch(204)

const result = await updateUserSettings({ shareNotificationEnabled: true })

expect(result).toEqual({ ok: true, data: undefined })
expect(global.fetch).toHaveBeenCalledWith(
`${BASE}/api/users/me/settings`,
expect.objectContaining({
method: "PUT",
body: JSON.stringify({ shareNotificationEnabled: true }),
headers: expect.objectContaining({
Authorization: "Bearer test-token",
"Content-Type": "application/json",
}),
}),
)
})

test("disables share notifications via PUT", async () => {
mockFetch(204)

const result = await updateUserSettings({ shareNotificationEnabled: false })

expect(result.ok).toBe(true)
expect(global.fetch).toHaveBeenCalledWith(
`${BASE}/api/users/me/settings`,
expect.objectContaining({
body: JSON.stringify({ shareNotificationEnabled: false }),
}),
)
})

test("returns auth_required when token is missing", async () => {
vi.mocked(getToken).mockResolvedValue(null)

const result = await updateUserSettings({ shareNotificationEnabled: true })

expect(result).toEqual({
ok: false,
message: expect.stringMatching(/sign in/i),
code: "auth_required",
})
expect(global.fetch).not.toHaveBeenCalled()
})

test("surfaces invalid payload errors from PUT", async () => {
mockFetch(400, { error: "shareNotificationEnabled is required." })

const result = await updateUserSettings({ shareNotificationEnabled: true })

expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.message).toBe("shareNotificationEnabled is required.")
expect(result.code).toBe("bad_request")
}
})
})
})
13 changes: 13 additions & 0 deletions frontend/lib/users/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import type {
ToggleUserFavoriteResponse,
UpdateUserFavoriteFolderAssignmentRequest,
UpdateUserFavoriteFolderRequest,
UpdateUserSettingsRequest,
UserFavoriteFolder,
UserGroup,
UserHistorySection,
UserPage,
UserSearchHistoryItem,
UserSettings,
UserSharedObjects,
UserStars,
UserSubscription,
Expand Down Expand Up @@ -243,3 +245,14 @@ export function toggleAdminMode() {
method: "POST",
})
}

export function getUserSettings() {
return authorizedGet<UserSettings>("/api/users/me/settings")
}

export function updateUserSettings(body: UpdateUserSettingsRequest) {
return authorizedMutation<void>("/api/users/me/settings", {
method: "PUT",
body: JSON.stringify(body),
})
}
8 changes: 8 additions & 0 deletions frontend/lib/users/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,11 @@ export type ToggleUserFavoriteResponse = {
export type ToggleAdminModeResponse = {
adminEnabled?: string | null
}

export type UserSettings = {
shareNotificationEnabled: boolean
}

export type UpdateUserSettingsRequest = {
shareNotificationEnabled: boolean
}
Loading