From ffc42aad7a5af0afafa77e133727bbd493c16051 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari Date: Thu, 5 Dec 2024 20:55:05 +0530 Subject: [PATCH] chore: add token manager tests --- .../OrgsCredentialProvider.spec.ts | 4 +- .../http/bearer_token/ApiTokenManager.spec.ts | 122 ++++++++++++++++++ .../bearer_token/OrgsTokenManager.spec.ts | 122 ++++++++++++++++++ src/http/bearer_token/OrgsTokenManager.ts | 4 +- 4 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 spec/unit/http/bearer_token/ApiTokenManager.spec.ts create mode 100644 spec/unit/http/bearer_token/OrgsTokenManager.spec.ts diff --git a/spec/unit/credential_provider/OrgsCredentialProvider.spec.ts b/spec/unit/credential_provider/OrgsCredentialProvider.spec.ts index 8fbed0d09..4c0ca203e 100644 --- a/spec/unit/credential_provider/OrgsCredentialProvider.spec.ts +++ b/spec/unit/credential_provider/OrgsCredentialProvider.spec.ts @@ -9,9 +9,7 @@ describe("OrgsCredentialProvider Constructor", () => { .build(); it("Should have client-credentials as its authType", () => { - expect(orgsCredentialProvider.getAuthType()).toEqual( - "client-credentials" - ); + expect(orgsCredentialProvider.getAuthType()).toEqual("client-credentials"); }); it("Should return NoAuthStrategy as its auth strategy", () => { diff --git a/spec/unit/http/bearer_token/ApiTokenManager.spec.ts b/spec/unit/http/bearer_token/ApiTokenManager.spec.ts new file mode 100644 index 000000000..f470f2637 --- /dev/null +++ b/spec/unit/http/bearer_token/ApiTokenManager.spec.ts @@ -0,0 +1,122 @@ +import ApiTokenManager from "../../../../src/http/bearer_token/ApiTokenManager"; +import axios from "axios"; +import { jest } from "@jest/globals"; + +function createMockAxios(promiseHandler: Promise) { + const instance = () => promiseHandler; + instance.defaults = { + headers: { + post: {}, + }, + }; + return instance; +} + +describe("ApiTokenManager constructor", function () { + const clientId = "clientId"; + const clientSecret = "clientSecret"; + const grantType = "client_credentials"; + + const apiTokenManager = new ApiTokenManager({ + grantType: grantType, + clientId: clientId, + clientSecret: clientSecret, + }); + + const params = apiTokenManager.getParams(); + + let createSpy: jest.Spied; + const initialHttpProxyValue = process.env.HTTP_PROXY; + + beforeEach(() => { + createSpy = jest.spyOn(axios, "create"); + createSpy.mockReturnValue( + createMockAxios( + Promise.resolve({ + status: 200, + data: { + access_token: "accessTokenValue", + expires_in: 86400, + id_token: null, + refresh_token: null, + token_type: "Bearer", + }, + }) + ) + ); + }); + + afterEach(() => { + createSpy.mockRestore(); + + if (initialHttpProxyValue) { + process.env.HTTP_PROXY = initialHttpProxyValue; + } else { + delete process.env.HTTP_PROXY; + } + }); + + it("Should have client-credentials as its grantType", function () { + expect(params.grantType).toEqual(grantType); + }); + + it("Should have clientId as its clientId", function () { + expect(params.clientId).toEqual(clientId); + }); + + it("Should have clientSecret as its clientSecret", function () { + expect(params.clientSecret).toEqual(clientSecret); + }); + + it("Should return an access token", async function () { + const token = await apiTokenManager.fetchToken(); + expect(token).toEqual("accessTokenValue"); + }); +}); + +describe("ApiTokenManager with error response", function () { + const clientId = "clientId"; + const clientSecret = "clientSecret"; + const grantType = "client_credentials"; + + const apiTokenManager = new ApiTokenManager({ + grantType: grantType, + clientId: clientId, + clientSecret: clientSecret, + }); + + const params = apiTokenManager.getParams(); + + let createSpy: jest.Spied; + const initialHttpProxyValue = process.env.HTTP_PROXY; + + beforeEach(() => { + createSpy = jest.spyOn(axios, "create"); + createSpy.mockReturnValue( + createMockAxios( + Promise.resolve({ + status: 400, + data: { + message: "Token error", + }, + }) + ) + ); + }); + + afterEach(() => { + createSpy.mockRestore(); + + if (initialHttpProxyValue) { + process.env.HTTP_PROXY = initialHttpProxyValue; + } else { + delete process.env.HTTP_PROXY; + } + }); + + it("Should return error message", async function () { + await expect(apiTokenManager.fetchToken()).rejects.toThrow( + `Error Status Code: 400\nFailed to fetch access token: Token error` + ); + }); +}); diff --git a/spec/unit/http/bearer_token/OrgsTokenManager.spec.ts b/spec/unit/http/bearer_token/OrgsTokenManager.spec.ts new file mode 100644 index 000000000..c353d4402 --- /dev/null +++ b/spec/unit/http/bearer_token/OrgsTokenManager.spec.ts @@ -0,0 +1,122 @@ +import OrgsTokenManager from "../../../../src/http/bearer_token/OrgsTokenManager"; +import axios from "axios"; +import { jest } from "@jest/globals"; + +function createMockAxios(promiseHandler: Promise) { + const instance = () => promiseHandler; + instance.defaults = { + headers: { + post: {}, + }, + }; + return instance; +} + +describe("OrgsTokenManager constructor", function () { + const clientId = "clientId"; + const clientSecret = "clientSecret"; + const grantType = "client_credentials"; + + const orgsTokenManager = new OrgsTokenManager({ + grantType: grantType, + clientId: clientId, + clientSecret: clientSecret, + }); + + const params = orgsTokenManager.getParams(); + + let createSpy: jest.Spied; + const initialHttpProxyValue = process.env.HTTP_PROXY; + + beforeEach(() => { + createSpy = jest.spyOn(axios, "create"); + createSpy.mockReturnValue( + createMockAxios( + Promise.resolve({ + status: 200, + data: { + access_token: "accessTokenValue", + expires_in: 86400, + id_token: null, + refresh_token: null, + token_type: "Bearer", + }, + }) + ) + ); + }); + + afterEach(() => { + createSpy.mockRestore(); + + if (initialHttpProxyValue) { + process.env.HTTP_PROXY = initialHttpProxyValue; + } else { + delete process.env.HTTP_PROXY; + } + }); + + it("Should have client-credentials as its grantType", function () { + expect(params.grantType).toEqual(grantType); + }); + + it("Should have clientId as its clientId", function () { + expect(params.clientId).toEqual(clientId); + }); + + it("Should have clientSecret as its clientSecret", function () { + expect(params.clientSecret).toEqual(clientSecret); + }); + + it("Should return an access token", async function () { + const token = await orgsTokenManager.fetchToken(); + expect(token).toEqual("accessTokenValue"); + }); +}); + +describe("OrgsTokenManager with error response", function () { + const clientId = "clientId"; + const clientSecret = "clientSecret"; + const grantType = "client_credentials"; + + const orgsTokenManager = new OrgsTokenManager({ + grantType: grantType, + clientId: clientId, + clientSecret: clientSecret, + }); + + const params = orgsTokenManager.getParams(); + + let createSpy: jest.Spied; + const initialHttpProxyValue = process.env.HTTP_PROXY; + + beforeEach(() => { + createSpy = jest.spyOn(axios, "create"); + createSpy.mockReturnValue( + createMockAxios( + Promise.resolve({ + status: 400, + data: { + message: "Token error", + }, + }) + ) + ); + }); + + afterEach(() => { + createSpy.mockRestore(); + + if (initialHttpProxyValue) { + process.env.HTTP_PROXY = initialHttpProxyValue; + } else { + delete process.env.HTTP_PROXY; + } + }); + + it("Should return error message", async function () { + await expect(orgsTokenManager.fetchToken()).rejects.toThrow( + `Error Status Code: 400\nFailed to fetch access token: Token error` + ); + }); +}); diff --git a/src/http/bearer_token/OrgsTokenManager.ts b/src/http/bearer_token/OrgsTokenManager.ts index b620ad2e8..88da181cf 100644 --- a/src/http/bearer_token/OrgsTokenManager.ts +++ b/src/http/bearer_token/OrgsTokenManager.ts @@ -34,7 +34,9 @@ export default class OrgsTokenManager implements TokenManager { return token.accessToken; }) .catch((error) => { - throw new Error(`Failed to fetch access token: ${error}`); + throw new Error( + `Error Status Code: ${error.status}\nFailed to fetch access token: ${error.message}` + ); }); } }