Implementando testes do getAccessToken
This commit is contained in:
parent
2fa4d5a933
commit
10a3dad640
1 changed files with 80 additions and 0 deletions
80
__tests__/lhisp-oauth-client.test.ts
Normal file
80
__tests__/lhisp-oauth-client.test.ts
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
import axios from 'axios';
|
||||||
|
import LhispOauthClient from "../src/lhisp-oauth-client";
|
||||||
|
import { LhispOauthClientConstructorParams } from '../src/lhisp-oauth-client.t';
|
||||||
|
|
||||||
|
// Mock jest and set the type
|
||||||
|
jest.mock('axios');
|
||||||
|
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
||||||
|
|
||||||
|
const apiUrl = "https://myapi.com";
|
||||||
|
const authUrl = "https://auth.myapi.com/oauth/token";
|
||||||
|
const clientId = "testClientdId";
|
||||||
|
const clientSecret = "testClientSecret";
|
||||||
|
const baseClientParams = { apiUrl, authUrl, clientId, clientSecret };
|
||||||
|
const basicAuth = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`;
|
||||||
|
|
||||||
|
describe("lhisp-oauth-client", ()=>{
|
||||||
|
it("Shout Get Access Token with Standard Config", async ()=>{
|
||||||
|
const cli = getOauthClient();
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({
|
||||||
|
data: mockedAccessToken
|
||||||
|
});
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
const accessToken = await cli.getAccessToken();
|
||||||
|
expect(accessToken).toBeDefined();
|
||||||
|
expect(accessToken.token_type).toBe(mockedAccessToken.token_type);
|
||||||
|
expect(accessToken.access_token).toBe(mockedAccessToken.access_token);
|
||||||
|
expect(accessToken.expires_in).toBe(mockedAccessToken.expires_in);
|
||||||
|
expect(accessToken.scope).toBe(mockedAccessToken.scope);
|
||||||
|
expect(accessToken.created_at).toBeGreaterThanOrEqual(now);
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: authUrl,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: basicAuth,
|
||||||
|
'Content-Type': "application/json",
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Shout Get Access Token with Custom Auth Header", async ()=>{
|
||||||
|
const cli = getOauthClient({
|
||||||
|
...baseClientParams,
|
||||||
|
authHeaderName: "CustomAuthorizationHeader"
|
||||||
|
});
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({
|
||||||
|
data: mockedAccessToken
|
||||||
|
});
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
const accessToken = await cli.getAccessToken();
|
||||||
|
expect(accessToken).toBeDefined();
|
||||||
|
expect(accessToken.token_type).toBe(mockedAccessToken.token_type);
|
||||||
|
expect(accessToken.access_token).toBe(mockedAccessToken.access_token);
|
||||||
|
expect(accessToken.expires_in).toBe(mockedAccessToken.expires_in);
|
||||||
|
expect(accessToken.scope).toBe(mockedAccessToken.scope);
|
||||||
|
expect(accessToken.created_at).toBeGreaterThanOrEqual(now);
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: authUrl,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
CustomAuthorizationHeader: basicAuth,
|
||||||
|
'Content-Type': "application/json",
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function getOauthClient(opt:LhispOauthClientConstructorParams=baseClientParams){
|
||||||
|
return new LhispOauthClient(opt);
|
||||||
|
}
|
||||||
|
|
||||||
|
const mockedAccessToken = {
|
||||||
|
token_type: "Bearer",
|
||||||
|
access_token: "SomeAccessToken",
|
||||||
|
expires_in: 600,
|
||||||
|
scope: "cobrancas.boletos-requisicao cobrancas.boletos-info",
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue