forked from tctinh/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.test.ts
More file actions
107 lines (90 loc) · 3.03 KB
/
oauth.test.ts
File metadata and controls
107 lines (90 loc) · 3.03 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { beforeEach, describe, expect, it, vi } from "vitest"
const { fetchWithProxyMock } = vi.hoisted(() => ({
fetchWithProxyMock: vi.fn(),
}))
vi.mock("../plugin/proxy", () => ({
fetchWithProxy: fetchWithProxyMock,
}))
import { exchangeAntigravity } from "./oauth"
function makeState(verifier: string, projectId = ""): string {
return Buffer.from(JSON.stringify({ verifier, projectId }), "utf8").toString("base64url")
}
describe("exchangeAntigravity proxy routing", () => {
beforeEach(() => {
vi.clearAllMocks()
})
it("routes token and userinfo requests through account-scoped proxy args", async () => {
fetchWithProxyMock
.mockResolvedValueOnce(
new Response(
JSON.stringify({
access_token: "access-1",
expires_in: 3600,
refresh_token: "refresh-1",
}),
{ status: 200 },
),
)
.mockResolvedValueOnce(
new Response(
JSON.stringify({ email: "user@example.com" }),
{ status: 200 },
),
)
const proxies = [{ url: "socks5://127.0.0.1:1080" }]
const accountIndex = 4
const result = await exchangeAntigravity(
"auth-code",
makeState("pkce-verifier", "project-1"),
proxies,
accountIndex,
)
expect(result.type).toBe("success")
expect(fetchWithProxyMock).toHaveBeenCalledTimes(2)
const firstCall = fetchWithProxyMock.mock.calls[0] ?? []
const secondCall = fetchWithProxyMock.mock.calls[1] ?? []
expect(firstCall[0]).toBe("https://oauth2.googleapis.com/token")
expect(secondCall[0]).toBe("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
expect(firstCall[2]).toEqual(proxies)
expect(firstCall[3]).toBe(accountIndex)
expect(secondCall[2]).toEqual(proxies)
expect(secondCall[3]).toBe(accountIndex)
})
it("routes project discovery request through account-scoped proxy args when projectId is missing", async () => {
fetchWithProxyMock
.mockResolvedValueOnce(
new Response(
JSON.stringify({
access_token: "access-2",
expires_in: 3600,
refresh_token: "refresh-2",
}),
{ status: 200 },
),
)
.mockResolvedValueOnce(new Response("{}", { status: 500 }))
.mockResolvedValueOnce(
new Response(
JSON.stringify({
cloudaicompanionProject: "resolved-project",
}),
{ status: 200 },
),
)
const proxies = [{ url: "http://127.0.0.1:8080" }]
const accountIndex = 9
const result = await exchangeAntigravity(
"auth-code",
makeState("pkce-verifier"),
proxies,
accountIndex,
)
expect(result.type).toBe("success")
expect(fetchWithProxyMock).toHaveBeenCalledTimes(3)
const projectCall = fetchWithProxyMock.mock.calls[2] ?? []
expect(typeof projectCall[0]).toBe("string")
expect(projectCall[0]).toContain("/v1internal:loadCodeAssist")
expect(projectCall[2]).toEqual(proxies)
expect(projectCall[3]).toBe(accountIndex)
})
})