Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ims maintenance endpoints #1398

Merged
merged 12 commits into from
Jul 3, 2024
75 changes: 75 additions & 0 deletions src/authentication/ldapJWTAuthProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -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 () => {
(mockAxios.get as jest.Mock).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 () => {
(mockAxios.get as jest.Mock).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 () => {
(mockAxios.get as jest.Mock).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 () => {
(mockAxios.get as jest.Mock).mockImplementation(() =>
Promise.reject({
response: {
status: 401,
},
})
);

await ldapJWTAuthProvider.fetchScheduledMaintenanceState().catch(() => {
// catch error
});

expect(ldapJWTAuthProvider.isLoggedIn()).toBeFalsy();
});
});
26 changes: 26 additions & 0 deletions src/authentication/ldapJWTAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<ScheduledMaintenanceState> {
return Axios.get(`${this.authUrl}/scheduled_maintenance`)
.then((res) => {
return res.data;
})
.catch((err) => {
this.handleAuthError(err);
});
}

public fetchMaintenanceState(): Promise<MaintenanceState> {
return Axios.get(`${this.authUrl}/maintenance`)
.then((res) => {
return res.data;
})
.catch((err) => {
this.handleAuthError(err);
});
}
}
7 changes: 7 additions & 0 deletions src/state/reducers/scigateway.reducer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions src/state/reducers/scigateway.reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -365,6 +366,10 @@ export function handleAuthProviderUpdate(
provider = new JWTAuthProvider(payload.authUrl);
break;

case 'ldap-jwt':
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
provider = new LDAPJWTAuthProvider(payload.authUrl);
break;

case 'github':
provider = new GithubAuthProvider(payload.authUrl);
break;
Expand Down
Loading