refactor: improve test structure for Bun compatibility
- Update all test files to use named mock variables instead of inline mocks - Clear mock history in beforeEach instead of using mock.restore() - Add default mock implementations stored at module level - Document that tests should not use --randomize flag due to mock.module() limitations - All 89 unit tests pass consistently without randomization
This commit is contained in:
@@ -4,12 +4,33 @@
|
||||
* Tests the permission system for tournament management
|
||||
*/
|
||||
|
||||
import { describe, test, expect, mock } from 'bun:test';
|
||||
import { describe, test, expect, mock, beforeEach } from 'bun:test';
|
||||
import { hasRole, canManageTournament, canCreateTournaments } from '@/lib/permissions';
|
||||
import { getSession } from '@/lib/auth-simple';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import type { User } from '@prisma/client';
|
||||
|
||||
// Create mock functions at module level
|
||||
const getSessionMock = mock(() => {});
|
||||
const userFindUniqueMock = mock(() => {});
|
||||
const eventFindUniqueMock = mock(() => {});
|
||||
|
||||
// Mock the getSession and prisma functions
|
||||
mock.module('@/lib/auth-simple', () => ({
|
||||
getSession: getSessionMock,
|
||||
}));
|
||||
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
user: {
|
||||
findUnique: userFindUniqueMock,
|
||||
},
|
||||
event: {
|
||||
findUnique: eventFindUniqueMock,
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
// Helper to create mock user
|
||||
const createMockUser = (id: string, email: string, role: string): User => ({
|
||||
id,
|
||||
@@ -23,30 +44,21 @@ const createMockUser = (id: string, email: string, role: string): User => ({
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
|
||||
// Mock the getSession and prisma functions
|
||||
mock.module('@/lib/auth-simple', () => ({
|
||||
getSession: mock(() => {}),
|
||||
}));
|
||||
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
user: {
|
||||
findUnique: mock(() => {}),
|
||||
},
|
||||
event: {
|
||||
findUnique: mock(() => {}),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
describe('Permissions', () => {
|
||||
beforeEach(() => {
|
||||
// Reset mock implementations to default (no-op) before each test
|
||||
getSessionMock.mockImplementation(() => undefined);
|
||||
userFindUniqueMock.mockImplementation(() => undefined);
|
||||
eventFindUniqueMock.mockImplementation(() => undefined);
|
||||
});
|
||||
|
||||
describe('hasRole', () => {
|
||||
test('should allow club_admin to access tournament_admin resources', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: '1', email: 'test@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('1', 'test@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -55,11 +67,11 @@ describe('Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny player from accessing tournament_admin resources', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: '1', email: 'test@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('1', 'test@example.com', 'player')
|
||||
);
|
||||
|
||||
@@ -68,7 +80,7 @@ describe('Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny unauthenticated user', async () => {
|
||||
getSession.mockImplementation(async () => null);
|
||||
getSessionMock.mockImplementation(async () => null);
|
||||
|
||||
const result = await hasRole('club_admin');
|
||||
expect(result.allowed).toBe(false);
|
||||
@@ -78,11 +90,11 @@ describe('Permissions', () => {
|
||||
|
||||
describe('canManageTournament', () => {
|
||||
test('should allow club_admin to manage any tournament', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -91,11 +103,11 @@ describe('Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny player from managing tournaments', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
@@ -107,11 +119,11 @@ describe('Permissions', () => {
|
||||
|
||||
describe('canCreateTournaments', () => {
|
||||
test('should allow tournament_admin to create tournaments', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('admin-1', 'admin@example.com', 'tournament_admin')
|
||||
);
|
||||
|
||||
@@ -120,11 +132,11 @@ describe('Permissions', () => {
|
||||
});
|
||||
|
||||
test('should allow club_admin to create tournaments', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -133,11 +145,11 @@ describe('Permissions', () => {
|
||||
});
|
||||
|
||||
test('should deny player from creating tournaments', async () => {
|
||||
getSession.mockImplementation(async () => ({
|
||||
getSessionMock.mockImplementation(async () => ({
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
userFindUniqueMock.mockImplementation(async () =>
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user