Skip to content

Commit 05b0d85

Browse files
refactor: creating constants for library permissions
1 parent d4a233f commit 05b0d85

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
lines changed

src/authz-module/libraries-manager/LibrariesTeamManager.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useLibrary, useUpdateLibrary } from '@src/authz-module/data/hooks';
66
import { useLibraryAuthZ } from './context';
77
import LibrariesTeamManager from './LibrariesTeamManager';
88
import { ToastManagerProvider } from './ToastManagerContext';
9+
import { CONTENT_LIBRARY_PERMISSIONS } from './constants';
910

1011
jest.mock('./context', () => {
1112
const actual = jest.requireActual('./context');
@@ -71,8 +72,8 @@ describe('LibrariesTeamManager', () => {
7172
},
7273
],
7374
permissions: [
74-
{ key: 'content_libraries.view_library', label: 'view', resource: 'library' },
75-
{ key: 'content_libraries.edit_library', label: 'edit', resource: 'library' },
75+
{ key: CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY, label: 'view', resource: 'library' },
76+
{ key: CONTENT_LIBRARY_PERMISSIONS.EDIT_LIBRARY_COLLECTION, label: 'edit', resource: 'library' },
7677
],
7778
resources: [{ key: 'library', label: 'Library' }],
7879
canManageTeam: true,

src/authz-module/libraries-manager/components/TeamTable/index.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { renderWrapper } from '@src/setupTest';
44
import { useTeamMembers } from '@src/authz-module/data/hooks';
55
import { useLibraryAuthZ } from '@src/authz-module/libraries-manager/context';
66
import { ToastManagerProvider } from '@src/authz-module/libraries-manager/ToastManagerContext';
7+
import { CONTENT_LIBRARY_PERMISSIONS } from '@src/authz-module/libraries-manager/constants';
78
import TeamTable from './index';
89

910
const mockNavigate = jest.fn();
@@ -45,9 +46,9 @@ describe('TeamTable', () => {
4546
{
4647
role: 'admin',
4748
permissions: [
48-
'content_libraries.delete_library',
49-
'content_libraries.publish_library_content',
50-
'content_libraries.manage_library_team',
49+
CONTENT_LIBRARY_PERMISSIONS.DELETE_LIBRARY,
50+
CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
51+
CONTENT_LIBRARY_PERMISSIONS.MANAGE_LIBRARY_TEAM,
5152
],
5253
userCount: 3,
5354
name: 'Admin',
@@ -56,8 +57,8 @@ describe('TeamTable', () => {
5657
{
5758
role: 'editor',
5859
permissions: [
59-
'content_libraries.edit_library_content',
60-
'content_libraries.publish_library_content',
60+
CONTENT_LIBRARY_PERMISSIONS.EDIT_LIBRARY_CONTENT,
61+
CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
6162
],
6263
userCount: 3,
6364
name: 'Editor',
@@ -66,7 +67,7 @@ describe('TeamTable', () => {
6667
{
6768
role: 'viewer',
6869
permissions: [
69-
'content_libraries.view_library',
70+
CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY,
7071
],
7172
userCount: 3,
7273
name: 'Viewer',
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import { PermissionMetadata, ResourceMetadata, RoleMetadata } from 'types';
22

3+
export const CONTENT_LIBRARY_PERMISSIONS = {
4+
DELETE_LIBRARY: 'content_libraries.delete_library',
5+
MANAGE_LIBRARY_TAGS: 'content_libraries.manage_library_tags',
6+
VIEW_LIBRARY: 'content_libraries.view_library',
7+
8+
EDIT_LIBRARY_CONTENT: 'content_libraries.edit_library_content',
9+
PUBLISH_LIBRARY_CONTENT: 'content_libraries.publish_library_content',
10+
REUSE_LIBRARY_CONTENT: 'content_libraries.reuse_library_content',
11+
12+
CREATE_LIBRARY_COLLECTION: 'content_libraries.create_library_collection',
13+
EDIT_LIBRARY_COLLECTION: 'content_libraries.edit_library_collection',
14+
DELETE_LIBRARY_COLLECTION: 'content_libraries.delete_library_collection',
15+
16+
MANAGE_LIBRARY_TEAM: 'content_libraries.manage_library_team',
17+
VIEW_LIBRARY_TEAM: 'content_libraries.view_library_team',
18+
};
19+
320
// Note: this information will eventually come from the backend API
421
// but for the MVP we decided to manage it in the frontend
522
export const libraryRolesMetadata: RoleMetadata[] = [
@@ -17,18 +34,18 @@ export const libraryResourceTypes: ResourceMetadata[] = [
1734
];
1835

1936
export const libraryPermissions: PermissionMetadata[] = [
20-
{ key: 'content_libraries.delete_library', resource: 'library', description: 'Allows the user to delete the library and all its contents.' },
21-
{ key: 'content_libraries.manage_library_tags', resource: 'library', description: 'Add or remove tags from content.' },
22-
{ key: 'content_libraries.view_library', resource: 'library', description: 'View content, search, filter, and sort within the library.' },
37+
{ key: CONTENT_LIBRARY_PERMISSIONS.DELETE_LIBRARY, resource: 'library', description: 'Allows the user to delete the library and all its contents.' },
38+
{ key: CONTENT_LIBRARY_PERMISSIONS.MANAGE_LIBRARY_TAGS, resource: 'library', description: 'Add or remove tags from content.' },
39+
{ key: CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY, resource: 'library', description: 'View content, search, filter, and sort within the library.' },
2340

24-
{ key: 'content_libraries.edit_library_content', resource: 'library_content', description: 'Edit content in draft mode' },
25-
{ key: 'content_libraries.publish_library_content', resource: 'library_content', description: 'Publish content, making it available for reuse' },
26-
{ key: 'content_libraries.reuse_library_content', resource: 'library_content', description: 'Reuse published content within a course.' },
41+
{ key: CONTENT_LIBRARY_PERMISSIONS.EDIT_LIBRARY_CONTENT, resource: 'library_content', description: 'Edit content in draft mode' },
42+
{ key: CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT, resource: 'library_content', description: 'Publish content, making it available for reuse' },
43+
{ key: CONTENT_LIBRARY_PERMISSIONS.REUSE_LIBRARY_CONTENT, resource: 'library_content', description: 'Reuse published content within a course.' },
2744

28-
{ key: 'content_libraries.create_library_collection', resource: 'library_collection', description: 'Create new collections within a library.' },
29-
{ key: 'content_libraries.edit_library_collection', resource: 'library_collection', description: 'Add or remove content from existing collections.' },
30-
{ key: 'content_libraries.delete_library_collection', resource: 'library_collection', description: 'Delete entire collections from the library.' },
45+
{ key: CONTENT_LIBRARY_PERMISSIONS.CREATE_LIBRARY_COLLECTION, resource: 'library_collection', description: 'Create new collections within a library.' },
46+
{ key: CONTENT_LIBRARY_PERMISSIONS.EDIT_LIBRARY_COLLECTION, resource: 'library_collection', description: 'Add or remove content from existing collections.' },
47+
{ key: CONTENT_LIBRARY_PERMISSIONS.DELETE_LIBRARY_COLLECTION, resource: 'library_collection', description: 'Delete entire collections from the library.' },
3148

32-
{ key: 'content_libraries.manage_library_team', resource: 'library_team', description: 'View the list of users who have access to the library.' },
33-
{ key: 'content_libraries.view_library_team', resource: 'library_team', description: 'Add, remove, and assign roles to users within the library.' },
49+
{ key: CONTENT_LIBRARY_PERMISSIONS.MANAGE_LIBRARY_TEAM, resource: 'library_team', description: 'View the list of users who have access to the library.' },
50+
{ key: CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY_TEAM, resource: 'library_team', description: 'Add, remove, and assign roles to users within the library.' },
3451
];

src/authz-module/libraries-manager/context.test.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useValidateUserPermissions } from '@src/data/hooks';
55
import { renderWrapper } from '@src/setupTest';
66
import { usePermissionsByRole } from '@src/authz-module/data/hooks';
77
import { CustomErrors } from '@src/constants';
8+
import { CONTENT_LIBRARY_PERMISSIONS } from './constants';
89
import { LibraryAuthZProvider, useLibraryAuthZ } from './context';
910

1011
jest.mock('react-router-dom', () => ({
@@ -15,16 +16,10 @@ jest.mock('react-router-dom', () => ({
1516
jest.mock('@src/data/hooks', () => ({
1617
useValidateUserPermissions: jest.fn(),
1718
}));
19+
20+
// Move the mock after imports and use actual values
1821
jest.mock('@src/authz-module/data/hooks', () => ({
19-
usePermissionsByRole: jest.fn().mockReturnValue({
20-
data: [
21-
{
22-
role: 'library_author',
23-
permissions: ['content_libraries.view_library_team', 'content_libraries.edit_library_content'],
24-
user_count: 12,
25-
},
26-
],
27-
}),
22+
usePermissionsByRole: jest.fn(),
2823
}));
2924

3025
class ErrorBoundary extends Component<{ children: ReactNode }, { hasError: boolean; error?: Error }> {
@@ -67,6 +62,14 @@ describe('LibraryAuthZProvider', () => {
6762
(useParams as jest.Mock).mockReturnValue({ libraryId: 'lib123' });
6863
(usePermissionsByRole as jest.Mock).mockReturnValue({
6964
data: [
65+
{
66+
role: 'library_author',
67+
permissions: [
68+
CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY_TEAM,
69+
CONTENT_LIBRARY_PERMISSIONS.EDIT_LIBRARY_CONTENT,
70+
],
71+
user_count: 12,
72+
},
7073
{
7174
role: 'instructor',
7275
description: 'Can create and edit content',

src/authz-module/libraries-manager/context.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import { useValidateUserPermissions } from '@src/data/hooks';
77
import { usePermissionsByRole } from '@src/authz-module/data/hooks';
88
import { PermissionMetadata, ResourceMetadata, Role } from 'types';
99
import { CustomErrors } from '@src/constants';
10-
import { libraryPermissions, libraryResourceTypes, libraryRolesMetadata } from './constants';
10+
import {
11+
CONTENT_LIBRARY_PERMISSIONS, libraryPermissions, libraryResourceTypes, libraryRolesMetadata,
12+
} from './constants';
1113

12-
const LIBRARY_TEAM_PERMISSIONS = ['content_libraries.view_library_team', 'content_libraries.manage_library_team'];
14+
const LIBRARY_TEAM_PERMISSIONS = [
15+
CONTENT_LIBRARY_PERMISSIONS.VIEW_LIBRARY_TEAM,
16+
CONTENT_LIBRARY_PERMISSIONS.MANAGE_LIBRARY_TEAM,
17+
];
1318

1419
export type AppContextType = {
1520
authenticatedUser: {

0 commit comments

Comments
 (0)