Skip to content

Commit 3abdcc5

Browse files
Add unit tests for token refresh handling in OAuth2TokenManager
- Introduced new test cases in `OAuth2TokenManager` to verify the handling of token refresh when the token is within the buffer time, ensuring that valid tokens are correctly returned. - Added tests for concurrent token refresh requests to ensure that multiple simultaneous requests are handled properly, returning the expected new access token. - Included tests for handling null and undefined token data in `setTokenDataForTesting`, ensuring that the token manager correctly identifies invalid token states. These updates enhance the reliability and robustness of the OAuth2 token management implementation by validating critical scenarios related to token refresh behavior and data integrity.
1 parent f6845a9 commit 3abdcc5

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎workers/main/src/services/OAuth2/OAuth2TokenManager.basic.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,18 @@ describe('OAuth2TokenManager - Basic', () => {
159159
tokenManager.setTokenDataForTesting(tokenData);
160160
expect(tokenManager.isTokenValid()).toBe(false);
161161
});
162+
163+
it('should handle token refresh when token is within buffer time', async () => {
164+
const tokenData: TokenData = {
165+
access_token: 'valid-token',
166+
refresh_token: 'refresh-token',
167+
expires_at: Date.now() + 600000,
168+
token_type: 'Bearer',
169+
};
170+
171+
tokenManager.setTokenDataForTesting(tokenData);
172+
const accessToken = await tokenManager.getAccessToken();
173+
174+
expect(accessToken).toBe('valid-token');
175+
});
162176
});

‎workers/main/src/services/OAuth2/OAuth2TokenManager.storage.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ describe('OAuth2TokenManager - Storage & Edge Cases', () => {
3636

3737
beforeEach(async () => {
3838
tokenManager = new OAuth2TokenManager('qbo', 'test-refresh-token');
39-
// Wait for async initialization to complete
4039
await new Promise((resolve) => setTimeout(resolve, 0));
4140
});
4241

@@ -112,4 +111,51 @@ describe('OAuth2TokenManager - Storage & Edge Cases', () => {
112111
it('should return default refresh token when no cached token', () => {
113112
expect(tokenManager.getCurrentRefreshToken()).toBe('test-refresh-token');
114113
});
114+
115+
it('should handle token refresh when token is within buffer time', async () => {
116+
const tokenData: TokenData = {
117+
access_token: 'valid-token',
118+
refresh_token: 'refresh-token',
119+
expires_at: Date.now() + 600000,
120+
token_type: 'Bearer',
121+
};
122+
123+
tokenManager.setTokenDataForTesting(tokenData);
124+
const accessToken = await tokenManager.getAccessToken();
125+
126+
expect(accessToken).toBe('valid-token');
127+
});
128+
129+
it('should handle concurrent token refresh requests', async () => {
130+
const expiredTokenData: TokenData = {
131+
access_token: 'expired-token',
132+
refresh_token: 'refresh-token',
133+
expires_at: Date.now() - 3600000,
134+
token_type: 'Bearer',
135+
};
136+
137+
tokenManager.setTokenDataForTesting(expiredTokenData);
138+
139+
const promises = [
140+
tokenManager.getAccessToken(),
141+
tokenManager.getAccessToken(),
142+
tokenManager.getAccessToken(),
143+
];
144+
145+
const results = await Promise.all(promises);
146+
147+
results.forEach((result) => {
148+
expect(result).toBe('new-access-token');
149+
});
150+
});
151+
152+
it('should handle setTokenDataForTesting with null data', () => {
153+
tokenManager.setTokenDataForTesting(null as unknown as TokenData);
154+
expect(tokenManager.isTokenValid()).toBe(false);
155+
});
156+
157+
it('should handle setTokenDataForTesting with undefined data', () => {
158+
tokenManager.setTokenDataForTesting(undefined as unknown as TokenData);
159+
expect(tokenManager.isTokenValid()).toBe(false);
160+
});
115161
});

0 commit comments

Comments
 (0)