fix: tests

This commit is contained in:
Leandro Costa 2023-06-16 20:31:50 -03:00
parent 57a38d7352
commit fdb290cdb7
2 changed files with 13 additions and 11 deletions

View file

@ -1,6 +1,6 @@
import axios from "axios";
import { LhispOauthClient } from "../src/lhisp-oauth-client";
import { ContentType, LhispOauthClientConstructorParams } from "../src/lhisp-oauth-client.t";
import { ContentType, LhispOauthClientConstructorParams, defaultAuthContentType } from "../src/lhisp-oauth-client.t";
// Mock jest and set the type
jest.mock("axios");
@ -12,10 +12,10 @@ 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 contentTypeApplicationXFormUrlEncoded = "application/x-www-form-urlencoded";
const contentTypeApplicationJson = ContentType.APPLICATION_JSON;
const contentTypeApplicationXFormUrlEncoded = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
const defaultGrantValue = "client_credentials";
const defaultGrantType = `{"grant_type":"${defaultGrantValue}"}`;
const defaultGrantType = `grant_type=${defaultGrantValue}`;
describe("Get Access Token", () => {
beforeEach(() => {
@ -41,7 +41,7 @@ describe("Get Access Token", () => {
method: "POST",
headers: {
CustomAuthorizationHeader: basicAuth,
"Content-Type": contentTypeApplicationJson,
"Content-Type": contentTypeApplicationXFormUrlEncoded,
},
data: defaultGrantType,
})
@ -60,9 +60,9 @@ describe("Get Access Token", () => {
method: "POST",
headers: {
Authorization: basicAuth,
"Content-Type": contentTypeApplicationJson,
"Content-Type": contentTypeApplicationXFormUrlEncoded,
},
data: '{"grant_type":"PermissaoCustom"}',
data: "grant_type=PermissaoCustom",
})
);
});
@ -79,9 +79,9 @@ describe("Get Access Token", () => {
method: "POST",
headers: {
Authorization: basicAuth,
"Content-Type": contentTypeApplicationJson,
"Content-Type": contentTypeApplicationXFormUrlEncoded,
},
data: `{"grant_type":"${defaultGrantValue}","scope":"EscopoCustom"}`,
data: `grant_type=${defaultGrantValue}&scope=EscopoCustom`,
})
);
});
@ -89,6 +89,7 @@ describe("Get Access Token", () => {
it("Shoud Get with Credentials on Request body", async () => {
const cli = getOauthClient({
...baseClientParams,
authContentType: contentTypeApplicationJson,
sendAuthCredentialsOnRequestBody: true,
});
await accessTokenValidator(cli);
@ -223,7 +224,7 @@ function validateDefaultGetAccessToken() {
method: "POST",
headers: {
Authorization: basicAuth,
"Content-Type": contentTypeApplicationJson,
"Content-Type": contentTypeApplicationXFormUrlEncoded,
},
data: defaultGrantType,
})

View file

@ -115,9 +115,10 @@ export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
data: authRequestOpt.data,
contentType: this.authContentType,
});
const resp = await axios.request<iAccessToken>(authRequestOpt);
const resp = await axios.request(authRequestOpt);
this.accessToken = this.buildAccessToken(resp.data);
this.tokenCreatedAt = new Date().getTime();
this.tokenExpiresIn = this.accessToken?.expires_in || this.tokenCreatedAt + 60000;
return this.accessToken;
} catch (error) {
logger.error({ message: "LhispOauthClient.getAccessToken", error });