From f9419ac8fca8ae91fa1903d9bcdacaa5d40d113d Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Mon, 26 Apr 2021 15:11:06 -0400 Subject: [PATCH] Fix comment toggling and add permissions test --- src/redux/studio-mutations.js | 2 +- src/redux/studio-permissions.js | 8 ++++- src/views/studio/studio-comments-allowed.jsx | 9 ++---- src/views/studio/studio-comments.jsx | 14 ++++++-- src/views/studio/studio-open-to-all.jsx | 9 ++---- src/views/studio/studio-projects.jsx | 10 +++--- test/unit/redux/studio-permissions.test.js | 34 +++++++++++++++++++- 7 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/redux/studio-mutations.js b/src/redux/studio-mutations.js index 3987d5e33..be7bb8f97 100644 --- a/src/redux/studio-mutations.js +++ b/src/redux/studio-mutations.js @@ -191,7 +191,7 @@ const mutateStudioCommentsAllowed = shouldAllow => ((dispatch, getState) => { api({ host: '', uri: `/site-api/comments/gallery/${studioId}/toggle-comments/`, - method: 'PUT', + method: 'POST', useCsrf: true }, (err, body, res) => { const error = normalizeError(err, body, res); diff --git a/src/redux/studio-permissions.js b/src/redux/studio-permissions.js index 438d2810b..b2d7caad7 100644 --- a/src/redux/studio-permissions.js +++ b/src/redux/studio-permissions.js @@ -25,6 +25,10 @@ const selectCanDeleteCommentWithoutConfirm = state => selectIsAdmin(state); const selectCanFollowStudio = state => selectIsLoggedIn(state); +// Matching existing behavior, only the creator is allowed to toggle comments. +const selectCanEditCommentsAllowed = state => selectIsAdmin(state) || isCreator(state); +const selectCanEditOpenToAll = state => selectIsAdmin(state) || isManager(state); + export { selectCanEditInfo, selectCanAddProjects, @@ -33,5 +37,7 @@ export { selectCanDeleteComment, selectCanDeleteCommentWithoutConfirm, selectCanReportComment, - selectCanRestoreComment + selectCanRestoreComment, + selectCanEditCommentsAllowed, + selectCanEditOpenToAll }; diff --git a/src/views/studio/studio-comments-allowed.jsx b/src/views/studio/studio-comments-allowed.jsx index b3a6393c8..ce464fbe1 100644 --- a/src/views/studio/studio-comments-allowed.jsx +++ b/src/views/studio/studio-comments-allowed.jsx @@ -4,13 +4,12 @@ import PropTypes from 'prop-types'; import {connect} from 'react-redux'; import {selectStudioCommentsAllowed, selectIsLoadingInfo} from '../../redux/studio'; -import {selectCanEditInfo} from '../../redux/studio-permissions'; import { mutateStudioCommentsAllowed, selectIsMutatingCommentsAllowed, selectCommentsAllowedMutationError } from '../../redux/studio-mutations'; const StudioCommentsAllowed = ({ - commentsAllowedError, isLoading, isMutating, commentsAllowed, canEditInfo, handleUpdate + commentsAllowedError, isLoading, isMutating, commentsAllowed, handleUpdate }) => (
{isLoading ? ( @@ -19,12 +18,12 @@ const StudioCommentsAllowed = ({
@@ -34,7 +33,6 @@ const StudioCommentsAllowed = ({ StudioCommentsAllowed.propTypes = { commentsAllowedError: PropTypes.string, - canEditInfo: PropTypes.bool, isLoading: PropTypes.bool, isMutating: PropTypes.bool, commentsAllowed: PropTypes.bool, @@ -44,7 +42,6 @@ StudioCommentsAllowed.propTypes = { export default connect( state => ({ commentsAllowed: selectStudioCommentsAllowed(state), - canEditInfo: selectCanEditInfo(state), isLoading: selectIsLoadingInfo(state), isMutating: selectIsMutatingCommentsAllowed(state), commentsAllowedError: selectCommentsAllowedMutationError(state) diff --git a/src/views/studio/studio-comments.jsx b/src/views/studio/studio-comments.jsx index 876065d06..78907e560 100644 --- a/src/views/studio/studio-comments.jsx +++ b/src/views/studio/studio-comments.jsx @@ -14,11 +14,14 @@ import { selectCanDeleteComment, selectCanDeleteCommentWithoutConfirm, selectCanReportComment, - selectCanRestoreComment + selectCanRestoreComment, + selectCanEditCommentsAllowed } from '../../redux/studio-permissions'; +import {selectStudioCommentsAllowed} from '../../redux/studio.js'; const StudioComments = ({ comments, + commentsAllowed, handleLoadMoreComments, handleNewComment, moreCommentsToLoad, @@ -27,6 +30,7 @@ const StudioComments = ({ shouldShowCommentComposer, canDeleteComment, canDeleteCommentWithoutConfirm, + canEditCommentsAllowed, canReportComment, canRestoreComment, handleDeleteComment, @@ -41,9 +45,9 @@ const StudioComments = ({ return (

Comments

- + {canEditCommentsAllowed && }
- {shouldShowCommentComposer && + {shouldShowCommentComposer && commentsAllowed && (
{isLoading ? ( @@ -19,12 +18,12 @@ const StudioOpenToAll = ({
@@ -34,7 +33,6 @@ const StudioOpenToAll = ({ StudioOpenToAll.propTypes = { openToAllError: PropTypes.string, - canEditInfo: PropTypes.bool, isLoading: PropTypes.bool, isMutating: PropTypes.bool, openToAll: PropTypes.bool, @@ -44,7 +42,6 @@ StudioOpenToAll.propTypes = { export default connect( state => ({ openToAll: selectStudioOpenToAll(state), - canEditInfo: selectCanEditInfo(state), isLoading: selectIsLoadingInfo(state), isMutating: selectIsMutatingOpenToAll(state), openToAllError: selectOpenToAllMutationError(state) diff --git a/src/views/studio/studio-projects.jsx b/src/views/studio/studio-projects.jsx index 5045e9982..c3ce6200c 100644 --- a/src/views/studio/studio-projects.jsx +++ b/src/views/studio/studio-projects.jsx @@ -6,13 +6,13 @@ import StudioOpenToAll from './studio-open-to-all.jsx'; import {projectFetcher} from './lib/fetchers'; import {projects} from './lib/redux-modules'; -import {selectCanAddProjects} from '../../redux/studio-permissions'; +import {selectCanAddProjects, selectCanEditOpenToAll} from '../../redux/studio-permissions'; import Debug from './debug.jsx'; const {actions, selector: projectsSelector} = projects; const StudioProjects = ({ - canAddProjects, items, error, loading, moreToLoad, onLoadMore + canAddProjects, canEditOpenToAll, items, error, loading, moreToLoad, onLoadMore }) => { const {studioId} = useParams(); @@ -25,7 +25,7 @@ const StudioProjects = ({ return (

Projects

- + {canEditOpenToAll && } {error && ({ ...projectsSelector(state), - canAddProjects: selectCanAddProjects(state) + canAddProjects: selectCanAddProjects(state), + canEditOpenToAll: selectCanEditOpenToAll(state) }); const mapDispatchToProps = dispatch => ({ diff --git a/test/unit/redux/studio-permissions.test.js b/test/unit/redux/studio-permissions.test.js index 8f3bef712..648a100bf 100644 --- a/test/unit/redux/studio-permissions.test.js +++ b/test/unit/redux/studio-permissions.test.js @@ -6,7 +6,9 @@ import { selectCanDeleteCommentWithoutConfirm, selectCanReportComment, selectCanRestoreComment, - selectCanFollowStudio + selectCanFollowStudio, + selectCanEditCommentsAllowed, + selectCanEditOpenToAll } from '../../../src/redux/studio-permissions'; import {getInitialState as getInitialStudioState} from '../../../src/redux/studio'; @@ -176,4 +178,34 @@ describe('studio comments', () => { expect(selectCanFollowStudio(state)).toBe(expected); }); }); + + describe('can set "comments allowed" on a studio', () => { + test.each([ + ['admin', true], + ['curator', false], + ['manager', false], + ['creator', true], + ['logged in', false], + ['unconfirmed', false], + ['logged out', false] + ])('%s: %s', (role, expected) => { + setStateByRole(role); + expect(selectCanEditCommentsAllowed(state)).toBe(expected); + }); + }); + + describe('can set "open to all" on a studio', () => { + test.each([ + ['admin', true], + ['curator', false], + ['manager', true], + ['creator', true], + ['logged in', false], + ['unconfirmed', false], + ['logged out', false] + ])('%s: %s', (role, expected) => { + setStateByRole(role); + expect(selectCanEditOpenToAll(state)).toBe(expected); + }); + }); });