feat: access token generic para permitir customização
This commit is contained in:
parent
477d9fc6a7
commit
71e18bc997
1 changed files with 226 additions and 207 deletions
|
@ -1,9 +1,9 @@
|
|||
import axios from 'axios';
|
||||
import axios from "axios";
|
||||
import { LhispOauthClient } from "../src/lhisp-oauth-client";
|
||||
import { ContentType, LhispOauthClientConstructorParams } from '../src/lhisp-oauth-client.t';
|
||||
import { ContentType, LhispOauthClientConstructorParams } from "../src/lhisp-oauth-client.t";
|
||||
|
||||
// Mock jest and set the type
|
||||
jest.mock('axios');
|
||||
jest.mock("axios");
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
||||
|
||||
const apiUrl = "https://myapi.com";
|
||||
|
@ -11,10 +11,10 @@ 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 basicAuth = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`;
|
||||
const contentTypeApplicationJson = "application/json";
|
||||
const contentTypeApplicationXFormUrlEncoded = "application/x-www-form-urlencoded";
|
||||
const defaultGrantValue='client_credentials';
|
||||
const defaultGrantValue = "client_credentials";
|
||||
const defaultGrantType = `{"grant_type":"${defaultGrantValue}"}`;
|
||||
|
||||
describe("Get Access Token", () => {
|
||||
|
@ -32,52 +32,58 @@ describe("Get Access Token", ()=>{
|
|||
it("Shoud Get with Custom Auth Header", async () => {
|
||||
const cli = getOauthClient({
|
||||
...baseClientParams,
|
||||
authHeaderName: 'CustomAuthorizationHeader',
|
||||
authHeaderName: "CustomAuthorizationHeader",
|
||||
});
|
||||
await accessTokenValidator(cli);
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: authUrl,
|
||||
method: "POST",
|
||||
headers: {
|
||||
CustomAuthorizationHeader: basicAuth,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: defaultGrantType,
|
||||
}));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("Shoud Get with Custom Grant Type", async () => {
|
||||
const cli = getOauthClient({
|
||||
...baseClientParams,
|
||||
grantType: 'PermissaoCustom',
|
||||
grantType: "PermissaoCustom",
|
||||
});
|
||||
await accessTokenValidator(cli);
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: authUrl,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: basicAuth,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: '{"grant_type":"PermissaoCustom"}',
|
||||
}));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("Shoud Get with Custom Auth Scope", async () => {
|
||||
const cli = getOauthClient({
|
||||
...baseClientParams,
|
||||
authScope: 'EscopoCustom',
|
||||
authScope: "EscopoCustom",
|
||||
});
|
||||
await accessTokenValidator(cli);
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: authUrl,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: basicAuth,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: `{"grant_type":"${defaultGrantValue}","scope":"EscopoCustom"}`,
|
||||
}));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("Shoud Get with Credentials on Request body", async () => {
|
||||
|
@ -86,15 +92,17 @@ describe("Get Access Token", ()=>{
|
|||
sendAuthCredentialsOnRequestBody: true,
|
||||
});
|
||||
await accessTokenValidator(cli);
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: authUrl,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: basicAuth,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: `{"grant_type":"${defaultGrantValue}","client_id":"${clientId}","client_secret":"${clientSecret}"}`,
|
||||
}));
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -102,76 +110,84 @@ describe("Request", ()=>{
|
|||
beforeEach(() => {
|
||||
mockedAxios.request.mockReset();
|
||||
mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken });
|
||||
mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} });
|
||||
mockedAxios.request.mockResolvedValueOnce({ data: { status: "ok" } });
|
||||
});
|
||||
|
||||
it("Get without Params", async () => {
|
||||
const cli = getOauthClient();
|
||||
const resp = await cli.get({ path: '/my-test-url' });
|
||||
const resp = await cli.get({ path: "/my-test-url" });
|
||||
validateDefaultGetAccessToken();
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: `${apiUrl}/my-test-url`,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer SomeAccessToken`,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: undefined
|
||||
}));
|
||||
expect(resp).toStrictEqual({"status": "ok"});
|
||||
data: undefined,
|
||||
})
|
||||
);
|
||||
expect(resp).toStrictEqual({ status: "ok" });
|
||||
});
|
||||
|
||||
it("Get with Params", async () => {
|
||||
const cli = getOauthClient();
|
||||
const resp = await cli.get({ path: '/my-test-url', params: { id: 1 } });
|
||||
const resp = await cli.get({ path: "/my-test-url", params: { id: 1 } });
|
||||
validateDefaultGetAccessToken();
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: `${apiUrl}/my-test-url`,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer SomeAccessToken`,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
params: { id: 1 },
|
||||
data: undefined
|
||||
}));
|
||||
expect(resp).toStrictEqual({"status": "ok"});
|
||||
data: undefined,
|
||||
})
|
||||
);
|
||||
expect(resp).toStrictEqual({ status: "ok" });
|
||||
});
|
||||
|
||||
it("Post", async () => {
|
||||
const cli = getOauthClient();
|
||||
const resp = await cli.post({ path: '/my-test-url-post', data: { id: 1, user: 'test' } });
|
||||
const resp = await cli.post({ path: "/my-test-url-post", data: { id: 1, user: "test" } });
|
||||
validateDefaultGetAccessToken();
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: `${apiUrl}/my-test-url-post`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer SomeAccessToken`,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: { id: 1, user: 'test' }
|
||||
}));
|
||||
expect(resp).toStrictEqual({"status": "ok"});
|
||||
data: { id: 1, user: "test" },
|
||||
})
|
||||
);
|
||||
expect(resp).toStrictEqual({ status: "ok" });
|
||||
});
|
||||
|
||||
it("Post with different contentType", async () => {
|
||||
const cli = getOauthClient();
|
||||
const resp = await cli.post({
|
||||
path: '/my-test-url-post',
|
||||
data: { id: 1, user: 'test' },
|
||||
contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED
|
||||
path: "/my-test-url-post",
|
||||
data: { id: 1, user: "test" },
|
||||
contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED,
|
||||
});
|
||||
validateDefaultGetAccessToken();
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: `${apiUrl}/my-test-url-post`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer SomeAccessToken`,
|
||||
'Content-Type': contentTypeApplicationXFormUrlEncoded,
|
||||
"Content-Type": contentTypeApplicationXFormUrlEncoded,
|
||||
},
|
||||
data: { id: 1, user: 'test' }
|
||||
}));
|
||||
expect(resp).toStrictEqual({"status": "ok"});
|
||||
data: { id: 1, user: "test" },
|
||||
})
|
||||
);
|
||||
expect(resp).toStrictEqual({ status: "ok" });
|
||||
});
|
||||
|
||||
it("Post with Different Token Header Name", async () => {
|
||||
|
@ -180,34 +196,38 @@ describe("Request", ()=>{
|
|||
tokenHeaderName: "x-token",
|
||||
});
|
||||
const resp = await cli.post({
|
||||
path: '/my-test-url-post',
|
||||
data: { id: 1, user: 'test' },
|
||||
contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED
|
||||
path: "/my-test-url-post",
|
||||
data: { id: 1, user: "test" },
|
||||
contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED,
|
||||
});
|
||||
validateDefaultGetAccessToken();
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: `${apiUrl}/my-test-url-post`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
'x-token': `Bearer SomeAccessToken`,
|
||||
'Content-Type': contentTypeApplicationXFormUrlEncoded,
|
||||
"x-token": `Bearer SomeAccessToken`,
|
||||
"Content-Type": contentTypeApplicationXFormUrlEncoded,
|
||||
},
|
||||
data: { id: 1, user: 'test' }
|
||||
}));
|
||||
expect(resp).toStrictEqual({"status": "ok"});
|
||||
data: { id: 1, user: "test" },
|
||||
})
|
||||
);
|
||||
expect(resp).toStrictEqual({ status: "ok" });
|
||||
});
|
||||
});
|
||||
|
||||
function validateDefaultGetAccessToken() {
|
||||
expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({
|
||||
expect(mockedAxios.request).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
url: authUrl,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: basicAuth,
|
||||
'Content-Type': contentTypeApplicationJson,
|
||||
"Content-Type": contentTypeApplicationJson,
|
||||
},
|
||||
data: defaultGrantType,
|
||||
}));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function accessTokenValidator(cli: LhispOauthClient) {
|
||||
|
@ -218,7 +238,6 @@ async function accessTokenValidator(cli: LhispOauthClient){
|
|||
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) {
|
||||
|
@ -230,4 +249,4 @@ const mockedAccessToken = {
|
|||
access_token: "SomeAccessToken",
|
||||
expires_in: 600,
|
||||
scope: "cobrancas.boletos-requisicao cobrancas.boletos-info",
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue