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