-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathauth-validation.test.ts
More file actions
111 lines (96 loc) · 2.92 KB
/
Copy pathauth-validation.test.ts
File metadata and controls
111 lines (96 loc) · 2.92 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
import cookiePlugin from '@fastify/cookie';
import jwtPlugin from '@fastify/jwt';
import Fastify, { type FastifyInstance } from 'fastify';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { authRoutes } from '../routes/auth.js';
async function buildTestApp() {
const app = Fastify({ logger: false });
await app.register(cookiePlugin as any);
await app.register(jwtPlugin as any, { secret: 'test-secret-for-unit-tests-only' });
app.decorate('prisma', {
refreshToken: {
findUnique: vi.fn(),
update: vi.fn(),
create: vi.fn(),
updateMany: vi.fn(),
},
user: {
findUnique: vi.fn()
}
} as any);
app.decorate('redis', {
getdel: vi.fn(),
set: vi.fn(),
exists: vi.fn(),
} as any);
app.decorate('authenticate', async () => {});
await app.register(authRoutes, { prefix: '/auth' });
await app.ready();
return app;
}
describe('auth validation', () => {
let app: FastifyInstance;
beforeEach(async () => {
app = await buildTestApp();
});
afterEach(async () => {
await app.close();
});
describe('POST /auth/mobile/exchange', () => {
it('rejects missing code', async () => {
const res = await app.inject({
method: 'POST',
url: '/auth/mobile/exchange',
payload: {}
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe('Invalid request body');
});
it('rejects non-string code', async () => {
const res = await app.inject({
method: 'POST',
url: '/auth/mobile/exchange',
payload: { code: 123 }
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe('Invalid request body');
});
it('rejects empty code', async () => {
const res = await app.inject({
method: 'POST',
url: '/auth/mobile/exchange',
payload: { code: '' }
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe('Invalid request body');
});
});
describe('POST /auth/refresh', () => {
it('rejects empty refresh_token in body', async () => {
const res = await app.inject({
method: 'POST',
url: '/auth/refresh',
payload: { refresh_token: '' }
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe('Invalid request body');
});
it('rejects non-string refresh_token in body', async () => {
const res = await app.inject({
method: 'POST',
url: '/auth/refresh',
payload: { refresh_token: 123 }
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe('Invalid request body');
});
it('fails when no token is provided', async () => {
const res = await app.inject({
method: 'POST',
url: '/auth/refresh'
});
expect(res.statusCode).toBe(401);
expect(res.json().error).toBe('Refresh token missing');
});
});
});