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 { RPC_REQUIRED_SCOPES, requiredScopeForRpcMethod } from "./RpcAuthorization.ts";
12
13describe("RPC authorization scopes", () => {
14 it("declares exactly one scope for every RPC in the server group", () => {
15 expect(new Set(Object.keys(RPC_REQUIRED_SCOPES))).toEqual(new Set(WsRpcGroup.requests.keys()));
16 });
17
18 it("authorizes background policy reporting and observation deliberately", () => {
19 expect(requiredScopeForRpcMethod(WS_METHODS.serverReportClientActivity)).toBe(
20 AuthOrchestrationReadScope,
21 );
22 expect(requiredScopeForRpcMethod(WS_METHODS.serverReportHostPowerState)).toBe(
23 AuthOrchestrationOperateScope,
24 );
25 expect(requiredScopeForRpcMethod(WS_METHODS.serverGetBackgroundPolicy)).toBe(
26 AuthOrchestrationReadScope,
27 );
28 expect(requiredScopeForRpcMethod(WS_METHODS.subscribeBackgroundPolicy)).toBe(
29 AuthOrchestrationReadScope,
30 );
31 });
32
33 it("allows relay status reads without granting relay installation access", () => {
34 expect(requiredScopeForRpcMethod(WS_METHODS.cloudGetRelayClientStatus)).toBe(
35 AuthRelayReadScope,
36 );
37 expect(requiredScopeForRpcMethod(WS_METHODS.cloudInstallRelayClient)).toBe(AuthRelayWriteScope);
38 });
39
40 it("reads the reviewer menu under the same scope as the pull request it belongs to", () => {
41 // The candidate list is a read like the detail beside it, and asking somebody for a review is
42 // a write like every other pull request operation.
43 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsReviewerCandidates)).toBe(
44 requiredScopeForRpcMethod(WS_METHODS.pullRequestsDetail),
45 );
46 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsRequestReviewers)).toBe(
47 requiredScopeForRpcMethod(WS_METHODS.pullRequestsComment),
48 );
49 });
50
51 it("rejects unknown RPC method names", () => {
52 for (const method of ["server.notRegistered", "toString", "constructor"]) {
53 expect(() => requiredScopeForRpcMethod(method)).toThrow(
54 `RPC method ${method} has no declared authorization scope.`,
55 );
56 }
57 });
58});