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

auth.test.ts

128 lines4.1 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3​
4import {
5 AuthEnvironmentScopes,
6 AuthEnvironmentScope,
7 authScopeRequiredResponse,
8 AuthGrantScopes,
9 AuthStandardClientScopes,
10 authScopeResponse,
11 AuthSessionState,
12 sessionGrantsScope,
13} from "./auth.ts";
14​
15describe("authorization grants", () => {
16 it("decodes legacy review credentials without offering them in new grants", () => {
17 expect(Schema.decodeUnknownSync(AuthEnvironmentScopes)(["review:write"])).toEqual([
18 "review:write",
19 ]);
20 expect(() => Schema.decodeUnknownSync(AuthGrantScopes)(["review:write"])).toThrow();
21 expect(AuthStandardClientScopes).not.toContain("review:write");
22 });
23​
24 // Keep the pre-granular vocabulary frozen to expose compatibility regressions.
25 const oldScopes = Schema.Array(
26 Schema.Literals([
27 "orchestration:read",
28 "orchestration:operate",
29 "terminal:operate",
30 "review:write",
31 "access:read",
32 "access:write",
33 "relay:read",
34 "relay:write",
35 ]),
36 );
37​
38 const decodeOldScopes = Schema.decodeUnknownSync(oldScopes);
39​
40 it.each(AuthEnvironmentScope.literals)(
41 "keeps %s permission errors decodable by old clients",
42 (scope) => {
43 const response = authScopeRequiredResponse(scope);
44 expect(decodeOldScopes([response.requiredScope])).toEqual([response.requiredScope]);
45 expect(response.requiredPermission).toBe(scope);
46 },
47 );
48​
49 it("keeps old clients able to decode grants with new permissions", () => {
50 const response = authScopeResponse(AuthStandardClientScopes);
51 expect(decodeOldScopes(response.scopes)).toEqual(response.scopes);
52 expect(response.permissions).toEqual(AuthStandardClientScopes);
53 expect(response.scopes).not.toContain("filesystem:read");
54 });
55​
56 it("ignores unknown response permissions without falling back to broader scopes", () => {
57 const session = Schema.decodeUnknownSync(AuthSessionState)({
58 authenticated: true,
59 auth: {
60 policy: "loopback-browser",
61 bootstrapMethods: [],
62 sessionMethods: [],
63 sessionCookieName: "session",
64 },
65 scopes: ["orchestration:operate"],
66 permissions: ["future:permission"],
67 });
68 expect(session.permissions).toEqual([]);
69 expect(sessionGrantsScope(session, "orchestration:operate")).toBe(false);
70 expect(sessionGrantsScope(session, "settings:write")).toBe(false);
71 });
72​
73 it.each([
74 {
75 label: "exact permissions over the legacy presentation",
76 session: {
77 authenticated: true,
78 scopes: ["orchestration:operate"],
79 permissions: ["filesystem:read"],
80 },
81 scope: "settings:write",
82 expected: false,
83 },
84 {
85 label: "a permission absent from the legacy presentation",
86 session: { authenticated: true, scopes: [], permissions: ["filesystem:read"] },
87 scope: "filesystem:read",
88 expected: true,
89 },
90​
91 {
92 label: "the parent on a server that predates the split",
93 session: { authenticated: true, scopes: ["orchestration:operate"], auth: {} },
94 scope: "settings:write",
95 expected: true,
96 },
97 {
98 label: "only the exact scope on a server that knows the split",
99 session: {
100 authenticated: true,
101 scopes: ["orchestration:operate"],
102 auth: { serverUpdateScope: "environment:maintain" },
103 },
104 scope: "settings:write",
105 expected: false,
106 },
107 {
108 label: "the exact scope regardless of server version",
109 session: { authenticated: true, scopes: ["settings:write"], auth: {} },
110 scope: "settings:write",
111 expected: true,
112 },
113 {
114 label: "nothing for an unauthenticated session",
115 session: { authenticated: false, scopes: ["orchestration:operate"], auth: {} },
116 scope: "settings:write",
117 expected: false,
118 },
119 {
120 label: "no parent for scopes that were never split",
121 session: { authenticated: true, scopes: ["orchestration:operate"], auth: {} },
122 scope: "access:write",
123 expected: false,
124 },
125 ] as const)("sessionGrantsScope accepts $label", ({ session, scope, expected }) => {
126 expect(sessionGrantsScope(session, scope)).toBe(expected);
127 });
128});