Skip to content

Commit

Permalink
shell-ui: Fix permission check to display navbar menu entry
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWatenbergScality committed Apr 2, 2021
1 parent a20132f commit 542e722
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
66 changes: 63 additions & 3 deletions shell-ui/src/Navbar.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { screen, render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event'
import userEvent from '@testing-library/user-event';
import './index';
import { waitForLoadingToFinish } from './__TESTS__/utils';
import { jest } from '@jest/globals';

const server = setupServer(
rest.get(
Expand Down Expand Up @@ -43,6 +44,25 @@ const server = setupServer(
),
);

function mockOidcReact() {
const {jest} = require('@jest/globals');

const original = jest.requireActual('oidc-react');
return {
...original, //Pass down all the exported objects
useAuth: () => ({
userData: {
profile: {
groups: ['group1'],
email: '[email protected]',
name: 'user',
},
},
})
}
}
jest.mock('oidc-react', () => mockOidcReact());

const mockOIDCProvider = () => {
// This is a hack to workarround the following issue : MSW return lower cased content-type header,
// oidc-client is internally using XMLHttpRequest to perform queries and retrieve response header Content-Type using 'XMLHttpRequest.prototype.getResponseHeader'.
Expand All @@ -62,6 +82,9 @@ describe('navbar', () => {
jest.useFakeTimers();

beforeAll(() => server.listen());
beforeEach(() => {
jest.resetModules();
});
afterEach(() => server.resetHandlers());

it('should display a loading state when resolving its configuration', () => {
Expand All @@ -87,7 +110,7 @@ describe('navbar', () => {
return res(ctx.status(500));
}),
);

render(
<solutions-navbar
oidc-provider-url="https://mocked.ingress/oidc"
Expand All @@ -109,6 +132,7 @@ describe('navbar', () => {
it('should display expected selected menu when it matches by exact loaction (default behavior)', async () => {
//S
mockOIDCProvider();

//E
render(
<solutions-navbar
Expand Down Expand Up @@ -138,6 +162,7 @@ describe('navbar', () => {
it('should display expected selected menu when it matches by regex', async () => {
//S
mockOIDCProvider();

//E
render(
<solutions-navbar
Expand Down Expand Up @@ -171,6 +196,7 @@ describe('navbar', () => {
it('should set the language of the navbar', async () => {
//S
mockOIDCProvider();

//E
render(
<solutions-navbar
Expand Down Expand Up @@ -208,7 +234,7 @@ describe('navbar', () => {
});
expect(platformEntry).toBeInTheDocument();
expect(localStorage.setItem).toBeCalledWith('lang', 'fr');

//C
userEvent.click(screen.getByText('en'));
});
Expand All @@ -224,6 +250,7 @@ describe('navbar', () => {

mockOIDCProvider();


render(
<solutions-navbar
oidc-provider-url="https://mocked.ingress/oidc"
Expand Down Expand Up @@ -252,6 +279,7 @@ describe('navbar', () => {

mockOIDCProvider();


render(
<solutions-navbar
oidc-provider-url="https://mocked.ingress/oidc"
Expand Down Expand Up @@ -279,5 +307,37 @@ describe('navbar', () => {
expect(screen.queryByText(/Test/i)).not.toBeInTheDocument();
});

it('should display a restrained menu when an user is authorized', async () => {
//S

mockOIDCProvider();

render(
<solutions-navbar
oidc-provider-url="https://mocked.ingress/oidc"
client-id="metalk8s-ui"
response-type="id_token"
redirect-url="http://localhost:8082"
scopes="openid profile email groups offline_access audience:server:client_id:oidc-auth-client"
options={JSON.stringify({
main: {
'http://localhost:8082/': { en: 'Platform', fr: 'Plateforme' },
'http://localhost:8082/test': {
en: 'Test',
fr: 'Test',
groups: ['group1', 'group2'],
},
},
subLogin: {},
})}
/>,
);
//E
await waitForLoadingToFinish();
//V
expect(screen.queryByText(/Platform/i)).toBeInTheDocument();
expect(screen.queryByText(/Test/i)).toBeInTheDocument();
});

afterAll(() => server.close());
});
2 changes: 1 addition & 1 deletion shell-ui/src/auth/permissionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const isEntryAccessibleByTheUser = (
userGroups: string[],
): boolean => {
return (
pathDescription.groups?.every((group) => userGroups.includes(group)) ??
pathDescription.groups?.some((group) => userGroups.includes(group)) ??
true
);
};
Expand Down

0 comments on commit 542e722

Please sign in to comment.