-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaccess-token-cache.spec.ts
More file actions
263 lines (224 loc) · 8.36 KB
/
access-token-cache.spec.ts
File metadata and controls
263 lines (224 loc) · 8.36 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { AccessTokenCache } from "../../src/access-token-cache";
import { TokenCredential, AccessToken, GetTokenOptions } from "@azure/identity";
/**
* Mock TokenCredential that tracks call counts and supports configurable delays.
*/
class MockTokenCredential implements TokenCredential {
callCount = 0;
private readonly tokenFactory: () => AccessToken | null;
private readonly delay: number;
constructor(
tokenFactory?: () => AccessToken | null,
delayMs: number = 0,
) {
this.tokenFactory =
tokenFactory ??
(() => ({
token: `token-${this.callCount}`,
expiresOnTimestamp: Date.now() + 3600000,
}));
this.delay = delayMs;
}
async getToken(
_scopes: string | string[],
_options?: GetTokenOptions,
): Promise<AccessToken | null> {
this.callCount++;
if (this.delay > 0) {
await new Promise((resolve) => setTimeout(resolve, this.delay));
}
return this.tokenFactory();
}
}
describe("AccessTokenCache", () => {
describe("basic caching", () => {
it("should call credential.getToken on first call", async () => {
const cred = new MockTokenCredential();
const cache = new AccessTokenCache(cred, "scope");
const token = await cache.getToken();
expect(token).toBeDefined();
expect(token.token).toContain("token-");
expect(cred.callCount).toBe(1);
});
it("should return cached token on subsequent calls", async () => {
const cred = new MockTokenCredential();
const cache = new AccessTokenCache(cred, "scope");
const token1 = await cache.getToken();
const token2 = await cache.getToken();
expect(token1).toBe(token2);
expect(cred.callCount).toBe(1);
});
it("should refresh token when expired", async () => {
let callNum = 0;
const cred = new MockTokenCredential(() => {
callNum++;
return {
token: `token-${callNum}`,
// First token expires immediately (in the past), second lasts 1 hour
expiresOnTimestamp: callNum === 1 ? Date.now() - 1000 : Date.now() + 3600000,
};
});
const cache = new AccessTokenCache(cred, "scope", 0);
const token1 = await cache.getToken();
expect(token1.token).toBe("token-1");
expect(cred.callCount).toBe(1);
// Token is expired, should refresh
const token2 = await cache.getToken();
expect(token2.token).toBe("token-2");
expect(cred.callCount).toBe(2);
});
it("should refresh when within margin of expiration", async () => {
const marginMs = 60000; // 1 minute margin
let callNum = 0;
const cred = new MockTokenCredential(() => {
callNum++;
return {
token: `token-${callNum}`,
// First token expires within margin, second lasts 1 hour
expiresOnTimestamp: callNum === 1 ? Date.now() + 30000 : Date.now() + 3600000,
};
});
const cache = new AccessTokenCache(cred, "scope", marginMs);
await cache.getToken();
expect(cred.callCount).toBe(1);
// Token is within margin, should refresh
const token2 = await cache.getToken();
expect(token2.token).toBe("token-2");
expect(cred.callCount).toBe(2);
});
it("should refresh when refreshAfterTimestamp is in the past", async () => {
let callNum = 0;
const cred = new MockTokenCredential(() => {
callNum++;
return {
token: `token-${callNum}`,
expiresOnTimestamp: Date.now() + 3600000,
// First token signals it should be refreshed now
refreshAfterTimestamp: callNum === 1 ? Date.now() - 1000 : undefined,
};
});
const cache = new AccessTokenCache(cred, "scope", 0);
await cache.getToken();
expect(cred.callCount).toBe(1);
// refreshAfterTimestamp is in the past, should refresh
const token2 = await cache.getToken();
expect(token2.token).toBe("token-2");
expect(cred.callCount).toBe(2);
});
});
describe("error handling", () => {
it("should throw when credential returns null", async () => {
const cred = new MockTokenCredential(() => null);
const cache = new AccessTokenCache(cred, "scope");
await expect(cache.getToken()).rejects.toThrow(
"Failed to obtain access token from credential",
);
});
it("should propagate credential errors", async () => {
const cred: TokenCredential = {
getToken: async () => {
throw new Error("Network error");
},
};
const cache = new AccessTokenCache(cred, "scope");
await expect(cache.getToken()).rejects.toThrow("Network error");
});
it("should allow retry after a failed fetch", async () => {
let callNum = 0;
const cred: TokenCredential = {
getToken: async () => {
callNum++;
if (callNum === 1) {
throw new Error("Transient error");
}
return {
token: "recovered-token",
expiresOnTimestamp: Date.now() + 3600000,
};
},
};
const cache = new AccessTokenCache(cred, "scope");
await expect(cache.getToken()).rejects.toThrow("Transient error");
// Second call should succeed since pendingTokenFetch was cleared
const token = await cache.getToken();
expect(token.token).toBe("recovered-token");
});
});
describe("concurrent access (race condition prevention)", () => {
it("should make only one credential call when multiple callers request simultaneously", async () => {
const cred = new MockTokenCredential(
() => ({
token: "shared-token",
expiresOnTimestamp: Date.now() + 3600000,
}),
50, // 50ms delay to simulate network call
);
const cache = new AccessTokenCache(cred, "scope");
// Launch 10 concurrent getToken calls
const promises = Array.from({ length: 10 }, () => cache.getToken());
const results = await Promise.all(promises);
// All should get the same token
for (const result of results) {
expect(result.token).toBe("shared-token");
}
// Only ONE credential call should have been made
expect(cred.callCount).toBe(1);
});
it("should share the same promise for concurrent callers during token refresh", async () => {
let callNum = 0;
const cred = new MockTokenCredential(
() => {
callNum++;
return {
token: `token-${callNum}`,
// First token expires immediately
expiresOnTimestamp: callNum === 1 ? Date.now() - 1000 : Date.now() + 3600000,
};
},
50,
);
const cache = new AccessTokenCache(cred, "scope", 0);
// First call to populate cache with an expired token
await cache.getToken();
expect(cred.callCount).toBe(1);
// Now launch concurrent refresh requests
const promises = Array.from({ length: 5 }, () => cache.getToken());
const results = await Promise.all(promises);
// All should get the refreshed token
for (const result of results) {
expect(result.token).toBe("token-2");
}
// Only one additional credential call should have been made
expect(cred.callCount).toBe(2);
});
it("should clear pending fetch on error so subsequent callers can retry", async () => {
let callNum = 0;
const cred: TokenCredential = {
getToken: async () => {
callNum++;
if (callNum === 1) {
await new Promise((resolve) => setTimeout(resolve, 50));
throw new Error("Auth service unavailable");
}
return {
token: "success-token",
expiresOnTimestamp: Date.now() + 3600000,
};
},
};
const cache = new AccessTokenCache(cred, "scope");
// Multiple concurrent calls - all should fail together
const failingPromises = Array.from({ length: 3 }, () => cache.getToken());
const settledResults = await Promise.allSettled(failingPromises);
for (const result of settledResults) {
expect(result.status).toBe("rejected");
}
// After failure, pendingTokenFetch should be cleared
// Next call should succeed
const token = await cache.getToken();
expect(token.token).toBe("success-token");
});
});
});