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

View file

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