Skip to content

Commit 8703f75

Browse files
fix tests
1 parent 0a33cc9 commit 8703f75

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

packages/web/src/__mocks__/prisma.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_NAME } from '@/lib/constants';
2-
import { ApiKey, Org, PrismaClient, User } from '@prisma/client';
2+
import { Account, ApiKey, Org, PrismaClient, User } from '@prisma/client';
33
import { beforeEach, vi } from 'vitest';
44
import { mockDeep, mockReset } from 'vitest-mock-extended';
55

@@ -35,7 +35,7 @@ export const MOCK_API_KEY: ApiKey = {
3535
createdById: '1',
3636
}
3737

38-
export const MOCK_USER: User = {
38+
export const MOCK_USER_WITH_ACCOUNTS: User & { accounts: Account[] } = {
3939
id: '1',
4040
name: 'Test User',
4141
@@ -44,7 +44,7 @@ export const MOCK_USER: User = {
4444
hashedPassword: null,
4545
emailVerified: null,
4646
image: null,
47-
permissionSyncedAt: null
47+
accounts: [],
4848
}
4949

5050
export const userScopedPrismaClientExtension = vi.fn();

packages/web/src/withAuthV2.test.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, test, vi, beforeEach, describe } from 'vitest';
22
import { Session } from 'next-auth';
33
import { notAuthenticated } from './lib/serviceError';
44
import { getAuthContext, getAuthenticatedUser, withAuthV2, withOptionalAuthV2 } from './withAuthV2';
5-
import { MOCK_API_KEY, MOCK_ORG, MOCK_USER, prisma } from './__mocks__/prisma';
5+
import { MOCK_API_KEY, MOCK_ORG, MOCK_USER_WITH_ACCOUNTS, prisma } from './__mocks__/prisma';
66
import { OrgRole } from '@sourcebot/db';
77

88
const mocks = vi.hoisted(() => {
@@ -83,7 +83,7 @@ describe('getAuthenticatedUser', () => {
8383
test('should return a user object if a valid session is present', async () => {
8484
const userId = 'test-user-id';
8585
prisma.user.findUnique.mockResolvedValue({
86-
...MOCK_USER,
86+
...MOCK_USER_WITH_ACCOUNTS,
8787
id: userId,
8888
});
8989
setMockSession(createMockSession({ user: { id: 'test-user-id' } }));
@@ -95,7 +95,7 @@ describe('getAuthenticatedUser', () => {
9595
test('should return a user object if a valid api key is present', async () => {
9696
const userId = 'test-user-id';
9797
prisma.user.findUnique.mockResolvedValue({
98-
...MOCK_USER,
98+
...MOCK_USER_WITH_ACCOUNTS,
9999
id: userId,
100100
});
101101
prisma.apiKey.findUnique.mockResolvedValue({
@@ -165,7 +165,7 @@ describe('getAuthContext', () => {
165165
test('should return a auth context object if a valid session is present and the user is a member of the organization', async () => {
166166
const userId = 'test-user-id';
167167
prisma.user.findUnique.mockResolvedValue({
168-
...MOCK_USER,
168+
...MOCK_USER_WITH_ACCOUNTS,
169169
id: userId,
170170
});
171171
prisma.org.findUnique.mockResolvedValue({
@@ -183,7 +183,7 @@ describe('getAuthContext', () => {
183183
expect(authContext).not.toBeUndefined();
184184
expect(authContext).toStrictEqual({
185185
user: {
186-
...MOCK_USER,
186+
...MOCK_USER_WITH_ACCOUNTS,
187187
id: userId,
188188
},
189189
org: MOCK_ORG,
@@ -195,7 +195,7 @@ describe('getAuthContext', () => {
195195
test('should return a auth context object if a valid session is present and the user is a member of the organization with OWNER role', async () => {
196196
const userId = 'test-user-id';
197197
prisma.user.findUnique.mockResolvedValue({
198-
...MOCK_USER,
198+
...MOCK_USER_WITH_ACCOUNTS,
199199
id: userId,
200200
});
201201
prisma.org.findUnique.mockResolvedValue({
@@ -213,7 +213,7 @@ describe('getAuthContext', () => {
213213
expect(authContext).not.toBeUndefined();
214214
expect(authContext).toStrictEqual({
215215
user: {
216-
...MOCK_USER,
216+
...MOCK_USER_WITH_ACCOUNTS,
217217
id: userId,
218218
},
219219
org: MOCK_ORG,
@@ -225,7 +225,7 @@ describe('getAuthContext', () => {
225225
test('should return a auth context object if a valid session is present and the user is not a member of the organization. The role should be GUEST.', async () => {
226226
const userId = 'test-user-id';
227227
prisma.user.findUnique.mockResolvedValue({
228-
...MOCK_USER,
228+
...MOCK_USER_WITH_ACCOUNTS,
229229
id: userId,
230230
});
231231
prisma.org.findUnique.mockResolvedValue({
@@ -238,7 +238,7 @@ describe('getAuthContext', () => {
238238
expect(authContext).not.toBeUndefined();
239239
expect(authContext).toStrictEqual({
240240
user: {
241-
...MOCK_USER,
241+
...MOCK_USER_WITH_ACCOUNTS,
242242
id: userId,
243243
},
244244
org: MOCK_ORG,
@@ -268,7 +268,7 @@ describe('withAuthV2', () => {
268268
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization', async () => {
269269
const userId = 'test-user-id';
270270
prisma.user.findUnique.mockResolvedValue({
271-
...MOCK_USER,
271+
...MOCK_USER_WITH_ACCOUNTS,
272272
id: userId,
273273
});
274274
prisma.org.findUnique.mockResolvedValue({
@@ -286,7 +286,7 @@ describe('withAuthV2', () => {
286286
const result = await withAuthV2(cb);
287287
expect(cb).toHaveBeenCalledWith({
288288
user: {
289-
...MOCK_USER,
289+
...MOCK_USER_WITH_ACCOUNTS,
290290
id: userId,
291291
},
292292
org: MOCK_ORG,
@@ -298,7 +298,7 @@ describe('withAuthV2', () => {
298298
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization with OWNER role', async () => {
299299
const userId = 'test-user-id';
300300
prisma.user.findUnique.mockResolvedValue({
301-
...MOCK_USER,
301+
...MOCK_USER_WITH_ACCOUNTS,
302302
id: userId,
303303
});
304304
prisma.org.findUnique.mockResolvedValue({
@@ -316,7 +316,7 @@ describe('withAuthV2', () => {
316316
const result = await withAuthV2(cb);
317317
expect(cb).toHaveBeenCalledWith({
318318
user: {
319-
...MOCK_USER,
319+
...MOCK_USER_WITH_ACCOUNTS,
320320
id: userId,
321321
},
322322
org: MOCK_ORG,
@@ -328,7 +328,7 @@ describe('withAuthV2', () => {
328328
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization (api key)', async () => {
329329
const userId = 'test-user-id';
330330
prisma.user.findUnique.mockResolvedValue({
331-
...MOCK_USER,
331+
...MOCK_USER_WITH_ACCOUNTS,
332332
id: userId,
333333
});
334334
prisma.org.findUnique.mockResolvedValue({
@@ -351,7 +351,7 @@ describe('withAuthV2', () => {
351351
const result = await withAuthV2(cb);
352352
expect(cb).toHaveBeenCalledWith({
353353
user: {
354-
...MOCK_USER,
354+
...MOCK_USER_WITH_ACCOUNTS,
355355
id: userId,
356356
},
357357
org: MOCK_ORG,
@@ -363,7 +363,7 @@ describe('withAuthV2', () => {
363363
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization with OWNER role (api key)', async () => {
364364
const userId = 'test-user-id';
365365
prisma.user.findUnique.mockResolvedValue({
366-
...MOCK_USER,
366+
...MOCK_USER_WITH_ACCOUNTS,
367367
id: userId,
368368
});
369369
prisma.org.findUnique.mockResolvedValue({
@@ -386,7 +386,7 @@ describe('withAuthV2', () => {
386386
const result = await withAuthV2(cb);
387387
expect(cb).toHaveBeenCalledWith({
388388
user: {
389-
...MOCK_USER,
389+
...MOCK_USER_WITH_ACCOUNTS,
390390
id: userId,
391391
},
392392
org: MOCK_ORG,
@@ -398,7 +398,7 @@ describe('withAuthV2', () => {
398398
test('should return a service error if the user is a member of the organization but does not have a valid session', async () => {
399399
const userId = 'test-user-id';
400400
prisma.user.findUnique.mockResolvedValue({
401-
...MOCK_USER,
401+
...MOCK_USER_WITH_ACCOUNTS,
402402
id: userId,
403403
});
404404
prisma.org.findUnique.mockResolvedValue({
@@ -421,7 +421,7 @@ describe('withAuthV2', () => {
421421
test('should return a service error if the user is a guest of the organization', async () => {
422422
const userId = 'test-user-id';
423423
prisma.user.findUnique.mockResolvedValue({
424-
...MOCK_USER,
424+
...MOCK_USER_WITH_ACCOUNTS,
425425
id: userId,
426426
});
427427
prisma.org.findUnique.mockResolvedValue({
@@ -445,7 +445,7 @@ describe('withAuthV2', () => {
445445
test('should return a service error if the user is not a member of the organization (guest role)', async () => {
446446
const userId = 'test-user-id';
447447
prisma.user.findUnique.mockResolvedValue({
448-
...MOCK_USER,
448+
...MOCK_USER_WITH_ACCOUNTS,
449449
id: userId,
450450
});
451451
prisma.org.findUnique.mockResolvedValue({
@@ -465,7 +465,7 @@ describe('withOptionalAuthV2', () => {
465465
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization', async () => {
466466
const userId = 'test-user-id';
467467
prisma.user.findUnique.mockResolvedValue({
468-
...MOCK_USER,
468+
...MOCK_USER_WITH_ACCOUNTS,
469469
id: userId,
470470
});
471471
prisma.org.findUnique.mockResolvedValue({
@@ -483,7 +483,7 @@ describe('withOptionalAuthV2', () => {
483483
const result = await withOptionalAuthV2(cb);
484484
expect(cb).toHaveBeenCalledWith({
485485
user: {
486-
...MOCK_USER,
486+
...MOCK_USER_WITH_ACCOUNTS,
487487
id: userId,
488488
},
489489
org: MOCK_ORG,
@@ -495,7 +495,7 @@ describe('withOptionalAuthV2', () => {
495495
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization with OWNER role', async () => {
496496
const userId = 'test-user-id';
497497
prisma.user.findUnique.mockResolvedValue({
498-
...MOCK_USER,
498+
...MOCK_USER_WITH_ACCOUNTS,
499499
id: userId,
500500
});
501501
prisma.org.findUnique.mockResolvedValue({
@@ -513,7 +513,7 @@ describe('withOptionalAuthV2', () => {
513513
const result = await withOptionalAuthV2(cb);
514514
expect(cb).toHaveBeenCalledWith({
515515
user: {
516-
...MOCK_USER,
516+
...MOCK_USER_WITH_ACCOUNTS,
517517
id: userId,
518518
},
519519
org: MOCK_ORG,
@@ -525,7 +525,7 @@ describe('withOptionalAuthV2', () => {
525525
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization (api key)', async () => {
526526
const userId = 'test-user-id';
527527
prisma.user.findUnique.mockResolvedValue({
528-
...MOCK_USER,
528+
...MOCK_USER_WITH_ACCOUNTS,
529529
id: userId,
530530
});
531531
prisma.org.findUnique.mockResolvedValue({
@@ -548,7 +548,7 @@ describe('withOptionalAuthV2', () => {
548548
const result = await withOptionalAuthV2(cb);
549549
expect(cb).toHaveBeenCalledWith({
550550
user: {
551-
...MOCK_USER,
551+
...MOCK_USER_WITH_ACCOUNTS,
552552
id: userId,
553553
},
554554
org: MOCK_ORG,
@@ -560,7 +560,7 @@ describe('withOptionalAuthV2', () => {
560560
test('should call the callback with the auth context object if a valid session is present and the user is a member of the organization with OWNER role (api key)', async () => {
561561
const userId = 'test-user-id';
562562
prisma.user.findUnique.mockResolvedValue({
563-
...MOCK_USER,
563+
...MOCK_USER_WITH_ACCOUNTS,
564564
id: userId,
565565
});
566566
prisma.org.findUnique.mockResolvedValue({
@@ -583,7 +583,7 @@ describe('withOptionalAuthV2', () => {
583583
const result = await withOptionalAuthV2(cb);
584584
expect(cb).toHaveBeenCalledWith({
585585
user: {
586-
...MOCK_USER,
586+
...MOCK_USER_WITH_ACCOUNTS,
587587
id: userId,
588588
},
589589
org: MOCK_ORG,
@@ -595,7 +595,7 @@ describe('withOptionalAuthV2', () => {
595595
test('should return a service error if the user is a member of the organization but does not have a valid session', async () => {
596596
const userId = 'test-user-id';
597597
prisma.user.findUnique.mockResolvedValue({
598-
...MOCK_USER,
598+
...MOCK_USER_WITH_ACCOUNTS,
599599
id: userId,
600600
});
601601
prisma.org.findUnique.mockResolvedValue({
@@ -618,7 +618,7 @@ describe('withOptionalAuthV2', () => {
618618
test('should return a service error if the user is a guest of the organization', async () => {
619619
const userId = 'test-user-id';
620620
prisma.user.findUnique.mockResolvedValue({
621-
...MOCK_USER,
621+
...MOCK_USER_WITH_ACCOUNTS,
622622
id: userId,
623623
});
624624
prisma.org.findUnique.mockResolvedValue({
@@ -642,7 +642,7 @@ describe('withOptionalAuthV2', () => {
642642
test('should return a service error if the user is not a member of the organization (guest role)', async () => {
643643
const userId = 'test-user-id';
644644
prisma.user.findUnique.mockResolvedValue({
645-
...MOCK_USER,
645+
...MOCK_USER_WITH_ACCOUNTS,
646646
id: userId,
647647
});
648648
prisma.org.findUnique.mockResolvedValue({
@@ -662,7 +662,7 @@ describe('withOptionalAuthV2', () => {
662662

663663
const userId = 'test-user-id';
664664
prisma.user.findUnique.mockResolvedValue({
665-
...MOCK_USER,
665+
...MOCK_USER_WITH_ACCOUNTS,
666666
id: userId,
667667
});
668668
prisma.org.findUnique.mockResolvedValue({
@@ -677,7 +677,7 @@ describe('withOptionalAuthV2', () => {
677677
const result = await withOptionalAuthV2(cb);
678678
expect(cb).toHaveBeenCalledWith({
679679
user: {
680-
...MOCK_USER,
680+
...MOCK_USER_WITH_ACCOUNTS,
681681
id: userId,
682682
},
683683
org: {
@@ -696,7 +696,7 @@ describe('withOptionalAuthV2', () => {
696696

697697
const userId = 'test-user-id';
698698
prisma.user.findUnique.mockResolvedValue({
699-
...MOCK_USER,
699+
...MOCK_USER_WITH_ACCOUNTS,
700700
id: userId,
701701
});
702702
prisma.org.findUnique.mockResolvedValue({
@@ -718,7 +718,7 @@ describe('withOptionalAuthV2', () => {
718718

719719
const userId = 'test-user-id';
720720
prisma.user.findUnique.mockResolvedValue({
721-
...MOCK_USER,
721+
...MOCK_USER_WITH_ACCOUNTS,
722722
id: userId,
723723
});
724724
prisma.org.findUnique.mockResolvedValue({

0 commit comments

Comments
 (0)