|
| 1 | +import type { ResolvedCredential } from "../core/types.ts"; |
| 2 | +import type { OAuthClientConfigService } from "./oauth-client-config-service.ts"; |
| 3 | + |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { OAuthCredentialRefreshService } from "./oauth-credential-refresh-service.ts"; |
| 6 | + |
| 7 | +type OAuthCredential = Extract<ResolvedCredential, { authType: "oauth2" }>; |
| 8 | + |
| 9 | +const clientConfigs = { |
| 10 | + getOAuthDefinition: () => ({ type: "oauth2", tokenUrl: "https://provider.example.com/oauth/token" }), |
| 11 | + getConfig: async () => ({ clientId: "client-id", clientSecret: "client-secret", extra: {} }), |
| 12 | + resolveEndpointUrl: (_service: string, endpointUrl: string) => endpointUrl, |
| 13 | +} as unknown as OAuthClientConfigService; |
| 14 | + |
| 15 | +/** A stored credential whose access token has already lapsed, which is when a refresh runs. */ |
| 16 | +function expiredCredential(metadata: Record<string, unknown>): OAuthCredential { |
| 17 | + return { |
| 18 | + authType: "oauth2", |
| 19 | + accessToken: "old-access-token", |
| 20 | + tokenType: "Bearer", |
| 21 | + expiresAt: new Date(Date.now() - 60_000).toISOString(), |
| 22 | + refreshToken: "refresh-token", |
| 23 | + profile: { accountId: "oauth2", displayName: "OAuth Credential", grantedScopes: [] }, |
| 24 | + metadata, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +function stubRefreshResponse(payload: Record<string, unknown>): void { |
| 29 | + vi.stubGlobal( |
| 30 | + "fetch", |
| 31 | + vi.fn(async () => Response.json({ access_token: "new-access-token", ...payload })), |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +describe("OAuthCredentialRefreshService", () => { |
| 36 | + afterEach(() => { |
| 37 | + vi.unstubAllGlobals(); |
| 38 | + vi.restoreAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("keeps an expiry when the refresh response omits expires_in", async () => { |
| 42 | + const now = Date.now(); |
| 43 | + vi.spyOn(Date, "now").mockReturnValue(now); |
| 44 | + stubRefreshResponse({}); |
| 45 | + |
| 46 | + const refreshed = await new OAuthCredentialRefreshService(clientConfigs).refresh( |
| 47 | + "example", |
| 48 | + expiredCredential({ expires_in: 3600 }), |
| 49 | + ); |
| 50 | + |
| 51 | + expect(refreshed.expiresAt).toBe(new Date(now + 3600_000).toISOString()); |
| 52 | + }); |
| 53 | + |
| 54 | + it("never carries the lapsed expiry forward, which would refresh on every call", async () => { |
| 55 | + const credential = expiredCredential({ expires_in: 3600 }); |
| 56 | + stubRefreshResponse({}); |
| 57 | + |
| 58 | + const refreshed = await new OAuthCredentialRefreshService(clientConfigs).refresh("example", credential); |
| 59 | + |
| 60 | + expect(refreshed.expiresAt).not.toBe(credential.expiresAt); |
| 61 | + expect(Date.parse(refreshed.expiresAt!)).toBeGreaterThan(Date.now()); |
| 62 | + }); |
| 63 | + |
| 64 | + it("prefers the expiry the refresh response reports", async () => { |
| 65 | + const now = Date.now(); |
| 66 | + vi.spyOn(Date, "now").mockReturnValue(now); |
| 67 | + stubRefreshResponse({ expires_in: 120 }); |
| 68 | + |
| 69 | + const refreshed = await new OAuthCredentialRefreshService(clientConfigs).refresh( |
| 70 | + "example", |
| 71 | + expiredCredential({ expires_in: 3600 }), |
| 72 | + ); |
| 73 | + |
| 74 | + expect(refreshed.expiresAt).toBe(new Date(now + 120_000).toISOString()); |
| 75 | + }); |
| 76 | + |
| 77 | + it("leaves the expiry unset when no lifetime was ever reported", async () => { |
| 78 | + stubRefreshResponse({}); |
| 79 | + |
| 80 | + const refreshed = await new OAuthCredentialRefreshService(clientConfigs).refresh("example", expiredCredential({})); |
| 81 | + |
| 82 | + expect(refreshed.expiresAt).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("carries a reported lifetime through a later refresh that omits it", async () => { |
| 86 | + const service = new OAuthCredentialRefreshService(clientConfigs); |
| 87 | + stubRefreshResponse({ expires_in: 3600 }); |
| 88 | + const first = await service.refresh("example", expiredCredential({})); |
| 89 | + |
| 90 | + const now = Date.now(); |
| 91 | + vi.spyOn(Date, "now").mockReturnValue(now); |
| 92 | + stubRefreshResponse({}); |
| 93 | + const second = await service.refresh("example", { ...first, expiresAt: new Date(now - 60_000).toISOString() }); |
| 94 | + |
| 95 | + expect(second.expiresAt).toBe(new Date(now + 3600_000).toISOString()); |
| 96 | + }); |
| 97 | + |
| 98 | + it("keeps the last usable lifetime when a refresh reports an unusable value", async () => { |
| 99 | + const now = Date.now(); |
| 100 | + vi.spyOn(Date, "now").mockReturnValue(now); |
| 101 | + const service = new OAuthCredentialRefreshService(clientConfigs); |
| 102 | + stubRefreshResponse({ expires_in: 0 }); |
| 103 | + const first = await service.refresh("example", expiredCredential({ expires_in: 3600 })); |
| 104 | + |
| 105 | + stubRefreshResponse({}); |
| 106 | + const second = await service.refresh("example", { ...first, expiresAt: new Date(now - 60_000).toISOString() }); |
| 107 | + |
| 108 | + expect(second.expiresAt).toBe(new Date(now + 3600_000).toISOString()); |
| 109 | + }); |
| 110 | + |
| 111 | + it("keeps the stored refresh token when the response omits a new one", async () => { |
| 112 | + stubRefreshResponse({}); |
| 113 | + |
| 114 | + const refreshed = await new OAuthCredentialRefreshService(clientConfigs).refresh( |
| 115 | + "example", |
| 116 | + expiredCredential({ expires_in: 3600 }), |
| 117 | + ); |
| 118 | + |
| 119 | + expect(refreshed.refreshToken).toBe("refresh-token"); |
| 120 | + }); |
| 121 | +}); |
0 commit comments