152 lines
5.8 KiB
TypeScript
152 lines
5.8 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import Login from '../src/pages/Login';
|
|
import { useAuth } from '../src/store/auth';
|
|
import { setAccessToken } 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 renderLogin() {
|
|
return render(
|
|
<MemoryRouter initialEntries={['/login']}>
|
|
<Routes>
|
|
<Route path="/login" element={<Login />} />
|
|
<Route path="/" element={<div>HOME</div>} />
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
await new Promise((r) => setTimeout(r, 25));
|
|
setAccessToken(null);
|
|
localStorage.clear();
|
|
useAuth.setState({ user: null, ready: false });
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin Login page', () => {
|
|
it('renders the form', () => {
|
|
renderLogin();
|
|
expect(screen.getByRole('heading', { name: 'FitOS Admin' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'Zaloguj' })).toBeInTheDocument();
|
|
});
|
|
|
|
it('logs in an admin and navigates home', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () => jsonResponse(200, { user: ADMIN, accessToken: 'tok-1' })),
|
|
);
|
|
const user = userEvent.setup();
|
|
const { container } = renderLogin();
|
|
const email = container.querySelector('input[type="email"]') as HTMLInputElement;
|
|
const password = container.querySelector('input[type="password"]') as HTMLInputElement;
|
|
await user.type(email, 'admin@test.local');
|
|
await user.type(password, 'Password123!');
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
expect(await screen.findByText('HOME')).toBeInTheDocument();
|
|
expect(useAuth.getState().user).toEqual(ADMIN);
|
|
});
|
|
|
|
it('rejects a non-admin login with a dedicated error and stays on the page', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () => jsonResponse(200, { user: NON_ADMIN, accessToken: 'tok-1' })),
|
|
);
|
|
const user = userEvent.setup();
|
|
const { container } = renderLogin();
|
|
const email = container.querySelector('input[type="email"]') as HTMLInputElement;
|
|
const password = container.querySelector('input[type="password"]') as HTMLInputElement;
|
|
await user.type(email, 'jan@test.local');
|
|
await user.type(password, 'Password123!');
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
expect(await screen.findByText('Dostęp tylko dla administratorów')).toBeInTheDocument();
|
|
expect(screen.queryByText('HOME')).not.toBeInTheDocument();
|
|
expect(useAuth.getState().user).toBeNull();
|
|
});
|
|
|
|
it('saves the email to localStorage when "Zapamiętaj mnie" is checked', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () => jsonResponse(200, { user: ADMIN, accessToken: 'tok-1' })),
|
|
);
|
|
const user = userEvent.setup();
|
|
const { container } = renderLogin();
|
|
const email = container.querySelector('input[type="email"]') as HTMLInputElement;
|
|
const password = container.querySelector('input[type="password"]') as HTMLInputElement;
|
|
await user.type(email, 'admin@test.local');
|
|
await user.type(password, 'Password123!');
|
|
await user.click(screen.getByRole('checkbox', { name: 'Zapamiętaj mnie' }));
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
expect(await screen.findByText('HOME')).toBeInTheDocument();
|
|
expect(localStorage.getItem('fitos-remembered-email')).toBe('admin@test.local');
|
|
});
|
|
|
|
it('pre-fills the email from localStorage and clears it when unchecked', async () => {
|
|
localStorage.setItem('fitos-remembered-email', 'admin@test.local');
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () => jsonResponse(200, { user: ADMIN, accessToken: 'tok-1' })),
|
|
);
|
|
const user = userEvent.setup();
|
|
const { container } = renderLogin();
|
|
const email = container.querySelector('input[type="email"]') as HTMLInputElement;
|
|
expect(email.value).toBe('admin@test.local');
|
|
const password = container.querySelector('input[type="password"]') as HTMLInputElement;
|
|
await user.type(password, 'Password123!');
|
|
await user.click(screen.getByRole('checkbox', { name: 'Zapamiętaj mnie' }));
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
expect(await screen.findByText('HOME')).toBeInTheDocument();
|
|
expect(localStorage.getItem('fitos-remembered-email')).toBeNull();
|
|
});
|
|
|
|
it('shows the server error message on invalid credentials', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () =>
|
|
jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'Invalid credentials' } }),
|
|
),
|
|
);
|
|
const user = userEvent.setup();
|
|
const { container } = renderLogin();
|
|
const email = container.querySelector('input[type="email"]') as HTMLInputElement;
|
|
const password = container.querySelector('input[type="password"]') as HTMLInputElement;
|
|
await user.type(email, 'admin@test.local');
|
|
await user.type(password, 'wrong');
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
expect(await screen.findByText('Invalid credentials')).toBeInTheDocument();
|
|
});
|
|
});
|