UNPKG

@aws/bedrock-token-generator

Version:

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

151 lines 6.63 kB
"use strict"; /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const getTokenProvider_1 = require("./getTokenProvider"); // Define mocks with minimal implementation jest.mock("./token", () => ({ createToken: jest.fn(), validateTokenExpiryInput: jest.fn(), })); jest.mock("./runtimeConfig", () => ({ getCreateTokenConfig: jest.fn(), })); // Import after mocking to get the mocked versions const tokenModule = __importStar(require("./token")); const runtimeConfigModule = __importStar(require("./runtimeConfig")); describe("getTokenProvider", () => { // Constants for testing const MOCK_REGION = "us-west-2"; const MOCK_CREDENTIALS = { accessKeyId: "AKIAIOSFODNN7EXAMPLE", secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", }; const mockRuntimeCreateConfig = { credentials: { accessKeyId: "MOCK_ACCESS_KEY", secretAccessKey: "MOCK_SECRET_KEY", }, region: "us-west-2", expiresInSeconds: 3600, }; // Reset mocks before each test beforeEach(() => { jest.clearAllMocks(); // Setup default mock implementations for each test tokenModule.createToken.mockResolvedValue("mocked-token-value"); runtimeConfigModule.getCreateTokenConfig.mockReturnValue(mockRuntimeCreateConfig); }); it("should validate expiresInSeconds during initialization", () => { const config = { expiresInSeconds: 7200, }; (0, getTokenProvider_1.getTokenProvider)(config); expect(tokenModule.validateTokenExpiryInput).toHaveBeenCalledWith(7200); }); it("should not throw when expiresInSeconds is not provided", () => { expect(() => (0, getTokenProvider_1.getTokenProvider)()).not.toThrow(); expect(tokenModule.validateTokenExpiryInput).toHaveBeenCalledWith(undefined); }); it("should return a function that provides tokens", async () => { const provideToken = (0, getTokenProvider_1.getTokenProvider)(); expect(typeof provideToken).toBe("function"); const token = await provideToken(); expect(token).toBe("mocked-token-value"); }); it("should call getCreateTokenConfig only once across multiple invocations", async () => { const config = { credentials: MOCK_CREDENTIALS, region: MOCK_REGION, }; const provideToken = (0, getTokenProvider_1.getTokenProvider)(config); // First call await provideToken(); expect(runtimeConfigModule.getCreateTokenConfig).toHaveBeenCalledTimes(1); expect(runtimeConfigModule.getCreateTokenConfig).toHaveBeenCalledWith(config); // Second call await provideToken(); expect(runtimeConfigModule.getCreateTokenConfig).toHaveBeenCalledTimes(1); // getCreateTokenConfig should not be called again }); it("should call createToken on each invocation", async () => { const provideToken = (0, getTokenProvider_1.getTokenProvider)(); // First call await provideToken(); expect(tokenModule.createToken).toHaveBeenCalledTimes(1); // Second call await provideToken(); expect(tokenModule.createToken).toHaveBeenCalledTimes(2); }); it("should pass the config from getCreateTokenConfig to createToken", async () => { const provideToken = (0, getTokenProvider_1.getTokenProvider)(); await provideToken(); expect(tokenModule.createToken).toHaveBeenCalledWith(mockRuntimeCreateConfig); }); it("should propagate errors from validateTokenExpiryInput", () => { const validationError = new Error("Invalid expiry time"); tokenModule.validateTokenExpiryInput.mockImplementationOnce(() => { throw validationError; }); expect(() => (0, getTokenProvider_1.getTokenProvider)({ expiresInSeconds: -1 })).toThrow(validationError); }); it("should propagate errors from createToken", async () => { const createTokenError = new Error("Token creation failed"); tokenModule.createToken.mockRejectedValueOnce(createTokenError); const provideToken = (0, getTokenProvider_1.getTokenProvider)(); await expect(provideToken()).rejects.toThrow(createTokenError); }); it("should handle profile option", async () => { const config = { profile: "test-profile", }; const provideToken = (0, getTokenProvider_1.getTokenProvider)(config); // Actually invoke the provider function await provideToken(); expect(runtimeConfigModule.getCreateTokenConfig).toHaveBeenCalledWith(config); expect(tokenModule.createToken).toHaveBeenCalled(); }); it("should handle empty config", async () => { const provideToken = (0, getTokenProvider_1.getTokenProvider)(); // Actually invoke the provider function await provideToken(); expect(runtimeConfigModule.getCreateTokenConfig).toHaveBeenCalledWith({}); expect(tokenModule.createToken).toHaveBeenCalled(); }); }); //# sourceMappingURL=getTokenProvider.spec.js.map