Skip to content

Commit

Permalink
chore: add token manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 committed Dec 5, 2024
1 parent 4968d9f commit ffc42aa
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 4 deletions.
4 changes: 1 addition & 3 deletions spec/unit/credential_provider/OrgsCredentialProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
122 changes: 122 additions & 0 deletions spec/unit/http/bearer_token/ApiTokenManager.spec.ts
Original file line number Diff line number Diff line change
@@ -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<any>) {
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<any>;
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<any>;
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`
);
});
});
122 changes: 122 additions & 0 deletions spec/unit/http/bearer_token/OrgsTokenManager.spec.ts
Original file line number Diff line number Diff line change
@@ -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<any>) {
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<any>;
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<any>;
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`
);
});
});
4 changes: 3 additions & 1 deletion src/http/bearer_token/OrgsTokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
});
}
}

0 comments on commit ffc42aa

Please sign in to comment.