|
| 1 | +jest.mock("../../../src/core/fetcher/Fetcher", () => ({ |
| 2 | + fetcherImpl: jest.fn(), |
| 3 | +})); |
| 4 | + |
| 5 | +jest.mock("jsonwebtoken", () => ({ |
| 6 | + __esModule: true, |
| 7 | + default: { |
| 8 | + sign: jest.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +import jwt from "jsonwebtoken"; |
| 13 | + |
| 14 | +import { ReferralExchangeJwtClient } from "../../../src/wrapper/ReferralExchangeJwtClient"; |
| 15 | + |
| 16 | +const fetcherImplMock = jest.requireMock("../../../src/core/fetcher/Fetcher").fetcherImpl as jest.Mock; |
| 17 | +const signMock = jwt.sign as jest.Mock<string, any[]>; |
| 18 | + |
| 19 | +describe("ReferralExchangeJwtClient", () => { |
| 20 | + const requestArgs = { url: "https://refx.test/resource", method: "GET" }; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + fetcherImplMock.mockReset(); |
| 24 | + fetcherImplMock.mockResolvedValue({ ok: true }); |
| 25 | + signMock.mockReset(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("signs a new token for every request when caching is disabled", async () => { |
| 29 | + signMock.mockReturnValueOnce("token-1").mockReturnValueOnce("token-2"); |
| 30 | + |
| 31 | + const client = new ReferralExchangeJwtClient({ |
| 32 | + privateKey: "fake-private-key", |
| 33 | + apiKeyName: "issuer", |
| 34 | + }); |
| 35 | + |
| 36 | + const fetcher = ((client as any)._options.fetcher) as (args: typeof requestArgs) => Promise<unknown>; |
| 37 | + |
| 38 | + await fetcher(requestArgs); |
| 39 | + await fetcher(requestArgs); |
| 40 | + |
| 41 | + expect(signMock).toHaveBeenCalledTimes(2); |
| 42 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 43 | + 1, |
| 44 | + expect.objectContaining({ |
| 45 | + headers: expect.objectContaining({ |
| 46 | + Authorization: "Bearer token-1", |
| 47 | + }), |
| 48 | + }), |
| 49 | + ); |
| 50 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 51 | + 2, |
| 52 | + expect.objectContaining({ |
| 53 | + headers: expect.objectContaining({ |
| 54 | + Authorization: "Bearer token-2", |
| 55 | + }), |
| 56 | + }), |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it("reuses cached tokens until the refresh buffer elapses", async () => { |
| 61 | + signMock.mockReturnValueOnce("token-1").mockReturnValueOnce("token-2"); |
| 62 | + |
| 63 | + const client = new ReferralExchangeJwtClient({ |
| 64 | + privateKey: "fake-private-key", |
| 65 | + apiKeyName: "issuer", |
| 66 | + tokenCache: { refreshBufferSeconds: 5 }, |
| 67 | + }); |
| 68 | + const fetcher = ((client as any)._options.fetcher) as (args: typeof requestArgs) => Promise<unknown>; |
| 69 | + |
| 70 | + let nowMs = 0; |
| 71 | + const dateSpy = jest.spyOn(Date, "now").mockImplementation(() => nowMs); |
| 72 | + |
| 73 | + nowMs = 0; |
| 74 | + await fetcher(requestArgs); |
| 75 | + nowMs = 2_000; // 2 seconds, still outside the 5 second refresh buffer window. |
| 76 | + await fetcher(requestArgs); |
| 77 | + nowMs = 11_000; // 11 seconds -> only 4 seconds remain before expiry, so refresh. |
| 78 | + await fetcher(requestArgs); |
| 79 | + |
| 80 | + expect(signMock).toHaveBeenCalledTimes(2); |
| 81 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 82 | + 1, |
| 83 | + expect.objectContaining({ |
| 84 | + headers: expect.objectContaining({ |
| 85 | + Authorization: "Bearer token-1", |
| 86 | + }), |
| 87 | + }), |
| 88 | + ); |
| 89 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 90 | + 2, |
| 91 | + expect.objectContaining({ |
| 92 | + headers: expect.objectContaining({ |
| 93 | + Authorization: "Bearer token-1", |
| 94 | + }), |
| 95 | + }), |
| 96 | + ); |
| 97 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 98 | + 3, |
| 99 | + expect.objectContaining({ |
| 100 | + headers: expect.objectContaining({ |
| 101 | + Authorization: "Bearer token-2", |
| 102 | + }), |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + dateSpy.mockRestore(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("clamps refresh buffer to the JWT lifetime", async () => { |
| 110 | + signMock.mockReturnValueOnce("token-1").mockReturnValueOnce("token-2"); |
| 111 | + |
| 112 | + const client = new ReferralExchangeJwtClient({ |
| 113 | + privateKey: "fake-private-key", |
| 114 | + apiKeyName: "issuer", |
| 115 | + tokenCache: { refreshBufferSeconds: 30 }, // larger than JWT_TTL_SECONDS |
| 116 | + }); |
| 117 | + const fetcher = ((client as any)._options.fetcher) as (args: typeof requestArgs) => Promise<unknown>; |
| 118 | + |
| 119 | + let nowMs = 0; |
| 120 | + const dateSpy = jest.spyOn(Date, "now").mockImplementation(() => nowMs); |
| 121 | + |
| 122 | + nowMs = 0; |
| 123 | + await fetcher(requestArgs); |
| 124 | + nowMs = 500; // 0.5 seconds < (15s - 14s clamp) so the cached token is still valid |
| 125 | + await fetcher(requestArgs); |
| 126 | + nowMs = 1_500; // 1.5 seconds -> exceeds the clamped threshold, so a new token is signed |
| 127 | + await fetcher(requestArgs); |
| 128 | + |
| 129 | + expect(signMock).toHaveBeenCalledTimes(2); |
| 130 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 131 | + 1, |
| 132 | + expect.objectContaining({ |
| 133 | + headers: expect.objectContaining({ |
| 134 | + Authorization: "Bearer token-1", |
| 135 | + }), |
| 136 | + }), |
| 137 | + ); |
| 138 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 139 | + 2, |
| 140 | + expect.objectContaining({ |
| 141 | + headers: expect.objectContaining({ |
| 142 | + Authorization: "Bearer token-1", |
| 143 | + }), |
| 144 | + }), |
| 145 | + ); |
| 146 | + expect(fetcherImplMock).toHaveBeenNthCalledWith( |
| 147 | + 3, |
| 148 | + expect.objectContaining({ |
| 149 | + headers: expect.objectContaining({ |
| 150 | + Authorization: "Bearer token-2", |
| 151 | + }), |
| 152 | + }), |
| 153 | + ); |
| 154 | + |
| 155 | + dateSpy.mockRestore(); |
| 156 | + }); |
| 157 | +}); |
0 commit comments