|
| 1 | +/** |
| 2 | + * Tests for actions/admin/admin-servers.ts - Jellyfin login toggle |
| 3 | + * |
| 4 | + * These tests cover: |
| 5 | + * - toggleJellyfinLogin: enable/disable Jellyfin visibility on login page |
| 6 | + * - Admin authorization |
| 7 | + * - Input validation with Zod |
| 8 | + */ |
| 9 | + |
| 10 | +import { toggleJellyfinLogin } from '@/actions/admin/admin-servers' |
| 11 | +import { prisma } from '@/lib/prisma' |
| 12 | +import { getServerSession } from 'next-auth' |
| 13 | +import { revalidatePath } from 'next/cache' |
| 14 | + |
| 15 | +// Mock dependencies |
| 16 | +jest.mock('@/lib/prisma', () => ({ |
| 17 | + prisma: { |
| 18 | + jellyfinServer: { |
| 19 | + updateMany: jest.fn(), |
| 20 | + }, |
| 21 | + }, |
| 22 | +})) |
| 23 | + |
| 24 | +jest.mock('next-auth', () => ({ |
| 25 | + getServerSession: jest.fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +jest.mock('next/cache', () => ({ |
| 29 | + revalidatePath: jest.fn(), |
| 30 | +})) |
| 31 | + |
| 32 | +jest.mock('@/lib/auth', () => ({ |
| 33 | + authOptions: {}, |
| 34 | +})) |
| 35 | + |
| 36 | +const mockPrisma = prisma as jest.Mocked<typeof prisma> |
| 37 | +const mockGetServerSession = getServerSession as jest.MockedFunction<typeof getServerSession> |
| 38 | +const mockRevalidatePath = revalidatePath as jest.MockedFunction<typeof revalidatePath> |
| 39 | + |
| 40 | +describe('admin-servers actions', () => { |
| 41 | + const mockAdminSession = { |
| 42 | + user: { id: 'admin-123', name: 'Admin', email: '[email protected]', isAdmin: true }, |
| 43 | + expires: new Date(Date.now() + 86400000).toISOString(), |
| 44 | + } |
| 45 | + |
| 46 | + const mockNonAdminSession = { |
| 47 | + user: { id: 'user-123', name: 'User', email: '[email protected]', isAdmin: false }, |
| 48 | + expires: new Date(Date.now() + 86400000).toISOString(), |
| 49 | + } |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + jest.clearAllMocks() |
| 53 | + }) |
| 54 | + |
| 55 | + describe('toggleJellyfinLogin', () => { |
| 56 | + it('should enable Jellyfin login when passed true', async () => { |
| 57 | + mockGetServerSession.mockResolvedValue(mockAdminSession) |
| 58 | + mockPrisma.jellyfinServer.updateMany.mockResolvedValue({ count: 1 }) |
| 59 | + |
| 60 | + const result = await toggleJellyfinLogin(true) |
| 61 | + |
| 62 | + expect(result).toEqual({ success: true }) |
| 63 | + expect(mockPrisma.jellyfinServer.updateMany).toHaveBeenCalledWith({ |
| 64 | + where: { isActive: true }, |
| 65 | + data: { enabledForLogin: true }, |
| 66 | + }) |
| 67 | + expect(mockRevalidatePath).toHaveBeenCalledWith('/admin/settings') |
| 68 | + expect(mockRevalidatePath).toHaveBeenCalledWith('/') |
| 69 | + }) |
| 70 | + |
| 71 | + it('should disable Jellyfin login when passed false', async () => { |
| 72 | + mockGetServerSession.mockResolvedValue(mockAdminSession) |
| 73 | + mockPrisma.jellyfinServer.updateMany.mockResolvedValue({ count: 1 }) |
| 74 | + |
| 75 | + const result = await toggleJellyfinLogin(false) |
| 76 | + |
| 77 | + expect(result).toEqual({ success: true }) |
| 78 | + expect(mockPrisma.jellyfinServer.updateMany).toHaveBeenCalledWith({ |
| 79 | + where: { isActive: true }, |
| 80 | + data: { enabledForLogin: false }, |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should reject non-admin users', async () => { |
| 85 | + mockGetServerSession.mockResolvedValue(mockNonAdminSession) |
| 86 | + |
| 87 | + await expect(toggleJellyfinLogin(true)).rejects.toThrow() |
| 88 | + }) |
| 89 | + |
| 90 | + it('should reject unauthenticated users', async () => { |
| 91 | + mockGetServerSession.mockResolvedValue(null) |
| 92 | + |
| 93 | + await expect(toggleJellyfinLogin(true)).rejects.toThrow() |
| 94 | + }) |
| 95 | + |
| 96 | + it('should return error for invalid input', async () => { |
| 97 | + mockGetServerSession.mockResolvedValue(mockAdminSession) |
| 98 | + |
| 99 | + // TypeScript would normally catch this, but testing runtime validation |
| 100 | + const result = await toggleJellyfinLogin('invalid' as unknown as boolean) |
| 101 | + |
| 102 | + expect(result).toEqual({ |
| 103 | + success: false, |
| 104 | + error: 'Invalid input: enabled must be a boolean', |
| 105 | + }) |
| 106 | + expect(mockPrisma.jellyfinServer.updateMany).not.toHaveBeenCalled() |
| 107 | + }) |
| 108 | + |
| 109 | + it('should handle database errors gracefully', async () => { |
| 110 | + mockGetServerSession.mockResolvedValue(mockAdminSession) |
| 111 | + mockPrisma.jellyfinServer.updateMany.mockRejectedValue(new Error('Database error')) |
| 112 | + |
| 113 | + const result = await toggleJellyfinLogin(true) |
| 114 | + |
| 115 | + expect(result).toEqual({ |
| 116 | + success: false, |
| 117 | + error: 'Database error', |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + it('should handle unknown errors gracefully', async () => { |
| 122 | + mockGetServerSession.mockResolvedValue(mockAdminSession) |
| 123 | + mockPrisma.jellyfinServer.updateMany.mockRejectedValue('unknown error') |
| 124 | + |
| 125 | + const result = await toggleJellyfinLogin(true) |
| 126 | + |
| 127 | + expect(result).toEqual({ |
| 128 | + success: false, |
| 129 | + error: 'Failed to toggle Jellyfin login setting', |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments