Skip to content

Commit

Permalink
Do not allow existing users to be added again
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-schu committed Oct 4, 2024
1 parent 64713ea commit 3b8a585
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/pages/workflows/workflow/common/PermissionsModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ describe('PermissionsModal', () => {
expect(addButton).toHaveAttribute('aria-disabled', 'true');
});

it('gives an error with a user email that already exists', async () => {
// ARRANGE
mockAjax();
const user: UserEvent = userEvent.setup();

await act(async () => {
renderWithAppContexts(
<PermissionsModal
name='test'
namespace='namespace'
snapshotOrNamespace='Snapshot'
selectedSnapshot={3}
setPermissionsModalOpen={jest.fn()}
refresh={jest.fn()}
/>
);
});

// ACT/ASSERT
const textbox = screen.getByRole('textbox');
const addButton = screen.getByRole('button', { name: 'Add' });

fireEvent.change(textbox, { target: { value: 'user1@foo.com' } });

expect(screen.getByText('User has already been added')).toBeInTheDocument();
expect(addButton).toHaveAttribute('aria-disabled', 'true');

fireEvent.change(textbox, { target: { value: 'user3@test.com' } });

expect(screen.queryByText('User has already been added')).not.toBeInTheDocument();
expect(addButton).toHaveAttribute('aria-disabled', 'false');

await user.click(addButton);

fireEvent.change(textbox, { target: { value: 'user3@test.com' } });

expect(screen.getByText('User has already been added')).toBeInTheDocument();
expect(addButton).toHaveAttribute('aria-disabled', 'true');
});

it('lets you make a public workflow private', async () => {
// ARRANGE
mockAjax({
Expand Down
16 changes: 11 additions & 5 deletions src/pages/workflows/workflow/common/PermissionsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ type CurrentUsersProps = {
setAllPermissions: Dispatch<SetStateAction<WorkflowsPermissions>>;
};

const constraints = {
searchValue: {
email: true,
},
const constraints = (existingUserEmails: string[]) => {
return {
searchValue: {
email: true,
exclusion: {
within: existingUserEmails,
message: 'has already been added',
},
},
};
};

const styles: CSSProperties = {
Expand Down Expand Up @@ -171,7 +177,7 @@ export const PermissionsModal = (props: WorkflowPermissionsModalProps) => {
const userEmails = _.map('user', permissions);
const [userValueModified, setUserValueModified] = useState<boolean>(false);
const publicAccessLevel: WorkflowAccessLevel = _.find(publicUser, permissions)?.role ?? 'NO ACCESS';
const errors = validate({ searchValue }, constraints, {
const errors = validate({ searchValue }, constraints(userEmails), {
prettify: (v) => ({ searchValue: 'User' }[v] || validate.prettify(v)),
});

Expand Down

0 comments on commit 3b8a585

Please sign in to comment.