Add permissions selectors and tests for studio comments

This commit is contained in:
Paul Kaplan 2021-04-08 10:36:56 -04:00
parent 7a8f2b61dc
commit 11794d885a
2 changed files with 164 additions and 73 deletions

View file

@ -91,8 +91,14 @@ const selectCanAddProjects = state =>
isCurator(state) || isCurator(state) ||
(selectIsSocial(state) && state.studio.openToAll); (selectIsSocial(state) && state.studio.openToAll);
// This isn't "canComment" since they could be muted, but comment composer handles that
const selectShowCommentComposer = state => selectIsSocial(state); const selectShowCommentComposer = state => selectIsSocial(state);
const selectCanReportComment = state => selectIsSocial(state);
const selectCanRestoreComment = state => selectIsAdmin(state);
// On the project page, project owners can delete comments with a confirmation,
// and admins can delete comments without a confirmation. For now, only admins
// can delete studio comments, so the following two are the same.
const selectCanDeleteComment = state => selectIsAdmin(state);
const selectCanDeleteCommentWithoutConfirm = state => selectIsAdmin(state);
// Data selectors // Data selectors
const selectStudioId = state => state.studio.id; const selectStudioId = state => state.studio.id;
@ -157,5 +163,9 @@ module.exports = {
selectStudioId, selectStudioId,
selectCanEditInfo, selectCanEditInfo,
selectCanAddProjects, selectCanAddProjects,
selectShowCommentComposer selectShowCommentComposer,
selectCanDeleteComment,
selectCanDeleteCommentWithoutConfirm,
selectCanReportComment,
selectCanRestoreComment
}; };

View file

