130 lines
4.3 KiB
TypeScript
130 lines
4.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { useAuth } from '../src/store/auth';
|
|
import { setAccessToken, getAccessToken } from '../src/api/client';
|
|
import type { User } from '../src/api/types';
|
|
|
|
const ADMIN: User = {
|
|
id: 'u1',
|
|
email: 'admin@test.local',
|
|
firstName: 'Admin',
|
|
lastName: null,
|
|
age: null,
|
|
heightCm: null,
|
|
phone: null,
|
|
isAdmin: true,
|
|
availability: null,
|
|
priorities: null,
|
|
exclusions: null,
|
|
developMuscles: [],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: [],
|
|
createdAt: '2026-08-22T10:00:00.000Z',
|
|
};
|
|
|
|
const NON_ADMIN: User = { ...ADMIN, id: 'u2', email: 'jan@test.local', isAdmin: false };
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
function mockFetchSequence(...responses: Response[]) {
|
|
const fetchMock = vi.fn(async (url: string) => {
|
|
const next = responses.shift();
|
|
if (!next) throw new Error(`No more mocked responses for url: ${url}`);
|
|
return next;
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
return fetchMock;
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
await new Promise((r) => setTimeout(r, 25));
|
|
setAccessToken(null);
|
|
localStorage.clear();
|
|
useAuth.setState({ user: null, ready: false });
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin auth store', () => {
|
|
it('login stores the admin user and the access token', async () => {
|
|
mockFetchSequence(jsonResponse(200, { user: ADMIN, accessToken: 'tok-1' }));
|
|
await useAuth.getState().login('admin@test.local', 'Password123!');
|
|
expect(useAuth.getState().user).toEqual(ADMIN);
|
|
expect(getAccessToken()).toBe('tok-1');
|
|
expect(localStorage.getItem('fitos_admin_access')).toBe('tok-1');
|
|
});
|
|
|
|
it('login rejects for non-admin users and clears the token', async () => {
|
|
mockFetchSequence(jsonResponse(200, { user: NON_ADMIN, accessToken: 'tok-1' }));
|
|
await expect(useAuth.getState().login('jan@test.local', 'Password123!')).rejects.toThrow(
|
|
'Dostęp tylko dla administratorów',
|
|
);
|
|
expect(useAuth.getState().user).toBeNull();
|
|
expect(getAccessToken()).toBeNull();
|
|
expect(localStorage.getItem('fitos_admin_access')).toBeNull();
|
|
});
|
|
|
|
it('login rejects on invalid credentials', async () => {
|
|
mockFetchSequence(
|
|
jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'Invalid credentials' } }),
|
|
);
|
|
await expect(useAuth.getState().login('admin@test.local', 'wrong')).rejects.toThrow(
|
|
'Invalid credentials',
|
|
);
|
|
expect(useAuth.getState().user).toBeNull();
|
|
});
|
|
|
|
it('logout clears the user and the token even when the API call fails', async () => {
|
|
useAuth.setState({ user: ADMIN });
|
|
setAccessToken('tok-2');
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response('', { status: 500 })));
|
|
await useAuth.getState().logout();
|
|
expect(useAuth.getState().user).toBeNull();
|
|
expect(getAccessToken()).toBeNull();
|
|
});
|
|
|
|
it('init restores an admin session via refresh + /me', async () => {
|
|
const fetchMock = mockFetchSequence(
|
|
jsonResponse(200, { accessToken: 'tok-3' }),
|
|
jsonResponse(200, { user: ADMIN }),
|
|
);
|
|
await useAuth.getState().init();
|
|
expect(useAuth.getState().user).toEqual(ADMIN);
|
|
expect(useAuth.getState().ready).toBe(true);
|
|
expect(getAccessToken()).toBe('tok-3');
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('init rejects a non-admin session', async () => {
|
|
mockFetchSequence(
|
|
jsonResponse(200, { accessToken: 'tok-4' }),
|
|
jsonResponse(200, { user: NON_ADMIN }),
|
|
);
|
|
await useAuth.getState().init();
|
|
expect(useAuth.getState().user).toBeNull();
|
|
expect(useAuth.getState().ready).toBe(true);
|
|
});
|
|
|
|
it('init without a valid session ends with a null user and ready=true', async () => {
|
|
mockFetchSequence(
|
|
jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'Invalid refresh token' } }),
|
|
);
|
|
await useAuth.getState().init();
|
|
expect(useAuth.getState().user).toBeNull();
|
|
expect(useAuth.getState().ready).toBe(true);
|
|
});
|
|
|
|
it('the fitos:logout event clears the user', () => {
|
|
useAuth.setState({ user: ADMIN });
|
|
window.dispatchEvent(new Event('fitos:logout'));
|
|
expect(useAuth.getState().user).toBeNull();
|
|
});
|
|
});
|