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