|
| 1 | +// Which screens exist, who can see them, and how to sign in. |
| 2 | +// |
| 3 | +// **This is the seam between the audits and the application.** Everything above it is a rule about |
| 4 | +// design -- is this control named, is this focus ring visible. Everything below it is Rails: how to |
| 5 | +// ask the router for a list of screens, and what the sign-in form looks like. An audit that reaches |
| 6 | +// past this file to talk to the app directly is an audit that cannot be pointed at another app. |
| 7 | +// |
| 8 | +// It replaces three things that were copied by hand across the suite: **21 copies of `signIn`**, |
| 9 | +// seven of the role predicates, and six of the targets-file read. Four of the `signIn` copies had |
| 10 | +// drifted -- one did not sign out first, so a second role silently reused the first one's session; |
| 11 | +// one waited on `networkidle`, which times out on the slowest screens in this app and has already |
| 12 | +// caused two audits to give up mid-run and report what they had as though it were the whole app. |
| 13 | +// |
| 14 | +// **Why a generated file rather than each audit shelling out to Rails.** The file is the contract: |
| 15 | +// a list of `{path, controller, action}`. Any framework can produce it -- `urls.py`, `route:list |
| 16 | +// --json`, a walk of an app directory -- and no audit needs to know which one did. Shelling out |
| 17 | +// would put `bin/rails` in twenty files and cost a Rails boot each time. |
| 18 | +// |
| 19 | +// Staleness is the obvious objection to a generated file, so it regenerates itself: if the file is |
| 20 | +// missing, or older than `config/routes.rb`, it is rebuilt before it is read. |
| 21 | +const fs = require("fs"); |
| 22 | +const path = require("path"); |
| 23 | +const { execSync } = require("child_process"); |
| 24 | + |
| 25 | +const BASE = process.env.BASE_URL || "http://127.0.0.1:3000"; |
| 26 | +const PASSWORD = process.env.SEED_PASSWORD || "password!"; |
| 27 | + |
| 28 | +const TARGETS_FILE = process.env.TARGETS || "/tmp/targets.json"; |
| 29 | +const ROOT = path.resolve(__dirname, "../.."); |
| 30 | +// The one Rails-shaped line in this file. Override it and the rest works unchanged. |
| 31 | +const GENERATE = process.env.TARGETS_CMD || |
| 32 | + "bin/rails runner bin/design/route-targets.rb"; |
| 33 | +const ROUTES_FILE = path.join(ROOT, "config/routes.rb"); |
| 34 | + |
| 35 | +function stale() { |
| 36 | + if (!fs.existsSync(TARGETS_FILE)) return true; |
| 37 | + if (!fs.existsSync(ROUTES_FILE)) return false; |
| 38 | + return fs.statSync(TARGETS_FILE).mtimeMs < fs.statSync(ROUTES_FILE).mtimeMs; |
| 39 | +} |
| 40 | + |
| 41 | +let cached = null; |
| 42 | + |
| 43 | +// Every screen the router knows about, with a real record id substituted for `:id`. |
| 44 | +function targets() { |
| 45 | + if (cached) return cached; |
| 46 | + if (stale()) { |
| 47 | + process.stderr.write("targets.json is missing or older than config/routes.rb; regenerating\n"); |
| 48 | + fs.writeFileSync(TARGETS_FILE, execSync(GENERATE, { cwd: ROOT, maxBuffer: 32 * 1024 * 1024 })); |
| 49 | + } |
| 50 | + cached = JSON.parse(fs.readFileSync(TARGETS_FILE, "utf8")); |
| 51 | + return cached; |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * Who can see what. |
| 56 | + * |
| 57 | + * `/partners/...` is the partner portal, except `/partners/12`, which is a bank user looking at a |
| 58 | + * partner record -- the two live under one prefix and are different applications. |
| 59 | + */ |
| 60 | +const PARTNER = (p) => (p.startsWith("/partners/") && !/^\/partners\/\d+/.test(p)) || p === "/partners/profile"; |
| 61 | +const ADMIN = (p) => p.startsWith("/admin"); |
| 62 | +const BANK = (p) => !ADMIN(p) && !PARTNER(p); |
| 63 | + |
| 64 | +// The three passes an audit makes if it wants to see the whole app. |
| 65 | +const RUNS = [ |
| 66 | + ["org_admin1@example.com", BANK], |
| 67 | + ["verified@example.com", PARTNER], |
| 68 | + ["superadmin@example.com", ADMIN] |
| 69 | +]; |
| 70 | + |
| 71 | +/* |
| 72 | + * Sign in as somebody. |
| 73 | + * |
| 74 | + * **Signs out first**, because these run several roles through one browser context and a stale |
| 75 | + * session means the second role silently audits the first role's pages. |
| 76 | + * |
| 77 | + * Fields are found by `name`, not by `id`: the ids come from Devise's form builder and change if |
| 78 | + * the form is ever rebuilt, and one copy of this function was already selecting on `#user_email` |
| 79 | + * while another used the name. |
| 80 | + * |
| 81 | + * Waits for the URL to stop being the sign-in page rather than for `networkidle`, which never |
| 82 | + * settles on the slowest screens here. |
| 83 | + */ |
| 84 | +async function signIn(page, email, password = PASSWORD) { |
| 85 | + await page.goto(BASE + "/users/sign_out", { waitUntil: "domcontentloaded" }).catch(() => {}); |
| 86 | + await page.goto(BASE + "/users/sign_in", { waitUntil: "domcontentloaded" }); |
| 87 | + await page.fill('input[name="user[email]"]', email); |
| 88 | + await page.fill('input[name="user[password]"]', password); |
| 89 | + await page.click('form[action="/users/sign_in"] button[type="submit"], input[type=submit]'); |
| 90 | + await page.waitForURL((u) => !u.pathname.includes("/sign_in"), { timeout: 60000 }); |
| 91 | +} |
| 92 | + |
| 93 | +/* |
| 94 | + * Go to a screen and wait for it to be usable. |
| 95 | + * |
| 96 | + * `domcontentloaded` plus `load`, never `networkidle`: three form pages in this app take longer |
| 97 | + * than the 30-second idle timeout, and an audit that gives up on a page reports it as clean. |
| 98 | + * Returns null for anything that did not load, so a caller can tell "not checked" from "fine". |
| 99 | + */ |
| 100 | +async function visit(page, urlPath, { timeout = 60000 } = {}) { |
| 101 | + const res = await page.goto(BASE + urlPath, { waitUntil: "domcontentloaded", timeout }) |
| 102 | + .catch(() => null); |
| 103 | + if (!res || res.status() >= 400) return null; |
| 104 | + await page.waitForLoadState("load", { timeout: 15000 }).catch(() => {}); |
| 105 | + await page.waitForTimeout(120); |
| 106 | + return res; |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { BASE, PASSWORD, targets, signIn, visit, RUNS, PARTNER, ADMIN, BANK }; |
0 commit comments