-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathforce-mcp-consent.test.ts
More file actions
67 lines (58 loc) · 2.61 KB
/
Copy pathforce-mcp-consent.test.ts
File metadata and controls
67 lines (58 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, expect, it } from "@effect/vitest";
import {
consentRedirectClientId,
promptWithConsent,
withClientName,
withForcedMcpConsent,
} from "./force-mcp-consent";
describe("promptWithConsent", () => {
it("adds consent to an empty prompt", () => {
expect(promptWithConsent(null)).toBe("consent");
expect(promptWithConsent("")).toBe("consent");
});
it("preserves other prompt values without duplicating consent", () => {
expect(promptWithConsent("login")).toBe("login consent");
expect(promptWithConsent("consent")).toBe("consent");
expect(promptWithConsent("login consent")).toBe("login consent");
});
});
describe("withForcedMcpConsent", () => {
const authorize = (qs: string) => new Request(`https://host.example/api/auth/mcp/authorize${qs}`);
it("injects prompt=consent on MCP authorize", () => {
const out = withForcedMcpConsent(authorize("?client_id=abc&response_type=code"));
expect(new URL(out.url).searchParams.get("prompt")).toBe("consent");
});
it("merges with an existing prompt", () => {
const out = withForcedMcpConsent(authorize("?client_id=abc&prompt=login"));
expect(new URL(out.url).searchParams.get("prompt")).toBe("login consent");
});
it("leaves an already-consent request unchanged (same instance)", () => {
const req = authorize("?client_id=abc&prompt=consent");
expect(withForcedMcpConsent(req)).toBe(req);
});
it("never touches non-authorize or non-GET requests", () => {
const other = new Request("https://host.example/api/auth/mcp/token", { method: "POST" });
expect(withForcedMcpConsent(other)).toBe(other);
const consent = new Request("https://host.example/api/auth/oauth2/consent", { method: "POST" });
expect(withForcedMcpConsent(consent)).toBe(consent);
});
});
describe("consentRedirectClientId", () => {
it("returns the client id of a consent redirect lacking a name", () => {
expect(consentRedirectClientId("/mcp-consent?consent_code=c&client_id=abc&scope=openid")).toBe(
"abc",
);
});
it("returns null when the name is already present, or it isn't a consent redirect", () => {
expect(consentRedirectClientId("/mcp-consent?client_id=abc&client_name=Codex")).toBeNull();
expect(consentRedirectClientId("/login?client_id=abc")).toBeNull();
expect(consentRedirectClientId(null)).toBeNull();
});
});
describe("withClientName", () => {
it("appends client_name to a consent redirect (path+query only)", () => {
expect(withClientName("/mcp-consent?consent_code=c&client_id=abc", "Claude Code")).toBe(
"/mcp-consent?consent_code=c&client_id=abc&client_name=Claude+Code",
);
});
});