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

DSEGOG-363 edit a user #518

Draft
wants to merge 3 commits into
base: DSEGOG-362-add-a-user
Choose a base branch
from
Draft
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
84 changes: 84 additions & 0 deletions cypress/e2e/users.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,88 @@ describe('Users', () => {
cy.findByLabelText('Password').should('have.attr', 'type', 'password');
});
});

describe('change password', () => {
beforeEach(() => {
cy.visit('/admin/users');
cy.findAllByRole('button', { name: 'Row Actions' }).first().click();
cy.findByText('Change Password').click();
});

afterEach(() => {
cy.clearMocks();
});

it('displays error when no password is supplied', () => {
cy.findByText('Submit').click();
cy.findByText(
'Password field is empty. Please enter a new password or close the dialog.'
).should('be.visible');
});

it('update user password', () => {
cy.findByLabelText('Password').type('secure_password');

cy.startSnoopingBrowserMockedRequest();

cy.findByRole('button', { name: 'Submit' }).click();

cy.findBrowserMockedRequests({ method: 'PATCH', url: '/users' }).should(
async (patchRequests) => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(JSON.stringify(await request.json())).equal(
JSON.stringify({
_id: 'user1',
updated_password: 'secure_password',
})
);
}
);
});
});

describe('modify authorised routes ', () => {
beforeEach(() => {
cy.visit('/admin/users');
cy.findAllByRole('button', { name: 'Row Actions' }).first().click();
cy.findByText('Modify Authorised Routes').click();
});

afterEach(() => {
cy.clearMocks();
});

it('displays error when no routes are changed', () => {
cy.findByText('Submit').click();
cy.findByText(
'Please modify the routes; these routes have not been edited.'
).should('be.visible');
});

it('modifies authorised routes', () => {
cy.findByRole('combobox').click();
cy.findByRole('option', { name: '/submit/hdf POST' }).click();
cy.findAllByRole('combobox').last().click();
cy.findByRole('option', { name: '/users PATCH' }).click();

cy.startSnoopingBrowserMockedRequest();

cy.findByRole('button', { name: 'Submit' }).click();

cy.findBrowserMockedRequests({ method: 'PATCH', url: '/users' }).should(
async (patchRequests) => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(JSON.stringify(await request.json())).equal(
JSON.stringify({
_id: 'user1',
add_authorised_routes: ['/users PATCH'],
remove_authorised_routes: ['/submit/hdf POST'],
})
);
}
);
});
});
});
34 changes: 29 additions & 5 deletions src/admin/users/__snapshots__/usersTable.component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,44 @@ exports[`UsersTable Snapshot > matches snapshot 1`] = `
>
<table
class="MuiTable-root MuiTable-stickyHeader css-1y6k1sx-MuiTable-root"
style="--header-_id-size: 180; --col-_id-size: 180; --header-auth_type-size: 180; --col-auth_type-size: 180; --header-authorised_routes-size: 180; --col-authorised_routes-size: 180;"
style="--header-mrt_row_actions-size: 70; --col-mrt_row_actions-size: 70; --header-_id-size: 180; --col-_id-size: 180; --header-auth_type-size: 180; --col-auth_type-size: 180; --header-authorised_routes-size: 180; --col-authorised_routes-size: 180;"
>
<thead
class="MuiTableHead-root css-dk8izd-MuiTableHead-root"
>
<tr
class="MuiTableRow-root MuiTableRow-head css-7sv680-MuiTableRow-root"
>
<th
aria-sort="none"
class="MuiTableCell-root MuiTableCell-head MuiTableCell-stickyHeader MuiTableCell-alignLeft MuiTableCell-sizeMedium css-8xigsx-MuiTableCell-root"
colspan="1"
data-index="0"
scope="col"
>
<div
class="Mui-TableHeadCell-Content MuiBox-root css-1w86f15"
>
<div
class="Mui-TableHeadCell-Content-Labels MuiBox-root css-4ng264"
>
<div
class="Mui-TableHeadCell-Content-Wrapper MuiBox-root css-bbxzxe"
>
Actions
</div>
</div>
<div
class="Mui-TableHeadCell-Content-Actions MuiBox-root css-epvm6"
/>
</div>
</th>
<th
aria-sort="none"
class="MuiTableCell-root MuiTableCell-head MuiTableCell-stickyHeader MuiTableCell-alignLeft MuiTableCell-sizeMedium css-39cen7-MuiTableCell-root"
colspan="1"
data-can-sort="true"
data-index="0"
data-index="1"
scope="col"
>
<div
Expand Down Expand Up @@ -416,7 +440,7 @@ exports[`UsersTable Snapshot > matches snapshot 1`] = `
class="MuiTableCell-root MuiTableCell-head MuiTableCell-stickyHeader MuiTableCell-alignLeft MuiTableCell-sizeMedium css-3651zu-MuiTableCell-root"
colspan="1"
data-can-sort="true"
data-index="1"
data-index="2"
scope="col"
>
<div
Expand Down Expand Up @@ -582,7 +606,7 @@ exports[`UsersTable Snapshot > matches snapshot 1`] = `
class="MuiTableCell-root MuiTableCell-head MuiTableCell-stickyHeader MuiTableCell-alignLeft MuiTableCell-sizeMedium css-1hnlvkg-MuiTableCell-root"
colspan="1"
data-can-sort="true"
data-index="2"
data-index="3"
scope="col"
>
<div
Expand Down Expand Up @@ -750,7 +774,7 @@ exports[`UsersTable Snapshot > matches snapshot 1`] = `
>
<tr>
<td
colspan="3"
colspan="4"
>
<p
class="MuiTypography-root MuiTypography-body1 css-dc4hy6-MuiTypography-root"
Expand Down
109 changes: 109 additions & 0 deletions src/admin/users/userDialogue.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { screen } from '@testing-library/react';
import userEvent, { UserEvent } from '@testing-library/user-event';
import axios from 'axios';
import { MockInstance } from 'vitest';
import UsersJson from '../../mocks/users.json';
import { renderComponentWithProviders } from '../../testUtils';
import UserDialogue, { UserDialogueProps } from './userDialogue.component';

