@aws/bedrock-token-generator
Version:
A lightweight library for generating short-term bearer tokens for AWS Bedrock API authentication
123 lines • 4.93 kB
JavaScript
;
/**
* 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 getToken_1 = require("./getToken");
const tokenModule = __importStar(require("./token"));
// Mock the token module
jest.mock("./token", () => ({
createToken: jest.fn().mockResolvedValue("mocked-token-value"),
validateTokenExpiryInput: jest.fn(),
}));
describe("getToken", () => {
// 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 call validateTokenExpiryInput with the provided expiresInSeconds", async () => {
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
expiresInSeconds: 7200,
};
await (0, getToken_1.getToken)(config);
expect(tokenModule.validateTokenExpiryInput).toHaveBeenCalledWith(7200);
});
it("should call validateTokenExpiryInput with undefined when expiresInSeconds is not provided", async () => {
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
};
await (0, getToken_1.getToken)(config);
expect(tokenModule.validateTokenExpiryInput).toHaveBeenCalledWith(undefined);
});
it("should call createToken with the provided config", async () => {
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
expiresInSeconds: 7200,
};
await (0, getToken_1.getToken)(config);
expect(tokenModule.createToken).toHaveBeenCalledWith(config);
});
it("should return the token from createToken", async () => {
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
};
const token = await (0, getToken_1.getToken)(config);
expect(token).toBe("mocked-token-value");
});
it("should handle credential provider functions", async () => {
const credentialProvider = jest.fn().mockResolvedValue(MOCK_CREDENTIALS);
const config = {
credentials: credentialProvider,
region: MOCK_REGION,
};
await (0, getToken_1.getToken)(config);
expect(tokenModule.createToken).toHaveBeenCalledWith(config);
});
it("should propagate errors from validateTokenExpiryInput", async () => {
const validationError = new Error("Invalid expiry time");
tokenModule.validateTokenExpiryInput.mockImplementationOnce(() => {
throw validationError;
});
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
expiresInSeconds: -1, // Invalid value
};
await expect((0, getToken_1.getToken)(config)).rejects.toThrow(validationError);
});
it("should propagate errors from createToken", async () => {
const createTokenError = new Error("Token creation failed");
tokenModule.createToken.mockRejectedValueOnce(createTokenError);
const config = {
credentials: MOCK_CREDENTIALS,
region: MOCK_REGION,
};
await expect((0, getToken_1.getToken)(config)).rejects.toThrow(createTokenError);
});
});
//# sourceMappingURL=getToken.spec.js.map