Files
gridpilot.gg/tests/unit/website/auth/InMemoryAuthService.test.ts
2025-12-04 17:07:59 +01:00

63 lines
2.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
const cookieStore = {
get: vi.fn(),
set: vi.fn(),
delete: vi.fn(),
};
vi.mock('next/headers', () => ({
cookies: () => cookieStore,
}));
import { InMemoryAuthService } from '../../../../apps/website/lib/auth/InMemoryAuthService';
describe('InMemoryAuthService', () => {
beforeEach(() => {
cookieStore.get.mockReset();
cookieStore.set.mockReset();
cookieStore.delete.mockReset();
});
it('startIracingAuthRedirect returns redirectUrl with returnTo and state without touching cookies', async () => {
const service = new InMemoryAuthService();
const { redirectUrl, state } = await service.startIracingAuthRedirect('some');
expect(typeof state).toBe('string');
expect(state.length).toBeGreaterThan(0);
const url = new URL(redirectUrl, 'http://localhost');
expect(url.pathname).toBe('/auth/iracing/callback');
expect(url.searchParams.get('returnTo')).toBe('some');
expect(url.searchParams.get('state')).toBe(state);
expect(url.searchParams.get('code')).toBeTruthy();
expect(cookieStore.get).not.toHaveBeenCalled();
expect(cookieStore.set).not.toHaveBeenCalled();
expect(cookieStore.delete).not.toHaveBeenCalled();
});
it('loginWithIracingCallback returns deterministic demo session', async () => {
const service = new InMemoryAuthService();
const session = await service.loginWithIracingCallback({
code: 'dummy-code',
state: 'any-state',
});
expect(session.user.id).toBe('demo-user');
expect(session.user.primaryDriverId).toBeDefined();
expect(session.user.primaryDriverId).not.toBe('');
});
it('logout clears the demo session cookie via adapter', async () => {
const service = new InMemoryAuthService();
await service.logout();
expect(cookieStore.get).not.toHaveBeenCalled();
expect(cookieStore.set).not.toHaveBeenCalled();
expect(cookieStore.delete).toHaveBeenCalledWith('gp_demo_session');
});
});