Files
rust-discovery/web/src/login.test.tsx
T

48 lines
4.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { renderToStaticMarkup } from 'react-dom/server';
import { authConfig, session } from './api';
import { Login } from './login';
const onLogin = () => {};
describe('server-announced auth configuration', () => {
it('allows only the fixed same-origin OIDC entrypoint with explicit boolean modes', () => {
expect(authConfig({ oidc_configured: true, oidc_login_url: '/auth/oidc/login', dev_login_enabled: false }).oidc_login_url).toBe('/auth/oidc/login');
for (const url of ['//evil.invalid/auth/oidc/login', 'https://evil.invalid/auth/oidc/login', 'javascript:alert(1)', '/auth/oidc/login?return=https://evil.invalid', '/auth/oidc/login#token', '/auth/oidc/callback', '/auth/oidc/../oidc/login', '/auth/oidc/login\\evil', null]) expect(() => authConfig({ oidc_configured: true, oidc_login_url: url, dev_login_enabled: true })).toThrow();
expect(() => authConfig({ oidc_configured: false, oidc_login_url: '/auth/oidc/login', dev_login_enabled: true })).toThrow();
expect(() => authConfig({ oidc_configured: false, oidc_login_url: null })).toThrow();
expect(() => authConfig({ oidc_configured: 'true', oidc_login_url: '/auth/oidc/login', dev_login_enabled: true })).toThrow();
});
it('renders production SSO only, without password form or simulation impersonation', () => {
const config = authConfig({ oidc_configured: true, oidc_login_url: '/auth/oidc/login', dev_login_enabled: false });
const html = renderToStaticMarkup(<Login config={config} configError={null} onLogin={onLogin}/>);
expect(html).toContain('href="/auth/oidc/login"'); expect(html).toContain('Mit SSO anmelden');
expect(html).not.toContain('type="password"'); expect(html).not.toContain('SIMULIERTER'); expect(html).not.toContain('/auth/oidc/callback?');
});
it('shows dev form only when enabled and explicitly labels the simulated profile', () => {
const html = renderToStaticMarkup(<Login config={authConfig({ oidc_configured: false, oidc_login_url: null, dev_login_enabled: true })} configError={null} onLogin={onLogin}/>);
expect(html).toContain('type="password"'); expect(html).toContain('SIMULIERTER TESTPRINCIPAL'); expect(html).toContain('OIDC: not_configured'); expect(html).not.toContain('Mit SSO anmelden');
});
it('fails closed on loading/config failure or no announced modes; never defaults to dev auth', () => {
const enabled = authConfig({ oidc_configured: false, oidc_login_url: null, dev_login_enabled: true });
for (const props of [{ config: null, configError: null }, { config: enabled, configError: 'DEPENDENCY_UNAVAILABLE' }, { config: authConfig({ oidc_configured: false, oidc_login_url: null, dev_login_enabled: false }), configError: null }]) {
const html = renderToStaticMarkup(<Login {...props} onLogin={onLogin}/>); expect(html).not.toContain('type="password"'); expect(html).not.toContain('href=');
}
});
it('accepts explicit non-simulated OIDC sessions without deriving simulation from a name or role', () => {
const auth = session({ principal: 'simulated-name-is-not-authority', tenant_id: 't', testsimulation: false, csrf_token: 'csrf', roles: ['member'], permissions: ['operations:read'], approval_origin: 'human_webui' });
expect(auth.principal.simulated).toBe(false); expect(auth.roles).toEqual(['member']); expect(auth.permissions).toEqual(['operations:read']);
expect(() => session({ principal: 'simulated-testprincipal-a', tenant_id: 't', csrf_token: 'csrf', roles: ['dev_test_member'] })).toThrow();
});
});
it('renders the local password and TOTP login only when explicitly enabled', () => {
const config = authConfig({ oidc_configured: false, oidc_login_url: null, dev_login_enabled: false, local_login_enabled: true });
const html = renderToStaticMarkup(<Login config={config} configError={null} onLogin={onLogin}/>);
expect(html).toContain('type="password"');
expect(html).toContain('Authenticator-Code');
expect(html).toContain('autoComplete="one-time-code"');
expect(html).not.toContain('SIMULIERTER');
expect(html).not.toContain('OIDC');
expect(() => authConfig({ oidc_configured:false, oidc_login_url:null, dev_login_enabled:false, local_login_enabled:'true' })).toThrow();
});