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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
import { describe, expect, it } from "bun:test";
import { parseExpiry, INVALID_EXPIRY } from "../routes/tokens";
const NOW = new Date("2026-07-27T09:00:00.000Z");
const DAY = 86_400_000;
describe("parseExpiry — presets", () => {
for (const days of [7, 30, 60, 90]) {
it(`"${days}" → ${days} days from now`, () => {
const got = parseExpiry(String(days), "", NOW);
expect(got).toBeInstanceOf(Date);
expect((got as Date).getTime()).toBe(NOW.getTime() + days * DAY);
});
}
it('"never" → null (no expiration)', () => {
expect(parseExpiry("never", "", NOW)).toBeNull();
});
it("a blank choice defaults to 30 days, NOT to no-expiration", () => {
const got = parseExpiry("", "", NOW);
expect(got).toBeInstanceOf(Date);
expect((got as Date).getTime()).toBe(NOW.getTime() + 30 * DAY);
});
});
describe("parseExpiry — custom date", () => {
it("accepts a future yyyy-mm-dd and anchors to end-of-day UTC", () => {
const got = parseExpiry("custom", "2026-08-15", NOW);
expect(got).toBeInstanceOf(Date);
expect((got as Date).toISOString()).toBe("2026-08-15T23:59:59.999Z");
});
it("end-of-day anchoring keeps today's date usable, not instantly dead", () => {
const got = parseExpiry("custom", "2026-07-27", NOW);
expect(got).toBeInstanceOf(Date);
expect((got as Date).getTime()).toBeGreaterThan(NOW.getTime());
});
it("rejects a past date", () => {
expect(parseExpiry("custom", "2026-07-01", NOW)).toBe(INVALID_EXPIRY);
});
it("rejects a blank custom date", () => {
expect(parseExpiry("custom", "", NOW)).toBe(INVALID_EXPIRY);
});
it("rejects garbage", () => {
expect(parseExpiry("custom", "not-a-date", NOW)).toBe(INVALID_EXPIRY);
});
});
describe("parseExpiry — rejected input never becomes 'no expiration'", () => {
for (const bad of ["0", "-30", "1.5", "abc"]) {
it(`"${bad}" is rejected, not treated as never-expires`, () => {
const got = parseExpiry(bad, "", NOW);
expect(got).toBe(INVALID_EXPIRY);
expect(got).not.toBeNull();
});
}
});
describe("expiry enforcement contract", () => {
it("all three auth paths null-guard expiresAt before comparing", async () => {
const { readFileSync } = await import("fs");
const paths = [
"src/middleware/api-auth.ts",
"src/middleware/auth.ts",
"src/lib/git-push-auth.ts",
];
for (const p of paths) {
const src = readFileSync(p, "utf8");
expect(src).toContain("expiresAt &&");
}
});
});
|