-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathuserAuth.social.test.js
More file actions
611 lines (521 loc) · 23.8 KB
/
Copy pathuserAuth.social.test.js
File metadata and controls
611 lines (521 loc) · 23.8 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
'use strict';
const crypto = require('crypto');
jest.mock('bcryptjs', () => ({
genSalt: jest.fn().mockResolvedValue('salt'),
hash: jest.fn().mockResolvedValue('hashed-social-password'),
compare: jest.fn(),
}));
jest.mock('jsonwebtoken', () => ({
sign: jest.fn(() => 'signed_access_token'),
verify: jest.fn(),
}));
jest.mock('mongoose', () => ({
Types: {
ObjectId: jest.fn((id) => id),
},
}));
jest.mock('../utils/refreshToken', () => ({
assertRefreshRateLimits: jest.fn(),
clearRefreshCookie: jest.fn(),
hashRefreshToken: jest.fn(),
issueAuthTokens: jest.fn().mockResolvedValue({
accessToken: 'issued_access_token',
refreshToken: 'issued_refresh_token',
expiresIn: '15m',
tokenId: 'token_1',
}),
parseRefreshToken: jest.fn(),
readRefreshTokenFromRequest: jest.fn(),
shouldExposeRefreshToken: jest.fn(() => false),
}));
const mockProjectFindByIdChain = {
select: jest.fn().mockReturnThis(),
lean: jest.fn(),
};
const mockUsersModel = {
findOne: jest.fn(),
create: jest.fn(),
updateOne: jest.fn(),
};
jest.mock('@urbackend/common', () => {
const z = require('zod');
return {
redis: {
set: jest.fn().mockResolvedValue('OK'),
get: jest.fn(),
del: jest.fn().mockResolvedValue(1),
},
Project: {
findById: jest.fn(() => mockProjectFindByIdChain),
},
authEmailQueue: { add: jest.fn() },
getRefreshSession: jest.fn(),
persistRefreshSession: jest.fn(),
revokeSessionChain: jest.fn(),
loginSchema: z.object({ email: z.string().email(), password: z.string().min(1) }),
userSignupSchema: z.object({ email: z.string().email(), password: z.string().min(6) }).passthrough(),
resetPasswordSchema: z.object({ email: z.string().email(), otp: z.string(), newPassword: z.string().min(6) }),
onlyEmailSchema: z.object({ email: z.string().email() }),
verifyOtpSchema: z.object({ email: z.string().email(), otp: z.string() }),
changePasswordSchema: z.object({ currentPassword: z.string(), newPassword: z.string().min(6) }),
sanitize: jest.fn((value) => value),
getConnection: jest.fn().mockResolvedValue({}),
getCompiledModel: jest.fn(() => mockUsersModel),
decrypt: jest.fn((encrypted) => {
if (!encrypted?.encrypted) return null;
if (encrypted.encrypted === 'github') return 'github_secret';
if (encrypted.encrypted === 'google') return 'google_secret';
return null;
}),
};
});
const { redis } = require('@urbackend/common');
const { issueAuthTokens } = require('../utils/refreshToken');
const controller = require('../controllers/userAuth.controller');
const { privateKey: googlePrivateKey, publicKey: googlePublicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
});
const GOOGLE_PRIVATE_KEY_PEM = googlePrivateKey.export({ format: 'pem', type: 'pkcs8' });
const GOOGLE_JWK = {
...googlePublicKey.export({ format: 'jwk' }),
kid: 'google-kid-1',
alg: 'RS256',
use: 'sig',
};
const makeProject = () => ({
_id: 'project_1',
name: 'Demo',
jwtSecret: 'jwt_secret',
siteUrl: 'http://localhost:5173',
isAuthEnabled: true,
resources: { db: { isExternal: false } },
collections: [
{
name: 'users',
model: [
{ key: 'email', type: 'String', required: true },
{ key: 'password', type: 'String', required: true },
{ key: 'username', type: 'String', required: false },
],
},
],
authProviders: {
github: {
enabled: true,
clientId: 'github_client_id',
clientSecret: { encrypted: 'github', iv: 'y', tag: 'z' },
redirectUri: 'http://localhost:1235/api/userAuth/social/github/callback',
},
google: {
enabled: true,
clientId: 'google_client_id',
clientSecret: { encrypted: 'google', iv: 'y', tag: 'z' },
redirectUri: 'http://localhost:1235/api/userAuth/social/google/callback',
},
},
});
const makeReq = ({ params = {}, query = {}, project = makeProject() } = {}) => ({
params,
query,
body: {},
project,
header: jest.fn(() => null),
headers: {},
cookies: {},
ip: '127.0.0.1',
socket: { remoteAddress: '127.0.0.1' },
});
const makeRes = () => {
const res = {
status: jest.fn(),
json: jest.fn(),
redirect: jest.fn(),
cookie: jest.fn(),
};
res.status.mockReturnValue(res);
res.json.mockReturnValue(res);
return res;
};
const base64UrlEncode = (input) => Buffer.from(input).toString('base64url');
const signGoogleIdToken = (claims = {}) => {
const header = {
alg: 'RS256',
kid: GOOGLE_JWK.kid,
typ: 'JWT',
};
const payload = {
iss: 'https://accounts.google.com',
aud: 'google_client_id',
sub: 'google-user-1',
email: 'alice@example.com',
email_verified: true,
name: 'Alice Example',
picture: 'https://example.com/avatar.png',
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(Date.now() / 1000),
...claims,
};
const encodedHeader = base64UrlEncode(JSON.stringify(header));
const encodedPayload = base64UrlEncode(JSON.stringify(payload));
const signer = crypto.createSign('RSA-SHA256');
signer.update(`${encodedHeader}.${encodedPayload}`);
signer.end();
const signature = signer.sign(GOOGLE_PRIVATE_KEY_PEM).toString('base64url');
return `${encodedHeader}.${encodedPayload}.${signature}`;
};
describe('public userAuth social auth', () => {
beforeEach(() => {
jest.clearAllMocks();
global.fetch = jest.fn();
process.env.FRONTEND_URL = 'http://localhost:5173';
mockProjectFindByIdChain.select.mockReturnValue(mockProjectFindByIdChain);
});
test('startSocialAuth redirects to GitHub authorize URL', async () => {
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
const req = makeReq({ params: { provider: 'github' } });
const res = makeRes();
await controller.startSocialAuth(req, res);
expect(redis.set).toHaveBeenCalled();
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('https://github.com/login/oauth/authorize?'));
});
test('startSocialAuth redirects to Google authorize URL', async () => {
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
const req = makeReq({ params: { provider: 'google' } });
const res = makeRes();
await controller.startSocialAuth(req, res);
expect(redis.set).toHaveBeenCalled();
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('https://accounts.google.com/o/oauth2/v2/auth?'));
});
test('handleSocialAuthCallback rejects invalid state', async () => {
redis.get.mockResolvedValueOnce(null);
const req = makeReq({ params: { provider: 'github' }, query: { code: 'code_1', state: 'missing' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({ error: 'Invalid or expired OAuth state' }));
});
test('handleSocialAuthCallback rejects when GitHub returns no email', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'github', callbackUrl: 'http://localhost:5173/auth/callback' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ access_token: 'github_access_token' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id: 123, login: 'alice', avatar_url: '' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ([]),
});
const req = makeReq({ params: { provider: 'github' }, query: { code: 'code_1', state: 'state_1' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
// P2: errors now redirect to frontend instead of JSON
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('error='));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('did+not+return+an+email'));
});
test('handleSocialAuthCallback redirects after GitHub signup', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'github' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
mockUsersModel.findOne
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(null);
mockUsersModel.create.mockResolvedValueOnce({ _id: 'user_new_1' });
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ access_token: 'github_access_token' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id: 123, login: 'alice', name: 'Alice' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ([{ email: 'alice@example.com', primary: true, verified: true }]),
});
const req = makeReq({ params: { provider: 'github' }, query: { code: 'code_1', state: 'state_1' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
expect(mockUsersModel.create).toHaveBeenCalledWith(expect.objectContaining({
email: 'alice@example.com',
githubId: '123',
authProviders: ['github'],
}));
expect(issueAuthTokens).toHaveBeenCalled();
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('/auth/callback?'));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('rtCode='));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('#token=issued_access_token'));
expect(res.redirect).not.toHaveBeenCalledWith(expect.stringContaining('?token=issued_access_token'));
expect(res.redirect).not.toHaveBeenCalledWith(expect.stringContaining('refreshToken=issued_refresh_token'));
});
test('handleSocialAuthCallback links existing email GitHub user and redirects', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'github' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
mockUsersModel.findOne
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({ _id: 'user_existing_1', email: 'alice@example.com' })
.mockResolvedValueOnce({ _id: 'user_existing_1', email: 'alice@example.com', githubId: '123' });
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ access_token: 'github_access_token' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id: 123, login: 'alice', name: 'Alice' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ([{ email: 'alice@example.com', primary: true, verified: true }]),
});
const req = makeReq({ params: { provider: 'github' }, query: { code: 'code_1', state: 'state_1' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
expect(mockUsersModel.updateOne).toHaveBeenCalledWith(
{ _id: 'user_existing_1' },
expect.objectContaining({
$set: expect.objectContaining({ githubId: '123' }),
$addToSet: { authProviders: 'github' },
})
);
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('linkedByEmail=true'));
});
test('handleSocialAuthCallback redirects after Google signup', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'google' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
mockUsersModel.findOne
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(null);
mockUsersModel.create.mockResolvedValueOnce({ _id: 'user_google_1' });
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id_token: signGoogleIdToken() }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ keys: [GOOGLE_JWK] }),
});
const req = makeReq({ params: { provider: 'google' }, query: { code: 'code_google', state: 'state_google' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
expect(mockUsersModel.create).toHaveBeenCalledWith(expect.objectContaining({
email: 'alice@example.com',
googleId: 'google-user-1',
authProviders: ['google'],
}));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('provider=google'));
});
test('handleSocialAuthCallback links existing email Google user and redirects', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'google' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
mockUsersModel.findOne
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({ _id: 'user_existing_google', email: 'alice@example.com' })
.mockResolvedValueOnce({ _id: 'user_existing_google', email: 'alice@example.com', googleId: 'google-user-1' });
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id_token: signGoogleIdToken() }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ keys: [GOOGLE_JWK] }),
});
const req = makeReq({ params: { provider: 'google' }, query: { code: 'code_google', state: 'state_google' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
expect(mockUsersModel.updateOne).toHaveBeenCalledWith(
{ _id: 'user_existing_google' },
expect.objectContaining({
$set: expect.objectContaining({ googleId: 'google-user-1' }),
$addToSet: { authProviders: 'google' },
})
);
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('provider=google'));
});
test('handleSocialAuthCallback rejects when Google returns no email', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'google', callbackUrl: 'http://localhost:5173/auth/callback' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id_token: signGoogleIdToken({ email: '', email_verified: false }) }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ keys: [GOOGLE_JWK] }),
});
const req = makeReq({ params: { provider: 'google' }, query: { code: 'code_google', state: 'state_google' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
// P2: errors now redirect to frontend instead of JSON
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('error='));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('did+not+return+an+email'));
});
test('handleSocialAuthCallback rejects invalid Google id_token audience', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({ projectId: 'project_1', provider: 'google', callbackUrl: 'http://localhost:5173/auth/callback' }));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id_token: signGoogleIdToken({ aud: 'wrong_audience' }) }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ keys: [GOOGLE_JWK] }),
});
const req = makeReq({ params: { provider: 'google' }, query: { code: 'code_google', state: 'state_google' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
// P2: errors now redirect to frontend instead of JSON
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('error='));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('audience'));
});
test('exchangeSocialRefreshToken returns refresh token and deletes exchange code', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({
token: 'issued_access_token',
refreshToken: 'issued_refresh_token',
}));
const req = makeReq();
req.body = {
rtCode: 'code_123',
token: 'issued_access_token',
};
const res = makeRes();
await controller.exchangeSocialRefreshToken(req, res);
expect(redis.get).toHaveBeenCalledWith('project:social-auth:refresh-exchange:code_123');
expect(redis.del).toHaveBeenCalledWith('project:social-auth:refresh-exchange:code_123');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: {
refreshToken: 'issued_refresh_token',
},
message: 'Refresh token exchanged successfully',
});
});
test('exchangeSocialRefreshToken rejects invalid or expired code', async () => {
redis.get.mockResolvedValueOnce(null);
const req = makeReq();
req.body = {
rtCode: 'missing_code',
token: 'issued_access_token',
};
const res = makeRes();
await controller.exchangeSocialRefreshToken(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'Invalid or expired refresh token exchange code',
});
});
test('exchangeSocialRefreshToken rejects mismatched token and deletes exchange code', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({
token: 'expected_access_token',
refreshToken: 'issued_refresh_token',
}));
const req = makeReq();
req.body = {
rtCode: 'code_456',
token: 'wrong_access_token',
};
const res = makeRes();
await controller.exchangeSocialRefreshToken(req, res);
expect(redis.del).toHaveBeenCalledWith('project:social-auth:refresh-exchange:code_456');
expect(res.status).toHaveBeenCalledWith(403);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'Invalid refresh token exchange payload',
});
});
// P2: Provider error forwarding
test('handleSocialAuthCallback forwards provider error to frontend callback', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({
projectId: 'project_1',
provider: 'github',
callbackUrl: 'http://localhost:5173/auth/callback',
}));
const req = makeReq({
params: { provider: 'github' },
query: {
error: 'access_denied',
error_description: 'The user denied the request',
state: 'state_with_error',
},
});
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('error='));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('The+user+denied'));
});
// P1: Email collision when provider email is not verified
test('handleSocialAuthCallback rejects if email exists but provider email not verified', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({
projectId: 'project_1',
provider: 'github',
callbackUrl: 'http://localhost:5173/auth/callback',
}));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
const existingUser = { _id: 'existing_user', email: 'alice@example.com' };
// User not found by githubId, but found by email, then refetched after update
mockUsersModel.findOne
.mockResolvedValueOnce(null) // by githubId
.mockResolvedValueOnce(existingUser) // by email
.mockResolvedValueOnce({ ...existingUser, githubId: '123' }); // after updateOne
mockUsersModel.updateOne.mockResolvedValueOnce({ modifiedCount: 1 });
// GitHub returns an email that IS verified at provider
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ access_token: 'github_access_token' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id: 123, login: 'alice', avatar_url: '' }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ([{ email: 'alice@example.com', verified: true, primary: true }]),
});
const req = makeReq({ params: { provider: 'github' }, query: { code: 'code_1', state: 'state_1' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
// With verified=true, should successfully link and redirect
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('rtCode='));
expect(mockUsersModel.updateOne).toHaveBeenCalled();
});
// P1: Email collision when provider email is NOT verified (rejection case)
test('handleSocialAuthCallback rejects existing email when provider email unverified', async () => {
redis.get.mockResolvedValueOnce(JSON.stringify({
projectId: 'project_1',
provider: 'google',
callbackUrl: 'http://localhost:5173/auth/callback',
}));
mockProjectFindByIdChain.lean.mockResolvedValueOnce(makeProject());
// User not found by googleId, but found by email
mockUsersModel.findOne
.mockResolvedValueOnce(null) // by googleId
.mockResolvedValueOnce({ _id: 'existing_user', email: 'alice@example.com' }); // by email
// Google returns unverified email (email_verified: false)
global.fetch
.mockResolvedValueOnce({
ok: true,
json: async () => ({ id_token: signGoogleIdToken({ email: 'alice@example.com', email_verified: false }) }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ keys: [GOOGLE_JWK] }),
});
const req = makeReq({ params: { provider: 'google' }, query: { code: 'code_1', state: 'state_1' } });
const res = makeRes();
await controller.handleSocialAuthCallback(req, res);
// Should redirect with error because email exists but not verified for linking
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('error='));
expect(res.redirect).toHaveBeenCalledWith(expect.stringContaining('not+verified'));
});
});