|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import type { AuthorizationInput } from "./authorize/index.js"; |
| 4 | +import { getAuthHeaders } from "./get-auth-headers.js"; |
| 5 | + |
| 6 | +describe("getAuthHeaders", () => { |
| 7 | + const mockServiceApiKey = "test-service-api-key"; |
| 8 | + const defaultAuthData: AuthorizationInput = { |
| 9 | + incomingServiceApiKey: null, |
| 10 | + incomingServiceApiKeyHash: null, |
| 11 | + secretKey: null, |
| 12 | + clientId: null, |
| 13 | + ecosystemId: null, |
| 14 | + ecosystemPartnerId: null, |
| 15 | + origin: null, |
| 16 | + bundleId: null, |
| 17 | + secretKeyHash: null, |
| 18 | + jwt: null, |
| 19 | + hashedJWT: null, |
| 20 | + }; |
| 21 | + |
| 22 | + it("should use secret key when provided", () => { |
| 23 | + const authData: AuthorizationInput = { |
| 24 | + ...defaultAuthData, |
| 25 | + secretKey: "test-secret-key", |
| 26 | + }; |
| 27 | + |
| 28 | + const headers = getAuthHeaders(authData, mockServiceApiKey); |
| 29 | + |
| 30 | + expect(headers).toEqual({ |
| 31 | + "x-secret-key": "test-secret-key", |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should use JWT when both JWT and teamId are provided", () => { |
| 36 | + const authData: AuthorizationInput = { |
| 37 | + ...defaultAuthData, |
| 38 | + jwt: "test-jwt", |
| 39 | + teamId: "test-team-id", |
| 40 | + }; |
| 41 | + |
| 42 | + const headers = getAuthHeaders(authData, mockServiceApiKey); |
| 43 | + |
| 44 | + expect(headers).toEqual({ |
| 45 | + Authorization: "Bearer test-jwt", |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should use JWT when both JWT and clientId are provided", () => { |
| 50 | + const authData: AuthorizationInput = { |
| 51 | + ...defaultAuthData, |
| 52 | + jwt: "test-jwt", |
| 53 | + clientId: "test-client-id", |
| 54 | + }; |
| 55 | + |
| 56 | + const headers = getAuthHeaders(authData, mockServiceApiKey); |
| 57 | + |
| 58 | + expect(headers).toEqual({ |
| 59 | + Authorization: "Bearer test-jwt", |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should use incoming service api key when provided", () => { |
| 64 | + const authData: AuthorizationInput = { |
| 65 | + ...defaultAuthData, |
| 66 | + incomingServiceApiKey: "test-incoming-service-api-key", |
| 67 | + }; |
| 68 | + |
| 69 | + const headers = getAuthHeaders(authData, mockServiceApiKey); |
| 70 | + |
| 71 | + expect(headers).toEqual({ |
| 72 | + "x-service-api-key": "test-incoming-service-api-key", |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should fall back to service api key when no other auth method is provided", () => { |
| 77 | + const headers = getAuthHeaders(defaultAuthData, mockServiceApiKey); |
| 78 | + |
| 79 | + expect(headers).toEqual({ |
| 80 | + "x-service-api-key": mockServiceApiKey, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should prioritize secret key over other auth methods", () => { |
| 85 | + const authData: AuthorizationInput = { |
| 86 | + ...defaultAuthData, |
| 87 | + secretKey: "test-secret-key", |
| 88 | + jwt: "test-jwt", |
| 89 | + teamId: "test-team-id", |
| 90 | + incomingServiceApiKey: "test-incoming-service-api-key", |
| 91 | + }; |
| 92 | + |
| 93 | + const headers = getAuthHeaders(authData, mockServiceApiKey); |
| 94 | + |
| 95 | + expect(headers).toEqual({ |
| 96 | + "x-secret-key": "test-secret-key", |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should prioritize JWT over incoming service api key when teamId is present", () => { |
| 101 | + const authData: AuthorizationInput = { |
| 102 | + ...defaultAuthData, |
| 103 | + jwt: "test-jwt", |
| 104 | + teamId: "test-team-id", |
| 105 | + incomingServiceApiKey: "test-incoming-service-api-key", |
| 106 | + }; |
| 107 | + |
| 108 | + const headers = getAuthHeaders(authData, mockServiceApiKey); |
| 109 | + |
| 110 | + expect(headers).toEqual({ |
| 111 | + Authorization: "Bearer test-jwt", |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments