Repository navigation
Expand file tree
/
Copy pathglobal-teardown.ts
More file actions
74 lines (67 loc) · 2.83 KB
/
Copy pathglobal-teardown.ts
File metadata and controls
74 lines (67 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* End-to-end global teardown.
*
* Puts `rate_limit_overrides` back exactly as the run found it.
*
* `global-setup.ts` raises every abuse limit roughly a hundredfold so a suite
* that signs up a dozen accounts from one address is not throttled by the
* limiter it shares with production. That is reasonable for the duration of a
* run and dangerous for a second longer: without this file the raised values
* simply stayed in the database, and a developer machine was found still
* allowing 500 signups per hour per IP instead of 5 long after the run that
* raised it. Anyone testing anti-abuse behaviour by hand after that was testing
* a system with its alarms switched off.
*
* Two properties matter more than tidiness here:
*
* - It restores the PREVIOUS value, not a hardcoded default. If an operator
* had legitimately widened a bucket before the run, that choice survives.
* - It runs even when tests fail. Playwright calls a global teardown after a
* failing run as well as a passing one, which is the case that matters:
* a suite that breaks halfway is exactly when limits would otherwise be
* left wide open.
*/
import { config } from "dotenv";
import pg from "pg";
import fs from "node:fs";
import { LOCAL_HOSTS, OVERRIDE_BACKUP } from "./global-setup";
config({ path: ".env", quiet: true });
export default async function globalTeardown() {
const url = process.env.DATABASE_URL;
if (!url) return;
// Same guard as setup. A teardown that wrote to production would be worse
// than one that did nothing.
const parsed = new URL(url);
if (!LOCAL_HOSTS.has(parsed.hostname)) return;
if (!fs.existsSync(OVERRIDE_BACKUP)) {
console.warn(
" e2e: no rate-limit backup found — leaving overrides alone.\n" +
" Check them by hand: SELECT value FROM system_settings WHERE key = 'rate_limit_overrides';",
);
return;
}
const backup = JSON.parse(fs.readFileSync(OVERRIDE_BACKUP, "utf8")) as {
existed: boolean;
value: unknown;
};
const client = new pg.Client({ connectionString: url });
await client.connect();
try {
if (backup.existed) {
await client.query(
`UPDATE system_settings SET value = $1::jsonb, updated_at = now()
WHERE key = 'rate_limit_overrides'`,
[JSON.stringify(backup.value)],
);
console.log(" e2e: rate-limit overrides restored to their pre-run value");
} else {
// There was nothing before, so leaving an empty row behind would still be
// a change. Remove it and let the code defaults be the policy again.
await client.query("DELETE FROM system_settings WHERE key = 'rate_limit_overrides'");
console.log(" e2e: rate-limit overrides removed — code defaults are in force again");
}
fs.rmSync(OVERRIDE_BACKUP, { force: true });
} finally {
await client.end();
}
}