-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathuser-settings-api.test.ts
More file actions
273 lines (242 loc) · 9.26 KB
/
Copy pathuser-settings-api.test.ts
File metadata and controls
273 lines (242 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { describe, it, expect, vi, beforeEach } from "vitest";
import { GET, PATCH } from "@/app/api/user/settings/route";
import { NextRequest } from "next/server";
import { getServerSession } from "next-auth";
import { resolveAppUser } from "@/lib/resolve-user";
// Mock next-auth
vi.mock("next-auth", () => ({
getServerSession: vi.fn(),
}));
// Mock resolve-user
vi.mock("@/lib/resolve-user", () => ({
resolveAppUser: vi.fn(),
}));
// Mock crypto
vi.mock("@/lib/crypto", () => ({
encryptToken: vi.fn().mockReturnValue({ encrypted: "encrypted-val", iv: "iv-val" }),
}));
// Mock Supabase admin client methods
const mockSingle = vi.fn();
const mockEq = vi.fn().mockReturnValue({ single: mockSingle });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
const mockUpdate = vi.fn();
const mockFrom = vi.fn().mockImplementation((table: string) => {
return {
select: mockSelect,
update: mockUpdate,
};
});
vi.mock("@/lib/supabase", () => ({
supabaseAdmin: {
from: (table: string) => mockFrom(table),
},
}));
describe("User Settings API Endpoints", () => {
beforeEach(() => {
vi.clearAllMocks();
// Default mock data returned from database select
mockSingle.mockResolvedValue({
data: {
id: "user-uuid-123",
github_login: "test-user",
is_public: true,
leaderboard_opt_in: true,
pinned_repos: ["repo-1"],
wakatime_api_key_encrypted: "encrypted-key",
wakatime_api_key_iv: "iv",
weekly_digest_opt_in: false,
discord_webhook_url: null,
timezone: "UTC",
},
error: null,
});
// Default select/eq chain
mockSelect.mockReturnValue({
eq: mockEq.mockReturnValue({
single: mockSingle,
}),
});
// Default mock implementation for database updates
mockUpdate.mockImplementation((updatesObj: any) => {
return {
eq: vi.fn().mockReturnValue({
select: vi.fn().mockReturnValue({
single: vi.fn().mockResolvedValue({
data: {
id: "user-uuid-123",
github_login: "test-user",
is_public: updatesObj.is_public !== undefined ? updatesObj.is_public : true,
leaderboard_opt_in: updatesObj.leaderboard_opt_in !== undefined ? updatesObj.leaderboard_opt_in : true,
pinned_repos: updatesObj.pinned_repos !== undefined ? updatesObj.pinned_repos : ["repo-1"],
wakatime_api_key_encrypted: updatesObj.wakatime_api_key_encrypted !== undefined ? updatesObj.wakatime_api_key_encrypted : "encrypted-key",
wakatime_api_key_iv: updatesObj.wakatime_api_key_iv !== undefined ? updatesObj.wakatime_api_key_iv : "iv",
weekly_digest_opt_in: updatesObj.weekly_digest_opt_in !== undefined ? updatesObj.weekly_digest_opt_in : false,
discord_webhook_url: updatesObj.discord_webhook_url !== undefined ? updatesObj.discord_webhook_url : null,
timezone: updatesObj.timezone !== undefined ? updatesObj.timezone : "UTC",
},
error: null,
}),
}),
}),
};
});
// Default session and user resolution
(getServerSession as any).mockResolvedValue({
githubId: "12345",
githubLogin: "test-user",
});
(resolveAppUser as any).mockResolvedValue({
id: "user-uuid-123",
github_id: "12345",
github_login: "test-user",
});
});
describe("GET /api/user/settings", () => {
it("returns 401 when user is not authenticated", async () => {
(getServerSession as any).mockResolvedValue(null);
const req = new NextRequest("http://localhost/api/user/settings");
const res = await GET(req);
expect(res.status).toBe(401);
expect(await res.json()).toEqual({ error: "Unauthorized" });
});
it("returns 500 when resolving user fails", async () => {
(resolveAppUser as any).mockResolvedValue(null);
const req = new NextRequest("http://localhost/api/user/settings");
const res = await GET(req);
expect(res.status).toBe(500);
expect(await res.json()).toEqual({ error: "Failed to fetch user settings" });
});
it("returns 500 when fetching user settings from database fails", async () => {
mockSingle.mockResolvedValue({ data: null, error: { message: "DB Error" } });
const req = new NextRequest("http://localhost/api/user/settings");
const res = await GET(req);
expect(res.status).toBe(500);
expect(await res.json()).toEqual({ error: "Failed to fetch user settings" });
});
it("successfully retrieves user settings", async () => {
const req = new NextRequest("http://localhost/api/user/settings");
const res = await GET(req);
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
id: "user-uuid-123",
github_login: "test-user",
is_public: true,
leaderboard_opt_in: true,
weekly_digest_opt_in: false,
pinned_repos: ["repo-1"],
has_wakatime_key: true,
discord_webhook_url: null,
webhook_url: null,
timezone: "UTC",
bio: "",
});
});
});
describe("PATCH /api/user/settings", () => {
it("returns 401 when user is not authenticated", async () => {
(getServerSession as any).mockResolvedValue(null);
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: JSON.stringify({ is_public: false }),
});
const res = await PATCH(req);
expect(res.status).toBe(401);
expect(await res.json()).toEqual({ error: "Unauthorized" });
});
it("returns 404 when user is not found in database", async () => {
(resolveAppUser as any).mockResolvedValue(null);
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: JSON.stringify({ is_public: false }),
});
const res = await PATCH(req);
expect(res.status).toBe(404);
expect(await res.json()).toEqual({ error: "User not found" });
});
it("returns 400 when request body is invalid JSON", async () => {
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: "invalid-json",
});
const res = await PATCH(req);
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: "Invalid request body" });
});
it("returns 500 when fetching user settings fails before update", async () => {
mockSingle.mockResolvedValue({ data: null, error: { message: "DB Error" } });
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: JSON.stringify({ is_public: false }),
});
const res = await PATCH(req);
expect(res.status).toBe(500);
expect(await res.json()).toEqual({ error: "Failed to update settings" });
});
it("returns 400 when pinning more than 3 repositories", async () => {
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: JSON.stringify({ pinned_repos: ["repo-1", "repo-2", "repo-3", "repo-4"] }),
});
const res = await PATCH(req);
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: "Maximum 3 pins allowed" });
});
it("ignores null settings values (treated as field omission) and does not update database", async () => {
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: JSON.stringify({
is_public: null,
leaderboard_opt_in: null,
pinned_repos: null,
}),
});
const res = await PATCH(req);
expect(res.status).toBe(200);
// Verify returned value contains existing fields unchanged
expect(await res.json()).toEqual({
id: "user-uuid-123",
github_login: "test-user",
is_public: true,
leaderboard_opt_in: true,
weekly_digest_opt_in: false,
pinned_repos: ["repo-1"],
has_wakatime_key: true,
discord_webhook_url: null,
webhook_url: null,
timezone: "UTC",
bio: "",
});
// Verify that no database updates were triggered (mockUpdate not called because updates is empty)
expect(mockUpdate).not.toHaveBeenCalled();
});
it("applies updates when valid fields are supplied in PATCH body", async () => {
const req = new NextRequest("http://localhost/api/user/settings", {
method: "PATCH",
body: JSON.stringify({
is_public: false,
pinned_repos: ["repo-2", "repo-3"],
}),
});
const res = await PATCH(req);
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
id: "user-uuid-123",
github_login: "test-user",
is_public: false,
leaderboard_opt_in: true,
weekly_digest_opt_in: false,
pinned_repos: ["repo-2", "repo-3"],
has_wakatime_key: true,
discord_webhook_url: null,
webhook_url: null,
timezone: "UTC",
bio: "",
});
// Verify update database query was called with the updates object
expect(mockUpdate).toHaveBeenCalledWith({
is_public: false,
pinned_repos: ["repo-2", "repo-3"],
});
});
});
});