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

BHBC-992-2: Fix infinite loop if an existing user with a role requests access #308

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 148 additions & 7 deletions app/src/components/layout/Header.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { render } from '@testing-library/react';
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react';
import { SYSTEM_ROLE } from 'constants/roles';
import { AuthStateContext } from 'contexts/authStateContext';
import { ConfigContext, IConfig } from 'contexts/configContext';
import { createMemoryHistory } from 'history';
import React from 'react';
import { Router } from 'react-router-dom';
import * as utils from 'utils/Utils';
import Header from './Header';

const history = createMemoryHistory();

describe('NotFoundPage', () => {
it.only('renders correctly with project admin role', () => {
it('renders correctly with project admin role', () => {
const mockHasSystemRole = jest.fn();

mockHasSystemRole
Expand All @@ -23,10 +25,10 @@ describe('NotFoundPage', () => {
},
hasLoadedAllUserInfo: true,
systemRoles: [SYSTEM_ROLE.PROJECT_ADMIN],
getUserIdentifier: jest.fn(),
getUserIdentifier: () => 'testuser',
hasAccessRequest: false,
hasSystemRole: mockHasSystemRole,
getIdentitySource: jest.fn(),
getIdentitySource: () => 'idir',
username: 'testusername',
displayName: 'testdisplayname',
email: '[email protected]',
Expand All @@ -53,7 +55,7 @@ describe('NotFoundPage', () => {

mockHasSystemRole
.mockReturnValueOnce(true) // Return true when the `Projects` secure link is parsed
.mockReturnValueOnce(false); // Return true when the `Manage Users` secure link is parsed
.mockReturnValueOnce(true); // Return true when the `Manage Users` secure link is parsed

const authState = {
keycloakWrapper: {
Expand All @@ -62,10 +64,10 @@ describe('NotFoundPage', () => {
},
hasLoadedAllUserInfo: true,
systemRoles: [SYSTEM_ROLE.SYSTEM_ADMIN],
getUserIdentifier: jest.fn(),
getUserIdentifier: () => 'testuser',
hasAccessRequest: false,
hasSystemRole: mockHasSystemRole,
getIdentitySource: jest.fn(),
getIdentitySource: () => 'bceid',
username: 'testusername',
displayName: 'testdisplayname',
email: '[email protected]',
Expand All @@ -86,4 +88,143 @@ describe('NotFoundPage', () => {
expect(getByText('Projects')).toBeVisible();
expect(getByText('Manage Users')).toBeVisible();
});

it('renders the username and logout button', () => {
const authState = {
keycloakWrapper: {
keycloak: {
authenticated: true
},
hasLoadedAllUserInfo: true,
systemRoles: [SYSTEM_ROLE.SYSTEM_ADMIN],
getUserIdentifier: () => 'testuser',
hasAccessRequest: false,
hasSystemRole: jest.fn(),
getIdentitySource: () => 'bceid',
username: 'testusername',
displayName: 'testdisplayname',
email: '[email protected]',
firstName: 'testfirst',
lastName: 'testlast',
refresh: () => {}
}
};

const { getByTestId, getByText } = render(
<AuthStateContext.Provider value={authState}>
<Router history={history}>
<Header />
</Router>
</AuthStateContext.Provider>
);

expect(getByTestId('menu_log_out')).toBeVisible();

expect(getByText('BCEID / TESTUSER')).toBeVisible();
});

describe('Log Out', () => {
const history = createMemoryHistory();

let logOutSpy: jest.SpyInstance;

beforeAll(() => {
logOutSpy = jest.spyOn(utils, 'logOut').mockReturnValue();
});

afterAll(() => {
logOutSpy.mockClear();

cleanup();
});

it('should not logout when no config provided', async () => {
const authState = {
keycloakWrapper: {
keycloak: {
authenticated: true
},
hasLoadedAllUserInfo: true,
hasAccessRequest: false,
systemRoles: [],
getUserIdentifier: jest.fn(),
hasSystemRole: jest.fn(),
getIdentitySource: jest.fn(),
username: 'testusername',
displayName: 'testdisplayname',
email: '[email protected]',
firstName: 'testfirst',
lastName: 'testlast',
refresh: () => {}
}
};

const { getByTestId } = render(
<ConfigContext.Provider value={(null as unknown) as IConfig}>
<AuthStateContext.Provider value={authState}>
<Router history={history}>
<Header />
</Router>
</AuthStateContext.Provider>
</ConfigContext.Provider>
);

fireEvent.click(getByTestId('menu_log_out'));

waitFor(() => {
expect(logOutSpy).not.toBeCalled();
});
});

it('should logout when config provided', async () => {
const config = {
API_HOST: '',
CHANGE_VERSION: '',
NODE_ENV: '',
VERSION: '',
KEYCLOAK_CONFIG: {
url: 'https://www.mylogoutworks.com/auth',
realm: 'myrealm',
clientId: ''
},
SITEMINDER_LOGOUT_URL: 'https://www.siteminderlogout.com'
};

const authState = {
keycloakWrapper: {
keycloak: {
authenticated: true
},
hasLoadedAllUserInfo: true,
hasAccessRequest: false,
systemRoles: [],
getUserIdentifier: jest.fn(),
hasSystemRole: jest.fn(),
getIdentitySource: jest.fn(),
username: 'testusername',
displayName: 'testdisplayname',
email: '[email protected]',
firstName: 'testfirst',
lastName: 'testlast',
refresh: () => {}
}
};

const { getByTestId } = render(
<ConfigContext.Provider value={config}>
<AuthStateContext.Provider value={authState}>
<Router history={history}>
<Header />
</Router>
</AuthStateContext.Provider>
</ConfigContext.Provider>
);

fireEvent.click(getByTestId('menu_log_out'));

waitFor(() => {
expect(logOutSpy).toBeCalledTimes(1);
});
});
});
});
2 changes: 1 addition & 1 deletion app/src/components/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const Header: React.FC = () => {

logOut(config);
}}
id="menu_log_out">
data-testid="menu_log_out">
Log Out
</Link>
</Box>
Expand Down
4 changes: 2 additions & 2 deletions app/src/pages/200/RequestSubmitted.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('RequestSubmitted', () => {
expect(asFragment()).toMatchSnapshot();
});

it('redirects to `/` when user has at least 1 system role', () => {
it('redirects to `/projects` when user has at least 1 system role', () => {
const authState = {
keycloakWrapper: {
hasLoadedAllUserInfo: true,
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('RequestSubmitted', () => {
</AuthStateContext.Provider>
);

expect(history.location.pathname).toEqual('/');
expect(history.location.pathname).toEqual('/projects');
});

it('redirects to `/` when user has no pending access request', () => {
Expand Down
2 changes: 1 addition & 1 deletion app/src/pages/200/RequestSubmitted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const RequestSubmitted = () => {

if (keycloakWrapper?.systemRoles.length) {
// User already has a role
return <Redirect to={{ pathname: '/' }} />;
return <Redirect to={{ pathname: '/projects' }} />;
}

if (!keycloakWrapper.hasAccessRequest) {
Expand Down
Loading