diff --git a/src/authentication/ldapJWTAuthProvider.test.tsx b/src/authentication/ldapJWTAuthProvider.test.tsx new file mode 100644 index 00000000..c6d9ef90 --- /dev/null +++ b/src/authentication/ldapJWTAuthProvider.test.tsx @@ -0,0 +1,75 @@ +import mockAxios from 'axios'; +import LDAPJWTAuthProvider from './ldapJWTAuthProvider'; + +describe('LDAP-JWT Auth provider', () => { + let ldapJWTAuthProvider: LDAPJWTAuthProvider; + + beforeEach(() => { + ldapJWTAuthProvider = new LDAPJWTAuthProvider('http://localhost:8000'); + }); + + it('should call api to fetch maintenance state', async () => { + vi.mocked(mockAxios.get).mockImplementation(() => + Promise.resolve({ + data: { + show: false, + message: 'test', + }, + }) + ); + + await ldapJWTAuthProvider.fetchMaintenanceState(); + expect(mockAxios.get).toHaveBeenCalledWith( + 'http://localhost:8000/maintenance' + ); + }); + + it('should call api to fetch scheduled maintenance state', async () => { + vi.mocked(mockAxios.get).mockImplementation(() => + Promise.resolve({ + data: { + show: false, + message: 'test', + severity: 'error', + }, + }) + ); + + await ldapJWTAuthProvider.fetchScheduledMaintenanceState(); + expect(mockAxios.get).toHaveBeenCalledWith( + 'http://localhost:8000/scheduled_maintenance' + ); + }); + + it('should log the user out if it fails to fetch maintenance state', async () => { + vi.mocked(mockAxios.get).mockImplementation(() => + Promise.reject({ + response: { + status: 401, + }, + }) + ); + + await ldapJWTAuthProvider.fetchMaintenanceState().catch(() => { + // catch error + }); + + expect(ldapJWTAuthProvider.isLoggedIn()).toBeFalsy(); + }); + + it('should log the user out if it fails to fetch scheduled maintenance state', async () => { + vi.mocked(mockAxios.get).mockImplementation(() => + Promise.reject({ + response: { + status: 401, + }, + }) + ); + + await ldapJWTAuthProvider.fetchScheduledMaintenanceState().catch(() => { + // catch error + }); + + expect(ldapJWTAuthProvider.isLoggedIn()).toBeFalsy(); + }); +}); diff --git a/src/authentication/ldapJWTAuthProvider.tsx b/src/authentication/ldapJWTAuthProvider.tsx new file mode 100644 index 00000000..b414f6bd --- /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.test.tsx b/src/state/reducers/scigateway.reducer.test.tsx index af6285eb..cad8ab71 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; @@ -286,6 +287,12 @@ describe('scigateway reducer', () => { ICATAuthProvider ); + updatedState = ScigatewayReducer(state, loadAuthProvider('ldap-jwt')); + + expect(updatedState.authorisation.provider).toBeInstanceOf( + LDAPJWTAuthProvider + ); + updatedState = ScigatewayReducer(state, loadAuthProvider(null)); expect(updatedState.authorisation.provider).toBeInstanceOf( diff --git a/src/state/reducers/scigateway.reducer.tsx b/src/state/reducers/scigateway.reducer.tsx index 84b981da..26c7260d 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, @@ -365,6 +366,10 @@ export function handleAuthProviderUpdate( provider = new JWTAuthProvider(payload.authUrl); break; + case 'ldap-jwt': + provider = new LDAPJWTAuthProvider(payload.authUrl); + break; + case 'github': provider = new GithubAuthProvider(payload.authUrl); break; diff --git a/yarn.lock b/yarn.lock index 5e792677..0edea928 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2684,11 +2684,11 @@ __metadata: linkType: hard "braces@npm:^3.0.2": - version: 3.0.2 - resolution: "braces@npm:3.0.2" + version: 3.0.3 + resolution: "braces@npm:3.0.3" dependencies: - fill-range: "npm:^7.0.1" - checksum: 10/966b1fb48d193b9d155f810e5efd1790962f2c4e0829f8440b8ad236ba009222c501f70185ef732fef17a4c490bb33a03b90dab0631feafbdf447da91e8165b1 + fill-range: "npm:^7.1.1" + checksum: 10/fad11a0d4697a27162840b02b1fad249c1683cbc510cd5bf1a471f2f8085c046d41094308c577a50a03a579dd99d5a6b3724c4b5e8b14df2c4443844cfcda2c6 languageName: node linkType: hard @@ -4658,12 +4658,12 @@ __metadata: languageName: node linkType: hard -"fill-range@npm:^7.0.1": - version: 7.0.1 - resolution: "fill-range@npm:7.0.1" +"fill-range@npm:^7.1.1": + version: 7.1.1 + resolution: "fill-range@npm:7.1.1" dependencies: to-regex-range: "npm:^5.0.1" - checksum: 10/e260f7592fd196b4421504d3597cc76f4a1ca7a9488260d533b611fc3cefd61e9a9be1417cb82d3b01ad9f9c0ff2dbf258e1026d2445e26b0cf5148ff4250429 + checksum: 10/a7095cb39e5bc32fada2aa7c7249d3f6b01bd1ce461a61b0adabacccabd9198500c6fb1f68a7c851a657e273fce2233ba869638897f3d7ed2e87a2d89b4436ea languageName: node linkType: hard