-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathauth.test.ts
More file actions
152 lines (128 loc) · 4.39 KB
/
Copy pathauth.test.ts
File metadata and controls
152 lines (128 loc) · 4.39 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
import cookie from '@fastify/cookie';
import jwt from '@fastify/jwt';
import Fastify from 'fastify';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { authRoutes } from '../routes/auth.js';
import type { PrismaClient } from '@prisma/client';
const mockPrisma = {
user: {
upsert: vi.fn(),
findUnique: vi.fn(),
},
oAuthToken: {
upsert: vi.fn(),
},
};
const originalEnv = process.env;
const originalFetch = global.fetch;
async function buildApp() {
const app = Fastify();
await app.register(jwt, { secret: 'test-secret' });
await app.register(cookie);
app.decorate('prisma', mockPrisma as unknown as PrismaClient);
app.decorate('authenticate', async () => {});
app.register(authRoutes, { prefix: '/auth' });
await app.ready();
return app;
}
describe('GET /auth/google/callback', () => {
beforeEach(() => {
vi.clearAllMocks();
process.env = { ...originalEnv };
process.env.NODE_ENV = 'test';
process.env.PUBLIC_APP_URL = 'http://localhost:3000';
process.env.BACKEND_URL = 'http://localhost:3001';
process.env.MOBILE_REDIRECT_URI = 'devcard://auth';
process.env.GOOGLE_CLIENT_ID = 'test-google-id';
process.env.GOOGLE_CLIENT_SECRET = 'test-google-secret';
process.env.ENCRYPTION_KEY = '12345678901234567890123456789012';
global.fetch = vi.fn();
});
afterEach(() => {
process.env = originalEnv;
global.fetch = originalFetch;
});
it('persists Google OAuthToken upon successful login', async () => {
const mockUser = { id: 'user-123', username: 'testuser' };
mockPrisma.user.upsert.mockResolvedValue(mockUser);
mockPrisma.oAuthToken.upsert.mockResolvedValue({});
(global.fetch as any)
.mockResolvedValueOnce({
json: vi.fn().mockResolvedValue({
access_token: 'fake-google-token',
scope: 'openid email profile',
}),
}) // tokenRes
.mockResolvedValueOnce({
json: vi.fn().mockResolvedValue({
id: 'google-id-123',
email: 'test@gmail.com',
name: 'Test User',
picture: 'https://avatar.com/test',
}),
}); // userRes
const app = await buildApp();
const res = await app.inject({
method: 'GET',
url: '/auth/google/callback?code=fake-code&state=fake-state',
cookies: {
oauth_state: 'fake-state',
},
});
expect(res.statusCode).toBe(302);
expect(res.headers.location).toBe('http://localhost:3000/dashboard');
// Verify user upsert
expect(mockPrisma.user.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { provider_providerId: { provider: 'google', providerId: 'google-id-123' } },
})
);
// Verify oAuthToken upsert
expect(mockPrisma.oAuthToken.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { userId_platform: { userId: 'user-123', platform: 'google' } },
create: expect.objectContaining({
platform: 'google',
scopes: 'openid email profile',
}),
})
);
});
it('allows authentication to succeed even if token persistence fails', async () => {
const mockUser = { id: 'user-123', username: 'testuser' };
mockPrisma.user.upsert.mockResolvedValue(mockUser);
// Simulate upsert failure
mockPrisma.oAuthToken.upsert.mockRejectedValue(new Error('DB Error'));
(global.fetch as any)
.mockResolvedValueOnce({
json: vi.fn().mockResolvedValue({
access_token: 'fake-google-token',
}),
})
.mockResolvedValueOnce({
json: vi.fn().mockResolvedValue({
id: 'google-id-123',
email: 'test@gmail.com',
name: 'Test User',
}),
});
const app = await buildApp();
// Spy on app.log.error to ensure it gets logged
const logSpy = vi.spyOn(app.log, 'error');
const res = await app.inject({
method: 'GET',
url: '/auth/google/callback?code=fake-code&state=fake-state',
cookies: {
oauth_state: 'fake-state',
},
});
// Should still succeed and redirect to dashboard
expect(res.statusCode).toBe(302);
expect(res.headers.location).toBe('http://localhost:3000/dashboard');
// Verify the error was logged
expect(logSpy).toHaveBeenCalledWith(
expect.objectContaining({ userId: 'user-123' }),
'Failed to persist Google OAuth token — authentication proceeds'
);
});
});