2026-09-19 21:53:35 +03:00
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
|
|
2026-09-19 22:03:46 +03:00
|
|
|
import {
|
|
|
|
|
apiError,
|
|
|
|
|
findRequest,
|
|
|
|
|
installFetch,
|
|
|
|
|
json,
|
|
|
|
|
makeUser,
|
|
|
|
|
recordedRequests,
|
|
|
|
|
renderApp,
|
|
|
|
|
} from './helpers';
|
2026-09-19 21:53:35 +03:00
|
|
|
|
|
|
|
|
const instanceOpen = {
|
|
|
|
|
name: 'glchat-test',
|
|
|
|
|
version: 'v0.1.0-test',
|
|
|
|
|
registration_enabled: true,
|
|
|
|
|
allow_guild_creation: true,
|
|
|
|
|
max_guilds_per_user: 5,
|
|
|
|
|
max_members_per_guild: 100,
|
|
|
|
|
max_message_length: 2000,
|
|
|
|
|
main_guild_id: 'g-main',
|
|
|
|
|
user_count: 1,
|
|
|
|
|
guild_count: 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe('страница входа', () => {
|
|
|
|
|
it('успешный вход переводит в /app', async () => {
|
|
|
|
|
const user = makeUser();
|
|
|
|
|
const fetchMock = installFetch([
|
|
|
|
|
{ match: '/api/v1/instance', response: () => json({ instance: instanceOpen }) },
|
|
|
|
|
{ match: '/api/v1/auth/login', method: 'POST', response: () => json({ user }) },
|
|
|
|
|
{ match: '/api/v1/users/@me', response: () => json({ user }) },
|
|
|
|
|
{ match: '/api/v1/users/@me/guilds', response: () => json({ guilds: [] }) },
|
|
|
|
|
{ match: '/api/v1/guilds/g-main/join', method: 'POST', response: () => json({ ok: true }) },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { router } = renderApp('/login');
|
|
|
|
|
const visitor = userEvent.setup();
|
|
|
|
|
|
|
|
|
|
await visitor.type(await screen.findByLabelText('Почта'), 'alice@example.com');
|
|
|
|
|
await visitor.type(screen.getByLabelText('Пароль'), 'super-secret-1');
|
|
|
|
|
await visitor.click(screen.getByRole('button', { name: 'Войти' }));
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
expect(router.state.location.pathname).toBe('/app/empty');
|
|
|
|
|
});
|
|
|
|
|
expect(
|
|
|
|
|
await screen.findByRole('button', { name: 'Присоединиться к главному серверу' }),
|
|
|
|
|
).toBeVisible();
|
|
|
|
|
|
2026-09-19 22:03:46 +03:00
|
|
|
expect(findRequest(fetchMock, { url: '/auth/login', method: 'POST' })?.body).toEqual({
|
2026-09-19 21:53:35 +03:00
|
|
|
email: 'alice@example.com',
|
|
|
|
|
password: 'super-secret-1',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('при auth.2fa_required показывает поле кода и повторяет вход с totp_code', async () => {
|
|
|
|
|
const user = makeUser();
|
|
|
|
|
let loginAttempts = 0;
|
|
|
|
|
const fetchMock = installFetch([
|
|
|
|
|
{ match: '/api/v1/instance', response: () => json({ instance: instanceOpen }) },
|
|
|
|
|
{
|
|
|
|
|
match: '/api/v1/auth/login',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
response: () => {
|
|
|
|
|
loginAttempts += 1;
|
|
|
|
|
return loginAttempts === 1 ? apiError('auth.2fa_required', 401) : json({ user });
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ match: '/api/v1/users/@me', response: () => json({ user }) },
|
|
|
|
|
{ match: '/api/v1/users/@me/guilds', response: () => json({ guilds: [] }) },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { router } = renderApp('/login');
|
|
|
|
|
const visitor = userEvent.setup();
|
|
|
|
|
|
|
|
|
|
await visitor.type(await screen.findByLabelText('Почта'), 'alice@example.com');
|
|
|
|
|
await visitor.type(screen.getByLabelText('Пароль'), 'super-secret-1');
|
|
|
|
|
await visitor.click(screen.getByRole('button', { name: 'Войти' }));
|
|
|
|
|
|
|
|
|
|
const totpField = await screen.findByLabelText('Код 2FA');
|
|
|
|
|
expect(totpField).toBeVisible();
|
|
|
|
|
|
|
|
|
|
await visitor.type(totpField, '123456');
|
|
|
|
|
await visitor.click(screen.getByRole('button', { name: 'Войти' }));
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
expect(router.state.location.pathname).toBe('/app/empty');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-19 22:03:46 +03:00
|
|
|
const logins = recordedRequests(fetchMock).filter(
|
|
|
|
|
(request) => request.url.includes('/auth/login') && request.method === 'POST',
|
|
|
|
|
);
|
|
|
|
|
expect(logins).toHaveLength(2);
|
|
|
|
|
expect(logins[1]?.body).toEqual({
|
2026-09-19 21:53:35 +03:00
|
|
|
email: 'alice@example.com',
|
|
|
|
|
password: 'super-secret-1',
|
|
|
|
|
totp_code: '123456',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|