forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.test.ts
More file actions
87 lines (75 loc) · 2.47 KB
/
Copy pathtoken.test.ts
File metadata and controls
87 lines (75 loc) · 2.47 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ANTIGRAVITY_PROVIDER_ID } from "../constants";
import { AntigravityTokenRefreshError, refreshAccessToken } from "./token";
import type { OAuthAuthDetails, PluginClient } from "./types";
const baseAuth: OAuthAuthDetails = {
type: "oauth",
refresh: "refresh-token|project-123",
access: "old-access",
expires: Date.now() - 1000,
};
function createClient() {
return {
auth: {
set: vi.fn(async () => {}),
},
} as PluginClient & {
auth: { set: ReturnType<typeof vi.fn> };
};
}
describe("refreshAccessToken", () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it("updates the caller when refresh token is unchanged", async () => {
const client = createClient();
const fetchMock = vi.fn(async () => {
return new Response(
JSON.stringify({
access_token: "new-access",
expires_in: 3600,
}),
{ status: 200 },
);
});
global.fetch = fetchMock as unknown as typeof fetch;
const result = await refreshAccessToken(baseAuth, client, ANTIGRAVITY_PROVIDER_ID);
expect(result?.access).toBe("new-access");
expect(client.auth.set.mock.calls.length).toBe(0);
});
it("handles Google refresh token rotation", async () => {
const client = createClient();
const fetchMock = vi.fn(async () => {
return new Response(
JSON.stringify({
access_token: "next-access",
expires_in: 3600,
refresh_token: "rotated-token",
}),
{ status: 200 },
);
});
global.fetch = fetchMock as unknown as typeof fetch;
const result = await refreshAccessToken(baseAuth, client, ANTIGRAVITY_PROVIDER_ID);
expect(result?.access).toBe("next-access");
expect(result?.refresh).toContain("rotated-token");
expect(client.auth.set.mock.calls.length).toBe(0);
});
it("throws a typed error on invalid_grant", async () => {
const client = createClient();
const fetchMock = vi.fn(async () => {
return new Response(
JSON.stringify({
error: "invalid_grant",
error_description: "Refresh token revoked",
}),
{ status: 400, statusText: "Bad Request" },
);
});
global.fetch = fetchMock as unknown as typeof fetch;
await expect(refreshAccessToken(baseAuth, client, ANTIGRAVITY_PROVIDER_ID)).rejects.toMatchObject({
name: "AntigravityTokenRefreshError",
code: "invalid_grant",
});
});
});