Shuffle/e2e/tests/api/krit01-registrierung.spec.ts
cschlaefke f302c56904 test(auth): add Playwright project with api tests for KTO-01 (GoTrue, Mailpit, db helper)
Refs #38

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-03 10:29:18 +01:00

64 lines
2.7 KiB
TypeScript

import { expect, test } from '@playwright/test';
import { subjects, TERMS_VERSION } from '../../support/auth-config';
import { profileByEmail, userByEmail } from '../../support/db';
import { GoTrueClient, randomEmail, randomPassword, validMetadata } from '../../support/gotrue';
import { extractTokenHash, waitForMail } from '../../support/mailpit';
const gotrue = new GoTrueClient();
test.describe('KTO-01 Kriterium 1: Registrierung und Bestaetigung (api)', () => {
test('KTO-01 Kriterium 1: Signup legt Nutzer und Profil an, Sitzung bleibt aus, Bestaetigungsmail im Faenger', async () => {
const email = randomEmail();
const res = await gotrue.signUp(
email,
randomPassword(),
validMetadata({ display_name: ' Anna Beispiel ', locale: 'de' }),
);
expect(res.status).toBe(200);
expect(res.body.id).toBeTruthy();
expect(res.body.session ?? res.body.access_token ?? null).toBeNull();
expect(res.body.email_confirmed_at ?? null).toBeNull();
const profile = await profileByEmail(email);
expect(profile).toBeTruthy();
expect(profile.display_name).toBe('Anna Beispiel');
expect(profile.locale).toBe('de');
expect(profile.terms_version).toBe(TERMS_VERSION);
expect(profile.terms_accepted_at).toBeTruthy();
expect(profile.age_confirmed_at).toBeTruthy();
const mail = await waitForMail(email);
expect(mail.Subject).toBe(subjects.confirmation.de);
const { type, link } = extractTokenHash(mail);
expect(type).toBe('email');
// AUTH-CFG-01: Link baut auf der Site-URL auf.
expect(link.origin).toBe('http://localhost:3000');
expect(link.pathname).toBe('/auth/confirm');
});
test('KTO-01 Kriterium 1: Link einloesen bestaetigt die Adresse, danach ist die Anmeldung moeglich', async () => {
const email = randomEmail();
const password = randomPassword();
await gotrue.signUp(email, password);
const { tokenHash } = extractTokenHash(await waitForMail(email));
const verified = await gotrue.verify(tokenHash, 'email');
expect(verified.status).toBe(200);
expect(verified.body.access_token).toBeTruthy();
expect(verified.body.user.email_confirmed_at).toBeTruthy();
const login = await gotrue.signIn(email, password);
expect(login.status).toBe(200);
expect(login.body.access_token).toBeTruthy();
expect((await userByEmail(email)).email_confirmed_at).toBeTruthy();
});
test('KTO-01 Kriterium 1: ohne Bestaetigung kein Zugang (AUTH-CFG-04)', async () => {
const email = randomEmail();
const password = randomPassword();
await gotrue.signUp(email, password);
const login = await gotrue.signIn(email, password);
expect(login.status).toBe(400);
expect(login.body.error_code).toBe('email_not_confirmed');
});
});