fix tests

This commit is contained in:
bkellam 2025-11-04 20:05:42 -08:00
parent 0a33cc9a36
commit 8703f757b9
2 changed files with 38 additions and 38 deletions

View file

@ -1,5 +1,5 @@
import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_NAME } from '@/lib/constants'; 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 { beforeEach, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended'; import { mockDeep, mockReset } from 'vitest-mock-extended';
@ -35,7 +35,7 @@ export const MOCK_API_KEY: ApiKey = {
createdById: '1', createdById: '1',
} }
export const MOCK_USER: User = { export const MOCK_USER_WITH_ACCOUNTS: User & { accounts: Account[] } = {
id: '1', id: '1',
name: 'Test User', name: 'Test User',
email: 'test@test.com', email: 'test@test.com',
@ -44,7 +44,7 @@ export const MOCK_USER: User = {
hashedPassword: null, hashedPassword: null,
emailVerified: null, emailVerified: null,
image: null, image: null,
permissionSyncedAt: null accounts: [],
} }
export const userScopedPrismaClientExtension = vi.fn(); export const userScopedPrismaClientExtension = vi.fn();

View file

@ -2,7 +2,7 @@ import { expect, test, vi, beforeEach, describe } from 'vitest';
import { Session } from 'next-auth'; import { Session } from 'next-auth';
import { notAuthenticated } from './lib/serviceError'; import { notAuthenticated } from './lib/serviceError';
import { getAuthContext, getAuthenticatedUser, withAuthV2, withOptionalAuthV2 } from './withAuthV2'; 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'; import { OrgRole } from '@sourcebot/db';
const mocks = vi.hoisted(() => { const mocks = vi.hoisted(() => {
@ -83,7 +83,7 @@ describe('getAuthenticatedUser', () => {
test('should return a user object if a valid session is present', async () => { test('should return a user object if a valid session is present', async () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
setMockSession(createMockSession({ user: { id: 'test-user-id' } })); 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 () => { test('should return a user object if a valid api key is present', async () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.apiKey.findUnique.mockResolvedValue({ 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -183,7 +183,7 @@ describe('getAuthContext', () => {
expect(authContext).not.toBeUndefined(); expect(authContext).not.toBeUndefined();
expect(authContext).toStrictEqual({ expect(authContext).toStrictEqual({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -213,7 +213,7 @@ describe('getAuthContext', () => {
expect(authContext).not.toBeUndefined(); expect(authContext).not.toBeUndefined();
expect(authContext).toStrictEqual({ expect(authContext).toStrictEqual({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -238,7 +238,7 @@ describe('getAuthContext', () => {
expect(authContext).not.toBeUndefined(); expect(authContext).not.toBeUndefined();
expect(authContext).toStrictEqual({ expect(authContext).toStrictEqual({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -286,7 +286,7 @@ describe('withAuthV2', () => {
const result = await withAuthV2(cb); const result = await withAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -316,7 +316,7 @@ describe('withAuthV2', () => {
const result = await withAuthV2(cb); const result = await withAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -351,7 +351,7 @@ describe('withAuthV2', () => {
const result = await withAuthV2(cb); const result = await withAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -386,7 +386,7 @@ describe('withAuthV2', () => {
const result = await withAuthV2(cb); const result = await withAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ 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 () => { test('should return a service error if the user is a guest of the organization', async () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ 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 () => { test('should return a service error if the user is not a member of the organization (guest role)', async () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -483,7 +483,7 @@ describe('withOptionalAuthV2', () => {
const result = await withOptionalAuthV2(cb); const result = await withOptionalAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -513,7 +513,7 @@ describe('withOptionalAuthV2', () => {
const result = await withOptionalAuthV2(cb); const result = await withOptionalAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -548,7 +548,7 @@ describe('withOptionalAuthV2', () => {
const result = await withOptionalAuthV2(cb); const result = await withOptionalAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -583,7 +583,7 @@ describe('withOptionalAuthV2', () => {
const result = await withOptionalAuthV2(cb); const result = await withOptionalAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: MOCK_ORG, 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 () => { 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'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ 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 () => { test('should return a service error if the user is a guest of the organization', async () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ 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 () => { test('should return a service error if the user is not a member of the organization (guest role)', async () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -662,7 +662,7 @@ describe('withOptionalAuthV2', () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -677,7 +677,7 @@ describe('withOptionalAuthV2', () => {
const result = await withOptionalAuthV2(cb); const result = await withOptionalAuthV2(cb);
expect(cb).toHaveBeenCalledWith({ expect(cb).toHaveBeenCalledWith({
user: { user: {
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}, },
org: { org: {
@ -696,7 +696,7 @@ describe('withOptionalAuthV2', () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({
@ -718,7 +718,7 @@ describe('withOptionalAuthV2', () => {
const userId = 'test-user-id'; const userId = 'test-user-id';
prisma.user.findUnique.mockResolvedValue({ prisma.user.findUnique.mockResolvedValue({
...MOCK_USER, ...MOCK_USER_WITH_ACCOUNTS,
id: userId, id: userId,
}); });
prisma.org.findUnique.mockResolvedValue({ prisma.org.findUnique.mockResolvedValue({