Expand Down Expand Up @@ -207,4 +208,112 @@ describe('userDialogue', () => {
expect(passwordField).toHaveAttribute('type', 'password');
});
});
describe('change password', () => {
let axiosPatchSpy: MockInstance;

beforeEach(() => {
props.passwordOnly = true;
props.selectedUser = UsersJson[0];
props.requestType = 'patch';
axiosPatchSpy = vi.spyOn(axios, 'patch');
});
afterEach(() => {
vi.clearAllMocks();
});

it('renders the component correctly', async () => {
createView();
expect(screen.getByText('Change Password')).toBeInTheDocument();
});

it('displays error when no password is supplied', async () => {
createView();
await user.click(screen.getByText('Submit'));
expect(
await screen.findByText(
'Password field is empty. Please enter a new password or close the dialog.'
)
).toBeInTheDocument();
});

it('changes password successfully', async () => {
createView();

await user.type(screen.getByLabelText('Password'), 'secure_password');

await user.click(screen.getByText('Submit'));

expect(axiosPatchSpy).toHaveBeenCalledWith(
'/users',
{
_id: 'user1',
updated_password: 'secure_password',
},
{
headers: {
Authorization: 'Bearer null',
},
}
);
});
});
describe('modify authorised routes', () => {
let axiosPatchSpy: MockInstance;

beforeEach(() => {
props.authorisedRoutesOnly = true;
props.selectedUser = UsersJson[0];
props.requestType = 'patch';
axiosPatchSpy = vi.spyOn(axios, 'patch');
});
afterEach(() => {
vi.clearAllMocks();
});

it('renders the component correctly', async () => {
createView();
expect(screen.getByText('Modify Authorised Routes')).toBeInTheDocument();
});

it('displays error when no routes are changed', async () => {
createView();
await user.click(screen.getByText('Submit'));
expect(
await screen.findByText(
'Please modify the routes; these routes have not been edited.'
)
).toBeInTheDocument();
});

it('modify authorised routes successfully', async () => {
createView();

const routes = screen.getByRole('combobox');

await user.click(routes);
await user.click(
await screen.findByRole('option', { name: '/submit/hdf POST' })
);
await user.click(routes);
await user.click(
await screen.findByRole('option', { name: '/users PATCH' })
);

await user.click(screen.getByText('Submit'));

expect(axiosPatchSpy).toHaveBeenCalledWith(
'/users',
{
_id: 'user1',
add_authorised_routes: ['/users PATCH'],
remove_authorised_routes: ['/submit/hdf POST'],
},
{
headers: {
Authorization: 'Bearer null',
},
}
);
});
});
});
Loading
Loading