@aws/bedrock-token-generator
Version:
A lightweight library for generating short-term bearer tokens for AWS Bedrock API authentication
127 lines • 6.03 kB
JavaScript
"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_1 = require("./runtimeConfig");
const credential_providers_1 = require("@aws-sdk/credential-providers");
const node_config_provider_1 = require("@smithy/node-config-provider");
const config_resolver_1 = require("@smithy/config-resolver");
// Define mocks with minimal implementation
jest.mock("@aws-sdk/credential-providers", () => ({
fromNodeProviderChain: jest.fn(),
}));
jest.mock("@smithy/node-config-provider", () => ({
loadConfig: jest.fn(),
}));
describe("runtimeConfig", () => {
// Constants for testing
const MOCK_REGION = "us-west-2";
const MOCK_CREDENTIALS = {
accessKeyId: "AKIAIOSFODNN7EXAMPLE",
secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
};
const MOCK_PROFILE = "test-profile";
const MOCK_PROVIDER_CHAIN_CREDENTIALS = {
accessKeyId: "PROVIDER_CHAIN_ACCESS_KEY",
secretAccessKey: "PROVIDER_CHAIN_SECRET_KEY",
};
const MOCK_LOADED_REGION = "eu-central-1";
// Reset mocks before each test
beforeEach(() => {
jest.clearAllMocks();
// Setup default mock implementations for each test
credential_providers_1.fromNodeProviderChain.mockReturnValue(MOCK_PROVIDER_CHAIN_CREDENTIALS);
// Create a mock region provider function that returns the mock region
const mockRegionProvider = jest.fn().mockResolvedValue(MOCK_LOADED_REGION);
node_config_provider_1.loadConfig.mockReturnValue(mockRegionProvider);
});
it("should use provided credentials and region when available", () => {
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
expiresInSeconds: 7200,
};
const result = (0, runtimeConfig_1.getCreateTokenConfig)(config);
expect(result).toEqual({
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
expiresInSeconds: 7200,
});
// Verify provider functions were not called
expect(credential_providers_1.fromNodeProviderChain).not.toHaveBeenCalled();
expect(node_config_provider_1.loadConfig).not.toHaveBeenCalled();
});
it("should use fromNodeProviderChain when credentials are not provided", () => {
const config = {
region: MOCK_REGION,
};
const result = (0, runtimeConfig_1.getCreateTokenConfig)(config);
expect(result).toEqual({
credentials: MOCK_PROVIDER_CHAIN_CREDENTIALS,
region: MOCK_REGION,
});
expect(credential_providers_1.fromNodeProviderChain).toHaveBeenCalledWith({
profile: undefined,
});
expect(node_config_provider_1.loadConfig).not.toHaveBeenCalled();
});
it("should use loadConfig when region is not provided", async () => {
const config = {
credentials: MOCK_CREDENTIALS,
};
const result = (0, runtimeConfig_1.getCreateTokenConfig)(config);
// Verify the result contains the credentials and a Provider<string> for region
expect(result.credentials).toEqual(MOCK_CREDENTIALS);
expect(typeof result.region).toBe("function");
expect(credential_providers_1.fromNodeProviderChain).not.toHaveBeenCalled();
expect(node_config_provider_1.loadConfig).toHaveBeenCalledWith(config_resolver_1.NODE_REGION_CONFIG_OPTIONS, {
...config_resolver_1.NODE_REGION_CONFIG_FILE_OPTIONS,
profile: undefined,
});
// Verify the region provider function returns the expected value
const regionProvider = result.region;
const region = await regionProvider();
expect(region).toBe(MOCK_LOADED_REGION);
});
it("should use both provider functions when neither credentials nor region are provided", async () => {
const config = {};
const result = (0, runtimeConfig_1.getCreateTokenConfig)(config);
// Verify the result contains the provider chain credentials and a Provider<string> for region
expect(result.credentials).toEqual(MOCK_PROVIDER_CHAIN_CREDENTIALS);
expect(typeof result.region).toBe("function");
expect(credential_providers_1.fromNodeProviderChain).toHaveBeenCalledWith({
profile: undefined,
});
expect(node_config_provider_1.loadConfig).toHaveBeenCalledWith(config_resolver_1.NODE_REGION_CONFIG_OPTIONS, {
...config_resolver_1.NODE_REGION_CONFIG_FILE_OPTIONS,
profile: undefined,
});
// Verify the region provider function returns the expected value
const regionProvider = result.region;
const region = await regionProvider();
expect(region).toBe(MOCK_LOADED_REGION);
});
it("should pass profile to provider functions when specified", async () => {
const config = {
profile: MOCK_PROFILE,
};
const result = (0, runtimeConfig_1.getCreateTokenConfig)(config);
// Verify the result contains the provider chain credentials, a Provider<string> for region, and the profile
expect(result.credentials).toEqual(MOCK_PROVIDER_CHAIN_CREDENTIALS);
expect(typeof result.region).toBe("function");
expect(credential_providers_1.fromNodeProviderChain).toHaveBeenCalledWith({
profile: MOCK_PROFILE,
});
expect(node_config_provider_1.loadConfig).toHaveBeenCalledWith(config_resolver_1.NODE_REGION_CONFIG_OPTIONS, {
...config_resolver_1.NODE_REGION_CONFIG_FILE_OPTIONS,
profile: MOCK_PROFILE,
});
// Verify the region provider function returns the expected value
const regionProvider = result.region;
const region = await regionProvider();
expect(region).toBe(MOCK_LOADED_REGION);
});
});
//# sourceMappingURL=runtimeConfig.spec.js.map