@ -2,7 +2,11 @@ import {
getInitialState as getInitialStudioState, getInitialState as getInitialStudioState,
selectCanEditInfo, selectCanEditInfo,
selectCanAddProjects, selectCanAddProjects,
selectShowCommentComposer selectShowCommentComposer,
selectCanDeleteComment,
selectCanDeleteCommentWithoutConfirm,
selectCanReportComment,
selectCanRestoreComment
} from '../../../src/redux/studio'; } from '../../../src/redux/studio';
import { import {
@ -11,79 +15,156 @@ import {
import {sessions, studios} from '../../helpers/state-fixtures.json'; import {sessions, studios} from '../../helpers/state-fixtures.json';
describe('studio selectors', () => { let state;
let state;
beforeEach(() => { const setStateByRole = (role) => {
state = { switch (role) {
session: getInitialSessionState(), case 'admin':
studio: getInitialStudioState() state.session = sessions.user1Admin;
}; break;
}); case 'curator':
state.studio = studios.isCurator;
state.session = sessions.user1Social;
break;
case 'manager':
state.studio = studios.isManager;
state.session = sessions.user1Social;
break;
case 'creator':
state.studio = studios.creator1;
state.session = sessions.user1Social;
break;
case 'logged in':
state.session = sessions.user1Social;
break;
case 'unconfirmed':
state.session = sessions.user1;
break;
case 'logged out': // Default state set in beforeEach
break;
default:
throw new Error('Unknown user role in test: ' + role);
}
};
describe('studio info', () => { beforeEach(() => {
test('is editable by admin', () => { state = {
state.session = sessions.user1Admin; session: getInitialSessionState(),
expect(selectCanEditInfo(state)).toBe(true); studio: getInitialStudioState()
}); };
test('is editable by managers and studio creator', () => { });
state.studio = studios.isManager;
expect(selectCanEditInfo(state)).toBe(true);
state.studio = studios.creator1; describe('studio info', () => {
state.session = sessions.user1; describe('can edit studio info', () => {
expect(selectCanEditInfo(state)).toBe(true); test.each([
}); ['admin', true],
test('is not editable by curators', () => { ['curator', false],
state.studio = studios.isCurator; ['manager', true],
state.session = sessions.user1; ['creator', true],
expect(selectCanEditInfo(state)).toBe(false); ['logged in', false],
}); ['unconfirmed', false],
test('is not editable by other logged in users', () => { ['logged out', false]
state.session = sessions.user1; ])('%s: %s', (role, expected) => {
expect(selectCanEditInfo(state)).toBe(false); setStateByRole(role);
}); expect(selectCanEditInfo(state)).toBe(expected);
test('is not editable by logged out users', () => { });
expect(selectCanEditInfo(state)).toBe(false); });
}); });
});
describe('studio projects', () => {
describe('studio projects', () => { describe('can add project, not open to all', () => {
test('cannot be added by admin', () => { test.each([
state.session = sessions.user1Admin; ['admin', false],
expect(selectCanAddProjects(state)).toBe(false); ['curator', true],
}); ['manager', true],
test('can be added by managers and studio creator', () => { ['creator', true],
state.studio = studios.isManager; ['logged in', false],
expect(selectCanAddProjects(state)).toBe(true); ['unconfirmed', false],
['logged out', false]
state.studio = studios.creator1; ])('%s: %s', (role, expected) => {
state.session = sessions.user1; setStateByRole(role);
expect(selectCanAddProjects(state)).toBe(true); expect(selectCanAddProjects(state)).toBe(expected);
}); });
test('can be added by curators', () => { });
state.studio = studios.isCurator;
state.session = sessions.user1; describe('can add project, open to all', () => {
expect(selectCanAddProjects(state)).toBe(true); test.each([
}); ['logged in', true],
test('can be added by social users if studio is openToAll', () => { ['unconfirmed', false],
state.studio = studios.openToAll; ['logged out', false]
state.session = sessions.user1Social; ])('%s: %s', (role, expected) => {
expect(selectCanAddProjects(state)).toBe(true); setStateByRole(role);
}); state.studio.openToAll = true;
test('cannot be added by social users if not openToAll', () => { expect(selectCanAddProjects(state)).toBe(expected);
state.session = sessions.user1Social; });
expect(selectCanAddProjects(state)).toBe(false); });
}); });
});
describe('studio comments', () => {
describe('studio comments', () => { describe('showing comment composer', () => {
test('show comment composer only for social users', () => { test.each([
expect(selectShowCommentComposer(state)).toBe(false); ['logged in', true],
state.session = sessions.user1; ['unconfirmed', false],
expect(selectShowCommentComposer(state)).toBe(false); ['logged out', false]
state.session = sessions.user1Social; ])('%s: %s', (role, expected) => {
expect(selectShowCommentComposer(state)).toBe(true); setStateByRole(role);
expect(selectShowCommentComposer(state)).toBe(expected);
});
});
describe('can report comment', () => {
test.each([
['logged in', true],
['unconfirmed', false],
['logged out', false]
])('%s: %s', (role, expected) => {
setStateByRole(role);
expect(selectCanReportComment(state)).toBe(expected);
});
});
describe('can delete comment', () => {
test.each([
['admin', true],
['curator', false],
['manager', false],
['creator', false],
['logged in', false],
['unconfirmed', false],
['logged out', false]
])('%s: %s', (role, expected) => {
setStateByRole(role);
expect(selectCanDeleteComment(state)).toBe(expected);
});
});
describe('can delete comment without confirmation', () => {
test.each([
['admin', true],
['curator', false],
['manager', false],
['creator', false],
['logged in', false],
['unconfirmed', false],
['logged out', false]
])('%s: %s', (role, expected) => {
setStateByRole(role);
expect(selectCanDeleteCommentWithoutConfirm(state)).toBe(expected);
});
});
describe('can restore a comment', () => {
test.each([
['admin', true],
['curator', false],
['manager', false],
['creator', false],
['logged in', false],
['unconfirmed', false],
['logged out', false]
])('%s: %s', (role, expected) => {
setStateByRole(role);
expect(selectCanRestoreComment(state)).toBe(expected);
}); });
}); });
}); });