forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

users.test.ts282 lines, 9.7 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getMe } from "../../users";
3import { patchMe } from "../../users";
4import { getUsers } from "../../users";
5import { getUserById } from "../../users";
6import { getUserSearch } from "../../users";
7import { patchUserProfile } from "../../users";
8import { patchUserRole } from "../../users";
9import { patchMyPreferences } from "../../users";
10import { patchUserPreferences } from "../../users";
11import { getMyProfile } from "../../users";
12import { getUserProfile } from "../../users";
13import { postMyAvatar } from "../../users";
14import { deleteMyAvatar } from "../../users";
15import { deleteUserAvatar } from "../../users";
16import { getUserAvatarUrl } from "../../users";
17import { postUser } from "../../users";
18import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
19
20describe("users API", () => {
21 afterEach(() => {
22 vi.restoreAllMocks();
23 });
24
25 it("getMe calls /users/me", async () => {
26 const mockUser = { id: "u1", username: "test" };
27 mockFetchSuccess(mockUser);
28
29 const result = await getMe();
30 expect(result).toEqual(mockUser);
31
32 const [url, init] = lastFetchCall();
33 expect(url).toBe("/api/auth/me");
34 expect(init?.method).toBe("GET");
35 });
36
37 it("patchMe calls /users/me with updates", async () => {
38 const mockUser = { id: "u1", username: "test" };
39 mockFetchSuccess(mockUser);
40
41 const updates = { name: "New Name" };
42 const result = await patchMe(updates);
43 expect(result).toEqual(mockUser);
44
45 const [url, init] = lastFetchCall();
46 expect(url).toBe("/api/users/me");
47 expect(init?.method).toBe("PATCH");
48 expect(init?.body).toBe(JSON.stringify(updates));
49 });
50
51 it("getUsers calls /users with pagination", async () => {
52 const mockPage = { items: [{ id: "u1" }], total: 1 };
53 mockFetchSuccess(mockPage);
54
55 const result = await getUsers({ limit: 10, offset: 20 });
56 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); // Normalized
57
58 const [url, init] = lastFetchCall();
59 expect(url).toBe("/api/users?limit=10&offset=20");
60 expect(init?.method).toBe("GET");
61 });
62
63 it("getUsers comma-joins multiple roles (manager tab = manager+admin)", async () => {
64 mockFetchSuccess({ items: [], total: 0 });
65
66 await getUsers({ roles: ["manager", "admin"], limit: 200 });
67
68 const [url] = lastFetchCall();
69 expect(url).toBe("/api/users?limit=200&roles=manager%2Cadmin");
70 });
71
72 it("getUsers sends a single role unchanged", async () => {
73 mockFetchSuccess({ items: [], total: 0 });
74
75 await getUsers({ roles: ["teacher"] });
76
77 const [url] = lastFetchCall();
78 expect(url).toBe("/api/users?roles=teacher");
79 });
80
81 it("getUsers omits roles when the array is empty", async () => {
82 mockFetchSuccess({ items: [], total: 0 });
83
84 await getUsers({ roles: [], limit: 5 });
85
86 const [url] = lastFetchCall();
87 expect(url).toBe("/api/users?limit=5");
88 });
89
90 it("getUserSearch calls /users/search with query, role, and pagination", async () => {
91 const mockPage = { items: [{ id: "u1" }], total: 1 };
92 mockFetchSuccess(mockPage);
93
94 const result = await getUserSearch("john", undefined, "student", { limit: 5 });
95 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); // Normalized
96
97 const [url, init] = lastFetchCall();
98 expect(url).toBe("/api/users/search?q=john&role=student&limit=5");
99 expect(init?.method).toBe("GET");
100 });
101
102 it("getUserById calls /users/:id", async () => {
103 const mockUser = { id: "u1", username: "test" };
104 mockFetchSuccess(mockUser);
105
106 const result = await getUserById("u1");
107 expect(result).toEqual(mockUser);
108
109 const [url, init] = lastFetchCall();
110 expect(url).toBe("/api/users/u1");
111 expect(init?.method).toBe("GET");
112 });
113
114 it("patchUserProfile calls /users/:id/profile", async () => {
115 const mockUser = { id: "u1", name: "Updated" };
116 mockFetchSuccess(mockUser);
117
118 const updates = { name: "Updated" };
119 const result = await patchUserProfile("u1", updates);
120 expect(result).toEqual(mockUser);
121
122 const [url, init] = lastFetchCall();
123 expect(url).toBe("/api/users/u1/profile");
124 expect(init?.method).toBe("PATCH");
125 expect(init?.body).toBe(JSON.stringify(updates));
126 });
127
128 it("patchUserRole calls /users/:id/role", async () => {
129 const mockUser = { id: "u1", role: "teacher" };
130 mockFetchSuccess(mockUser);
131
132 const result = await patchUserRole("u1", "teacher");
133 expect(result).toEqual(mockUser);
134
135 const [url, init] = lastFetchCall();
136 expect(url).toBe("/api/users/u1/role");
137 expect(init?.method).toBe("PATCH");
138 expect(init?.body).toBe(JSON.stringify({ role: "teacher" }));
139 });
140
141 it("patchMyPreferences calls /users/me/preferences", async () => {
142 const mockUser = { id: "u1", theme: "dark" };
143 mockFetchSuccess(mockUser);
144
145 const updates = { theme: "dark" as const };
146 const result = await patchMyPreferences(updates);
147 expect(result).toEqual(mockUser);
148
149 const [url, init] = lastFetchCall();
150 expect(url).toBe("/api/users/me/preferences");
151 expect(init?.method).toBe("PATCH");
152 expect(init?.body).toBe(JSON.stringify(updates));
153 });
154
155 it("patchMyPreferences sends palette_color to set the accent", async () => {
156 const mockUser = { id: "u1", palette_color: "#fefae0" };
157 mockFetchSuccess(mockUser);
158
159 const updates = { palette_color: "#fefae0" };
160 await patchMyPreferences(updates);
161
162 const [url, init] = lastFetchCall();
163 expect(url).toBe("/api/users/me/preferences");
164 expect(init?.method).toBe("PATCH");
165 expect(init?.body).toBe(JSON.stringify(updates));
166 });
167
168 it("patchMyPreferences sends empty palette_color to clear the accent", async () => {
169 const mockUser = { id: "u1", palette_color: null };
170 mockFetchSuccess(mockUser);
171
172 const updates = { palette_color: "" };
173 await patchMyPreferences(updates);
174
175 const [, init] = lastFetchCall();
176 expect(init?.body).toBe(JSON.stringify({ palette_color: "" }));
177 });
178
179 it("patchUserPreferences calls /users/:id/preferences", async () => {
180 const mockUser = { id: "u1", language: "tr" };
181 mockFetchSuccess(mockUser);
182
183 const updates = { language: "tr" as const };
184 const result = await patchUserPreferences("u1", updates);
185 expect(result).toEqual(mockUser);
186
187 const [url, init] = lastFetchCall();
188 expect(url).toBe("/api/users/u1/preferences");
189 expect(init?.method).toBe("PATCH");
190 expect(init?.body).toBe(JSON.stringify(updates));
191 });
192
193 it("getMyProfile calls /users/me/profile", async () => {
194 const profile = { id: "u1", username: "test", badges: [] };
195 mockFetchSuccess(profile);
196
197 const result = await getMyProfile();
198 expect(result).toEqual(profile);
199 const [url] = lastFetchCall();
200 expect(url).toBe("/api/users/me/profile");
201 });
202
203 it("getUserProfile calls /users/:id/profile", async () => {
204 mockFetchSuccess({ id: "u2", username: "other", badges: [] });
205 await getUserProfile("u2");
206 const [url] = lastFetchCall();
207 expect(url).toBe("/api/users/u2/profile");
208 });
209
210 it("postMyAvatar POSTs the file as multipart under the `file` field", async () => {
211 mockFetchSuccess({ content_type: "image/png", size: 1024 }, 201);
212
213 const file = new File(["x"], "me.png", { type: "image/png" });
214 const result = await postMyAvatar(file);
215 expect(result).toEqual({ content_type: "image/png", size: 1024 });
216
217 const [url, init] = lastFetchCall();
218 expect(url).toBe("/api/users/me/avatar");
219 expect(init?.method).toBe("POST");
220 expect(init?.body).toBeInstanceOf(FormData);
221 expect((init?.body as FormData).get("file")).toBe(file);
222 });
223
224 it("deleteMyAvatar calls DELETE /users/me/avatar", async () => {
225 mockFetch204();
226 await deleteMyAvatar();
227 const [url, init] = lastFetchCall();
228 expect(url).toBe("/api/users/me/avatar");
229 expect(init?.method).toBe("DELETE");
230 });
231
232 it("deleteUserAvatar calls DELETE /users/:id/avatar", async () => {
233 mockFetch204();
234 await deleteUserAvatar("u2");
235 const [url, init] = lastFetchCall();
236 expect(url).toBe("/api/users/u2/avatar");
237 expect(init?.method).toBe("DELETE");
238 });
239
240 it("getUserAvatarUrl builds a same-origin URL, with the cache-buster only when versioned", () => {
241 expect(getUserAvatarUrl("u1")).toBe("/api/users/u1/avatar");
242 expect(getUserAvatarUrl("u1", 3)).toBe("/api/users/u1/avatar?v=3");
243 expect(getUserAvatarUrl("u/1")).toBe("/api/users/u%2F1/avatar");
244 });
245 it("postUser posts the new account to /users", async () => {
246 const mockUser = { id: "u9", username: "ayse", role: "teacher" };
247 mockFetchSuccess(mockUser);
248
249 const body = { username: "ayse", password: "secret123", role: "teacher" as const };
250 const result = await postUser(body);
251 expect(result).toEqual(mockUser);
252
253 const [url, init] = lastFetchCall();
254 expect(url).toBe("/api/users");
255 expect(init?.method).toBe("POST");
256 expect(JSON.parse(init?.body as string)).toEqual(body);
257 });
258
259 it("postUser carries the school-issued student number", async () => {
260 mockFetchSuccess({ id: "u9", username: "ayse", role: "student", student_number: "1234" });
261
262 const body = { username: "ayse", password: "secret123", role: "student" as const, student_number: "1234" };
263 await postUser(body);
264
265 const [url, init] = lastFetchCall();
266 expect(url).toBe("/api/users");
267 expect(init?.method).toBe("POST");
268 expect(JSON.parse(init?.body as string)).toEqual(body);
269 });
270
271 it("patchUserProfile sends an empty student number to clear it", async () => {
272 mockFetchSuccess({ id: "u1", student_number: null });
273
274 const updates = { student_number: "" };
275 await patchUserProfile("u1", updates);
276
277 const [url, init] = lastFetchCall();
278 expect(url).toBe("/api/users/u1/profile");
279 expect(init?.method).toBe("PATCH");
280 expect(JSON.parse(init?.body as string)).toEqual(updates);
281 });
282});