Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

developerAuth.test.ts

37 lines1.6 KB

1import * as Effect from "effect/Effect";
2import { expect, it } from "@effect/vitest";
3​
4import { developerIdentity } from "./developerAuth.ts";
5​
6const token = "fixture-token-012345678901234567890123456789";
7const keys = JSON.stringify({
8 julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
9});
10const request = (credentials: string) =>
11 new Request("https://cache.example/projects/t3code/store", {
12 headers: { authorization: `Basic ${btoa(credentials)}` },
13 });
14​
15it.effect(
16 "accepts the registered developer and rejects wrong, revoked, or malformed credentials",
17 () =>
18 Effect.gen(function* () {
19 expect(yield* developerIdentity(request(`julius:${token}`), keys)).toBe("julius");
20 expect(yield* developerIdentity(request(`theo:${token}`), keys)).toBeNull();
21 expect(yield* developerIdentity(request(`julius:${token}wrong`), keys)).toBeNull();
22 expect(yield* developerIdentity(request(`julius:${token}`), "{}")).toBeNull();
23 expect(yield* developerIdentity(request(`julius:${token}`), undefined)).toBeNull();
24 expect(yield* developerIdentity(request("malformed"), keys)).toBeNull();
25 expect(yield* developerIdentity(request(`constructor:${token}`), keys)).toBeNull();
26 }),
27);
28​
29it.effect("leaves anonymous reads and GitHub OIDC authorization to the upstream Worker", () =>
30 Effect.gen(function* () {
31 expect(yield* developerIdentity(new Request("https://cache.example"), keys)).toBeUndefined();
32 const oidc = new Request("https://cache.example", {
33 headers: { authorization: "Bearer github-oidc-token" },
34 });
35 expect(yield* developerIdentity(oidc, keys)).toBeUndefined();
36 }),
37);