Skip to content

Commit

Permalink
Update modal title; add publicly readable checkbox; update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kpierre13 committed Sep 27, 2024
1 parent 3b95dce commit f3e3b8a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/pages/workflows/workflow/WorkflowSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const BaseWorkflowSummary = () => {
]),
permissionsModalOpen &&
h(PermissionsModal, {
methodOrNamespace: 'method',
snapshotOrNamespace: 'Snapshot',
namespace,
name,
selectedSnapshot: snapshotId,
Expand Down
78 changes: 72 additions & 6 deletions src/pages/workflows/workflow/common/PermissionsModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { act, fireEvent, screen } from '@testing-library/react';
import userEvent, { UserEvent } from '@testing-library/user-event';
import React from 'react';
import { Ajax } from 'src/libs/ajax';
import { snapshotStore } from 'src/libs/state';
import { PermissionsModal } from 'src/pages/workflows/workflow/common/PermissionsModal';
import { asMockedFn, renderWithAppContexts, SelectHelper } from 'src/testing/test-utils';

Expand Down Expand Up @@ -50,7 +51,7 @@ describe('PermissionsModal', () => {
<PermissionsModal
name='test'
namespace='namespace'
methodOrNamespace='method'
snapshotOrNamespace='Snapshot'
selectedSnapshot='3'
setPermissionsModalOpen={jest.fn()}
/>
Expand All @@ -71,7 +72,7 @@ describe('PermissionsModal', () => {
<PermissionsModal
name='test'
namespace='namespace'
methodOrNamespace='method'
snapshotOrNamespace='Snapshot'
selectedSnapshot='3'
setPermissionsModalOpen={jest.fn()}
/>
Expand All @@ -93,7 +94,7 @@ describe('PermissionsModal', () => {
<PermissionsModal
name='test'
namespace='namespace'
methodOrNamespace='method'
snapshotOrNamespace='Snapshot'
selectedSnapshot='3'
setPermissionsModalOpen={jest.fn()}
/>
Expand Down Expand Up @@ -131,7 +132,7 @@ describe('PermissionsModal', () => {
<PermissionsModal
name='test'
namespace='namespace'
methodOrNamespace='method'
snapshotOrNamespace='Snapshot'
selectedSnapshot='3'
setPermissionsModalOpen={jest.fn()}
/>
Expand Down Expand Up @@ -165,7 +166,7 @@ describe('PermissionsModal', () => {
<PermissionsModal
name='test'
namespace='namespace'
methodOrNamespace='method'
snapshotOrNamespace='Snapshot'
selectedSnapshot='3'
setPermissionsModalOpen={jest.fn()}
/>
Expand All @@ -184,7 +185,7 @@ describe('PermissionsModal', () => {
<PermissionsModal
name='test'
namespace='namespace'
methodOrNamespace='method'
snapshotOrNamespace='Snapshot'
selectedSnapshot='3'
setPermissionsModalOpen={jest.fn()}
/>
Expand All @@ -200,4 +201,69 @@ describe('PermissionsModal', () => {
expect(screen.getByText('User is not a valid email')).toBeInTheDocument();
expect(addButton).toHaveAttribute('aria-disabled', 'true');
});

it('checkbox is checked for public workflow', async () => {
// ARRANGE
jest.spyOn(snapshotStore, 'get').mockImplementation(
jest.fn().mockReturnValue({
namespace: 'nameFoo',
name: 'cnv_somatic_pair_workflow',
snapshotId: 1,
createDate: '2017-10-20T20:07:22Z',
managers: ['nameFoo@fooname.com'],
synopsis: 'a very fancy method',
documentation: '',
public: true,
snapshotComment: 'a fake snapshot',
})
);

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

// ASSERT
const publiclyReadableButton = screen.queryByLabelText('Make Publicly Readable?');
expect(publiclyReadableButton).toBeChecked();
});

it('checkbox is unchecked for private workflow', async () => {
// ARRANGE
jest.spyOn(snapshotStore, 'get').mockImplementation(
jest.fn().mockReturnValue({
namespace: 'nameFoo',
name: 'cnv_somatic_pair_workflow',
snapshotId: 1,
createDate: '2017-10-20T20:07:22Z',
managers: ['nameFoo@fooname.com'],
synopsis: 'a very fancy method',
documentation: '',
public: false,
snapshotComment: 'a fake snapshot',
})
);

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

const publiclyReadableButton = screen.queryByLabelText('Make Publicly Readable?');
expect(publiclyReadableButton).not.toBeChecked();
});
});
48 changes: 30 additions & 18 deletions src/pages/workflows/workflow/common/PermissionsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Modal,
modalStyles,
Select,
useStore,
} from '@terra-ui-packages/components';
import _ from 'lodash/fp';
import React, { CSSProperties, Dispatch, SetStateAction, useRef, useState } from 'react';
Expand All @@ -18,7 +19,7 @@ import colors from 'src/libs/colors';
import { reportError } from 'src/libs/error';
import { FormLabel } from 'src/libs/forms';
import { useCancellation, useOnMount } from 'src/libs/react-utils';
import { getTerraUser } from 'src/libs/state';
import { getTerraUser, snapshotStore } from 'src/libs/state';
import * as Style from 'src/libs/style';
import { append, withBusyState } from 'src/libs/utils';
import * as Utils from 'src/libs/utils';
Expand All @@ -31,7 +32,7 @@ import {
import validate from 'validate.js';

type WorkflowPermissionsModalProps = {
methodOrNamespace: 'method' | 'namespace';
snapshotOrNamespace: 'Snapshot' | 'Namespace';
namespace: string;
name: string;
selectedSnapshot: string;
Expand Down Expand Up @@ -165,20 +166,18 @@ const CurrentUsers = (props: CurrentUserProps) => {
};

export const PermissionsModal = (props: WorkflowPermissionsModalProps) => {
const { methodOrNamespace, namespace, name, selectedSnapshot, setPermissionsModalOpen } = props;
const { snapshotOrNamespace, namespace, name, selectedSnapshot, setPermissionsModalOpen } = props;
const signal: AbortSignal = useCancellation();
const [searchValue, setSearchValue] = useState<string>('');
const [permissions, setPermissions] = useState<WorkflowsPermissions>([]);
const [working, setWorking] = useState(false);
const [loaded, setLoaded] = useState<boolean>(false);
const [originalPermissions, setOriginalPermissions] = useState<WorkflowsPermissions>([]);
const userEmails = _.map('user', permissions);
const publicUser = _.find({ user: 'public' }, originalPermissions);
// console.log(publicUser);
const [isPublic, setIsPublic] = useState<boolean>(_.isUndefined(publicUser));
const publicUser = _.find({ user: 'public' }, permissions);
const [userValueModified, setUserValueModified] = useState<boolean>(false);
// console.log(isPublic);

const { public: isPublic } = useStore(snapshotStore);
const [isPublicSnapshot, setIsPublicSnapshot] = useState<boolean | undefined>(isPublic);
const errors = validate({ searchValue }, constraints, {
prettify: (v) => ({ searchValue: 'User' }[v] || validate.prettify(v)),
});
Expand Down Expand Up @@ -206,12 +205,24 @@ export const PermissionsModal = (props: WorkflowPermissionsModalProps) => {
setPermissions(append({ user: userEmail, role: 'READER' } as RawWorkflowsPermissions));
};

const updatePublicUser = (publicUser) => {
return _.update(publicUser.role, (v) =>
v === false ? publicUser.role === 'NO ACCESS' : publicUser.role === 'READER'
);
const updatePublicUser = (v) => {
if (publicUser) {
permissions.map((pub) => {
if (v === false) {
if (pub.user === 'public') {
Object.assign(pub, { user: 'public', role: 'NO ACCESS' });
}
} else if (v === true) {
if (pub.user === 'public') {
Object.assign(pub, { user: 'public', role: 'READER' });
}
}

_.update('role', _.set(publicUser.role, ''));
return null;
});
} else {
addUser('public');
}
};

const save = withBusyState(setWorking, async () => {
Expand All @@ -221,6 +232,7 @@ export const PermissionsModal = (props: WorkflowPermissionsModalProps) => {

try {
await Ajax(signal).Methods.method(namespace, name, selectedSnapshot).setPermissions(permissionUpdates);
window.location.reload();
setPermissionsModalOpen(false);
} catch (error) {
await reportError('Error saving permissions.', error);
Expand All @@ -230,7 +242,7 @@ export const PermissionsModal = (props: WorkflowPermissionsModalProps) => {

return (
<Modal
title={`Permissions for ${methodOrNamespace} ${namespace}/${name}`}
title={`Edit ${snapshotOrNamespace} Permissions`}
onDismiss={() => setPermissionsModalOpen(false)}
width='30rem'
showButtons={false}
Expand Down Expand Up @@ -266,10 +278,10 @@ export const PermissionsModal = (props: WorkflowPermissionsModalProps) => {
<div style={{ ...modalStyles.buttonRow, justifyContent: 'space-between' }}>
<div>
<LabeledCheckbox
checked={isPublic}
onChange={(v) => {
updatePublicUser(publicUser);
setIsPublic(v);
checked={isPublicSnapshot}
onChange={(v: boolean) => {
setIsPublicSnapshot(v);
updatePublicUser(v);
}}
>
<span style={{ marginLeft: '0.3rem' }}>Make Publicly Readable?</span>
Expand Down

0 comments on commit f3e3b8a

Please sign in to comment.