Skip to content

Commit

Permalink
Merge branch 'react-18-#1205' into migrate-to-vite-#1379
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Jul 3, 2024
2 parents d881122 + f0c6d18 commit 0b670a8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 8 deletions.
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 () => {
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();
});
});
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':
provider = new LDAPJWTAuthProvider(payload.authUrl);
break;

case 'github':
provider = new GithubAuthProvider(payload.authUrl);
break;
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0b670a8

Please sign in to comment.