diff --git a/packages/web/src/__mocks__/prisma.ts b/packages/web/src/__mocks__/prisma.ts index 4db4de46..473dab64 100644 --- a/packages/web/src/__mocks__/prisma.ts +++ b/packages/web/src/__mocks__/prisma.ts @@ -1,5 +1,5 @@ import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_NAME } from '@/lib/constants'; -import { ApiKey, Org, PrismaClient, User } from '@prisma/client'; +import { Account, ApiKey, Org, PrismaClient, User } from '@prisma/client'; import { beforeEach, vi } from 'vitest'; import { mockDeep, mockReset } from 'vitest-mock-extended'; @@ -35,7 +35,7 @@ export const MOCK_API_KEY: ApiKey = { createdById: '1', } -export const MOCK_USER: User = { +export const MOCK_USER_WITH_ACCOUNTS: User & { accounts: Account[] } = { id: '1', name: 'Test User', email: 'test@test.com', @@ -44,7 +44,7 @@ export const MOCK_USER: User = { hashedPassword: null, emailVerified: null, image: null, - permissionSyncedAt: null + accounts: [], } export const userScopedPrismaClientExtension = vi.fn(); \ No newline at end of file diff --git a/packages/web/src/withAuthV2.test.ts b/packages/web/src/withAuthV2.test.ts index 23d7d05b..5a1dd343 100644 --- a/packages/web/src/withAuthV2.test.ts +++ b/packages/web/src/withAuthV2.test.ts @@ -2,7 +2,7 @@ import { expect, test, vi, beforeEach, describe } from 'vitest'; import { Session } from 'next-auth'; import { notAuthenticated } from './lib/serviceError'; import { getAuthContext, getAuthenticatedUser, withAuthV2, withOptionalAuthV2 } from './withAuthV2'; -import { MOCK_API_KEY, MOCK_ORG, MOCK_USER, prisma } from './__mocks__/prisma'; +import { MOCK_API_KEY, MOCK_ORG, MOCK_USER_WITH_ACCOUNTS, prisma } from './__mocks__/prisma'; import { OrgRole } from '@sourcebot/db'; const mocks = vi.hoisted(() => { @@ -83,7 +83,7 @@ describe('getAuthenticatedUser', () => { test('should return a user object if a valid session is present', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); setMockSession(createMockSession({ user: { id: 'test-user-id' } })); @@ -95,7 +95,7 @@ describe('getAuthenticatedUser', () => { test('should return a user object if a valid api key is present', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.apiKey.findUnique.mockResolvedValue({ @@ -165,7 +165,7 @@ describe('getAuthContext', () => { test('should return a auth context object if a valid session is present and the user is a member of the organization', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -183,7 +183,7 @@ describe('getAuthContext', () => { expect(authContext).not.toBeUndefined(); expect(authContext).toStrictEqual({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -195,7 +195,7 @@ describe('getAuthContext', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -213,7 +213,7 @@ describe('getAuthContext', () => { expect(authContext).not.toBeUndefined(); expect(authContext).toStrictEqual({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -225,7 +225,7 @@ describe('getAuthContext', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -238,7 +238,7 @@ describe('getAuthContext', () => { expect(authContext).not.toBeUndefined(); expect(authContext).toStrictEqual({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -268,7 +268,7 @@ describe('withAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -286,7 +286,7 @@ describe('withAuthV2', () => { const result = await withAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -298,7 +298,7 @@ describe('withAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -316,7 +316,7 @@ describe('withAuthV2', () => { const result = await withAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -328,7 +328,7 @@ describe('withAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -351,7 +351,7 @@ describe('withAuthV2', () => { const result = await withAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -363,7 +363,7 @@ describe('withAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -386,7 +386,7 @@ describe('withAuthV2', () => { const result = await withAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -398,7 +398,7 @@ describe('withAuthV2', () => { test('should return a service error if the user is a member of the organization but does not have a valid session', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -421,7 +421,7 @@ describe('withAuthV2', () => { test('should return a service error if the user is a guest of the organization', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -445,7 +445,7 @@ describe('withAuthV2', () => { test('should return a service error if the user is not a member of the organization (guest role)', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -465,7 +465,7 @@ describe('withOptionalAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -483,7 +483,7 @@ describe('withOptionalAuthV2', () => { const result = await withOptionalAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -495,7 +495,7 @@ describe('withOptionalAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -513,7 +513,7 @@ describe('withOptionalAuthV2', () => { const result = await withOptionalAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -525,7 +525,7 @@ describe('withOptionalAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -548,7 +548,7 @@ describe('withOptionalAuthV2', () => { const result = await withOptionalAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -560,7 +560,7 @@ describe('withOptionalAuthV2', () => { 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 () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -583,7 +583,7 @@ describe('withOptionalAuthV2', () => { const result = await withOptionalAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: MOCK_ORG, @@ -595,7 +595,7 @@ describe('withOptionalAuthV2', () => { test('should return a service error if the user is a member of the organization but does not have a valid session', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -618,7 +618,7 @@ describe('withOptionalAuthV2', () => { test('should return a service error if the user is a guest of the organization', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -642,7 +642,7 @@ describe('withOptionalAuthV2', () => { test('should return a service error if the user is not a member of the organization (guest role)', async () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -662,7 +662,7 @@ describe('withOptionalAuthV2', () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -677,7 +677,7 @@ describe('withOptionalAuthV2', () => { const result = await withOptionalAuthV2(cb); expect(cb).toHaveBeenCalledWith({ user: { - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }, org: { @@ -696,7 +696,7 @@ describe('withOptionalAuthV2', () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({ @@ -718,7 +718,7 @@ describe('withOptionalAuthV2', () => { const userId = 'test-user-id'; prisma.user.findUnique.mockResolvedValue({ - ...MOCK_USER, + ...MOCK_USER_WITH_ACCOUNTS, id: userId, }); prisma.org.findUnique.mockResolvedValue({