components.test.ts
| 1 | import { expect, test } from "bun:test"; |
| 2 | import { humanReadableTime, ShowFile } from "./components"; |
| 3 | |
| 4 | const UUID = "00000000-0000-0000-0000-000000000000"; |
| 5 | |
| 6 | test("escapes filename in the file view (no stored XSS)", () => { |
| 7 | const evil = "<img src=x onerror=alert(1)>"; |
| 8 | const html = ShowFile({ |
| 9 | filename: evil, |
| 10 | uuid: UUID, |
| 11 | filetype: "none", |
| 12 | deleteAt: null, |
| 13 | size: 2, |
| 14 | preview: { kind: "text", html: "hi" }, |
| 15 | theme: "auto", |
| 16 | }); |
| 17 | expect(html).not.toContain(evil); |
| 18 | expect(html).toContain("<img"); |
| 19 | }); |
| 20 | |
| 21 | test("escapes filename in the encrypted overlay (no stored XSS)", () => { |
| 22 | const evil = "<script>alert(1)</script>"; |
| 23 | const html = ShowFile({ |
| 24 | filename: evil, |
| 25 | uuid: UUID, |
| 26 | filetype: "none", |
| 27 | deleteAt: null, |
| 28 | size: null, |
| 29 | preview: { kind: "await" }, |
| 30 | theme: "auto", |
| 31 | }); |
| 32 | expect(html).not.toContain(evil); |
| 33 | expect(html).toContain("<script>"); |
| 34 | }); |
| 35 | |
| 36 | test("humanReadableTime breaks a total down (no spurious 'expired')", () => { |
| 37 | expect(humanReadableTime(4320)).toBe("3 days"); // exact multiple of a day |
| 38 | expect(humanReadableTime(120)).toBe("2 hours"); // exact multiple of an hour |
| 39 | expect(humanReadableTime(1)).toBe("1 minute"); |
| 40 | expect(humanReadableTime(1501)).toBe("1 day 1 hour 1 minute"); |
| 41 | expect(humanReadableTime(0)).toBe("expired"); |
| 42 | expect(humanReadableTime(-5)).toBe("expired"); |
| 43 | }); |
| 44 |