138 lines
No EOL
5 KiB
TypeScript
138 lines
No EOL
5 KiB
TypeScript
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')}`;
|
|
const contentTypeApplicationJson = "application/json";
|
|
const defaultGrantValue='client_credentials';
|
|
const defaultGrantType=`{"grant_type":"${defaultGrantValue}"}`;
|
|
|
|
describe("Get Access Token", ()=>{
|
|
it("Shoud Get with Standard Config", async ()=>{
|
|
const cli = getOauthClient();
|
|
mockedAxios.request.mockReset();
|
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
|
|
|
await accessTokenValidator(cli);
|
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
|
url: authUrl,
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: basicAuth,
|
|
'Content-Type': contentTypeApplicationJson,
|
|
},
|
|
data: defaultGrantType,
|
|
}));
|
|
});
|
|
|
|
it("Shoud Get with Custom Auth Header", async ()=>{
|
|
const cli = getOauthClient({
|
|
...baseClientParams,
|
|
authHeaderName: 'CustomAuthorizationHeader',
|
|
});
|
|
mockedAxios.request.mockReset();
|
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
|
|
|
await accessTokenValidator(cli);
|
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
|
url: authUrl,
|
|
method: "POST",
|
|
headers: {
|
|
CustomAuthorizationHeader: basicAuth,
|
|
'Content-Type': contentTypeApplicationJson,
|
|
},
|
|
data: defaultGrantType,
|
|
}));
|
|
});
|
|
|
|
it("Shoud Get with Custom Grant Type", async ()=>{
|
|
const cli = getOauthClient({
|
|
...baseClientParams,
|
|
grantType: 'PermissaoCustom',
|
|
});
|
|
mockedAxios.request.mockReset();
|
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
|
|
|
await accessTokenValidator(cli);
|
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
|
url: authUrl,
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: basicAuth,
|
|
'Content-Type': contentTypeApplicationJson,
|
|
},
|
|
data: '{"grant_type":"PermissaoCustom"}',
|
|
}));
|
|
});
|
|
|
|
it("Shoud Get with Custom Auth Scope", async ()=>{
|
|
const cli = getOauthClient({
|
|
...baseClientParams,
|
|
authScope: 'EscopoCustom',
|
|
});
|
|
mockedAxios.request.mockReset();
|
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
|
|
|
await accessTokenValidator(cli);
|
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
|
url: authUrl,
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: basicAuth,
|
|
'Content-Type': contentTypeApplicationJson,
|
|
},
|
|
data: `{"grant_type":"${defaultGrantValue}","scope":"EscopoCustom"}`,
|
|
}));
|
|
});
|
|
|
|
it("Shoud Get with Credentials on Request body", async ()=>{
|
|
const cli = getOauthClient({
|
|
...baseClientParams,
|
|
sendAuthCredentialsOnRequestBody: true,
|
|
});
|
|
mockedAxios.request.mockReset();
|
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
|
|
|
await accessTokenValidator(cli);
|
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
|
url: authUrl,
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: basicAuth,
|
|
'Content-Type': contentTypeApplicationJson,
|
|
},
|
|
data: `{"grant_type":"${defaultGrantValue}","client_id":"${clientId}","client_secret":"${clientSecret}"}`,
|
|
}));
|
|
});
|
|
});
|
|
|
|
async function accessTokenValidator(cli: LhispOauthClient){
|
|
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);
|
|
}
|
|
|
|
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",
|
|
} |