Apply the #109 review panel: fork guard, loopback dev server, real authenticated assertions.
Playwright's webServer replaces the custom globalSetup. The e2e job is gated to same-repo PRs because a fork PR controls both the test files and the workflow steps while the E2E_PASSWORD secret is in the environment. The dev server is pinned to loopback, the login screen is asserted before filling, the authenticated state is proven by a token-authenticated request, the token value is never printed, the failed-login test no longer asserts a tautological exitCode, and the DevTools window is no longer mistaken for the app window.
This commit is contained in:
+49
-22
@@ -9,47 +9,66 @@ const PASSWORD = process.env.E2E_PASSWORD;
|
||||
|
||||
let app;
|
||||
let page;
|
||||
let logs;
|
||||
let logs = [];
|
||||
let profile;
|
||||
|
||||
// ServerPicker gives its input no name or test id, so position is the only handle on it;
|
||||
// this breaks if the form ever gains a field before the ones below.
|
||||
const serverInput = () => page.locator('input').first();
|
||||
const usernameInput = () => page.locator('input[name="usernameInput"]');
|
||||
const passwordInput = () => page.locator('input[name="passwordInput"]');
|
||||
const submitButton = () => page.locator('button[type="submit"]');
|
||||
|
||||
async function expectLoginForm() {
|
||||
await expect(serverInput()).toBeVisible();
|
||||
await expect(usernameInput()).toBeVisible();
|
||||
await expect(passwordInput()).toBeVisible();
|
||||
await expect(submitButton()).toBeVisible();
|
||||
}
|
||||
|
||||
async function chooseServer(server) {
|
||||
await serverInput().fill(server);
|
||||
// The server field commits on Enter, which re-runs discovery and reloads the auth flows.
|
||||
await serverInput().press('Enter');
|
||||
await expect(usernameInput()).toBeVisible();
|
||||
await expectLoginForm();
|
||||
}
|
||||
|
||||
async function submitLogin(username, password) {
|
||||
await usernameInput().fill(username);
|
||||
await passwordInput().fill(password);
|
||||
await page.locator('button[type="submit"]').click();
|
||||
await submitButton().click();
|
||||
}
|
||||
|
||||
test.beforeEach(async () => {
|
||||
logs = [];
|
||||
|
||||
if (!PASSWORD) {
|
||||
throw new Error('E2E_PASSWORD is not set. Export it, or add it as the CI secret.');
|
||||
}
|
||||
|
||||
logs = [];
|
||||
|
||||
// Fresh profile per run. electron/main.js takes a single-instance lock keyed to
|
||||
// the user data dir, so a shared profile collides with a running Paarrot.
|
||||
const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'paarrot-e2e-'));
|
||||
profile = fs.mkdtempSync(path.join(os.tmpdir(), 'paarrot-e2e-'));
|
||||
app = await _electron.launch({ args: ['.', `--user-data-dir=${profile}`] });
|
||||
|
||||
app.on('console', (msg) => logs.push(`[main] ${msg.type()}: ${msg.text()}`));
|
||||
|
||||
page = await app.firstWindow();
|
||||
// main.js opens DevTools in dev mode, and that window can be the first one registered.
|
||||
while (page.url().startsWith('devtools://')) {
|
||||
page = app.windows().find((w) => !w.url().startsWith('devtools://'))
|
||||
?? await new Promise((resolve) => app.once('window', resolve));
|
||||
}
|
||||
page.on('console', (msg) => logs.push(`[renderer] ${msg.type()}: ${msg.text()}`));
|
||||
|
||||
await page.waitForSelector('input');
|
||||
await chooseServer(HOMESERVER);
|
||||
});
|
||||
|
||||
// Config-based screenshots and traces do not fire for a page taken from app.firstWindow(),
|
||||
// and console capture is not covered by config at all, so this hook stays manual.
|
||||
test.afterEach(async ({}, testInfo) => {
|
||||
if (!app) return;
|
||||
|
||||
if (testInfo.status !== testInfo.expectedStatus) {
|
||||
if (page && !page.isClosed()) {
|
||||
await testInfo.attach('failure.png', { body: await page.screenshot(), contentType: 'image/png' });
|
||||
@@ -61,20 +80,30 @@ test.afterEach(async ({}, testInfo) => {
|
||||
.close()
|
||||
// main.js preventDefault()s window close to minimise to tray, so close can hang.
|
||||
.catch(() => app.process().kill('SIGTERM'));
|
||||
|
||||
fs.rmSync(profile, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
async function expectAuthenticated() {
|
||||
// Login complete reloads into the client shell, which lands on the last active
|
||||
// path or the inbox, so assert on the shell rather than one route.
|
||||
await expect(page).not.toHaveURL(/#\/login\//, { timeout: 30_000 });
|
||||
// Login complete reloads into the client shell, which lands on the last active path
|
||||
// (state/lastActivePath.ts) or the inbox, so assert a shell route rather than any URL.
|
||||
await expect(page).toHaveURL(/#\/(inbox|home|room|space)/);
|
||||
|
||||
const session = await page.evaluate(() => ({
|
||||
userId: JSON.parse(localStorage.getItem('currentSessionUserId') ?? 'null'),
|
||||
sessions: JSON.parse(localStorage.getItem('matrixSessions') ?? '[]'),
|
||||
}));
|
||||
await expect
|
||||
.poll(() => page.evaluate(() => JSON.parse(localStorage.getItem('currentSessionUserId') ?? 'null')))
|
||||
.toBe(`@${USER}:${HOMESERVER}`);
|
||||
|
||||
expect(session.userId).toBe(`@${USER}:${HOMESERVER}`);
|
||||
expect(session.sessions[0].accessToken).toBeTruthy();
|
||||
// A truthy accessToken only proves a string exists, so prove it authenticates. Only the
|
||||
// status is asserted, so a failure cannot put a live token into the CI log or the report.
|
||||
await expect
|
||||
.poll(async () => {
|
||||
const token = await page.evaluate(() => JSON.parse(localStorage.getItem('matrixSessions') ?? '[]')[0]?.accessToken);
|
||||
if (!token) return 0;
|
||||
// /versions and /account 404 on ruv.wtf; /capabilities exists and requires a valid token.
|
||||
const res = await fetch(`https://${HOMESERVER}/_matrix/client/v3/capabilities`, { headers: { Authorization: `Bearer ${token}` } });
|
||||
return res.status;
|
||||
})
|
||||
.toBe(200);
|
||||
|
||||
// The login form is gone, so the app is on the authenticated shell.
|
||||
await expect(usernameInput()).toHaveCount(0);
|
||||
@@ -88,12 +117,10 @@ test('test account can log in and reaches the authenticated app', async () => {
|
||||
test('failed login reports an error without crashing the app', async () => {
|
||||
await submitLogin(USER, 'not-the-password');
|
||||
|
||||
await expect(page.getByText('Invalid Username or Password.')).toBeVisible({ timeout: 30_000 });
|
||||
await expect(page.getByText('Invalid Username or Password.')).toBeVisible();
|
||||
|
||||
// The main process is still running and the form is still usable.
|
||||
expect(app.process().exitCode).toBeNull();
|
||||
// The renderer is still alive and the form is still usable. No second login here: every
|
||||
// run costs Synapse rc_login budget, and the re-login was the main source of flakes.
|
||||
await expect(page.evaluate(() => document.body.innerText.trim())).not.toBe('');
|
||||
await expect(usernameInput()).toBeVisible();
|
||||
|
||||
await submitLogin(USER, PASSWORD);
|
||||
await expectAuthenticated();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user