mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
test(e2e): meet every recent-proof gate from a stale session
The invitation journeys, the backup journey (archive, snapshot download and restores) and the delegated doctor report acted on jars signed in once in global setup, so they passed or met the re-proof gate depending on how far into the run they started. Each now acts from a session of its own, made stale before every gated press, and answers the dialog with the password; the delegated report asserts the 401 before re-proving over the wire. The helper gains expireRecentProof, reproveWithPassword and actWithReproof.
This commit is contained in:
1 parent
bf7bb0925d
commit
881a8d9061
5 files changed
+143
-6
No files matched your search
@@ -86,6 +86,80 @@ export async function useStaleSession(
|
||||
};
|
||||
}
|
||||
|
||||
/** The page's current session secret, hashed the way the server stores it. */
|
||||
async function pageSessionHash(page: Page): Promise<string> {
|
||||
const cookie = (await page.context().cookies()).find(
|
||||
(c) => c.name === SESSION_COOKIE,
|
||||
);
|
||||
if (!cookie) {
|
||||
throw new Error("[recent-proof] the page carries no session cookie");
|
||||
}
|
||||
return createHmac("sha256", env("API_TOKEN_HMAC_KEY"))
|
||||
.update(cookie.value)
|
||||
.digest("hex");
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the page's session stale again after a re-proof, so the next gated
|
||||
* action meets the dialog whatever the clock says. Only a row that
|
||||
* `useStaleSession` inserted is touched: a journey that re-proves more than
|
||||
* five minutes apart must not depend on which side of the window it lands.
|
||||
*/
|
||||
export async function expireRecentProof(page: Page): Promise<void> {
|
||||
const tokenHash = await pageSessionHash(page);
|
||||
const pool = new pg.Pool({ connectionString: env("DATABASE_URL") });
|
||||
try {
|
||||
const updated = await pool.query(
|
||||
`UPDATE sessions
|
||||
SET created_at = LEAST(created_at, NOW() - make_interval(mins => $2)),
|
||||
reproof_at = NULL,
|
||||
mfa_verified_at = NULL
|
||||
WHERE token_hash = $1 AND id LIKE 'e2estale%'`,
|
||||
[tokenHash, STALE_SIGN_IN_MINUTES],
|
||||
);
|
||||
if ((updated.rowCount ?? 0) !== 1) {
|
||||
throw new Error(
|
||||
"[recent-proof] the page is not on a session from useStaleSession",
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-prove with the password over the wire the dialog uses, for a spec that
|
||||
* calls a gated route from the page rather than through a control.
|
||||
*/
|
||||
export async function reproveWithPassword(
|
||||
page: Page,
|
||||
password: string,
|
||||
): Promise<void> {
|
||||
const status = await page.evaluate(async (secret: string) => {
|
||||
const res = await fetch("/api/auth/reproof", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ method: "password", password: secret }),
|
||||
});
|
||||
return res.status;
|
||||
}, password);
|
||||
expect(status, "the password re-proof was accepted").toBe(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stale the session, run the action that meets the gate, and answer the
|
||||
* dialog it opens. The page retries the action once the proof is in.
|
||||
*/
|
||||
export async function actWithReproof(
|
||||
page: Page,
|
||||
password: string,
|
||||
act: () => Promise<void>,
|
||||
): Promise<void> {
|
||||
await expireRecentProof(page);
|
||||
await act();
|
||||
await completeReproofWithPassword(page, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer the re-proof dialog with the account's password, the way a person
|
||||
* does, and wait for it to close. The action behind it is retried by the page.
|
||||
|
||||
Reference in new issue
Block a user