|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { AuthData } from "@/APL"; |
| 4 | +import { SALEOR_API_URL_HEADER } from "@/const"; |
| 5 | +import * as fetchRemoteJwksModule from "@/fetch-remote-jwks"; |
| 6 | +import * as getAppIdModule from "@/get-app-id"; |
| 7 | +import { MockAPL } from "@/test-utils/mock-apl"; |
| 8 | + |
| 9 | +import { |
| 10 | + createAppRegisterHandler, |
| 11 | + CreateAppRegisterHandlerOptions, |
| 12 | +} from "./create-app-register-handler"; |
| 13 | + |
| 14 | +describe("Fetch API createAppRegisterHandler", () => { |
| 15 | + const mockJwksValue = "{}"; |
| 16 | + const mockAppId = "42"; |
| 17 | + const saleorApiUrl = "https://mock-saleor-domain.saleor.cloud/graphql/"; |
| 18 | + const authToken = "mock-auth-token"; |
| 19 | + |
| 20 | + vi.spyOn(fetchRemoteJwksModule, "fetchRemoteJwks").mockResolvedValue(mockJwksValue); |
| 21 | + vi.spyOn(getAppIdModule, "getAppId").mockResolvedValue(mockAppId); |
| 22 | + let mockApl: MockAPL; |
| 23 | + let request: Request; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + mockApl = new MockAPL(); |
| 27 | + request = new Request("https://example.com", { |
| 28 | + method: "POST", |
| 29 | + headers: { |
| 30 | + "Content-Type": "application/json", |
| 31 | + Host: "mock-slaeor-domain.saleor.cloud", |
| 32 | + "X-Forwarded-Proto": "https", |
| 33 | + [SALEOR_API_URL_HEADER]: saleorApiUrl, |
| 34 | + }, |
| 35 | + body: JSON.stringify({ auth_token: authToken }), |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("Sets auth data for correct request", async () => { |
| 40 | + const handler = createAppRegisterHandler({ apl: mockApl }); |
| 41 | + const response = await handler(request); |
| 42 | + |
| 43 | + expect(response.status).toBe(200); |
| 44 | + expect(mockApl.set).toHaveBeenCalledWith({ |
| 45 | + saleorApiUrl, |
| 46 | + token: authToken, |
| 47 | + appId: mockAppId, |
| 48 | + jwks: mockJwksValue, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("Returns 403 for prohibited Saleor URLs", async () => { |
| 53 | + request.headers.set(SALEOR_API_URL_HEADER, "https://wrong-domain.saleor.cloud/graphql/"); |
| 54 | + const handler = createAppRegisterHandler({ |
| 55 | + apl: mockApl, |
| 56 | + allowedSaleorUrls: [saleorApiUrl], |
| 57 | + }); |
| 58 | + |
| 59 | + const response = await handler(request); |
| 60 | + const data = await response.json(); |
| 61 | + |
| 62 | + expect(response.status).toBe(403); |
| 63 | + expect(data.success).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it("Handles invalid JSON bodies", async () => { |
| 67 | + const brokenRequest = new Request("https://example.com", { |
| 68 | + method: "POST", |
| 69 | + headers: { |
| 70 | + "Content-Type": "application/json", |
| 71 | + Host: "mock-slaeor-domain.saleor.cloud", |
| 72 | + "X-Forwarded-Proto": "https", |
| 73 | + [SALEOR_API_URL_HEADER]: saleorApiUrl, |
| 74 | + }, |
| 75 | + body: "{ ", |
| 76 | + }); |
| 77 | + const handler = createAppRegisterHandler({ |
| 78 | + apl: mockApl, |
| 79 | + allowedSaleorUrls: [saleorApiUrl], |
| 80 | + }); |
| 81 | + |
| 82 | + const response = await handler(brokenRequest); |
| 83 | + |
| 84 | + expect(response.status).toBe(400); |
| 85 | + await expect(response.text()).resolves.toBe("Invalid request json."); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("Callback hooks", () => { |
| 89 | + const expectedAuthData: AuthData = { |
| 90 | + token: authToken, |
| 91 | + saleorApiUrl, |
| 92 | + jwks: mockJwksValue, |
| 93 | + appId: mockAppId, |
| 94 | + }; |
| 95 | + |
| 96 | + it("Triggers success callbacks when APL save succeeds", async () => { |
| 97 | + const mockOnRequestStart = vi.fn(); |
| 98 | + const mockOnRequestVerified = vi.fn(); |
| 99 | + const mockOnAuthAplFailed = vi.fn(); |
| 100 | + const mockOnAuthAplSaved = vi.fn(); |
| 101 | + |
| 102 | + const handler = createAppRegisterHandler({ |
| 103 | + apl: mockApl, |
| 104 | + onRequestStart: mockOnRequestStart, |
| 105 | + onRequestVerified: mockOnRequestVerified, |
| 106 | + onAplSetFailed: mockOnAuthAplFailed, |
| 107 | + onAuthAplSaved: mockOnAuthAplSaved, |
| 108 | + }); |
| 109 | + |
| 110 | + await handler(request); |
| 111 | + |
| 112 | + expect(mockOnRequestStart).toHaveBeenCalledWith( |
| 113 | + request, |
| 114 | + expect.objectContaining({ |
| 115 | + authToken, |
| 116 | + saleorApiUrl, |
| 117 | + }) |
| 118 | + ); |
| 119 | + expect(mockOnRequestVerified).toHaveBeenCalledWith( |
| 120 | + request, |
| 121 | + expect.objectContaining({ |
| 122 | + authData: expectedAuthData, |
| 123 | + }) |
| 124 | + ); |
| 125 | + expect(mockOnAuthAplSaved).toHaveBeenCalledWith( |
| 126 | + request, |
| 127 | + expect.objectContaining({ |
| 128 | + authData: expectedAuthData, |
| 129 | + }) |
| 130 | + ); |
| 131 | + expect(mockOnAuthAplFailed).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("Triggers failure callback when APL save fails", async () => { |
| 135 | + const mockOnAuthAplFailed = vi.fn(); |
| 136 | + const mockOnAuthAplSaved = vi.fn(); |
| 137 | + |
| 138 | + mockApl.set.mockRejectedValueOnce(new Error("Save failed")); |
| 139 | + |
| 140 | + const handler = createAppRegisterHandler({ |
| 141 | + apl: mockApl, |
| 142 | + onAplSetFailed: mockOnAuthAplFailed, |
| 143 | + onAuthAplSaved: mockOnAuthAplSaved, |
| 144 | + }); |
| 145 | + |
| 146 | + await handler(request); |
| 147 | + |
| 148 | + expect(mockOnAuthAplFailed).toHaveBeenCalledWith( |
| 149 | + request, |
| 150 | + expect.objectContaining({ |
| 151 | + error: expect.any(Error), |
| 152 | + authData: expectedAuthData, |
| 153 | + }) |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it("Allows custom error responses via hooks", async () => { |
| 158 | + const mockOnRequestStart = vi |
| 159 | + .fn<NonNullable<CreateAppRegisterHandlerOptions["onRequestStart"]>>() |
| 160 | + .mockImplementation((_req, context) => |
| 161 | + context.respondWithError({ |
| 162 | + status: 401, |
| 163 | + message: "test message", |
| 164 | + }) |
| 165 | + ); |
| 166 | + const handler = createAppRegisterHandler({ |
| 167 | + apl: mockApl, |
| 168 | + onRequestStart: mockOnRequestStart, |
| 169 | + }); |
| 170 | + |
| 171 | + const response = await handler(request); |
| 172 | + |
| 173 | + expect(response.status).toBe(401); |
| 174 | + await expect(response.json()).resolves.toStrictEqual({ |
| 175 | + error: { |
| 176 | + code: "REGISTER_HANDLER_HOOK_ERROR", |
| 177 | + message: "test message", |
| 178 | + }, |
| 179 | + success: false, |
| 180 | + }); |
| 181 | + expect(mockOnRequestStart).toHaveBeenCalled(); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments