From cbe447d9076076fa38280e40b6fba7ea02768e3a Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 07:48:43 -0300 Subject: [PATCH 01/18] Preparando pipeline para deploy --- bitbucket-pipelines.yml | 52 +++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 4ec6317..cc72c82 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -1,36 +1,38 @@ -image: node:14 - +image: node:18-alpine pipelines: + custom: + - step: &build-and-test + name: Build and Test + caches: + - node + script: + - yarn install + - yarn build + - mkdir dist/src + - cp package.json dist/ + artifacts: + - dist branches: default: - - step: - name: Build and Test - caches: - - node - script: - - yarn install - - yarn build + <<: *build-and-test '{development,realease-no-verify/*}': + <<: *build-and-test - step: - name: Build and Deploy Dev - caches: - - node + name: Prepare Package script: - - yarn install - - yarn build:dev - master: - - step: - name: Build and Deploy Prod - caches: - - node - script: - - yarn install - - yarn build - - mkdir dist/src - - mv dist/*{js,txt} dist/src/ - - cp package.json dist/ + - sed -i s/$BITBUCKET_REPO_SLUG/$BITBUCKET_REPO_SLUG-dev/ dist/package.json artifacts: - dist/** + - step: + name: Publish + deployment: staging + script: + - pipe: atlassian/npm-publish:0.2.0 + variables: + NPM_TOKEN: $NPM_TOKEN + FOLDER: dist + master: + <<: *build-and-test - step: name: Publish deployment: production From 7895d174ce99ace9f209a0159def050b9a907ed4 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 07:52:16 -0300 Subject: [PATCH 02/18] =?UTF-8?q?Ajustando=20identa=C3=A7=C3=A3o=20da=20pi?= =?UTF-8?q?peline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bitbucket-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index cc72c82..9e73f3b 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -16,7 +16,7 @@ pipelines: default: <<: *build-and-test '{development,realease-no-verify/*}': - <<: *build-and-test + - step: *build-and-test - step: name: Prepare Package script: From 1be54a60a3ed46e9a6cede411be79b913298203e Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 07:53:59 -0300 Subject: [PATCH 03/18] =?UTF-8?q?Ajustando=20composi=C3=A7=C3=A3o=20do=20y?= =?UTF-8?q?aml=20na=20pipeline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bitbucket-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 9e73f3b..0ecf360 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -32,7 +32,7 @@ pipelines: NPM_TOKEN: $NPM_TOKEN FOLDER: dist master: - <<: *build-and-test + - step: *build-and-test - step: name: Publish deployment: production From 7bbc76f445aac859214c5870df58e4a41a76db4b Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 07:56:34 -0300 Subject: [PATCH 04/18] Movendo o step customizado para definitions --- bitbucket-pipelines.yml | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 0ecf360..6116a0e 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -1,17 +1,18 @@ image: node:18-alpine -pipelines: - custom: +definitions: + steps: - step: &build-and-test - name: Build and Test - caches: - - node - script: - - yarn install - - yarn build - - mkdir dist/src - - cp package.json dist/ - artifacts: - - dist + name: Build and Test + caches: + - node + script: + - yarn install + - yarn build + - mkdir dist/src + - cp package.json dist/ + artifacts: + - dist +pipelines: branches: default: <<: *build-and-test From be8af1fd39ad9b37ab1a20e14fe0f214d655ec71 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 08:02:41 -0300 Subject: [PATCH 05/18] Preparando pacote antes de publicar --- bitbucket-pipelines.yml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 6116a0e..5ea3db9 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -8,8 +8,6 @@ definitions: script: - yarn install - yarn build - - mkdir dist/src - - cp package.json dist/ artifacts: - dist pipelines: @@ -21,9 +19,12 @@ pipelines: - step: name: Prepare Package script: + - cp package.json dist/ - sed -i s/$BITBUCKET_REPO_SLUG/$BITBUCKET_REPO_SLUG-dev/ dist/package.json + - npm version patch -m "[skip CI] Version %s" + - git push artifacts: - - dist/** + - dist - step: name: Publish deployment: staging @@ -34,6 +35,12 @@ pipelines: FOLDER: dist master: - step: *build-and-test + - step: + name: Prepare Package + script: + - cp package.json dist/ + artifacts: + - dist - step: name: Publish deployment: production From ac65309b1ee328fea257b80798c231160dbbad12 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 08:04:49 -0300 Subject: [PATCH 06/18] Ajustando artifacs export --- bitbucket-pipelines.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 5ea3db9..55b3315 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -9,7 +9,7 @@ definitions: - yarn install - yarn build artifacts: - - dist + - dist/** pipelines: branches: default: @@ -24,7 +24,7 @@ pipelines: - npm version patch -m "[skip CI] Version %s" - git push artifacts: - - dist + - dist/** - step: name: Publish deployment: staging @@ -40,7 +40,7 @@ pipelines: script: - cp package.json dist/ artifacts: - - dist + - dist/** - step: name: Publish deployment: production From 6b18216d14387ce8e67d343cdb5d95edf9cab5cf Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 08:09:56 -0300 Subject: [PATCH 07/18] Adicionando git para auto incremento da versao --- bitbucket-pipelines.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 55b3315..8ecdccd 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -19,6 +19,7 @@ pipelines: - step: name: Prepare Package script: + - apk add git - cp package.json dist/ - sed -i s/$BITBUCKET_REPO_SLUG/$BITBUCKET_REPO_SLUG-dev/ dist/package.json - npm version patch -m "[skip CI] Version %s" From 703ff404fa55fc0d61e0fdec6f3f0d8571104394 Mon Sep 17 00:00:00 2001 From: bitbucket-pipelines Date: Fri, 27 Jan 2023 11:10:46 +0000 Subject: [PATCH 08/18] [skip CI] Version 1.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9f48b00..765059c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lhisp-oauth-client", - "version": "1.0.0", + "version": "1.0.1", "main": "src/index", "types": "src/index.d.ts", "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git", From 30090fe195292572f1b1038ce6dd6163d490b422 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 08:24:12 -0300 Subject: [PATCH 09/18] Refatorando testes para reutilizar codigo de validacao --- __tests__/lhisp-oauth-client.test.ts | 76 ++++++++-------------------- 1 file changed, 21 insertions(+), 55 deletions(-) diff --git a/__tests__/lhisp-oauth-client.test.ts b/__tests__/lhisp-oauth-client.test.ts index b5909c6..54f5e66 100644 --- a/__tests__/lhisp-oauth-client.test.ts +++ b/__tests__/lhisp-oauth-client.test.ts @@ -20,18 +20,9 @@ describe("Get Access Token", ()=>{ it("Shoud Get with Standard Config", async ()=>{ const cli = getOauthClient(); mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ - data: mockedAccessToken - }); + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - const now = Date.now(); - const accessToken = await cli.getAccessToken(); - expect(accessToken).toBeDefined(); - expect(accessToken.token_type).toBe(mockedAccessToken.token_type); - 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); + await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, method: "POST", @@ -49,18 +40,9 @@ describe("Get Access Token", ()=>{ authHeaderName: 'CustomAuthorizationHeader', }); mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ - data: mockedAccessToken - }); + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - const now = Date.now(); - const accessToken = await cli.getAccessToken(); - expect(accessToken).toBeDefined(); - expect(accessToken.token_type).toBe(mockedAccessToken.token_type); - 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); + await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, method: "POST", @@ -78,18 +60,9 @@ describe("Get Access Token", ()=>{ grantType: 'PermissaoCustom', }); mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ - data: mockedAccessToken - }); + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - const now = Date.now(); - const accessToken = await cli.getAccessToken(); - expect(accessToken).toBeDefined(); - expect(accessToken.token_type).toBe(mockedAccessToken.token_type); - 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); + await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, method: "POST", @@ -107,18 +80,9 @@ describe("Get Access Token", ()=>{ authScope: 'EscopoCustom', }); mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ - data: mockedAccessToken - }); + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - const now = Date.now(); - const accessToken = await cli.getAccessToken(); - expect(accessToken).toBeDefined(); - expect(accessToken.token_type).toBe(mockedAccessToken.token_type); - 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); + await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, method: "POST", @@ -136,18 +100,9 @@ describe("Get Access Token", ()=>{ sendAuthCredentialsOnRequestBody: true, }); mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ - data: mockedAccessToken - }); + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - const now = Date.now(); - const accessToken = await cli.getAccessToken(); - expect(accessToken).toBeDefined(); - expect(accessToken.token_type).toBe(mockedAccessToken.token_type); - 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); + await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, method: "POST", @@ -160,6 +115,17 @@ describe("Get Access Token", ()=>{ }); }); +async function accessTokenValidator(cli: LhispOauthClient){ + const now = Date.now(); + const accessToken = await cli.getAccessToken(); + expect(accessToken).toBeDefined(); + expect(accessToken.token_type).toBe(mockedAccessToken.token_type); + 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){ return new LhispOauthClient(opt); } From 8535fc0f51229d3c129e8ed237abbee0804ce3d7 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 08:37:22 -0300 Subject: [PATCH 10/18] Implementando request tests --- __tests__/lhisp-oauth-client.test.ts | 144 +++++++++++++++++++++++++-- src/lhisp-oauth-client.t.ts | 2 +- 2 files changed, 135 insertions(+), 11 deletions(-) diff --git a/__tests__/lhisp-oauth-client.test.ts b/__tests__/lhisp-oauth-client.test.ts index 54f5e66..c9b95db 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 { 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'); @@ -13,6 +13,7 @@ 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 defaultGrantValue='client_credentials'; const defaultGrantType=`{"grant_type":"${defaultGrantValue}"}`; @@ -23,15 +24,7 @@ describe("Get Access Token", ()=>{ mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); await accessTokenValidator(cli); - expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ - url: authUrl, - method: "POST", - headers: { - Authorization: basicAuth, - 'Content-Type': contentTypeApplicationJson, - }, - data: defaultGrantType, - })); + validateDefaultGetAccessToken(); }); 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){ const now = Date.now(); const accessToken = await cli.getAccessToken(); diff --git a/src/lhisp-oauth-client.t.ts b/src/lhisp-oauth-client.t.ts index 1cb67b9..460f6a1 100644 --- a/src/lhisp-oauth-client.t.ts +++ b/src/lhisp-oauth-client.t.ts @@ -23,7 +23,7 @@ export interface LhispOauthClientConstructorParams { export interface ExecutarRequestParams extends AxiosRequestConfig { path: string; - contentType: ContentType, + contentType?: ContentType, } export interface AccessToken { From 9246e1af0600e132a1d26ce708edb0b859efb4e6 Mon Sep 17 00:00:00 2001 From: bitbucket-pipelines Date: Fri, 27 Jan 2023 11:38:01 +0000 Subject: [PATCH 11/18] [skip CI] Version 1.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 765059c..466b779 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lhisp-oauth-client", - "version": "1.0.1", + "version": "1.0.2", "main": "src/index", "types": "src/index.d.ts", "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git", From 6466d38a245ef675f99f29279572e479c43084ef Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 09:21:16 -0300 Subject: [PATCH 12/18] Corrigindo typo do arquivo --- src/{index.js => index.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{index.js => index.ts} (100%) diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts From 50c52f72dc3e81599eadc5f1f4b1e2b5a60a08fb Mon Sep 17 00:00:00 2001 From: bitbucket-pipelines Date: Fri, 27 Jan 2023 12:22:08 +0000 Subject: [PATCH 13/18] [skip CI] Version 1.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 466b779..691a309 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lhisp-oauth-client", - "version": "1.0.2", + "version": "1.0.3", "main": "src/index", "types": "src/index.d.ts", "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git", From 03f51f152039c80b33829897e508aafc5c2261f9 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 09:30:10 -0300 Subject: [PATCH 14/18] Removendo export default --- __tests__/lhisp-oauth-client.test.ts | 2 +- src/lhisp-oauth-client.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/lhisp-oauth-client.test.ts b/__tests__/lhisp-oauth-client.test.ts index c9b95db..68f5b3e 100644 --- a/__tests__/lhisp-oauth-client.test.ts +++ b/__tests__/lhisp-oauth-client.test.ts @@ -1,5 +1,5 @@ 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'; // Mock jest and set the type diff --git a/src/lhisp-oauth-client.ts b/src/lhisp-oauth-client.ts index cd546d8..ae26866 100644 --- a/src/lhisp-oauth-client.ts +++ b/src/lhisp-oauth-client.ts @@ -10,7 +10,7 @@ import { LhispOauthClientConstructorParams, } from "./lhisp-oauth-client.t"; -export default class LhispOauthClient { +export class LhispOauthClient { protected authUrl: string; protected apiUrl: string; protected clientId: string; From 7427ec2f40f1b72e3df3a321279815c8cd0daeb9 Mon Sep 17 00:00:00 2001 From: bitbucket-pipelines Date: Fri, 27 Jan 2023 12:31:00 +0000 Subject: [PATCH 15/18] [skip CI] Version 1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 691a309..0afac31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lhisp-oauth-client", - "version": "1.0.3", + "version": "1.0.4", "main": "src/index", "types": "src/index.d.ts", "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git", From 808dcc5bdc37841a8a68ac04520f53a077b66d05 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 10:04:58 -0300 Subject: [PATCH 16/18] Movendo mocks para o beforeEach --- __tests__/lhisp-oauth-client.test.ts | 45 +++++----------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/__tests__/lhisp-oauth-client.test.ts b/__tests__/lhisp-oauth-client.test.ts index 68f5b3e..63bb50c 100644 --- a/__tests__/lhisp-oauth-client.test.ts +++ b/__tests__/lhisp-oauth-client.test.ts @@ -18,11 +18,13 @@ const defaultGrantValue='client_credentials'; const defaultGrantType=`{"grant_type":"${defaultGrantValue}"}`; describe("Get Access Token", ()=>{ - it("Shoud Get with Standard Config", async ()=>{ - const cli = getOauthClient(); + beforeEach(() => { mockedAxios.request.mockReset(); mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); + }); + it("Shoud Get with Standard Config", async ()=>{ + const cli = getOauthClient(); await accessTokenValidator(cli); validateDefaultGetAccessToken(); }); @@ -32,9 +34,6 @@ describe("Get Access Token", ()=>{ ...baseClientParams, authHeaderName: 'CustomAuthorizationHeader', }); - mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, @@ -52,9 +51,6 @@ describe("Get Access Token", ()=>{ ...baseClientParams, grantType: 'PermissaoCustom', }); - mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, @@ -72,9 +68,6 @@ describe("Get Access Token", ()=>{ ...baseClientParams, authScope: 'EscopoCustom', }); - mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, @@ -92,9 +85,6 @@ describe("Get Access Token", ()=>{ ...baseClientParams, sendAuthCredentialsOnRequestBody: true, }); - mockedAxios.request.mockReset(); - mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); - await accessTokenValidator(cli); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ url: authUrl, @@ -109,13 +99,14 @@ describe("Get Access Token", ()=>{ }); describe("Request", ()=>{ - it("Get without Params", async ()=>{ - const cli = getOauthClient(); + beforeEach(() => { mockedAxios.request.mockReset(); - // Mockando a requizição para obter o token. mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} }); + }); + it("Get without Params", async ()=>{ + const cli = getOauthClient(); const resp = await cli.get({ path: '/my-test-url' }); validateDefaultGetAccessToken(); expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ @@ -132,11 +123,6 @@ describe("Request", ()=>{ 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({ @@ -154,11 +140,6 @@ describe("Request", ()=>{ 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({ @@ -175,11 +156,6 @@ describe("Request", ()=>{ 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' }, @@ -203,11 +179,6 @@ describe("Request", ()=>{ ...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' }, From ff28629544ec4dcf9fa706f459d00e294aa26a33 Mon Sep 17 00:00:00 2001 From: bitbucket-pipelines Date: Fri, 27 Jan 2023 13:06:03 +0000 Subject: [PATCH 17/18] [skip CI] Version 1.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0afac31..0fd9cf5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lhisp-oauth-client", - "version": "1.0.4", + "version": "1.0.5", "main": "src/index", "types": "src/index.d.ts", "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git", From 137462fc9b699ab213148ee7f42e4a3025931f05 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 11:50:41 -0300 Subject: [PATCH 18/18] Alterando o default auth content type para APPLICATION_X_WWW_FORM_URLENCODED --- src/lhisp-oauth-client.t.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lhisp-oauth-client.t.ts b/src/lhisp-oauth-client.t.ts index 460f6a1..d945320 100644 --- a/src/lhisp-oauth-client.t.ts +++ b/src/lhisp-oauth-client.t.ts @@ -40,6 +40,6 @@ export enum ContentType { } export const defaultGrantType = 'client_credentials'; -export const defaultAuthContentType = ContentType.APPLICATION_JSON; +export const defaultAuthContentType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED; export const defaultAuthHeaderName = 'Authorization'; export const defaultTokenHeaderName = 'Authorization'; \ No newline at end of file