UNPKG

@aws/bedrock-token-generator

Version:

A lightweight library for generating short-term bearer tokens for AWS Bedrock API authentication

89 lines 3.83 kB
"use strict"; /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); const runtimeConfig_browser_1 = require("./runtimeConfig.browser"); const invalid_dependency_1 = require("@smithy/invalid-dependency"); // Define mocks with minimal implementation jest.mock("@smithy/invalid-dependency", () => ({ invalidProvider: jest.fn((message) => { return () => { throw new Error(message); }; }), })); describe("runtimeConfig.browser", () => { // Constants for testing const MOCK_REGION = "us-west-2"; const MOCK_CREDENTIALS = { accessKeyId: "AKIAIOSFODNN7EXAMPLE", secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", }; // Reset mocks before each test beforeEach(() => { jest.clearAllMocks(); }); it("should use provided credentials and region when available", () => { const config = { credentials: MOCK_CREDENTIALS, region: MOCK_REGION, expiresInSeconds: 7200, }; const result = (0, runtimeConfig_browser_1.getCreateTokenConfig)(config); expect(result).toEqual({ credentials: MOCK_CREDENTIALS, region: MOCK_REGION, expiresInSeconds: 7200, }); // Verify invalidProvider was not called expect(invalid_dependency_1.invalidProvider).not.toHaveBeenCalled(); }); it("should use invalidProvider when credentials are not provided", () => { const config = { region: MOCK_REGION, }; const result = (0, runtimeConfig_browser_1.getCreateTokenConfig)(config); expect(result.region).toBe(MOCK_REGION); expect(typeof result.credentials).toBe("function"); expect(invalid_dependency_1.invalidProvider).toHaveBeenCalledWith("Credential is missing"); // Verify the invalidProvider function throws the expected error expect(() => { const credentialsProvider = result.credentials; credentialsProvider(); }).toThrow("Credential is missing"); }); it("should use invalidProvider when region is not provided", () => { const config = { credentials: MOCK_CREDENTIALS, }; const result = (0, runtimeConfig_browser_1.getCreateTokenConfig)(config); expect(result.credentials).toBe(MOCK_CREDENTIALS); expect(typeof result.region).toBe("function"); expect(invalid_dependency_1.invalidProvider).toHaveBeenCalledWith("Region is missing"); // Verify the invalidProvider function throws the expected error expect(() => { const regionProvider = result.region; regionProvider(); }).toThrow("Region is missing"); }); it("should use invalidProvider for both when neither credentials nor region are provided", () => { const config = {}; const result = (0, runtimeConfig_browser_1.getCreateTokenConfig)(config); expect(typeof result.credentials).toBe("function"); expect(typeof result.region).toBe("function"); expect(invalid_dependency_1.invalidProvider).toHaveBeenCalledWith("Credential is missing"); expect(invalid_dependency_1.invalidProvider).toHaveBeenCalledWith("Region is missing"); // Verify the invalidProvider functions throw the expected errors expect(() => { const credentialsProvider = result.credentials; credentialsProvider(); }).toThrow("Credential is missing"); expect(() => { const regionProvider = result.region; regionProvider(); }).toThrow("Region is missing"); }); }); //# sourceMappingURL=runtimeConfig.browser.spec.js.map