const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const { chromium } = require(process.env.PLAYWRIGHT_MODULE); (async () => { const browser = await chromium.launch({ executablePath: process.env.BROWSER_EXECUTABLE, headless: true, args: ["--host-resolver-rules=MAP *.localhost 127.0.0.1"], }); const output = process.env.FILEBROWSER_QA_OUTPUT; fs.mkdirSync(output, { recursive: true }); const errors = []; const contexts = []; const change = async (values) => { const response = await contexts[0].request.post( process.env.QA_ISSUER + "/__qa/change", { headers: { "X-QA-Control": process.env.QA_CONTROL }, data: { Name: "Alice", Version: "A-1", Status: "authenticated", Denied: false, Offline: false, ...values, }, } ); assert.equal(response.status(), 204); }; try { const context = await browser.newContext({ ignoreHTTPSErrors: true, viewport: { width: 1300, height: 900 }, locale: "en-US", }); contexts.push(context); await context.route("**/internal/session/frame?**", (route) => route.abort() ); const cookies = (device) => [ ["one", process.env.QA_APP_ONE], ["two", process.env.QA_APP_TWO], ].map(([id, url]) => ({ name: "__Host-internal_session", value: id + "-" + device + "-binding-123456789", url, secure: true, httpOnly: true, sameSite: "Lax", })); await context.addCookies(cookies("device-one")); const a = await context.newPage(), b = await context.newPage(); for (const p of [a, b]) p.on("pageerror", (e) => errors.push(String(e))); await Promise.all([ a.goto(process.env.QA_APP_ONE + "/files/"), b.goto(process.env.QA_APP_TWO + "/files/"), ]); try { await Promise.all([ a.getByText("Alice-private.txt", { exact: true }).waitFor(), b.getByText("Alice-private.txt", { exact: true }).waitFor(), ]); } catch (error) { console.log("INITIAL ERRORS", errors); for (const [index, page] of [a, b].entries()) { console.log("BODY", index, await page.locator("body").innerText()); console.log( "SESSION", await ( await context.request.get( (index === 0 ? process.env.QA_APP_ONE : process.env.QA_APP_TWO) + "/auth/session" ) ).text() ); await page.screenshot({ path: path.join(output, `initial-${index}.png`), }); } throw error; } const device2 = await browser.newContext({ ignoreHTTPSErrors: true }); contexts.push(device2); await device2.addCookies(cookies("device-two")); const independent = await device2.newPage(); await independent.goto(process.env.QA_APP_ONE + "/files/"); await independent.getByText("Alice-private.txt", { exact: true }).waitFor(); // The destination account has no such directory: switch must go home first. await b .locator("#listing") .getByRole("button", { name: "Alice-only", exact: true }) .dblclick(); await b.getByText("nested-Alice.txt", { exact: true }).waitFor(); // Hold a valid A response until after both applications have switched to B. let release; const held = new Promise((r) => (release = r)); let ready; const captured = new Promise((r) => (ready = r)); let didHold = false; await a.route("**/api/resources/", async (route) => { if (didHold) { await route.continue(); return; } didHold = true; const response = await route.fetch(); ready(); await held; await route.fulfill({ response }).catch(() => {}); }); await a.getByRole("button", { name: "Actions", exact: true }).click(); await a.getByRole("menuitem", { name: "Refresh", exact: true }).click(); await captured; await change({ Name: "Bob", Version: "B-1" }); await Promise.all([ a .getByText("Bob-private.txt", { exact: true }) .waitFor({ timeout: 15000 }), b .getByText("Bob-private.txt", { exact: true }) .waitFor({ timeout: 15000 }), ]); release(); assert.equal(new URL(a.url()).pathname, "/files/"); assert.equal(new URL(b.url()).pathname, "/files/"); await a.unroute("**/api/resources/"); await a.waitForTimeout(200); assert.equal( await a.getByText("Alice-private.txt", { exact: true }).count(), 0 ); assert.equal( await b.getByText("Alice-private.txt", { exact: true }).count(), 0 ); assert( await independent .getByText("Alice-private.txt", { exact: true }) .isVisible() ); assert(!new URL(a.url()).pathname.startsWith("/auth/")); assert(!new URL(b.url()).pathname.startsWith("/auth/")); await a.screenshot({ path: path.join(output, "account-B.png") }); const stale = await context.request.post( process.env.QA_APP_ONE + "/api/resources/stale.txt", { headers: { "X-FB-Request": "1", "X-FB-Session-State": "A-1" }, data: "must not write", } ); assert.equal(stale.status(), 409); await change({ Name: "Bob", Version: "B-1", Denied: true }); await b .getByRole("heading", { name: "Access denied", exact: true }) .waitFor({ timeout: 15000 }); assert.equal( await b.getByText("Alice-private.txt", { exact: true }).count(), 0 ); assert.equal( await b.getByText("Bob-private.txt", { exact: true }).count(), 0 ); await change({ Name: "Bob", Version: "B-1", Offline: true }); await a .getByRole("heading", { name: "Sign-in service unavailable", exact: true, }) .waitFor({ timeout: 15000 }); assert.equal( await a.getByText("Bob-private.txt", { exact: true }).count(), 0 ); assert( (await context.cookies(process.env.QA_APP_ONE)).some( (c) => c.name === "__Host-internal_session" ) ); await change({ Name: "Bob", Version: "B-2" }); await a .getByText("Bob-private.txt", { exact: true }) .waitFor({ timeout: 15000 }); const logout = await context.request.post( process.env.QA_APP_ONE + "/auth/logout", { headers: { "X-FB-Request": "1", "X-FB-Session-State": "B-2" } } ); assert.equal(logout.status(), 204); await b .getByRole("heading", { name: "Signed out", exact: true }) .waitFor({ timeout: 15000 }); assert( await independent .getByText("Alice-private.txt", { exact: true }) .isVisible() ); await change({ Status: "factor_required", Version: "factor" }); await b .getByRole("heading", { name: "Verify your sign-in", exact: true }) .waitFor({ timeout: 15000 }); assert(!new URL(b.url()).pathname.startsWith("/auth/")); const cookiesAtEnd = await context.cookies(process.env.QA_APP_TWO); const binding = cookiesAtEnd.find( (c) => c.name === "__Host-internal_session" ); assert( binding && binding.secure && binding.httpOnly && binding.path === "/" && !binding.domain.startsWith(".") ); assert.equal(errors.length, 0, errors.join("\n")); console.log( "Two apps: A→B, stale form, denied access, outage/recovery, shared logout, missing factor and independent device passed." ); } finally { for (const c of contexts) await c.close(); await browser.close(); } })().catch((error) => { console.error(error); process.exitCode = 1; });