Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthOrchestrationReadScope,
4 AuthRelayReadScope,
5 AuthRelayWriteScope,
6 WS_METHODS,
7 WsRpcGroup,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "@effect/vitest";
10
11import {
12 RPC_REQUIRED_SCOPES,
13 requiredScopeForRpcMethod,
14 requiredScopeForDeviceList,
15} from "./RpcAuthorization.ts";
16
17describe("RPC authorization scopes", () => {
18 it("declares exactly one scope for every RPC in the server group", () => {
19 expect(new Set(Object.keys(RPC_REQUIRED_SCOPES))).toEqual(new Set(WsRpcGroup.requests.keys()));
20 });
21
22 it("authorizes background policy reporting and observation deliberately", () => {
23 expect(requiredScopeForRpcMethod(WS_METHODS.serverReportClientActivity)).toBe(
24 AuthOrchestrationReadScope,
25 );
26 expect(requiredScopeForRpcMethod(WS_METHODS.serverReportHostPowerState)).toBe(
27 AuthOrchestrationOperateScope,
28 );
29 expect(requiredScopeForRpcMethod(WS_METHODS.serverGetBackgroundPolicy)).toBe(
30 AuthOrchestrationReadScope,
31 );
32 expect(requiredScopeForRpcMethod(WS_METHODS.subscribeBackgroundPolicy)).toBe(
33 AuthOrchestrationReadScope,
34 );
35 });
36
37 it("allows relay status reads without granting relay installation access", () => {
38 expect(requiredScopeForRpcMethod(WS_METHODS.cloudGetRelayClientStatus)).toBe(
39 AuthRelayReadScope,
40 );
41 expect(requiredScopeForRpcMethod(WS_METHODS.cloudInstallRelayClient)).toBe(AuthRelayWriteScope);
42 });
43
44 it("requires permission to operate on a thread before uploading feedback", () => {
45 expect(requiredScopeForRpcMethod(WS_METHODS.providerUploadFeedback)).toBe(
46 AuthOrchestrationOperateScope,
47 );
48 });
49
50 it("requires write access to import agent session history", () => {
51 expect(requiredScopeForRpcMethod(WS_METHODS.agentSessionsScan)).toBe(
52 AuthOrchestrationReadScope,
53 );
54 expect(requiredScopeForRpcMethod(WS_METHODS.agentSessionsImport)).toBe(
55 AuthOrchestrationOperateScope,
56 );
57 });
58
59 it("reads the reviewer menu under the same scope as the pull request it belongs to", () => {
60 // The candidate list is a read like the detail beside it, and asking somebody for a review is
61 // a write like every other pull request operation.
62 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsReviewerCandidates)).toBe(
63 requiredScopeForRpcMethod(WS_METHODS.pullRequestsDetail),
64 );
65 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsRequestReviewers)).toBe(
66 requiredScopeForRpcMethod(WS_METHODS.pullRequestsComment),
67 );
68 });
69
70 it("rejects unknown RPC method names", () => {
71 for (const method of ["server.notRegistered", "toString", "constructor"]) {
72 expect(() => requiredScopeForRpcMethod(method)).toThrow(
73 `RPC method ${method} has no declared authorization scope.`,
74 );
75 }
76 });
77});
78
79it("requires operate permission for host retry while preserving read-only listing", () => {
80 expect(requiredScopeForDeviceList({})).toBe(AuthOrchestrationReadScope);
81 expect(requiredScopeForDeviceList({ retryHostId: "remote-host" })).toBe(
82 AuthOrchestrationOperateScope,
83 );
84});
85
86it("requires operate permission for tool updates even alongside a read-only check", () => {
87 expect(requiredScopeForDeviceList({ updateTool: "agent", inspectOnly: true })).toBe(
88 AuthOrchestrationOperateScope,
89 );
90 expect(requiredScopeForDeviceList({ updateTool: "hub" })).toBe(AuthOrchestrationOperateScope);
91});