Implementando request tests
This commit is contained in:
parent
30090fe195
commit
8535fc0f51
2 changed files with 135 additions and 11 deletions
|
@ -1,6 +1,6 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import LhispOauthClient from "../src/lhisp-oauth-client";
|
import LhispOauthClient from "../src/lhisp-oauth-client";
|
||||||
import { LhispOauthClientConstructorParams } from '../src/lhisp-oauth-client.t';
|
import { ContentType, LhispOauthClientConstructorParams } from '../src/lhisp-oauth-client.t';
|
||||||
|
|
||||||
// Mock jest and set the type
|
// Mock jest and set the type
|
||||||
jest.mock('axios');
|
jest.mock('axios');
|
||||||
|
@ -13,6 +13,7 @@ const clientSecret = "testClientSecret";
|
||||||
const baseClientParams = { apiUrl, authUrl, clientId, clientSecret };
|
const baseClientParams = { apiUrl, authUrl, clientId, clientSecret };
|
||||||
const basicAuth = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`;
|
const basicAuth = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`;
|
||||||
const contentTypeApplicationJson = "application/json";
|
const contentTypeApplicationJson = "application/json";
|
||||||
|
const contentTypeApplicationXFormUrlEncoded = "application/x-www-form-urlencoded";
|
||||||
const defaultGrantValue='client_credentials';
|
const defaultGrantValue='client_credentials';
|
||||||
const defaultGrantType=`{"grant_type":"${defaultGrantValue}"}`;
|
const defaultGrantType=`{"grant_type":"${defaultGrantValue}"}`;
|
||||||
|
|
||||||
|
@ -23,15 +24,7 @@ describe("Get Access Token", ()=>{
|
||||||
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||||
|
|
||||||
await accessTokenValidator(cli);
|
await accessTokenValidator(cli);
|
||||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
validateDefaultGetAccessToken();
|
||||||
url: authUrl,
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Authorization: basicAuth,
|
|
||||||
'Content-Type': contentTypeApplicationJson,
|
|
||||||
},
|
|
||||||
data: defaultGrantType,
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Shoud Get with Custom Auth Header", async ()=>{
|
it("Shoud Get with Custom Auth Header", async ()=>{
|
||||||
|
@ -115,6 +108,137 @@ describe("Get Access Token", ()=>{
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Request", ()=>{
|
||||||
|
it("Get without Params", async ()=>{
|
||||||
|
const cli = getOauthClient();
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
// Mockando a requizição para obter o token.
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} });
|
||||||
|
|
||||||
|
const resp = await cli.get({ path: '/my-test-url' });
|
||||||
|
validateDefaultGetAccessToken();
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: `${apiUrl}/my-test-url`,
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer SomeAccessToken`,
|
||||||
|
'Content-Type': contentTypeApplicationJson,
|
||||||
|
},
|
||||||
|
data: undefined
|
||||||
|
}));
|
||||||
|
expect(resp).toStrictEqual({"status": "ok"});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Get with Params", async ()=>{
|
||||||
|
const cli = getOauthClient();
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
// Mockando a requizição para obter o token.
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} });
|
||||||
|
|
||||||
|
const resp = await cli.get({ path: '/my-test-url', params: { id: 1 } });
|
||||||
|
validateDefaultGetAccessToken();
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: `${apiUrl}/my-test-url`,
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer SomeAccessToken`,
|
||||||
|
'Content-Type': contentTypeApplicationJson,
|
||||||
|
},
|
||||||
|
params: { id: 1 },
|
||||||
|
data: undefined
|
||||||
|
}));
|
||||||
|
expect(resp).toStrictEqual({"status": "ok"});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Post", async ()=>{
|
||||||
|
const cli = getOauthClient();
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
// Mockando a requizição para obter o token.
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} });
|
||||||
|
|
||||||
|
const resp = await cli.post({ path: '/my-test-url-post', data: { id: 1, user: 'test' } });
|
||||||
|
validateDefaultGetAccessToken();
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: `${apiUrl}/my-test-url-post`,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer SomeAccessToken`,
|
||||||
|
'Content-Type': contentTypeApplicationJson,
|
||||||
|
},
|
||||||
|
data: { id: 1, user: 'test' }
|
||||||
|
}));
|
||||||
|
expect(resp).toStrictEqual({"status": "ok"});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Post with different contentType", async ()=>{
|
||||||
|
const cli = getOauthClient();
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
// Mockando a requizição para obter o token.
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} });
|
||||||
|
|
||||||
|
const resp = await cli.post({
|
||||||
|
path: '/my-test-url-post',
|
||||||
|
data: { id: 1, user: 'test' },
|
||||||
|
contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED
|
||||||
|
});
|
||||||
|
validateDefaultGetAccessToken();
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: `${apiUrl}/my-test-url-post`,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer SomeAccessToken`,
|
||||||
|
'Content-Type': contentTypeApplicationXFormUrlEncoded,
|
||||||
|
},
|
||||||
|
data: { id: 1, user: 'test' }
|
||||||
|
}));
|
||||||
|
expect(resp).toStrictEqual({"status": "ok"});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Post with Different Token Header Name", async ()=>{
|
||||||
|
const cli = getOauthClient({
|
||||||
|
...baseClientParams,
|
||||||
|
tokenHeaderName: "x-token",
|
||||||
|
});
|
||||||
|
mockedAxios.request.mockReset();
|
||||||
|
// Mockando a requizição para obter o token.
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||||
|
mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} });
|
||||||
|
|
||||||
|
const resp = await cli.post({
|
||||||
|
path: '/my-test-url-post',
|
||||||
|
data: { id: 1, user: 'test' },
|
||||||
|
contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED
|
||||||
|
});
|
||||||
|
validateDefaultGetAccessToken();
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: `${apiUrl}/my-test-url-post`,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
'x-token': `Bearer SomeAccessToken`,
|
||||||
|
'Content-Type': contentTypeApplicationXFormUrlEncoded,
|
||||||
|
},
|
||||||
|
data: { id: 1, user: 'test' }
|
||||||
|
}));
|
||||||
|
expect(resp).toStrictEqual({"status": "ok"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function validateDefaultGetAccessToken(){
|
||||||
|
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||||
|
url: authUrl,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: basicAuth,
|
||||||
|
'Content-Type': contentTypeApplicationJson,
|
||||||
|
},
|
||||||
|
data: defaultGrantType,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
async function accessTokenValidator(cli: LhispOauthClient){
|
async function accessTokenValidator(cli: LhispOauthClient){
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const accessToken = await cli.getAccessToken();
|
const accessToken = await cli.getAccessToken();
|
||||||
|
|
|
@ -23,7 +23,7 @@ export interface LhispOauthClientConstructorParams {
|
||||||
|
|
||||||
export interface ExecutarRequestParams extends AxiosRequestConfig {
|
export interface ExecutarRequestParams extends AxiosRequestConfig {
|
||||||
path: string;
|
path: string;
|
||||||
contentType: ContentType,
|
contentType?: ContentType,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AccessToken {
|
export interface AccessToken {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue