import { test, expect, type Page } from '@playwright/test'; const appUrl = process.env.CONVIVO_BASE_URL || 'http://localhost:3000'; const portalUrl = `${appUrl}/speaker/secret`; const invalidLink = 'This sign-in link has expired or is invalid. Request a new link.'; function mockSession(email = 'speaker@example.org') { const user = { id: '11111111-1111-4111-8111-111111111111', aud: 'authenticated', role: 'authenticated', email, app_metadata: {}, user_metadata: {}, 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 }; } function deferred() { let resolve!: () => void; const promise = new Promise((done) => { resolve = done; }); return { promise, resolve }; } async function mockApi(page: Page, talks: Record[] = [], email = 'speaker@example.org') { const session = mockSession(email); const calls = { users: 0, exchanges: 0, portal: 0, unexpected: [] 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')) { calls.exchanges++; await route.fulfill({ json: session }); } else if (url.pathname.endsWith('/logout')) { await route.fulfill({ status: 204 }); } else await route.fulfill({ json: {} }); }); await page.route('**/rest/v1/**', async (route) => { if (new URL(route.request().url()).pathname.endsWith('/rpc/get_my_speaker_sessions')) calls.portal++; else calls.unexpected.push(route.request().url()); await route.fulfill({ json: talks }); }); return { session, calls }; } test.describe('Speaker portal', () => { const talkId = '22222222-2222-4222-8222-222222222222'; const talk = { session_id: talkId, title: 'Past talk', description: null, room: 'Room A', material_path: null as string | null, event_id: '33333333-3333-4333-8333-333333333333', event_name: 'Past conference', event_start_date: '2020-01-01', event_end_date: '2020-01-01', is_active: false, slot_date: '2020-01-01', start_time: '09:00:00', end_time: '10:00:00', }; for (const { width, title } of [ { width: 1280, title: 'Past talk' }, { width: 390, title: 'Past talk' }, { width: 1280, title: 'Future talk' }, { width: 390, title: 'Future talk' }, ]) { test(`inactive ${title} materials can upload, replace and delete at ${width}px`, async ({ page }) => { await page.setViewportSize({ width, height: 844 }); const talks = [{ ...talk }, { ...talk, session_id: '44444444-4444-4444-8444-444444444444', title: 'Future talk', event_name: 'Future conference', slot_date: '2099-01-01' }]; const materialTalk = talks.find((entry) => entry.title === title)!; const materialId = materialTalk.session_id; const { session, calls } = await mockApi(page, talks); let materialPath: string | null = null; const uploaded: string[] = []; const removed: string[] = []; await page.route('**/rest/v1/sessions?**', (route) => route.fulfill({ json: { material_path: materialPath } })); await page.route('**/rest/v1/rpc/set_session_material', async (route) => { const body = route.request().postDataJSON(); expect(body.p_session_id).toBe(materialId); materialPath = body.p_material_path; materialTalk.material_path = materialPath; await route.fulfill({ json: { material_path: materialPath } }); }); await page.route('**/storage/v1/object/materials/**', async (route) => { const path = decodeURIComponent(new URL(route.request().url()).pathname.split('/object/materials/')[1]); expect(path).toMatch(new RegExp(`^sessions/${materialId}/[a-f0-9-]+-slides\\.txt$`)); uploaded.push(path); await route.fulfill({ json: { Key: `materials/${path}` } }); }); await page.route('**/storage/v1/object/materials', async (route) => { removed.push(...route.request().postDataJSON().prefixes); await route.fulfill({ json: [] }); }); await page.goto(`${portalUrl}#access_token=${session.access_token}&refresh_token=${session.refresh_token}`); const article = page.getByTestId(`speaker-talk-${materialId}`); await expect(page.getByText('Future talk', { exact: true })).toBeVisible(); await expect(page.getByTestId('checkin-required-gate')).toHaveCount(0); await article.getByLabel(`Upload material for ${title}`).setInputFiles({ name: 'slides.txt', mimeType: 'text/plain', buffer: Buffer.from('First slides') }); await expect(article.getByRole('button', { name: 'Download material' })).toBeVisible(); const firstPath = materialPath; await article.getByLabel(`Replace material for ${title}`).setInputFiles({ name: 'slides.txt', mimeType: 'text/plain', buffer: Buffer.from('Replacement slides') }); await expect.poll(() => uploaded.length).toBe(2); await expect(article.getByRole('button', { name: 'Remove material' })).toBeEnabled(); expect(materialPath).not.toBe(firstPath); expect(removed).toEqual([firstPath]); // Authenticated blob download (private bucket) — a button, no href anymore. await expect(article.getByRole('button', { name: 'Download material' })).toBeEnabled(); await page.screenshot({ path: `/tmp/convivo-speaker-${width}-${title.split(' ')[0]}.png`, fullPage: true }); expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true); page.once('dialog', (dialog) => dialog.accept()); await article.getByRole('button', { name: 'Remove material' }).click(); await expect(article.getByText('No material uploaded')).toBeVisible(); expect(materialPath).toBeNull(); expect(removed).toEqual(uploaded); expect(calls.unexpected).toEqual([]); }); } for (const email of ['staff@example.org', 'staff-admin@appe.at']) { test(`assigned staff identity ${email} can manage talk material without a registration role gate`, async ({ page }) => { const materialTalk = { ...talk }; const { session, calls } = await mockApi(page, [materialTalk], email); let materialPath: string | null = null; await page.route('**/rest/v1/sessions?**', (route) => route.fulfill({ json: { material_path: materialPath } })); await page.route('**/storage/v1/object/materials/**', (route) => route.fulfill({ json: { Key: 'uploaded' } })); await page.route('**/rest/v1/rpc/set_session_material', async (route) => { expect(route.request().postDataJSON().p_session_id).toBe(talkId); materialPath = route.request().postDataJSON().p_material_path; materialTalk.material_path = materialPath; await route.fulfill({ json: { material_path: materialPath } }); }); await page.goto(`${portalUrl}#access_token=${session.access_token}&refresh_token=${session.refresh_token}`); await expect(page.getByText(email, { exact: true })).toBeVisible(); await page.getByLabel('Upload material for Past talk').setInputFiles({ name: 'slides.txt', mimeType: 'text/plain', buffer: Buffer.from('Staff talk slides'), }); await expect(page.getByRole('link', { name: 'Download material' })).toBeVisible(); await expect(page.getByTestId('checkin-required-gate')).toHaveCount(0); expect(calls.unexpected).toEqual([]); }); } test('failed material attachment cleans up upload and re-enables controls', async ({ page }) => { const { session } = await mockApi(page, [{ ...talk }]); const removed: string[] = []; await page.route('**/rest/v1/sessions?**', (route) => route.fulfill({ json: { material_path: null } })); await page.route('**/storage/v1/object/materials/**', (route) => route.fulfill({ json: { Key: 'uploaded' } })); await page.route('**/rest/v1/rpc/set_session_material', (route) => route.fulfill({ status: 403, json: { message: 'Assignment removed', code: '42501' } })); await page.route('**/storage/v1/object/materials', async (route) => { removed.push(...route.request().postDataJSON().prefixes); await route.fulfill({ json: [] }); }); await page.goto(`${portalUrl}#access_token=${session.access_token}&refresh_token=${session.refresh_token}`); const input = page.getByLabel('Upload material for Past talk'); await input.setInputFiles({ name: 'slides.txt', mimeType: 'text/plain', buffer: Buffer.from('Slides') }); await expect(page.getByRole('alert')).toHaveText('Assignment removed'); await expect(input).toBeEnabled(); expect(removed).toHaveLength(1); await expect(page.getByRole('link', { name: 'Download material' })).toHaveCount(0); await input.setInputFiles({ name: 'bad.exe', mimeType: 'application/octet-stream', buffer: Buffer.from('Invalid') }); await expect(page.getByRole('alert')).toHaveText(/Choose a non-empty PDF/); expect(removed).toHaveLength(1); }); test('material upload control keeps the native input accessible', async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); const { session } = await mockApi(page, [{ ...talk }]); await page.goto(`${portalUrl}#access_token=${session.access_token}&refresh_token=${session.refresh_token}`); const article = page.getByTestId(`speaker-talk-${talkId}`); const upload = article.locator('.speaker-upload'); const input = article.getByLabel('Upload material for Past talk'); const box = await upload.boundingBox(); expect(box?.height).toBeGreaterThanOrEqual(44); expect(box?.width).toBeGreaterThanOrEqual(44); await expect(upload.locator('svg')).toBeVisible(); await expect(upload.getByText('Upload material', { exact: true })).toBeVisible(); await expect(input).toHaveAttribute('accept', '.pdf,.ppt,.pptx,.odp,.zip,.txt'); await expect(input).toHaveCSS('opacity', '0'); const inputBox = await input.boundingBox(); expect(inputBox?.height).toBeGreaterThanOrEqual(44); await expect(input).toHaveAttribute('aria-describedby', `material-help-${talkId}`); await expect(input).toHaveAccessibleDescription('PDF, PowerPoint, ODP, ZIP, TXT · Max 50 MB'); await page.keyboard.press('Tab'); await page.keyboard.press('Tab'); await expect(input).toBeFocused(); await expect.poll(() => upload.evaluate((element) => getComputedStyle(element).outlineStyle)).toBe('solid'); const fileChooser = page.waitForEvent('filechooser'); await input.press('Enter'); await (await fileChooser).setFiles([]); await expect.poll(() => input.evaluate((entry) => (entry as HTMLInputElement).value)).toBe(''); }); test('held storage request disables file inputs, reports failure and retries the same file', async ({ page }) => { const otherTalk = { ...talk, session_id: '55555555-5555-4555-8555-555555555555', title: 'Second talk' }; const materialTalk = { ...talk }; const { session } = await mockApi(page, [materialTalk, otherTalk]); const uploaded: string[] = []; const removed: string[] = []; const firstRpc = deferred(); const releaseFirstRpc = deferred(); let attempts = 0; await page.route('**/rest/v1/sessions?**', (route) => route.fulfill({ json: { material_path: materialTalk.material_path } })); await page.route('**/storage/v1/object/materials/**', async (route) => { uploaded.push(decodeURIComponent(new URL(route.request().url()).pathname.split('/object/materials/')[1])); await route.fulfill({ json: { Key: 'uploaded' } }); }); await page.route('**/storage/v1/object/materials', async (route) => { removed.push(...route.request().postDataJSON().prefixes); await route.fulfill({ json: [] }); }); await page.route('**/rest/v1/rpc/set_session_material', async (route) => { attempts++; const body = route.request().postDataJSON(); expect(body.p_session_id).toBe(talkId); if (attempts === 1) { firstRpc.resolve(); await releaseFirstRpc.promise; await route.fulfill({ status: 403, json: { message: 'Assignment removed', code: '42501' } }); return; } materialTalk.material_path = body.p_material_path; await route.fulfill({ json: { material_path: materialTalk.material_path } }); }); await page.goto(`${portalUrl}#access_token=${session.access_token}&refresh_token=${session.refresh_token}`); const article = page.getByTestId(`speaker-talk-${talkId}`); const input = article.getByLabel('Upload material for Past talk'); const slides = { name: 'slides.txt', mimeType: 'text/plain', buffer: Buffer.from('Slides') }; await input.setInputFiles(slides); await firstRpc.promise; await expect(article.locator('.speaker-upload')).toContainText('Saving material...'); await expect(article.getByRole('status')).toHaveText('Saving material...'); await expect(page.locator('input[type="file"]')).toHaveCount(2); await expect.poll(() => page.locator('input[type="file"]').evaluateAll((inputs) => inputs.every((entry) => (entry as HTMLInputElement).disabled))).toBe(true); releaseFirstRpc.resolve(); await expect(page.getByRole('alert')).toHaveText('Assignment removed'); await expect.poll(() => page.locator('input[type="file"]').evaluateAll((inputs) => inputs.every((entry) => !(entry as HTMLInputElement).disabled))).toBe(true); await expect(input).toBeEnabled(); expect(removed).toHaveLength(1); await expect.poll(() => input.evaluate((entry) => (entry as HTMLInputElement).value)).toBe(''); await input.setInputFiles(slides); await expect.poll(() => uploaded.length).toBe(2); await expect(article.getByRole('link', { name: 'Download material' })).toBeVisible(); await expect.poll(() => article.locator('input[type="file"]').evaluate((entry) => (entry as HTMLInputElement).value)).toBe(''); expect(attempts).toBe(2); }); test('portal authentication does not bypass the normal app check-in gate', async ({ page }) => { const { session } = await mockApi(page); await page.goto(`${portalUrl}#access_token=${session.access_token}&refresh_token=${session.refresh_token}`); await expect(page.getByRole('status')).toHaveText(/No assigned talks/); await page.route('**/rest/v1/contacts?**', (route) => route.fulfill({ json: { email: session.user.email, full_name: 'Speaker', is_admin: false } })); await page.route('**/rest/v1/registrations?**', (route) => route.fulfill({ json: { contact_email: session.user.email, type: 'speaker', checked_in: false, points: 37 } })); await page.goto(appUrl); await expect(page.getByTestId('checkin-required-gate')).toBeVisible(); await expect(page.getByTestId('speaker-portal')).toHaveCount(0); }); for (const callback of ['implicit', 'pkce']) { test(`rejected ${callback} callback leaves no session and allows a new link`, async ({ page }) => { const { session } = await mockApi(page); const endpoint = callback === 'implicit' ? '**/auth/v1/user' : '**/auth/v1/token?**'; await page.route(endpoint, (route) => route.fulfill({ status: 400, json: { msg: 'Sensitive server detail', code: 'test_error' } })); const suffix = callback === 'implicit' ? `#access_token=${session.access_token}&refresh_token=${session.refresh_token}` : '?code=invalid-code'; await page.goto(`${portalUrl}${suffix}`); await expect(page).toHaveURL(portalUrl); await expect(page.getByRole('alert')).toHaveText(callback === 'implicit' ? invalidLink : 'This sign-in link could not be opened on this device. Request a new link.'); await expect(page.getByLabel('Email', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Sign out', exact: true })).toHaveCount(0); expect(await page.evaluate(() => Object.keys(localStorage).filter((key) => key.endsWith('-auth-token')))).toEqual([]); }); } for (const path of ['/speaker/secret', '/speaker']) { test(`requests links for existing users only at ${path}`, async ({ page }) => { await mockApi(page); let requests = 0; await page.route('**/auth/v1/otp**', async (route) => { requests++; expect(route.request().postDataJSON()).toMatchObject({ email: 'speaker@example.org', create_user: false }); expect(new URL(route.request().url()).searchParams.get('redirect_to')).toBe(portalUrl); await route.fulfill({ json: {} }); }); await page.goto(`${appUrl}${path}`); await page.getByLabel('Email', { exact: true }).fill('Speaker@Example.org'); await page.getByRole('button', { name: 'Send sign-in link', exact: true }).click(); await expect(page.getByRole('status')).toHaveText('Sign-in link requested. Check your email.'); expect(requests).toBe(1); await expect(page.getByTestId('checkin-required-gate')).toHaveCount(0); }); } for (const suffix of [ '#error=access_denied&error_description=Sensitive+server+detail', '?error=access_denied&error_code=otp_expired', '#access_token=incomplete', ]) { test(`callback error is scrubbed and recoverable: ${suffix}`, async ({ page }) => { const { calls } = await mockApi(page); await page.goto(`${portalUrl}${suffix}`); await expect(page).toHaveURL(portalUrl); await expect(page.getByRole('alert')).toHaveText(suffix.includes('incomplete') ? 'Incomplete sign-in link. Request a new link.' : invalidLink); await expect(page.getByRole('button', { name: 'Send sign-in link', exact: true })).toBeVisible(); await expect(page.getByText('Sensitive server detail')).toHaveCount(0); expect(calls.users).toBe(0); expect(calls.exchanges).toBe(0); }); } for (const callback of ['implicit', 'pkce']) { test(`${callback} callback is consumed once under StrictMode and empty access stays isolated`, async ({ page }) => { const { session, calls } = await mockApi(page); const suffix = callback === 'implicit' ? `#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink` : '?code=mock-code&next=https://example.org'; await page.goto(`${portalUrl}${suffix}`); await expect(page).toHaveURL(portalUrl); await expect(page.getByRole('status')).toHaveText(/No assigned talks/); expect(calls.users).toBe(callback === 'implicit' ? 1 : 0); expect(calls.exchanges).toBe(callback === 'pkce' ? 1 : 0); expect(calls.portal).toBeGreaterThan(0); expect(calls.unexpected).toEqual([]); await expect(page.locator('input[type="file"]')).toHaveCount(0); await expect(page.getByTestId('checkin-required-gate')).toHaveCount(0); await page.getByRole('button', { name: 'Sign out', exact: true }).click(); await expect(page.getByLabel('Email', { exact: true })).toBeVisible(); expect(await page.evaluate(() => Object.keys(localStorage).filter((key) => key.endsWith('-auth-token')))).toEqual([]); }); } });