forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.ts
More file actions
195 lines (173 loc) · 5.45 KB
/
Copy pathauth.test.ts
File metadata and controls
195 lines (173 loc) · 5.45 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { beforeEach, describe, expect, it, vi } from "vitest";
import { isOAuthAuth, parseRefreshParts, formatRefreshParts, accessTokenExpired } from "./auth";
import type { OAuthAuthDetails, ApiKeyAuthDetails } from "./types";
describe("isOAuthAuth", () => {
it("returns true for oauth auth type", () => {
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token|project",
access: "access-token",
expires: Date.now() + 3600000,
};
expect(isOAuthAuth(auth)).toBe(true);
});
it("returns false for api_key auth type", () => {
const auth: ApiKeyAuthDetails = {
type: "api_key",
key: "some-api-key",
};
expect(isOAuthAuth(auth)).toBe(false);
});
});
describe("parseRefreshParts", () => {
it("parses refresh token with all parts", () => {
const result = parseRefreshParts("refreshToken|projectId|managedProjectId");
expect(result).toEqual({
refreshToken: "refreshToken",
projectId: "projectId",
managedProjectId: "managedProjectId",
});
});
it("parses refresh token with only refresh and project", () => {
const result = parseRefreshParts("refreshToken|projectId");
expect(result).toEqual({
refreshToken: "refreshToken",
projectId: "projectId",
managedProjectId: undefined,
});
});
it("parses refresh token with only refresh token", () => {
const result = parseRefreshParts("refreshToken");
expect(result).toEqual({
refreshToken: "refreshToken",
projectId: undefined,
managedProjectId: undefined,
});
});
it("handles empty string", () => {
const result = parseRefreshParts("");
expect(result).toEqual({
refreshToken: "",
projectId: undefined,
managedProjectId: undefined,
});
});
it("handles empty parts", () => {
const result = parseRefreshParts("refreshToken||managedProjectId");
expect(result).toEqual({
refreshToken: "refreshToken",
projectId: undefined,
managedProjectId: "managedProjectId",
});
});
it("handles undefined/null-like input", () => {
// @ts-expect-error - testing edge case
const result = parseRefreshParts(undefined);
expect(result).toEqual({
refreshToken: "",
projectId: undefined,
managedProjectId: undefined,
});
});
});
describe("formatRefreshParts", () => {
it("formats all parts", () => {
const result = formatRefreshParts({
refreshToken: "refreshToken",
projectId: "projectId",
managedProjectId: "managedProjectId",
});
expect(result).toBe("refreshToken|projectId|managedProjectId");
});
it("formats without managed project id", () => {
const result = formatRefreshParts({
refreshToken: "refreshToken",
projectId: "projectId",
});
expect(result).toBe("refreshToken|projectId");
});
it("formats without project id but with managed project id", () => {
const result = formatRefreshParts({
refreshToken: "refreshToken",
managedProjectId: "managedProjectId",
});
expect(result).toBe("refreshToken||managedProjectId");
});
it("formats with only refresh token", () => {
const result = formatRefreshParts({
refreshToken: "refreshToken",
});
expect(result).toBe("refreshToken|");
});
it("round-trips correctly with parseRefreshParts", () => {
const original = {
refreshToken: "rt123",
projectId: "proj456",
managedProjectId: "managed789",
};
const formatted = formatRefreshParts(original);
const parsed = parseRefreshParts(formatted);
expect(parsed).toEqual(original);
});
});
describe("accessTokenExpired", () => {
beforeEach(() => {
vi.useRealTimers();
});
it("returns true when access token is missing", () => {
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token",
access: undefined,
expires: Date.now() + 3600000,
};
expect(accessTokenExpired(auth)).toBe(true);
});
it("returns true when expires is missing", () => {
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token",
access: "access-token",
expires: undefined,
};
expect(accessTokenExpired(auth)).toBe(true);
});
it("returns true when token is expired", () => {
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token",
access: "access-token",
expires: Date.now() - 1000, // expired 1 second ago
};
expect(accessTokenExpired(auth)).toBe(true);
});
it("returns true when token expires within buffer period (60 seconds)", () => {
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token",
access: "access-token",
expires: Date.now() + 30000, // expires in 30 seconds (within 60s buffer)
};
expect(accessTokenExpired(auth)).toBe(true);
});
it("returns false when token is valid and outside buffer period", () => {
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token",
access: "access-token",
expires: Date.now() + 120000, // expires in 2 minutes
};
expect(accessTokenExpired(auth)).toBe(false);
});
it("returns false when token expires exactly at buffer boundary", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(0));
const auth: OAuthAuthDetails = {
type: "oauth",
refresh: "token",
access: "access-token",
expires: 60001, // expires 60001ms from now, just outside 60s buffer
};
expect(accessTokenExpired(auth)).toBe(false);
});
});