wip
This commit is contained in:
63
tests/unit/website/auth/InMemoryAuthService.test.ts
Normal file
63
tests/unit/website/auth/InMemoryAuthService.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
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 does not attempt to modify cookies directly', async () => {
|
||||
const service = new InMemoryAuthService();
|
||||
|
||||
await service.logout();
|
||||
|
||||
expect(cookieStore.get).not.toHaveBeenCalled();
|
||||
expect(cookieStore.set).not.toHaveBeenCalled();
|
||||
expect(cookieStore.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user