-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathauth-email.test.js
More file actions
300 lines (263 loc) · 10.1 KB
/
Copy pathauth-email.test.js
File metadata and controls
300 lines (263 loc) · 10.1 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
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
// Tests for email/password auth endpoints: login, logout, register.
// Dispatched via api/auth/[action].js.
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Readable } from 'node:stream';
process.env.PUBLIC_APP_ORIGIN ||= 'https://app.test';
process.env.JWT_SECRET ||= 'test-email-auth-secret-at-least-32ch';
// ── Mocks ─────────────────────────────────────────────────────────────────────
const sqlState = { queue: [], calls: [] };
vi.mock('../../api/_lib/db.js', () => ({
sql: vi.fn(async (strings, ...values) => {
sqlState.calls.push({ query: strings.join('?'), values });
if (sqlState.queue.length === 0) return [];
const next = sqlState.queue.shift();
if (next instanceof Error) throw next;
return next;
}),
}));
const rlState = { success: true };
vi.mock('../../api/_lib/rate-limit.js', () => ({
limits: {
authIp: vi.fn(async () => ({ success: rlState.success })),
registerIp: vi.fn(async () => ({ success: rlState.success })),
},
clientIp: () => '127.0.0.1',
}));
const authMock = {
sessionToken: 'test-session-token-abc',
verifyPassword: true,
};
vi.mock('../../api/_lib/auth.js', () => ({
verifyPassword: vi.fn(async () => authMock.verifyPassword),
hashPassword: vi.fn(async (p) => `hashed:${p}`),
createSession: vi.fn(async () => authMock.sessionToken),
destroySession: vi.fn(async () => {}),
sessionCookie: vi.fn((token, opts) => {
if (opts?.clear) {
return [
'__Host-sid=; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=0',
'sid=; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=0',
];
}
return `__Host-sid=${token}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=2592000`;
}),
getSessionUser: vi.fn(async () => null),
revokeRefreshToken: vi.fn(async () => {}),
}));
vi.mock('../../api/_lib/email.js', () => ({
sendPasswordResetEmail: vi.fn(async () => {}),
sendVerificationEmail: vi.fn(async () => {}),
sendWelcomeEmail: vi.fn(async () => {}),
}));
const { default: handler } = await import('../../api/auth/[action].js');
const authMod = await import('../../api/_lib/auth.js');
// ── Helpers ───────────────────────────────────────────────────────────────────
function makeReq({ action, body = null, headers = {} } = {}) {
const bodyStr = body ? JSON.stringify(body) : '';
const base = Readable.from([Buffer.from(bodyStr)]);
base.method = 'POST';
base.url = `/api/auth/${action}`;
base.query = { action };
base.headers = {
host: 'app.test',
...(body !== null ? { 'content-type': 'application/json' } : {}),
...headers,
};
return base;
}
function makeRes() {
return {
statusCode: 200,
headers: {},
body: '',
writableEnded: false,
setHeader(k, v) {
this.headers[k.toLowerCase()] = v;
},
getHeader(k) {
return this.headers[k.toLowerCase()];
},
end(chunk) {
if (chunk !== undefined) this.body += chunk;
this.writableEnded = true;
},
};
}
async function invoke(opts) {
const req = makeReq(opts);
const res = makeRes();
await handler(req, res);
let json = null;
try {
json = JSON.parse(res.body);
} catch {
json = res.body;
}
return { res, status: res.statusCode, body: json };
}
const USER_ROW = {
id: 'user-1',
email: 'alice@example.com',
password_hash: 'bcrypt-hash',
display_name: 'Alice',
plan: 'free',
avatar_url: null,
};
beforeEach(() => {
sqlState.queue = [];
sqlState.calls = [];
rlState.success = true;
authMock.verifyPassword = true;
authMock.sessionToken = 'test-session-token-abc';
vi.mocked(authMod.destroySession).mockClear();
vi.mocked(authMod.createSession).mockClear();
});
// ── login ──────────────────────────────────────────────────────────────────────
describe('POST /api/auth/login', () => {
it('returns 200 with user and sets session cookie on valid credentials', async () => {
sqlState.queue.push([USER_ROW]);
const { status, body, res } = await invoke({
action: 'login',
body: { email: 'alice@example.com', password: 'correctpassword' },
});
expect(status).toBe(200);
expect(body.user).toBeDefined();
expect(body.user.id).toBe('user-1');
expect(body.user.email).toBe('alice@example.com');
expect(body.user.password_hash).toBeUndefined();
const cookie = res.headers['set-cookie'];
expect(cookie).toContain('__Host-sid=test-session-token-abc');
});
it('returns 401 for wrong password', async () => {
sqlState.queue.push([USER_ROW]);
authMock.verifyPassword = false;
const { status, body } = await invoke({
action: 'login',
body: { email: 'alice@example.com', password: 'wrongpassword' },
});
expect(status).toBe(401);
expect(body.error).toBe('invalid_credentials');
});
it('returns 401 when email is not found', async () => {
sqlState.queue.push([]); // no user row
const { status, body } = await invoke({
action: 'login',
body: { email: 'nobody@example.com', password: 'anypassword' },
});
expect(status).toBe(401);
expect(body.error).toBe('invalid_credentials');
});
it('returns 429 when rate limited', async () => {
rlState.success = false;
const { status, body } = await invoke({
action: 'login',
body: { email: 'alice@example.com', password: 'pw' },
});
expect(status).toBe(429);
expect(body.error).toBe('rate_limited');
});
});
// ── logout ─────────────────────────────────────────────────────────────────────
describe('POST /api/auth/logout', () => {
it('returns 200 and clears session cookie', async () => {
const { status, body, res } = await invoke({ action: 'logout' });
expect(status).toBe(200);
expect(body.ok).toBe(true);
const cookies = res.headers['set-cookie'];
const cookieList = Array.isArray(cookies) ? cookies : [cookies];
expect(cookieList.some((c) => c.includes('Max-Age=0'))).toBe(true);
});
it('calls destroySession', async () => {
const { destroySession } = await import('../../api/_lib/auth.js');
vi.mocked(destroySession).mockClear();
await invoke({ action: 'logout' });
expect(vi.mocked(destroySession)).toHaveBeenCalledOnce();
});
});
// ── register ───────────────────────────────────────────────────────────────────
describe('POST /api/auth/register', () => {
it('creates a user and returns 201 with session cookie', async () => {
sqlState.queue.push([]); // no existing user
sqlState.queue.push([{ id: 'user-new', display_name: 'Bob', plan: 'free', created_at: '2024-01-01' }]);
const { status, body, res } = await invoke({
action: 'register',
body: { email: 'bob@example.com', password: 'supersecret123', display_name: 'Bob' },
});
expect(status).toBe(201);
expect(body.user).toBeDefined();
expect(body.user.id).toBe('user-new');
const cookie = res.headers['set-cookie'];
expect(cookie).toContain('__Host-sid=test-session-token-abc');
});
it('returns 409 when email is already registered', async () => {
sqlState.queue.push([{ id: 'existing-user' }]); // existing user found
const { status, body } = await invoke({
action: 'register',
body: { email: 'alice@example.com', password: 'supersecret123' },
});
expect(status).toBe(409);
expect(body.error).toBe('conflict');
});
it('returns 400 for short password (< 10 chars)', async () => {
const { status, body } = await invoke({
action: 'register',
body: { email: 'new@example.com', password: 'short' },
});
expect(status).toBe(400);
expect(body.error).toBeDefined();
});
it('returns 429 when rate limited', async () => {
rlState.success = false;
const { status, body } = await invoke({
action: 'register',
body: { email: 'new@example.com', password: 'supersecret123' },
});
expect(status).toBe(429);
expect(body.error).toBe('rate_limited');
});
it('retries when the first generated referral_code collides with the unique index', async () => {
// SELECT existing-user check → empty
sqlState.queue.push([]);
// First INSERT collides with the users_referral_code unique index.
const uniqueViolation = Object.assign(new Error(
'duplicate key value violates unique constraint "users_referral_code_key"',
), { code: '23505' });
sqlState.queue.push(uniqueViolation);
// Second INSERT succeeds.
sqlState.queue.push([{
id: 'user-new',
display_name: 'Carol',
plan: 'free',
created_at: '2024-01-01',
referral_code: 'SECONDOK',
}]);
const { status, body } = await invoke({
action: 'register',
body: { email: 'carol@example.com', password: 'supersecret123', display_name: 'Carol' },
});
expect(status).toBe(201);
expect(body.user.id).toBe('user-new');
expect(body.user.referral_code).toBe('SECONDOK');
// Two INSERTs attempted, each with a distinct referral_code (last value
// in the bound-parameters array). The CSPRNG should not repeat — and if
// it did, the handler would have surfaced the unique violation instead.
const inserts = sqlState.calls.filter((c) => /insert into users/.test(c.query));
expect(inserts).toHaveLength(2);
const firstCode = inserts[0].values.at(-1);
const secondCode = inserts[1].values.at(-1);
expect(typeof firstCode).toBe('string');
expect(firstCode).not.toBe(secondCode);
});
it('propagates non-collision insert errors instead of looping', async () => {
sqlState.queue.push([]); // no existing user
const fatal = Object.assign(new Error('connection terminated'), { code: '57P01' });
sqlState.queue.push(fatal);
const { status, body } = await invoke({
action: 'register',
body: { email: 'dora@example.com', password: 'supersecret123', display_name: 'Dora' },
});
expect(status).toBe(500);
const inserts = sqlState.calls.filter((c) => /insert into users/.test(c.query));
expect(inserts).toHaveLength(1);
});
});