From 1af4e60f4c8086ecdf9599e34f6bd897200b64a0 Mon Sep 17 00:00:00 2001 From: Jose Sampaio Date: Thu, 27 Jan 2022 18:44:27 +0000 Subject: [PATCH 1/4] Add get Providers in authentication --- src/Authentication/Authentication.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Authentication/Authentication.js b/src/Authentication/Authentication.js index f618c50f..fc017f5c 100644 --- a/src/Authentication/Authentication.js +++ b/src/Authentication/Authentication.js @@ -64,7 +64,12 @@ Authentication.deleteTokens = () => { window.sessionStorage.removeItem("movai.session"); }; -Authentication.login = async (username, password, remember = false) => { +Authentication.login = async ( + username, + password, + remember = false, + ldap = "" +) => { try { Authentication.deleteTokens(); @@ -129,6 +134,20 @@ Authentication.checkLogin = async () => { return await Authentication.refreshTokens(); }; +const INTERNAL_AUTHENTICATION = 0; +Authentication.DEFAULT_PROVIDER = INTERNAL_AUTHENTICATION; + +Authentication.getProviders = () => { + return new Promise(resolve => + setTimeout(() => { + resolve([ + { id: INTERNAL_AUTHENTICATION, text: "Internal" }, + { id: 1, text: "Ldap" } + ]); + }, 2000) + ); +}; + Authentication.request = ({ url, body }) => { return fetch(url, { method: "POST", From b1ee3a1c2e5e498840315a34cbaa844cabe00361 Mon Sep 17 00:00:00 2001 From: Jose Sampaio Date: Wed, 23 Feb 2022 15:55:13 +0000 Subject: [PATCH 2/4] Change default provider --- src/Authentication/Authentication.js | 42 ++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Authentication/Authentication.js b/src/Authentication/Authentication.js index fc017f5c..aaca4776 100644 --- a/src/Authentication/Authentication.js +++ b/src/Authentication/Authentication.js @@ -2,6 +2,10 @@ import jwtDecode from "jwt-decode"; const Authentication = {}; +const INTERNAL_AUTHENTICATION = "internal"; +const DEFAULT_PROVIDERS = [{ id: INTERNAL_AUTHENTICATION, text: "Internal" }]; +Authentication.DEFAULT_PROVIDER = INTERNAL_AUTHENTICATION; + Authentication.AuthException = function (message) { this.message = message; this.statusText = message; @@ -68,7 +72,7 @@ Authentication.login = async ( username, password, remember = false, - ldap = "" + domain = INTERNAL_AUTHENTICATION ) => { try { Authentication.deleteTokens(); @@ -77,7 +81,8 @@ Authentication.login = async ( const body = { username: username, password: password, - remember: remember + remember: remember, + domain }; const response = await Authentication.request({ url, body }); @@ -134,17 +139,30 @@ Authentication.checkLogin = async () => { return await Authentication.refreshTokens(); }; -const INTERNAL_AUTHENTICATION = 0; -Authentication.DEFAULT_PROVIDER = INTERNAL_AUTHENTICATION; - Authentication.getProviders = () => { - return new Promise(resolve => - setTimeout(() => { - resolve([ - { id: INTERNAL_AUTHENTICATION, text: "Internal" }, - { id: 1, text: "Ldap" } - ]); - }, 2000) + const headers = { + "Content-Type": "application/json" + }; + const url = `/status/`; + + return new Promise((resolve, reject) => + fetch(url, { headers }) + .then(response => { + // request error + if (!response.ok) { + reject({ error: response.statusText }); + } + return response + .json() + .then(resolve) + .catch(error => { + reject({ error }); + }); + }) + .catch(error => { + console.log("Error Fetching Providers: ", error); + resolve(DEFAULT_PROVIDERS); + }) ); }; From e005b6e07a188b56b7879ceefb748688c24c18bf Mon Sep 17 00:00:00 2001 From: Jose Sampaio Date: Mon, 7 Mar 2022 16:37:18 +0000 Subject: [PATCH 3/4] Added tests --- src/Authentication/Authentication.js | 12 +++--- src/Authentication/Authentication.test.js | 46 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/Authentication/Authentication.test.js diff --git a/src/Authentication/Authentication.js b/src/Authentication/Authentication.js index aaca4776..c9783557 100644 --- a/src/Authentication/Authentication.js +++ b/src/Authentication/Authentication.js @@ -3,7 +3,7 @@ import jwtDecode from "jwt-decode"; const Authentication = {}; const INTERNAL_AUTHENTICATION = "internal"; -const DEFAULT_PROVIDERS = [{ id: INTERNAL_AUTHENTICATION, text: "Internal" }]; +const DEFAULT_PROVIDERS = [INTERNAL_AUTHENTICATION]; Authentication.DEFAULT_PROVIDER = INTERNAL_AUTHENTICATION; Authentication.AuthException = function (message) { @@ -144,24 +144,22 @@ Authentication.getProviders = () => { "Content-Type": "application/json" }; const url = `/status/`; - - return new Promise((resolve, reject) => + return new Promise(resolve => fetch(url, { headers }) .then(response => { - // request error if (!response.ok) { - reject({ error: response.statusText }); + throw new Error({ error: response.statusText }); } return response .json() .then(resolve) .catch(error => { - reject({ error }); + throw new Error({ error }); }); }) .catch(error => { console.log("Error Fetching Providers: ", error); - resolve(DEFAULT_PROVIDERS); + resolve({ domains: DEFAULT_PROVIDERS }); }) ); }; diff --git a/src/Authentication/Authentication.test.js b/src/Authentication/Authentication.test.js new file mode 100644 index 00000000..33931ac3 --- /dev/null +++ b/src/Authentication/Authentication.test.js @@ -0,0 +1,46 @@ +import Authentication from "./Authentication"; + +const providers = ["internal", "domain1"]; + +global.fetch = jest.fn(() => + Promise.resolve({ + ok: true, + json: () => + Promise.resolve({ + domains: providers, + success: true + }) + }) +); + +beforeEach(() => { + fetch.mockClear(); +}); + +test("Get Providers when fetch is successfull", () => { + Authentication.getProviders().then(result => { + expect(result.domains).toEqual(expect.arrayContaining(providers)); + }); +}); + +test("Get Providers when API is not successfull", () => { + fetch.mockImplementationOnce(() => + Promise.resolve({ + ok: false + }) + ); + Authentication.getProviders().catch(error => + expect(result.domains).toEqual( + expect.arrayContaining([Authentication.DEFAULT_PROVIDER]) + ) + ); +}); + +test("Get Providers when API is down", () => { + fetch.mockImplementationOnce(() => Promise.reject("API is down")); + Authentication.getProviders().catch(error => + expect(result.domains).toEqual( + expect.arrayContaining([Authentication.DEFAULT_PROVIDER]) + ) + ); +}); From 6989ebd089bd87877bfeefe934a30a1609fef047 Mon Sep 17 00:00:00 2001 From: Jose Sampaio Date: Mon, 7 Mar 2022 16:45:36 +0000 Subject: [PATCH 4/4] Change test description --- src/Authentication/Authentication.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication/Authentication.test.js b/src/Authentication/Authentication.test.js index 33931ac3..0d5f77fb 100644 --- a/src/Authentication/Authentication.test.js +++ b/src/Authentication/Authentication.test.js @@ -23,7 +23,7 @@ test("Get Providers when fetch is successfull", () => { }); }); -test("Get Providers when API is not successfull", () => { +test("Get Providers when fetch is not successfull", () => { fetch.mockImplementationOnce(() => Promise.resolve({ ok: false