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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
import { beforeEach, describe, expect, test, vi } from "vitest";
import argon2 from "argon2";
import * as jose from "jose";
import { db } from "../src/db/db";
import { users, linkedDevices } from "../src/db/schema";
import { eq } from "drizzle-orm";
import signinRouter from "../src/routes/signin";
import type { Request, Response } from "express";
import { verifyGoogleIdToken } from "../src/account/google";
import { signJwt } from "../src/account/jwt";
import { redis } from "../src/db/redis/client";
const sendMail = vi.fn();
vi.mock("../src/email/email", () => ({
getTransporter: () => ({
sendMail,
}),
}));
vi.mock("../src/account/google", () => ({
verifyGoogleIdToken: vi.fn(),
}));
vi.mock("../src/account/jwt", async () => {
const actual = await vi.importActual<typeof import("../src/account/jwt")>(
"../src/account/jwt",
);
return {
...actual,
signJwt: vi.fn(),
};
});
describe("Signin Routes", () => {
beforeEach(() => {
process.env.RESET_PASSWORD_JWT_SECRET = "test-reset-secret";
process.env.SMTP_EMAIL = "buddy@example.com";
process.env.BASE_URL = "https://buddy.example";
sendMail.mockReset();
vi.mocked(verifyGoogleIdToken).mockReset();
vi.mocked(signJwt).mockReset();
vi.mocked(signJwt).mockResolvedValue("signed-jwt");
vi.mocked(redis.getdel).mockReset();
});
function getRouteHandler(path: string, method: "get" | "post") {
const layer = (
signinRouter as unknown as {
stack: Array<{
route?: {
path: string;
methods: Record<string, boolean>;
stack: Array<{ handle: (req: Request, res: Response) => unknown }>;
};
}>;
}
).stack.find(
(candidate) =>
candidate.route?.path === path && candidate.route.methods[method],
);
if (!layer?.route?.stack[0]) {
throw new Error(`Route handler for ${path} not found`);
}
return layer.route.stack[0].handle;
}
async function invokeRoute(
path: string,
method: "get" | "post",
options?: { body?: unknown; params?: Record<string, string> },
) {
const handler = getRouteHandler(path, method);
const req = {
body: options?.body,
params: options?.params ?? {},
} as Request;
const res = {
statusCode: 200,
body: undefined as unknown,
responseType: undefined as string | undefined,
status(code: number) {
this.statusCode = code;
return this;
},
type(value: string) {
this.responseType = value;
return this;
},
send(payload: unknown) {
this.body = payload;
return this;
},
} as Response & {
statusCode: number;
body: unknown;
responseType?: string;
};
await handler(req, res);
return res;
}
test("should send a password reset email with a signed reset link", async () => {
const response = await invokeRoute("/resetpassword", "post", {
body: {
email: "test@example.com",
},
});
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
success: true,
reason: "",
});
expect(sendMail).toHaveBeenCalledTimes(1);
const mail = sendMail.mock.calls[0]?.[0];
expect(mail.subject).toBe("Buddy password reset");
expect(mail.text).toContain("Your Buddy password reset link is ");
const sentLink = mail.text.replace(
"Your Buddy password reset link is ",
"",
);
expect(sentLink.startsWith("https://buddy.example/reset-password/")).toBe(
true,
);
const token = sentLink.split("/").pop();
expect(token).toBeTruthy();
const { payload } = await jose.jwtVerify(
token!,
new TextEncoder().encode(process.env.RESET_PASSWORD_JWT_SECRET),
{
issuer: "urn:lajosh:buddy",
audience: "urn:buddy:password-reset",
},
);
expect(payload.id).toBe(1);
expect(payload.type).toBe("password_reset");
});
test("should update the user password when given a valid reset token", async () => {
const token = await new jose.SignJWT({ id: 1, type: "password_reset" })
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setIssuer("urn:lajosh:buddy")
.setAudience("urn:buddy:password-reset")
.setExpirationTime("1h")
.sign(new TextEncoder().encode(process.env.RESET_PASSWORD_JWT_SECRET!));
const response = await invokeRoute("/reset-password/:token", "post", {
params: { token },
body: {
password: "new-password",
},
});
expect(response.statusCode).toBe(200);
expect(response.responseType).toBe("html");
expect(String(response.body)).toContain(
"Your password has been updated successfully.",
);
const updatedUser = (
await db.select().from(users).where(eq(users.id, 1)).limit(1)
)[0];
expect(updatedUser).toBeTruthy();
expect(await argon2.verify(updatedUser!.password, "new-password")).toBe(
true,
);
});
test("should create a new verified user from a valid Google login", async () => {
vi.mocked(verifyGoogleIdToken).mockResolvedValue({
email: "google-user@example.com",
emailVerified: true,
subject: "google-subject-1",
});
const response = await invokeRoute("/signin/google", "post", {
body: {
idToken: "google-id-token",
},
});
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
success: true,
token: "signed-jwt",
reason: "",
});
const insertedUser = (
await db
.select()
.from(users)
.where(eq(users.email, "google-user@example.com"))
.limit(1)
)[0];
expect(insertedUser).toBeTruthy();
expect(insertedUser!.emailVerified).toBe(true);
expect(insertedUser!.emailCode).toHaveLength(6);
expect(await argon2.verify(insertedUser!.password, "google-id-token")).toBe(
false,
);
expect(signJwt).toHaveBeenCalledWith(
{ id: insertedUser!.id, type: "parent" },
"urn:buddy:users",
);
});
test("should log in an existing user with Google and mark it verified", async () => {
await db
.update(users)
.set({ emailVerified: false })
.where(eq(users.email, "test@example.com"));
vi.mocked(verifyGoogleIdToken).mockResolvedValue({
email: "test@example.com",
emailVerified: true,
subject: "google-subject-2",
});
const response = await invokeRoute("/signin/google", "post", {
body: {
idToken: "google-id-token",
},
});
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
success: true,
token: "signed-jwt",
reason: "",
});
const updatedUser = (
await db.select().from(users).where(eq(users.email, "test@example.com"))
)[0];
expect(updatedUser).toBeTruthy();
expect(updatedUser!.emailVerified).toBe(true);
expect(signJwt).toHaveBeenCalledWith(
{ id: updatedUser!.id, type: "parent" },
"urn:buddy:users",
);
});
test("should render the hosted reset password form for a valid token", async () => {
const token = await new jose.SignJWT({ id: 1, type: "password_reset" })
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setIssuer("urn:lajosh:buddy")
.setAudience("urn:buddy:password-reset")
.setExpirationTime("1h")
.sign(new TextEncoder().encode(process.env.RESET_PASSWORD_JWT_SECRET!));
const response = await invokeRoute("/reset-password/:token", "get", {
params: { token },
});
expect(response.statusCode).toBe(200);
expect(response.responseType).toBe("html");
expect(String(response.body)).toContain("Reset Password");
expect(String(response.body)).toContain(
`action="/reset-password/${encodeURIComponent(token)}"`,
);
});
test("should link a kid device with a valid one-time code", async () => {
const beforeDevices = await db
.select()
.from(linkedDevices)
.where(eq(linkedDevices.parentId, 1));
vi.mocked(redis.getdel).mockResolvedValueOnce("1");
const response = await invokeRoute("/kid/link", "post", {
body: {
code: "abc-123",
},
});
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
success: true,
token: "signed-jwt",
reason: "",
});
expect(redis.getdel).toHaveBeenCalledWith("kid-link-code:ABC-123");
const afterDevices = await db
.select()
.from(linkedDevices)
.where(eq(linkedDevices.parentId, 1));
expect(afterDevices.length).toBe(beforeDevices.length + 1);
});
test("should reject linking when code is expired or invalid", async () => {
vi.mocked(redis.getdel).mockResolvedValueOnce(null);
const response = await invokeRoute("/kid/link", "post", {
body: {
code: "ABC-123",
},
});
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
success: false,
reason: "Invalid or expired code",
});
});
});
|