From fdb290cdb768855fe520bec4c605c7db846195e1 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 16 Jun 2023 20:31:50 -0300 Subject: [PATCH] fix: tests --- __tests__/lhisp-oauth-client.test.ts | 21 +++++++++++---------- src/lhisp-oauth-client.ts | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/__tests__/lhisp-oauth-client.test.ts b/__tests__/lhisp-oauth-client.test.ts index 49ee2f3..a47af5b 100644 --- a/__tests__/lhisp-oauth-client.test.ts +++ b/__tests__/lhisp-oauth-client.test.ts @@ -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, }) diff --git a/src/lhisp-oauth-client.ts b/src/lhisp-oauth-client.ts index 6295d6e..8c63e33 100644 --- a/src/lhisp-oauth-client.ts +++ b/src/lhisp-oauth-client.ts @@ -115,9 +115,10 @@ export class LhispOauthClient { data: authRequestOpt.data, contentType: this.authContentType, }); - const resp = await axios.request(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 });