Files
Convivo/e2e/tests/convivo-admin-entry.spec.ts
T
fbc827c2b8 feat: finalize ticket merging and speaker-aware imports
Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-10 13:28:18 +02:00

361 lines
20 KiB
TypeScript

import { test, expect, type Page } from '@playwright/test';
const appUrl = process.env.CONVIVO_BASE_URL || 'http://localhost:3000';
const notice = 'If this email belongs to an eligible admin account, a sign-in link has been sent.';
const invalidLink = 'This sign-in link is invalid or expired. Please request a new link.';
function mockSession(email: string) {
const user = {
id: '11111111-1111-4111-8111-111111111111', aud: 'authenticated', role: 'authenticated', email,
app_metadata: {}, user_metadata: { is_admin: true }, created_at: '2026-01-01T00:00:00Z',
};
const payload = Buffer.from(JSON.stringify({ sub: user.id, exp: Math.floor(Date.now() / 1000) + 3600 })).toString('base64url');
return { user, access_token: `eyJhbGciOiJIUzI1NiJ9.${payload}.test-signature`, refresh_token: 'mock-refresh-token', token_type: 'bearer', expires_in: 3600 };
}
async function mockApi(page: Page, options: { email?: string; admin?: boolean; registered?: boolean; checkedIn?: boolean; staff?: boolean } = {}) {
const email = options.email || '[email protected]';
const session = mockSession(email);
const calls = { users: 0, exchanges: 0, passwords: 0, logouts: 0, otp: 0, lanyards: 0, mutations: [] as string[] };
await page.route('**/auth/v1/**', async (route) => {
const url = new URL(route.request().url());
if (url.pathname.endsWith('/user')) {
calls.users++;
await route.fulfill({ json: session.user });
} else if (url.pathname.endsWith('/token')) {
if (url.searchParams.get('grant_type') === 'pkce') calls.exchanges++;
if (url.searchParams.get('grant_type') === 'password') calls.passwords++;
await route.fulfill({ json: session });
} else if (url.pathname.endsWith('/logout')) {
calls.logouts++;
await route.fulfill({ status: 204 });
} else {
if (url.pathname.endsWith('/otp')) calls.otp++;
await route.fulfill({ json: {} });
}
});
await page.route('**/rest/v1/**', async (route) => {
const request = route.request();
const pathname = new URL(request.url()).pathname;
const readRpc = /\/(is_admin|can_request_admin_magic_link|lanyard_lookup|get_[a-z_]+)$/.test(pathname);
if (request.method() !== 'GET' && !readRpc) calls.mutations.push(pathname);
if (pathname.endsWith('/rpc/can_request_admin_magic_link')) {
expect(request.postDataJSON()).toEqual({ p_email: email.toLowerCase() });
await route.fulfill({ json: options.admin !== false });
}
else if (pathname.endsWith('/rpc/is_admin')) await route.fulfill({ json: options.admin !== false });
else if (pathname.endsWith('/contacts')) await route.fulfill({ json: { email, full_name: 'Test Member', is_admin: false, show_email: false, show_linkedin: false } });
else if (pathname.endsWith('/registrations')) {
expect(new URL(request.url()).searchParams.get('event_id')).toBe('eq.33333333-3333-4333-8333-333333333333');
await route.fulfill({ json: options.registered ? {
id: '22222222-2222-4222-8222-222222222222', event_id: '33333333-3333-4333-8333-333333333333',
contact_email: email, type: options.staff ? 'staff' : 'attendee', checked_in: !!options.checkedIn, points: 0, qr_token: 'CVV-TEST-1234',
} : null });
}
else if (pathname.endsWith('/events')) await route.fulfill({ json: {
id: '33333333-3333-4333-8333-333333333333', name: 'Test Event', is_active: true,
start_date: '2026-02-20', end_date: '2026-02-20', use_realtime_clock: false,
} });
else if (pathname.endsWith('/rpc/lanyard_lookup')) {
calls.lanyards++;
expect(request.postDataJSON()).toEqual({ p_qr_token: 'CVV-TEST-1234' });
await route.fulfill({ json: [{ email, full_name: 'Test Member', registration_type: options.staff ? 'staff' : 'attendee' }] });
} else await route.fulfill({ json: [] });
});
return { session, calls };
}
async function openEntry(page: Page) {
await page.goto(appUrl);
await page.getByTestId('login-splash').click();
await expect(page.getByLabel('QR code or admin email')).toBeVisible();
}
test.describe('Convivo admin email entry', () => {
for (const email of ['[email protected]', '[email protected]', '[email protected]']) {
test(`ineligible ${email} gets private confirmation without OTP`, async ({ page }) => {
const { calls } = await mockApi(page, { email, admin: false });
await openEntry(page);
await page.getByLabel('QR code or admin email').fill(email);
await page.getByLabel('QR code or admin email').press('Enter');
await expect(page.getByRole('status')).toHaveText(notice);
expect(calls.otp).toBe(0);
expect(calls.lanyards).toBe(0);
expect(calls.mutations).toEqual([]);
});
}
test('eligibility server failure never sends OTP', async ({ page }) => {
const { calls } = await mockApi(page);
await page.route('**/rest/v1/rpc/can_request_admin_magic_link', (route) => route.fulfill({ status: 401, json: { message: 'Sensitive eligibility detail' } }));
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('[email protected]');
await page.getByTestId('scanner-manual-go').click();
await expect(page.getByRole('alert')).toHaveText('Unable to send a sign-in link right now. Please try again later.');
expect(calls.otp).toBe(0);
await expect(page.getByText('Sensitive eligibility detail')).toHaveCount(0);
});
for (const responseStatus of [400, 422, 429, 500]) {
test(`OTP response ${responseStatus} stays generic and recoverable`, async ({ page }) => {
await mockApi(page);
await page.route('**/auth/v1/otp**', (route) => route.fulfill({ status: responseStatus, json: { message: 'Sensitive account detail' } }));
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('[email protected]');
await page.getByTestId('scanner-manual-go').click();
if (responseStatus === 400 || responseStatus === 422) {
await expect(page.getByRole('status')).toHaveText(notice);
} else {
await expect(page.getByRole('alert')).toHaveText('Unable to send a sign-in link right now. Please try again later.');
await expect(page.getByTestId('scanner-manual-go')).toBeEnabled();
}
await expect(page.getByText('Sensitive account detail')).toHaveCount(0);
await expect(page.getByLabel('QR code or admin email')).toBeEnabled();
});
}
test('same field requests an admin-only link, normalizes email, supports keyboard and resend', async ({ page }) => {
const { calls } = await mockApi(page);
await page.clock.install();
let sends = 0;
await page.route('**/auth/v1/otp**', async (route) => {
sends++;
expect(route.request().postDataJSON()).toMatchObject({ email: '[email protected]', create_user: false });
expect(new URL(route.request().url()).searchParams.get('redirect_to')).toBe(`${appUrl}/auth/callback`);
await route.fulfill({ status: 202, json: { ok: true } });
});
await openEntry(page);
const entry = page.getByLabel('QR code or admin email');
await expect(entry).toHaveAttribute('type', 'text');
await entry.fill(' [email protected] ');
await expect(page.getByTestId('scanner-manual-go')).toHaveText('Send magic link');
await entry.press('Enter');
await expect(page.getByRole('status')).toHaveText(notice);
await expect(page.getByRole('button', { name: 'Resend in 60s' })).toBeDisabled();
await expect(entry).toBeEnabled();
await page.clock.runFor(60_000);
await page.getByRole('button', { name: 'Resend magic link', exact: true }).click();
await expect(page.getByRole('button', { name: 'Resend in 60s' })).toBeDisabled();
expect(sends).toBe(2);
expect(calls.otp).toBe(0);
expect(calls.lanyards).toBe(0);
expect(calls.passwords).toBe(0);
});
test('pending send locks controls and failed resend clears the sent state', async ({ page }) => {
await mockApi(page);
await page.clock.install();
let release!: () => void;
const pending = new Promise<void>((resolve) => { release = resolve; });
let sends = 0;
await page.route('**/auth/v1/otp**', async (route) => {
sends++;
if (sends === 1) { await pending; await route.fulfill({ status: 202, json: {} }); }
else await route.fulfill({ status: 503, json: { error: 'Sensitive server detail' } });
});
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('[email protected]');
await page.getByTestId('scanner-manual-go').click();
await expect(page.getByLabel('QR code or admin email')).toBeDisabled();
await expect(page.getByRole('button', { name: 'Sending...' })).toBeDisabled();
release();
await expect(page.getByRole('status')).toHaveText(notice);
await page.clock.runFor(60_000);
await page.getByRole('button', { name: 'Resend magic link', exact: true }).click();
await expect(page.getByRole('alert')).toHaveText('Unable to send a sign-in link right now. Please try again later.');
await expect(page.getByRole('status')).toHaveCount(0);
await expect(page.getByTestId('scanner-manual-go')).toBeEnabled();
await expect(page.getByText('Sensitive server detail')).toHaveCount(0);
});
test('invalid email and network failures are recoverable without QR lookup', async ({ page }) => {
const { calls } = await mockApi(page);
await page.route('**/auth/v1/otp**', (route) => route.abort());
await openEntry(page);
const entry = page.getByLabel('QR code or admin email');
await entry.fill('admin@');
await entry.press('Enter');
await expect(page.getByRole('alert')).toHaveText('Enter a valid email address.');
await entry.fill('[email protected]');
await entry.press('Enter');
await expect(page.getByRole('alert')).toHaveText('Unable to send a sign-in link right now. Please try again later.');
await expect(entry).toBeEnabled();
expect(calls.lanyards).toBe(0);
expect(calls.otp).toBe(0);
});
for (const registered of [false, true]) {
test(`verified admin callback enters Convivo with registered=${registered}, without attendance writes`, async ({ page }) => {
const { session, calls } = await mockApi(page, { registered, checkedIn: false });
await page.goto(`${appUrl}/auth/callback?next=https://example.org#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`);
await expect(page.getByTestId('tab-home')).toBeVisible();
await expect(page.getByTestId('checkin-required-gate')).toHaveCount(0);
await expect(page).toHaveURL(`${appUrl}/`);
await page.getByTestId('tab-scan').click();
await expect(page.getByTestId('scan-mode-staff')).toBeVisible();
await page.reload();
await expect(page.getByTestId('tab-home')).toBeVisible();
expect(calls.users).toBeGreaterThan(0);
expect(calls.passwords).toBe(0);
expect(calls.lanyards).toBe(0);
expect(calls.mutations).toEqual([]);
});
}
for (const email of ['[email protected]', '[email protected]', '[email protected]']) {
test(`server denies non-admin callback ${email} despite editable admin metadata`, async ({ page }) => {
const { session, calls } = await mockApi(page, { email, admin: false, registered: true, checkedIn: true });
await page.goto(`${appUrl}/auth/callback#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`);
await expect(page.getByRole('alert')).toHaveText('This account is not an admin.');
await expect(page.getByTestId('tab-home')).toHaveCount(0);
await expect(page).toHaveURL(`${appUrl}/`);
expect(calls.logouts).toBe(1);
expect(calls.mutations).toEqual([]);
expect(await page.evaluate(() => Object.keys(localStorage).filter((key) => key.endsWith('-auth-token')))).toEqual([]);
});
}
test('code exchange is consumed once under StrictMode and scrubbed', async ({ page }) => {
const { calls } = await mockApi(page);
await page.goto(`${appUrl}/auth/callback?code=mock-code&redirect_to=https://example.org`);
await expect(page.getByTestId('tab-home')).toBeVisible();
await expect(page).toHaveURL(`${appUrl}/`);
expect(calls.exchanges).toBe(1);
});
test('root callback also rejects non-admins and removes callback credentials', async ({ page }) => {
const { session } = await mockApi(page, { admin: false, registered: true, checkedIn: true });
await page.goto(`${appUrl}/#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`);
await expect(page.getByRole('alert')).toHaveText('This account is not an admin.');
await expect(page).toHaveURL(`${appUrl}/`);
await expect(page.getByTestId('tab-home')).toHaveCount(0);
});
test('denied callback clears local credentials even when logout fails', async ({ page }) => {
const { session } = await mockApi(page, { admin: false, registered: true, checkedIn: true });
await page.route('**/auth/v1/logout**', (route) => route.fulfill({ status: 500, json: { message: 'Logout unavailable' } }));
await page.goto(`${appUrl}/auth/callback#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`);
await expect(page.getByRole('alert')).toHaveText('This account is not an admin.');
expect(await page.evaluate(() => Object.keys(localStorage).filter((key) => key.endsWith('-auth-token')))).toEqual([]);
await page.reload();
await expect(page.getByTestId('tab-home')).toHaveCount(0);
await expect(page.getByTestId('login-splash')).toBeVisible();
});
test('rejected exchange clears an existing local session and offers a fresh request', async ({ page }) => {
const { session } = await mockApi(page);
await page.goto(`${appUrl}/auth/callback#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`);
await expect(page.getByTestId('tab-home')).toBeVisible();
await page.route('**/auth/v1/token**', (route) => route.fulfill({ status: 400, json: { error: 'invalid_grant', error_description: 'Sensitive exchange detail' } }));
await page.goto(`${appUrl}/auth/callback?code=expired-code`);
await expect(page.getByRole('alert')).toHaveText(invalidLink);
await expect(page).toHaveURL(`${appUrl}/`);
expect(await page.evaluate(() => Object.keys(localStorage).filter((key) => key.endsWith('-auth-token')))).toEqual([]);
await page.getByLabel('QR code or admin email').fill('[email protected]');
await page.getByRole('button', { name: 'Send magic link', exact: true }).click();
await expect(page.getByRole('status')).toHaveText(notice);
await expect(page.getByRole('alert')).toHaveCount(0);
await expect(page.getByText('Sensitive exchange detail')).toHaveCount(0);
});
for (const endpoint of ['user', 'is_admin', 'contacts']) {
test(`callback stays denied when server ${endpoint} verification fails`, async ({ page }) => {
const { session, calls } = await mockApi(page);
const path = endpoint === 'user' ? '**/auth/v1/user' : endpoint === 'is_admin' ? '**/rest/v1/rpc/is_admin' : '**/rest/v1/contacts**';
await page.route(path, (route) => route.fulfill({ status: 401, json: { message: 'Sensitive authorization detail' } }));
await page.goto(`${appUrl}/auth/callback#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`);
await expect(page.getByRole('alert')).toHaveText(invalidLink);
await expect(page.getByTestId('tab-home')).toHaveCount(0);
await expect(page).toHaveURL(`${appUrl}/`);
expect(calls.mutations).toEqual([]);
await expect(page.getByText('Sensitive authorization detail')).toHaveCount(0);
});
}
test('staff QR still requires the staff password and enables staff mode', async ({ page }) => {
const { calls } = await mockApi(page, { staff: true, registered: true, checkedIn: false });
await page.route('**/auth/v1/token**', async (route) => {
expect(route.request().postDataJSON()).toMatchObject({ email: '[email protected]', password: 'mock-staff-password' });
calls.passwords++;
await route.fulfill({ json: mockSession('[email protected]') });
});
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('CVV-TEST-1234');
await page.getByLabel('QR code or admin email').press('Enter');
await expect(page.getByTestId('staff-login-password')).toBeVisible();
await expect(page.getByTestId('login-confirm')).toBeDisabled();
await page.getByTestId('staff-login-password').fill('mock-staff-password');
await page.getByTestId('staff-login-password').press('Enter');
await expect(page.getByTestId('tab-home')).toBeVisible();
await page.getByTestId('tab-scan').click();
await expect(page.getByTestId('scan-mode-staff')).toBeVisible();
expect(calls.passwords).toBe(1);
expect(calls.otp).toBe(0);
expect(calls.mutations).toEqual([]);
});
test('failed attendee QR confirmation stays outside the app', async ({ page }) => {
await mockApi(page, { admin: false, registered: true, checkedIn: true });
await page.route('**/auth/v1/token**', (route) => route.fulfill({ status: 400, json: { error: 'invalid_grant', error_description: 'Invalid login credentials' } }));
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('CVV-TEST-1234');
await page.getByTestId('scanner-manual-go').click();
await page.getByTestId('attendee-login-email').fill('[email protected]');
await page.getByTestId('login-confirm').click();
await expect(page.getByTestId('login-error')).toHaveText('Email and QR code do not match. Please try again.');
await expect(page.getByTestId('tab-home')).toHaveCount(0);
});
test('mobile email controls fit and QR remains available during cooldown', async ({ page }) => {
await page.setViewportSize({ width: 375, height: 812 });
const { calls } = await mockApi(page);
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('[email protected]');
await page.getByRole('button', { name: 'Send magic link', exact: true }).click();
await expect(page.getByRole('status')).toHaveText(notice);
const button = await page.getByTestId('scanner-manual-go').boundingBox();
const input = await page.getByLabel('QR code or admin email').boundingBox();
expect(button).not.toBeNull();
expect(input).not.toBeNull();
expect(input!.x + input!.width).toBeLessThanOrEqual(button!.x);
expect(button!.x + button!.width).toBeLessThanOrEqual(375);
await page.screenshot({ path: '/tmp/convivo-admin-entry-mobile.png', fullPage: true });
await page.getByLabel('QR code or admin email').fill('CVV-TEST-1234');
await expect(page.getByTestId('scanner-manual-go')).toHaveText('Go');
await expect(page.getByTestId('scanner-manual-go')).toBeEnabled();
await page.getByTestId('scanner-manual-go').click();
await expect(page.getByTestId('attendee-login-email')).toBeVisible();
expect(calls.lanyards).toBe(1);
});
for (const suffix of ['#error=access_denied&error_code=otp_expired&error_description=Sensitive+detail', '#access_token=incomplete', '']) {
test(`invalid callback is scrubbed and recoverable ${suffix || 'empty'}`, async ({ page }) => {
const { calls } = await mockApi(page);
await page.goto(`${appUrl}/auth/callback${suffix}`);
await expect(page.getByRole('alert')).toHaveText(invalidLink);
await expect(page.getByLabel('QR code or admin email')).toBeVisible();
await expect(page).toHaveURL(`${appUrl}/`);
expect(calls.exchanges).toBe(0);
expect(calls.users).toBe(0);
await expect(page.getByText('Sensitive detail')).toHaveCount(0);
});
}
for (const checkedIn of [false, true]) {
test(`attendee QR login retains the checked-in=${checkedIn} gate`, async ({ page }) => {
const email = '[email protected]';
const { calls } = await mockApi(page, { email, admin: false, registered: true, checkedIn });
await openEntry(page);
await page.getByLabel('QR code or admin email').fill('CVV-TEST-1234');
await page.getByTestId('scanner-manual-go').click();
await page.getByTestId('attendee-login-email').fill(email);
await page.getByTestId('login-confirm').click();
if (checkedIn) await expect(page.getByTestId('tab-home')).toBeVisible();
else await expect(page.getByTestId('checkin-required-gate')).toBeVisible();
expect(calls.passwords).toBe(1);
expect(calls.lanyards).toBe(1);
expect(calls.otp).toBe(0);
expect(calls.mutations).toEqual([]);
});
}
});