|
| 1 | +import { server } from "@tests/server"; |
| 2 | +import handlers from "./handler"; |
| 3 | +import { readCardMockState, type CardTokenResponseId } from "./state"; |
| 4 | + |
| 5 | +const TOKEN_URL = "https://card.test/v1/auth/oauth2/token"; |
| 6 | + |
| 7 | +function renew(response: CardTokenResponseId) { |
| 8 | + const state = readCardMockState(); |
| 9 | + if (!state) throw new Error("Card mock state was not initialized"); |
| 10 | + state.tokenResponse = response; |
| 11 | + return fetch(TOKEN_URL, { |
| 12 | + method: "POST", |
| 13 | + headers: { "content-type": "application/json" }, |
| 14 | + body: JSON.stringify({ grant_type: "refresh_token" }), |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +describe("Card renewal mock handlers", () => { |
| 19 | + beforeEach(() => { |
| 20 | + const state = readCardMockState(); |
| 21 | + if (!state) throw new Error("Card mock state was not initialized"); |
| 22 | + state.tokenResponse = "pass"; |
| 23 | + state.userUnauthorizedOnce = false; |
| 24 | + state.refreshCount = 0; |
| 25 | + server.use(...handlers); |
| 26 | + }); |
| 27 | + |
| 28 | + it.each([ |
| 29 | + ["200", 200], |
| 30 | + ["200-bad-body", 200], |
| 31 | + ["400", 400], |
| 32 | + ["422", 422], |
| 33 | + ["498", 498], |
| 34 | + ["499", 499], |
| 35 | + ["500", 500], |
| 36 | + ] as const)("answers the %s renewal mode", async (mode, status) => { |
| 37 | + const response = await renew(mode); |
| 38 | + |
| 39 | + expect(response.status).toBe(status); |
| 40 | + expect(readCardMockState()?.refreshCount).toBe(1); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns rotated tokens for a successful renewal", async () => { |
| 44 | + const response = await renew("200"); |
| 45 | + |
| 46 | + await expect(response.json()).resolves.toEqual({ |
| 47 | + access_token: "at_mock_1", |
| 48 | + refresh_token: "rt_mock_1", |
| 49 | + expires_in: 3600, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("simulates a renewal network failure", async () => { |
| 54 | + await expect(renew("network-error")).rejects.toBeDefined(); |
| 55 | + expect(readCardMockState()?.refreshCount).toBe(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it("passes through requests the mock does not own", async () => { |
| 59 | + await expect(renew("pass")).rejects.toBeDefined(); |
| 60 | + await expect( |
| 61 | + fetch(TOKEN_URL, { |
| 62 | + method: "POST", |
| 63 | + headers: { "content-type": "application/json" }, |
| 64 | + body: JSON.stringify({ grant_type: "authorization_code" }), |
| 65 | + }), |
| 66 | + ).rejects.toBeDefined(); |
| 67 | + await expect(fetch("https://card.test/v1/user")).rejects.toBeDefined(); |
| 68 | + await expect(fetch("https://card.test/v1/card/status")).rejects.toBeDefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("arms one unauthorized user response", async () => { |
| 72 | + const state = readCardMockState(); |
| 73 | + if (!state) throw new Error("Card mock state was not initialized"); |
| 74 | + state.userUnauthorizedOnce = true; |
| 75 | + |
| 76 | + const response = await fetch("https://card.test/v1/user"); |
| 77 | + |
| 78 | + expect(response.status).toBe(401); |
| 79 | + expect(state.userUnauthorizedOnce).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + it("answers user and card reads for mock access tokens", async () => { |
| 83 | + const headers = { authorization: "Bearer at_mock_1" }; |
| 84 | + |
| 85 | + const [user, card] = await Promise.all([ |
| 86 | + fetch("https://card.test/v1/user", { headers }), |
| 87 | + fetch("https://card.test/v1/card/status", { headers }), |
| 88 | + ]); |
| 89 | + |
| 90 | + await expect(user.json()).resolves.toMatchObject({ |
| 91 | + id: expect.any(String), |
| 92 | + verificationState: "VERIFIED", |
| 93 | + }); |
| 94 | + await expect(card.json()).resolves.toMatchObject({ |
| 95 | + holderName: "JOHN DOE", |
| 96 | + status: "ACTIVE", |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments