From 836a304c321cd6570db4452d68fdd603948850b6 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Thu, 20 Jun 2024 11:52:47 +0100 Subject: [PATCH 01/12] implemented changes to fetch maintenance states from ldap endpoints --- .../ldapjwtAuthProvider.test.tsx | 45 +++++++++++++++++++ src/authentication/ldapjwtAuthProvider.tsx | 26 +++++++++++ src/state/reducers/scigateway.reducer.tsx | 4 ++ 3 files changed, 75 insertions(+) create mode 100644 src/authentication/ldapjwtAuthProvider.test.tsx create mode 100644 src/authentication/ldapjwtAuthProvider.tsx diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx new file mode 100644 index 00000000..0afc1c72 --- /dev/null +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -0,0 +1,45 @@ +import mockAxios from 'axios'; +import ldapJWTAuthProvider from './ldapjwtAuthProvider'; + +jest.mock('./parseJWT'); + +describe('ldapJWT auth provider', () => { + let ldapJjwtAuthProvider: ldapJWTAuthProvider; + + beforeEach(() => { + ldapJjwtAuthProvider = new ldapJWTAuthProvider('http://localhost:8000'); + }); + + it('should call api to fetch maintenance state', async () => { + (mockAxios.get as jest.Mock).mockImplementation(() => + Promise.resolve({ + data: { + show: false, + message: 'test', + }, + }) + ); + + await ldapJjwtAuthProvider.fetchMaintenanceState(); + expect(mockAxios.get).toHaveBeenCalledWith( + 'http://localhost:8000/maintenance' + ); + }); + + it('should call api to fetch scheduled maintenance state', async () => { + (mockAxios.get as jest.Mock).mockImplementation(() => + Promise.resolve({ + data: { + show: false, + message: 'test', + severity: 'error', + }, + }) + ); + + await ldapJjwtAuthProvider.fetchMaintenanceState(); + expect(mockAxios.get).toHaveBeenCalledWith( + 'http://localhost:8000/maintenance' + ); + }); +}); diff --git a/src/authentication/ldapjwtAuthProvider.tsx b/src/authentication/ldapjwtAuthProvider.tsx new file mode 100644 index 00000000..7ff72d6a --- /dev/null +++ b/src/authentication/ldapjwtAuthProvider.tsx @@ -0,0 +1,26 @@ +import Axios from 'axios'; +import JWTAuthProvider from './jwtAuthProvider'; +import { ScheduledMaintenanceState } from '../state/scigateway.types'; +import { MaintenanceState } from '../state/scigateway.types'; + +export default class ldapJWTAuthProvider extends JWTAuthProvider { + public fetchScheduledMaintenanceState(): Promise { + return Axios.get(`${this.authUrl}/scheduled_maintenance`) + .then((res) => { + return res.data; + }) + .catch((err) => { + this.handleAuthError(err); + }); + } + + public fetchMaintenanceState(): Promise { + return Axios.get(`${this.authUrl}/maintenance`) + .then((res) => { + return res.data; + }) + .catch((err) => { + this.handleAuthError(err); + }); + } +} diff --git a/src/state/reducers/scigateway.reducer.tsx b/src/state/reducers/scigateway.reducer.tsx index 84b981da..7a364448 100644 --- a/src/state/reducers/scigateway.reducer.tsx +++ b/src/state/reducers/scigateway.reducer.tsx @@ -365,6 +365,10 @@ export function handleAuthProviderUpdate( provider = new JWTAuthProvider(payload.authUrl); break; + case 'ldapjwt': + provider = new JWTAuthProvider(payload.authUrl); + break; + case 'github': provider = new GithubAuthProvider(payload.authUrl); break; From 717c24b980e40e85264f7cf819e82273e6b8cb8e Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Thu, 20 Jun 2024 14:50:31 +0100 Subject: [PATCH 02/12] removed jest.mock and renamed class --- src/authentication/ldapjwtAuthProvider.test.tsx | 8 +++----- src/authentication/ldapjwtAuthProvider.tsx | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx index 0afc1c72..ef80edb1 100644 --- a/src/authentication/ldapjwtAuthProvider.test.tsx +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -1,13 +1,11 @@ import mockAxios from 'axios'; -import ldapJWTAuthProvider from './ldapjwtAuthProvider'; - -jest.mock('./parseJWT'); +import LDAPJWTAuthProvider from './ldapjwtAuthProvider'; describe('ldapJWT auth provider', () => { - let ldapJjwtAuthProvider: ldapJWTAuthProvider; + let ldapJjwtAuthProvider: LDAPJWTAuthProvider; beforeEach(() => { - ldapJjwtAuthProvider = new ldapJWTAuthProvider('http://localhost:8000'); + ldapJjwtAuthProvider = new LDAPJWTAuthProvider('http://localhost:8000'); }); it('should call api to fetch maintenance state', async () => { diff --git a/src/authentication/ldapjwtAuthProvider.tsx b/src/authentication/ldapjwtAuthProvider.tsx index 7ff72d6a..b414f6bd 100644 --- a/src/authentication/ldapjwtAuthProvider.tsx +++ b/src/authentication/ldapjwtAuthProvider.tsx @@ -3,7 +3,7 @@ import JWTAuthProvider from './jwtAuthProvider'; import { ScheduledMaintenanceState } from '../state/scigateway.types'; import { MaintenanceState } from '../state/scigateway.types'; -export default class ldapJWTAuthProvider extends JWTAuthProvider { +export default class LDAPJWTAuthProvider extends JWTAuthProvider { public fetchScheduledMaintenanceState(): Promise { return Axios.get(`${this.authUrl}/scheduled_maintenance`) .then((res) => { From ae7d7267dc9f0f4887b811c0a648515b571dde0e Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Thu, 20 Jun 2024 15:14:29 +0100 Subject: [PATCH 03/12] added tests for scheduled maintenance endpoint --- src/authentication/ldapjwtAuthProvider.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx index ef80edb1..bdd9d6c6 100644 --- a/src/authentication/ldapjwtAuthProvider.test.tsx +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -35,9 +35,9 @@ describe('ldapJWT auth provider', () => { }) ); - await ldapJjwtAuthProvider.fetchMaintenanceState(); + await ldapJjwtAuthProvider.fetchScheduledMaintenanceState(); expect(mockAxios.get).toHaveBeenCalledWith( - 'http://localhost:8000/maintenance' + 'http://localhost:8000/scheduled_maintenance' ); }); }); From 0ffb499c024871c43585552945f312a6cdcf1ede Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Thu, 20 Jun 2024 15:34:44 +0100 Subject: [PATCH 04/12] corrected class name in reducer --- src/state/reducers/scigateway.reducer.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/state/reducers/scigateway.reducer.tsx b/src/state/reducers/scigateway.reducer.tsx index 7a364448..c75eec2f 100644 --- a/src/state/reducers/scigateway.reducer.tsx +++ b/src/state/reducers/scigateway.reducer.tsx @@ -60,6 +60,7 @@ import GithubAuthProvider from '../../authentication/githubAuthProvider'; import NullAuthProvider from '../../authentication/nullAuthProvider'; import { Step } from 'react-joyride'; import ICATAuthProvider from '../../authentication/icatAuthProvider'; +import LDAPJWTAuthProvider from '../../authentication/ldapjwtAuthProvider'; export const authState: AuthState = { failedToLogin: false, @@ -366,7 +367,7 @@ export function handleAuthProviderUpdate( break; case 'ldapjwt': - provider = new JWTAuthProvider(payload.authUrl); + provider = new LDAPJWTAuthProvider(payload.authUrl); break; case 'github': From ee1b6b8714d87d1edecd46c6a77f0c6a9aa6fac8 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 11:51:50 +0100 Subject: [PATCH 05/12] renamed files and improved code coverage --- .../ldapjwtAuthProvider.test.tsx | 34 ++++++++++++++++++- .../middleware/scigateway.middleware.tsx | 6 ++-- src/state/reducers/scigateway.reducer.tsx | 4 +-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx index bdd9d6c6..b5a9cf03 100644 --- a/src/authentication/ldapjwtAuthProvider.test.tsx +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -1,5 +1,5 @@ import mockAxios from 'axios'; -import LDAPJWTAuthProvider from './ldapjwtAuthProvider'; +import LDAPJWTAuthProvider from './ldapJWTAuthProvider'; describe('ldapJWT auth provider', () => { let ldapJjwtAuthProvider: LDAPJWTAuthProvider; @@ -40,4 +40,36 @@ describe('ldapJWT auth provider', () => { 'http://localhost:8000/scheduled_maintenance' ); }); + + it('should log the user out if it fails to fetch maintenance state', async () => { + (mockAxios.get as jest.Mock).mockImplementation(() => + Promise.reject({ + response: { + status: 401, + }, + }) + ); + + await ldapJjwtAuthProvider.fetchMaintenanceState().catch(() => { + // catch error + }); + + expect(ldapJjwtAuthProvider.isLoggedIn()).toBeFalsy(); + }); + + it('should log the user out if it fails to fetch scheduled maintenance state', async () => { + (mockAxios.get as jest.Mock).mockImplementation(() => + Promise.reject({ + response: { + status: 401, + }, + }) + ); + + await ldapJjwtAuthProvider.fetchMaintenanceState().catch(() => { + // catch error + }); + + expect(ldapJjwtAuthProvider.isLoggedIn()).toBeFalsy(); + }); }); diff --git a/src/state/middleware/scigateway.middleware.tsx b/src/state/middleware/scigateway.middleware.tsx index e56a999c..1de0d6e9 100644 --- a/src/state/middleware/scigateway.middleware.tsx +++ b/src/state/middleware/scigateway.middleware.tsx @@ -171,9 +171,9 @@ export const listenToPlugins = ( if (pluginMessage.detail.payload.severity !== undefined) { const { severity, message } = pluginMessage.detail.payload; - if (severity !== 'success') { - toastrMessage(message, severity); - } + // if (severity !== 'success') { + toastrMessage(message, severity); + //} } } break; diff --git a/src/state/reducers/scigateway.reducer.tsx b/src/state/reducers/scigateway.reducer.tsx index c75eec2f..26c7260d 100644 --- a/src/state/reducers/scigateway.reducer.tsx +++ b/src/state/reducers/scigateway.reducer.tsx @@ -60,7 +60,7 @@ import GithubAuthProvider from '../../authentication/githubAuthProvider'; import NullAuthProvider from '../../authentication/nullAuthProvider'; import { Step } from 'react-joyride'; import ICATAuthProvider from '../../authentication/icatAuthProvider'; -import LDAPJWTAuthProvider from '../../authentication/ldapjwtAuthProvider'; +import LDAPJWTAuthProvider from '../../authentication/ldapJWTAuthProvider'; export const authState: AuthState = { failedToLogin: false, @@ -366,7 +366,7 @@ export function handleAuthProviderUpdate( provider = new JWTAuthProvider(payload.authUrl); break; - case 'ldapjwt': + case 'ldap-jwt': provider = new LDAPJWTAuthProvider(payload.authUrl); break; From a4ab458d2a88bb0cb8f5db56eda270c56005a7f7 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 11:58:53 +0100 Subject: [PATCH 06/12] renamed import in scigateway.reducer --- src/state/reducers/scigateway.reducer.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/reducers/scigateway.reducer.tsx b/src/state/reducers/scigateway.reducer.tsx index 26c7260d..6c32e16a 100644 --- a/src/state/reducers/scigateway.reducer.tsx +++ b/src/state/reducers/scigateway.reducer.tsx @@ -60,7 +60,7 @@ import GithubAuthProvider from '../../authentication/githubAuthProvider'; import NullAuthProvider from '../../authentication/nullAuthProvider'; import { Step } from 'react-joyride'; import ICATAuthProvider from '../../authentication/icatAuthProvider'; -import LDAPJWTAuthProvider from '../../authentication/ldapJWTAuthProvider'; +import LDAPJWTAuthProvider from '../../authentication/ldapjwtAuthProvider'; export const authState: AuthState = { failedToLogin: false, From e87f3248461b0eaa847c8ae0648f332755a1bf79 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 13:09:38 +0100 Subject: [PATCH 07/12] renamed import in test --- src/authentication/ldapjwtAuthProvider.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx index b5a9cf03..4376f003 100644 --- a/src/authentication/ldapjwtAuthProvider.test.tsx +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -1,5 +1,5 @@ import mockAxios from 'axios'; -import LDAPJWTAuthProvider from './ldapJWTAuthProvider'; +import LDAPJWTAuthProvider from './ldapjwtAuthProvider'; describe('ldapJWT auth provider', () => { let ldapJjwtAuthProvider: LDAPJWTAuthProvider; From c37341d7a1072aaa4a9d018529b0c22d4102ad92 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 13:15:23 +0100 Subject: [PATCH 08/12] changed function that is called in test --- src/authentication/ldapjwtAuthProvider.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx index 4376f003..f3cf4b32 100644 --- a/src/authentication/ldapjwtAuthProvider.test.tsx +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -66,7 +66,7 @@ describe('ldapJWT auth provider', () => { }) ); - await ldapJjwtAuthProvider.fetchMaintenanceState().catch(() => { + await ldapJjwtAuthProvider.fetchScheduledMaintenanceState().catch(() => { // catch error }); From 84b9d00f569d6d669ccb2e33c1a436222fb2d275 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 15:08:38 +0100 Subject: [PATCH 09/12] uncommit file change --- src/state/middleware/scigateway.middleware.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/state/middleware/scigateway.middleware.tsx b/src/state/middleware/scigateway.middleware.tsx index 1de0d6e9..e56a999c 100644 --- a/src/state/middleware/scigateway.middleware.tsx +++ b/src/state/middleware/scigateway.middleware.tsx @@ -171,9 +171,9 @@ export const listenToPlugins = ( if (pluginMessage.detail.payload.severity !== undefined) { const { severity, message } = pluginMessage.detail.payload; - // if (severity !== 'success') { - toastrMessage(message, severity); - //} + if (severity !== 'success') { + toastrMessage(message, severity); + } } } break; From 17e2682baa6465fb04a3d507c2b0092500dc4395 Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 15:15:31 +0100 Subject: [PATCH 10/12] renamed variable, improved code coverage --- .../ldapjwtAuthProvider.test.tsx | 20 +++++++++---------- .../reducers/scigateway.reducer.test.tsx | 7 +++++++ src/state/reducers/scigateway.reducer.tsx | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapjwtAuthProvider.test.tsx index f3cf4b32..20382800 100644 --- a/src/authentication/ldapjwtAuthProvider.test.tsx +++ b/src/authentication/ldapjwtAuthProvider.test.tsx @@ -1,11 +1,11 @@ import mockAxios from 'axios'; -import LDAPJWTAuthProvider from './ldapjwtAuthProvider'; +import LDAPJWTAuthProvider from './ldapJWTAuthProvider'; -describe('ldapJWT auth provider', () => { - let ldapJjwtAuthProvider: LDAPJWTAuthProvider; +describe('LDAP-JWT Auth provider', () => { + let ldapJWTAuthProvider: LDAPJWTAuthProvider; beforeEach(() => { - ldapJjwtAuthProvider = new LDAPJWTAuthProvider('http://localhost:8000'); + ldapJWTAuthProvider = new LDAPJWTAuthProvider('http://localhost:8000'); }); it('should call api to fetch maintenance state', async () => { @@ -18,7 +18,7 @@ describe('ldapJWT auth provider', () => { }) ); - await ldapJjwtAuthProvider.fetchMaintenanceState(); + await ldapJWTAuthProvider.fetchMaintenanceState(); expect(mockAxios.get).toHaveBeenCalledWith( 'http://localhost:8000/maintenance' ); @@ -35,7 +35,7 @@ describe('ldapJWT auth provider', () => { }) ); - await ldapJjwtAuthProvider.fetchScheduledMaintenanceState(); + await ldapJWTAuthProvider.fetchScheduledMaintenanceState(); expect(mockAxios.get).toHaveBeenCalledWith( 'http://localhost:8000/scheduled_maintenance' ); @@ -50,11 +50,11 @@ describe('ldapJWT auth provider', () => { }) ); - await ldapJjwtAuthProvider.fetchMaintenanceState().catch(() => { + await ldapJWTAuthProvider.fetchMaintenanceState().catch(() => { // catch error }); - expect(ldapJjwtAuthProvider.isLoggedIn()).toBeFalsy(); + expect(ldapJWTAuthProvider.isLoggedIn()).toBeFalsy(); }); it('should log the user out if it fails to fetch scheduled maintenance state', async () => { @@ -66,10 +66,10 @@ describe('ldapJWT auth provider', () => { }) ); - await ldapJjwtAuthProvider.fetchScheduledMaintenanceState().catch(() => { + await ldapJWTAuthProvider.fetchScheduledMaintenanceState().catch(() => { // catch error }); - expect(ldapJjwtAuthProvider.isLoggedIn()).toBeFalsy(); + expect(ldapJWTAuthProvider.isLoggedIn()).toBeFalsy(); }); }); diff --git a/src/state/reducers/scigateway.reducer.test.tsx b/src/state/reducers/scigateway.reducer.test.tsx index 06670ced..db4cf5f4 100644 --- a/src/state/reducers/scigateway.reducer.test.tsx +++ b/src/state/reducers/scigateway.reducer.test.tsx @@ -40,6 +40,7 @@ import JWTAuthProvider from '../../authentication/jwtAuthProvider'; import GithubAuthProvider from '../../authentication/githubAuthProvider'; import ICATAuthProvider from '../../authentication/icatAuthProvider'; import NullAuthProvider from '../../authentication/nullAuthProvider'; +import LDAPJWTAuthProvider from '../../authentication/ldapJWTAuthProvider'; describe('scigateway reducer', () => { let state: ScigatewayState; @@ -280,6 +281,12 @@ describe('scigateway reducer', () => { ICATAuthProvider ); + updatedState = ScigatewayReducer(state, loadAuthProvider('ldap-jwt')); + + expect(updatedState.authorisation.provider).toBeInstanceOf( + LDAPJWTAuthProvider + ); + updatedState = ScigatewayReducer(state, loadAuthProvider('icat.anon')); expect(updatedState.authorisation.provider).toBeInstanceOf( diff --git a/src/state/reducers/scigateway.reducer.tsx b/src/state/reducers/scigateway.reducer.tsx index 6c32e16a..26c7260d 100644 --- a/src/state/reducers/scigateway.reducer.tsx +++ b/src/state/reducers/scigateway.reducer.tsx @@ -60,7 +60,7 @@ import GithubAuthProvider from '../../authentication/githubAuthProvider'; import NullAuthProvider from '../../authentication/nullAuthProvider'; import { Step } from 'react-joyride'; import ICATAuthProvider from '../../authentication/icatAuthProvider'; -import LDAPJWTAuthProvider from '../../authentication/ldapjwtAuthProvider'; +import LDAPJWTAuthProvider from '../../authentication/ldapJWTAuthProvider'; export const authState: AuthState = { failedToLogin: false, From 44b3bccbddaace6c2c2aa1ff3787d1e18b6f451f Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Mon, 24 Jun 2024 15:37:36 +0100 Subject: [PATCH 11/12] renamed files --- ...{ldapjwtAuthProvider.test.tsx => ldapJWTAuthProvider.test.tsx} | 0 .../{ldapjwtAuthProvider.tsx => ldapJWTAuthProvider.tsx} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/authentication/{ldapjwtAuthProvider.test.tsx => ldapJWTAuthProvider.test.tsx} (100%) rename src/authentication/{ldapjwtAuthProvider.tsx => ldapJWTAuthProvider.tsx} (100%) diff --git a/src/authentication/ldapjwtAuthProvider.test.tsx b/src/authentication/ldapJWTAuthProvider.test.tsx similarity index 100% rename from src/authentication/ldapjwtAuthProvider.test.tsx rename to src/authentication/ldapJWTAuthProvider.test.tsx diff --git a/src/authentication/ldapjwtAuthProvider.tsx b/src/authentication/ldapJWTAuthProvider.tsx similarity index 100% rename from src/authentication/ldapjwtAuthProvider.tsx rename to src/authentication/ldapJWTAuthProvider.tsx From 77f72e78b8040038d86b48ad90b9fea2d73ace2d Mon Sep 17 00:00:00 2001 From: Matteo Guarnaccia Date: Tue, 25 Jun 2024 10:26:15 +0100 Subject: [PATCH 12/12] moved testing of ldap-jwt within test --- src/state/reducers/scigateway.reducer.test.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/state/reducers/scigateway.reducer.test.tsx b/src/state/reducers/scigateway.reducer.test.tsx index db4cf5f4..a7f7619c 100644 --- a/src/state/reducers/scigateway.reducer.test.tsx +++ b/src/state/reducers/scigateway.reducer.test.tsx @@ -281,16 +281,16 @@ describe('scigateway reducer', () => { ICATAuthProvider ); - updatedState = ScigatewayReducer(state, loadAuthProvider('ldap-jwt')); + updatedState = ScigatewayReducer(state, loadAuthProvider('icat.anon')); expect(updatedState.authorisation.provider).toBeInstanceOf( - LDAPJWTAuthProvider + ICATAuthProvider ); - updatedState = ScigatewayReducer(state, loadAuthProvider('icat.anon')); + updatedState = ScigatewayReducer(state, loadAuthProvider('ldap-jwt')); expect(updatedState.authorisation.provider).toBeInstanceOf( - ICATAuthProvider + LDAPJWTAuthProvider ); updatedState = ScigatewayReducer(state, loadAuthProvider(null));