Spaces:
Sleeping
Sleeping
File size: 22,767 Bytes
cd99321 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen, waitFor, fireEvent } from '../../tests/helpers/render';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { server } from '../../tests/helpers/msw/server';
import { resetAllStores } from '../../tests/helpers/store';
import LoginPage from './LoginPage';
// LoginPage uses inline styles for labels (no htmlFor/id pairing).
// We find inputs by placeholder text.
const EMAIL_PLACEHOLDER = 'your@email.com';
const PASSWORD_PLACEHOLDER = '••••••••';
beforeEach(() => {
resetAllStores();
});
describe('LoginPage', () => {
describe('FE-PAGE-LOGIN-001: Renders login form', () => {
it('shows email and password inputs', async () => {
render(<LoginPage />);
// Wait for appConfig to load (useEffect fetches it)
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
expect(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER)).toBeInTheDocument();
});
});
describe('FE-PAGE-LOGIN-002: Submitting valid credentials triggers login', () => {
it('shows takeoff animation on successful login', async () => {
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
// On success, takeoff overlay appears
await waitFor(() => {
expect(document.querySelector('.takeoff-overlay')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-003: Invalid credentials shows error', () => {
it('displays error message on login failure', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({ error: 'Invalid credentials' }, { status: 401 });
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'bad@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'wrongpass');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
// authStore.login throws, LoginPage catches and sets error text from API response
expect(screen.getByText('Invalid credentials')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-004: Loading state while login in progress', () => {
it('disables submit button and shows spinner during login', async () => {
server.use(
http.post('/api/auth/login', async () => {
await new Promise(resolve => setTimeout(resolve, 150));
return HttpResponse.json({
user: { id: 1, username: 'test', email: 'test@example.com', role: 'user' },
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
// While loading, button becomes disabled with spinner text
await waitFor(() => {
const submitBtn = screen.getByRole('button', { name: /signing in/i });
expect(submitBtn).toBeDisabled();
});
});
});
describe('FE-PAGE-LOGIN-007: Remember me sends remember_me to the API', () => {
it('renders an off toggle and forwards remember_me: true when toggled on', async () => {
let capturedBody: Record<string, unknown> | null = null;
server.use(
http.post('/api/auth/login', async ({ request }) => {
capturedBody = (await request.json()) as Record<string, unknown>;
return HttpResponse.json({ user: { id: 1, username: 'test', email: 'test@example.com', role: 'user' } });
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
const toggle = screen.getByRole('button', { name: /remember me/i });
expect(toggle).toHaveAttribute('aria-pressed', 'false');
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(toggle);
expect(toggle).toHaveAttribute('aria-pressed', 'true');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(capturedBody).toEqual(expect.objectContaining({ remember_me: true }));
});
});
});
describe('FE-PAGE-LOGIN-005: Registration toggle visible', () => {
it('shows a Register button to switch to registration mode', async () => {
// Default appConfig has allow_registration: true, has_users: true
render(<LoginPage />);
await waitFor(() => {
// The register toggle link text appears
expect(screen.getByRole('button', { name: /^register$/i })).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-006: Register creates account', () => {
it('switches to register mode and submits registration form', async () => {
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByRole('button', { name: /^register$/i })).toBeInTheDocument();
});
await user.click(screen.getByRole('button', { name: /^register$/i }));
// Username field appears in register mode
await waitFor(() => {
expect(screen.getByPlaceholderText('admin')).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText('admin'), 'newuser');
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'new@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /create account/i }));
// On success, takeoff animation
await waitFor(() => {
expect(document.querySelector('.takeoff-overlay')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-007: OIDC button shown when configured', () => {
it('renders SSO sign-in link when oidc_configured is true', async () => {
server.use(
http.get('/api/auth/app-config', () => {
return HttpResponse.json({
has_users: true,
allow_registration: true,
demo_mode: false,
oidc_configured: true,
oidc_display_name: 'Okta',
oidc_only_mode: false,
oidc_login: true,
password_login: true,
password_registration: true,
setup_complete: true,
});
}),
);
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByText(/sign in with okta/i)).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-008: Demo login available in demo mode', () => {
it('shows demo button when demo_mode is true', async () => {
server.use(
http.get('/api/auth/app-config', () => {
return HttpResponse.json({
has_users: true,
allow_registration: false,
demo_mode: true,
oidc_configured: false,
oidc_only_mode: false,
setup_complete: true,
});
}),
);
render(<LoginPage />);
await waitFor(() => {
// Demo hint button appears
expect(screen.getByText(/try the demo/i)).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-009: MFA prompt after initial login', () => {
it('shows MFA code input when login returns mfa_required', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
mfa_required: true,
mfa_token: 'test-mfa-token-abc',
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
// MFA step: the title changes to "Two-factor authentication"
await waitFor(() => {
expect(screen.getByText(/two-factor authentication/i)).toBeInTheDocument();
});
// MFA code input with correct placeholder
expect(screen.getByPlaceholderText('000000 or XXXX-XXXX')).toBeInTheDocument();
});
});
describe('FE-PAGE-LOGIN-010: Successful login triggers navigation', () => {
it('shows takeoff overlay (navigation signal) after successful auth', async () => {
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'pass1234');
await user.click(screen.getByRole('button', { name: /sign in/i }));
// Takeoff animation signals navigation away from login
await waitFor(() => {
expect(document.querySelector('.takeoff-overlay')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-011: Password change step appears when must_change_password', () => {
it('transitions to change password form when login returns must_change_password=true', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
user: { id: 1, username: 'test', email: 'test@example.com', role: 'user', must_change_password: true },
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('New password')).toBeInTheDocument();
});
expect(screen.getByPlaceholderText('Confirm new password')).toBeInTheDocument();
});
});
describe('FE-PAGE-LOGIN-012: Password change form validates length', () => {
it('shows error when new password is shorter than 8 characters', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
user: { id: 1, username: 'test', email: 'test@example.com', role: 'user', must_change_password: true },
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('New password')).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText('New password'), 'short');
await user.type(screen.getByPlaceholderText('Confirm new password'), 'short');
await user.click(screen.getByRole('button', { name: /update password/i }));
await waitFor(() => {
expect(screen.getByText(/at least 8/i)).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-013: Password change form validates mismatch', () => {
it('shows error when new passwords do not match', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
user: { id: 1, username: 'test', email: 'test@example.com', role: 'user', must_change_password: true },
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('New password')).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText('New password'), 'newpassword123');
await user.type(screen.getByPlaceholderText('Confirm new password'), 'differentpassword123');
await user.click(screen.getByRole('button', { name: /update password/i }));
await waitFor(() => {
expect(screen.getByText(/do not match/i)).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-014: Password change success navigates', () => {
it('shows takeoff overlay after successful password change', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
user: { id: 1, username: 'test', email: 'test@example.com', role: 'user', must_change_password: true },
});
}),
http.put('/api/auth/me/password', () => {
return HttpResponse.json({ success: true });
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('New password')).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText('New password'), 'newpassword123');
await user.type(screen.getByPlaceholderText('Confirm new password'), 'newpassword123');
await user.click(screen.getByRole('button', { name: /update password/i }));
await waitFor(() => {
expect(document.querySelector('.takeoff-overlay')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-015: First-setup mode switches to register when has_users=false', () => {
it('shows register form automatically when has_users is false', async () => {
server.use(
http.get('/api/auth/app-config', () => {
return HttpResponse.json({
has_users: false,
allow_registration: true,
demo_mode: false,
oidc_configured: false,
oidc_only_mode: false,
setup_complete: true,
});
}),
);
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText('admin')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-016: Registration disabled hides register option', () => {
it('does not show register button when allow_registration is false', async () => {
server.use(
http.get('/api/auth/app-config', () => {
return HttpResponse.json({
has_users: true,
allow_registration: false,
demo_mode: false,
oidc_configured: false,
oidc_only_mode: false,
setup_complete: true,
});
}),
);
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
expect(screen.queryByRole('button', { name: /^register$/i })).toBeNull();
});
});
describe('FE-PAGE-LOGIN-017: OIDC-only mode hides standard login form', () => {
it('does not render email/password inputs in oidc_only_mode', async () => {
server.use(
http.get('/api/auth/app-config', () => {
return HttpResponse.json({
has_users: true,
allow_registration: false,
demo_mode: false,
oidc_configured: true,
oidc_only_mode: true,
password_login: false,
oidc_login: true,
setup_complete: true,
});
}),
);
// Pass noRedirect via location.state to prevent window.location.href redirect
render(<LoginPage />, {
initialEntries: [{ pathname: '/login', state: { noRedirect: true } }],
});
await waitFor(() => {
expect(screen.queryByPlaceholderText(EMAIL_PLACEHOLDER)).toBeNull();
expect(screen.queryByPlaceholderText(PASSWORD_PLACEHOLDER)).toBeNull();
});
});
});
describe('FE-PAGE-LOGIN-018: MFA code submission completes login', () => {
it('shows takeoff overlay after successful MFA verification', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
mfa_required: true,
mfa_token: 'test-mfa-token-abc',
});
}),
http.post('/api/auth/mfa/verify-login', () => {
return HttpResponse.json({
user: { id: 1, username: 'test', email: 'test@example.com', role: 'user' },
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('000000 or XXXX-XXXX')).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText('000000 or XXXX-XXXX'), '123456');
await user.click(screen.getByRole('button', { name: /verify/i }));
await waitFor(() => {
expect(document.querySelector('.takeoff-overlay')).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-019: Empty MFA code shows error', () => {
it('shows error when MFA code is empty and does not show takeoff overlay', async () => {
server.use(
http.post('/api/auth/login', () => {
return HttpResponse.json({
mfa_required: true,
mfa_token: 'test-mfa-token-abc',
});
}),
);
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'user@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
await user.click(screen.getByRole('button', { name: /sign in/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('000000 or XXXX-XXXX')).toBeInTheDocument();
});
// Submit the form directly (bypasses browser constraint validation on required field)
const form = document.querySelector('form')!;
fireEvent.submit(form);
await waitFor(() => {
expect(screen.getByText(/enter the code from your authenticator/i)).toBeInTheDocument();
});
expect(document.querySelector('.takeoff-overlay')).toBeNull();
});
});
describe('FE-PAGE-LOGIN-020: Register form validates password length', () => {
it('shows error when registration password is shorter than 8 characters', async () => {
const user = userEvent.setup();
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByRole('button', { name: /^register$/i })).toBeInTheDocument();
});
await user.click(screen.getByRole('button', { name: /^register$/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText('admin')).toBeInTheDocument();
});
await user.type(screen.getByPlaceholderText('admin'), 'newuser');
await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'new@example.com');
await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'short');
await user.click(screen.getByRole('button', { name: /create account/i }));
await waitFor(() => {
expect(screen.getByText(/at least 8/i)).toBeInTheDocument();
});
});
});
describe('FE-PAGE-LOGIN-021: Invite token pre-fills register mode', () => {
it('renders register form when invite query param is present', async () => {
server.use(
http.get('/api/auth/invite/:token', () => {
return HttpResponse.json({ valid: true });
}),
);
// Simulate ?invite=abc123 by replacing window.location.search
const originalSearch = window.location.search;
Object.defineProperty(window, 'location', {
configurable: true,
writable: true,
value: { ...window.location, search: '?invite=abc123' },
});
render(<LoginPage />);
await waitFor(() => {
expect(screen.getByPlaceholderText('admin')).toBeInTheDocument();
});
Object.defineProperty(window, 'location', {
configurable: true,
writable: true,
value: { ...window.location, search: originalSearch },
});
});
});
});
|