import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import type { ReactNode } from 'react';
// -----------------------------------------------------------------------------
// M-029 Pass 3 (Audit M-026): LoginPage XSS-hardening + render coverage.
//
// LoginPage surfaces an error from useAuth().error verbatim into the login
// form. A backend that round-trips the user-supplied API key into an error
// message ("invalid API key XYZ123 ...") would let an attacker deliver an
// XSS payload by trying to log in with `` as the key.
// React's JSX text-interpolation escapes by default, so the payload should
// render as literal text with no script execution; this test pins that
// invariant against future refactors that might switch to
// dangerouslySetInnerHTML or v-html-style rendering.
//
// Pins:
// 1. The login form renders.
// 2. An auth error containing a literal ';
let mockError: string | null = null;
vi.mock('../components/AuthProvider', () => ({
useAuth: () => ({
loading: false,
authRequired: true,
authenticated: false,
authType: 'api-key',
user: '',
admin: false,
login: vi.fn(),
logout: vi.fn(),
error: mockError,
}),
}));
import LoginPage from './LoginPage';
function renderWithRouter(ui: ReactNode) {
return render({ui});
}
describe('LoginPage — render + XSS hardening (M-026 / M-029 Pass 3)', () => {
beforeEach(() => {
vi.clearAllMocks();
cleanup();
mockError = null;
delete (window as unknown as { __xss_pwned__?: number }).__xss_pwned__;
});
it('renders the login form', () => {
renderWithRouter();
expect(screen.getByLabelText('API Key')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Sign In/i })).toBeInTheDocument();
});
it('does NOT execute a