ihm_client/tests/e2e/channel.spec.ts

83 lines
3.3 KiB
TypeScript

import { test, expect } from '@playwright/test';
test('channel: REQUEST_LINK -> LINK_ACCEPTED', async ({ page }) => {
await page.goto('/?e2e=1');
await page.exposeFunction('captureMessage', (data: any) => {
(window as any).__lastMsg = data;
});
await page.evaluate(() => {
window.addEventListener('message', (ev) => {
(window as any).captureMessage(ev.data);
});
});
await page.evaluate(() => {
const iframe = document.createElement('iframe');
iframe.src = '/?e2e=1';
iframe.id = 'app-frame';
document.body.appendChild(iframe);
});
await page.waitForFunction(() => (window as any).__lastMsg?.type === 'LISTENING', { timeout: 5000 });
await page.evaluate(() => {
const iframe = document.getElementById('app-frame') as HTMLIFrameElement;
setTimeout(() => {
iframe.contentWindow!.postMessage({ type: 'REQUEST_LINK', messageId: 'e2e-1' }, window.origin);
}, 50);
});
await page.waitForFunction(() => (window as any).__lastMsg?.type === 'LINK_ACCEPTED');
const msg: any = await page.evaluate(() => (window as any).__lastMsg);
expect(msg.type).toBe('LINK_ACCEPTED');
expect(typeof msg.accessToken).toBe('string');
expect(typeof msg.refreshToken).toBe('string');
});
test('channel: VALIDATE_TOKEN puis RENEW_TOKEN', async ({ page }) => {
await page.goto('/?e2e=1');
await page.exposeFunction('captureMessage', (data: any) => {
(window as any).__lastMsg = data;
});
await page.evaluate(() => {
window.addEventListener('message', (ev) => {
(window as any).captureMessage(ev.data);
});
});
await page.evaluate(() => {
const iframe = document.createElement('iframe');
iframe.src = '/?e2e=1';
iframe.id = 'app-frame';
document.body.appendChild(iframe);
});
await page.waitForFunction(() => (window as any).__lastMsg?.type === 'LISTENING', { timeout: 5000 });
await page.evaluate(() => {
const iframe = document.getElementById('app-frame') as HTMLIFrameElement;
setTimeout(() => {
iframe.contentWindow!.postMessage({ type: 'REQUEST_LINK', messageId: 'e2e-2' }, window.origin);
}, 50);
});
await page.waitForFunction(() => (window as any).__lastMsg?.type === 'LINK_ACCEPTED');
const accepted: any = await page.evaluate(() => (window as any).__lastMsg);
await page.evaluate((tokens) => {
const iframe = document.getElementById('app-frame') as HTMLIFrameElement;
iframe.contentWindow!.postMessage({ type: 'VALIDATE_TOKEN', accessToken: tokens.accessToken, refreshToken: tokens.refreshToken, messageId: 'e2e-3' }, window.origin);
}, accepted);
await page.waitForFunction(() => (window as any).__lastMsg?.type === 'VALIDATE_TOKEN');
const validated: any = await page.evaluate(() => (window as any).__lastMsg);
expect(validated.isValid).toBe(true);
await page.waitForTimeout(200);
await page.evaluate((tokens) => {
const iframe = document.getElementById('app-frame') as HTMLIFrameElement;
iframe.contentWindow!.postMessage({ type: 'RENEW_TOKEN', refreshToken: tokens.refreshToken, messageId: 'e2e-4' }, window.origin);
}, accepted);
await page.waitForFunction(() => (window as any).__lastMsg?.type === 'RENEW_TOKEN');
const renewed: any = await page.evaluate(() => (window as any).__lastMsg);
expect(typeof renewed.accessToken).toBe('string');
});