Skip to content

Commit 7b2b3f6

Browse files
test(lifecycle): add tests for lifecycle endpoint
This commit introduces a new test file for the account lifecycle cron. It includes tests for both private and public HTTP origins. Additionally, it checks for unsafe and insecure origins to ensure proper validation.
1 parent ae5dee0 commit 7b2b3f6

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { lifecycleEndpoint } from "./run-account-lifecycle-cron.mjs";
5+
6+
const CRON_SECRET = "a".repeat(32);
7+
8+
test("uses a Railway-private HTTP origin when configured", () => {
9+
const result = lifecycleEndpoint({
10+
ACCOUNT_LIFECYCLE_CRON_ORIGIN:
11+
"http://study-buddy-v2.railway.internal:8080/",
12+
ACCOUNT_DELETION_CRON_SECRET: CRON_SECRET,
13+
});
14+
15+
assert.equal(
16+
result.endpoint.toString(),
17+
"http://study-buddy-v2.railway.internal:8080/api/v1/account/deletion/cron"
18+
);
19+
assert.equal(result.cronSecret, CRON_SECRET);
20+
});
21+
22+
test("falls back to the public HTTPS app origin", () => {
23+
const result = lifecycleEndpoint({
24+
APP_ORIGIN: "https://staging.studybuddyng.com/",
25+
ACCOUNT_DELETION_CRON_SECRET: CRON_SECRET,
26+
});
27+
28+
assert.equal(
29+
result.endpoint.toString(),
30+
"https://staging.studybuddyng.com/api/v1/account/deletion/cron"
31+
);
32+
});
33+
34+
for (const origin of [
35+
"http://example.com:8080",
36+
"https://study-buddy-v2.railway.internal:8080",
37+
"http://railway.internal:8080",
38+
"http://study-buddy-v2.railway.internal.example.com:8080",
39+
"http://user:password@study-buddy-v2.railway.internal:8080",
40+
"http://study-buddy-v2.railway.internal:8080/unexpected-path",
41+
"http://study-buddy-v2.railway.internal:8080/?unexpected=query",
42+
]) {
43+
test(`rejects unsafe private cron origin ${origin}`, () => {
44+
assert.throws(
45+
() =>
46+
lifecycleEndpoint({
47+
ACCOUNT_LIFECYCLE_CRON_ORIGIN: origin,
48+
ACCOUNT_DELETION_CRON_SECRET: CRON_SECRET,
49+
}),
50+
/ACCOUNT_LIFECYCLE_CRON_ORIGIN/
51+
);
52+
});
53+
}
54+
55+
test("still rejects insecure public fallback origins", () => {
56+
assert.throws(
57+
() =>
58+
lifecycleEndpoint({
59+
APP_ORIGIN: "http://staging.studybuddyng.com",
60+
ACCOUNT_DELETION_CRON_SECRET: CRON_SECRET,
61+
}),
62+
/APP_ORIGIN_MUST_USE_HTTPS/
63+
);
64+
});

0 commit comments

Comments
 (0)