Skip to content

Commit f5d8f80

Browse files
committed
fix: add constants and improve test class
1 parent ab9e2fb commit f5d8f80

5 files changed

Lines changed: 173 additions & 149 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Default permission constants.
3+
"""
4+
5+
# Content Library Permissions
6+
VIEW_LIBRARY = "view_library"
7+
MANAGE_LIBRARY_TAGS = "manage_library_tags"
8+
DELETE_LIBRARY = "delete_library"
9+
EDIT_LIBRARY_CONTENT = "edit_library_content"
10+
PUBLISH_LIBRARY_CONTENT = "publish_library_content"
11+
REUSE_LIBRARY_CONTENT = "reuse_library_content"
12+
VIEW_LIBRARY_TEAM = "view_library_team"
13+
MANAGE_LIBRARY_TEAM = "manage_library_team"
14+
CREATE_LIBRARY_COLLECTION = "create_library_collection"
15+
EDIT_LIBRARY_COLLECTION = "edit_library_collection"
16+
DELETE_LIBRARY_COLLECTION = "delete_library_collection"

openedx_authz/constants/roles.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
"""
2+
Default roles and their associated permissions.
3+
"""
4+
5+
from openedx_authz.api.data import ActionData, PermissionData
6+
from openedx_authz.constants import permissions
7+
8+
# Library Roles
9+
LIBRARY_ROLE_LIBRARY_ADMIN = "library_admin"
10+
LIBRARY_ROLE_LIBRARY_AUTHOR = "library_author"
11+
LIBRARY_ROLE_LIBRARY_CONTRIBUTOR = "library_contributor"
12+
LIBRARY_ROLE_LIBRARY_USER = "library_user"
13+
14+
LIST_LIBRARY_ADMIN_PERMISSIONS = [
15+
PermissionData(
16+
action=ActionData(external_key=permissions.VIEW_LIBRARY),
17+
effect="allow",
18+
),
19+
PermissionData(
20+
action=ActionData(external_key=permissions.MANAGE_LIBRARY_TAGS),
21+
effect="allow",
22+
),
23+
PermissionData(
24+
action=ActionData(external_key=permissions.DELETE_LIBRARY),
25+
effect="allow",
26+
),
27+
PermissionData(
28+
action=ActionData(external_key=permissions.EDIT_LIBRARY_CONTENT),
29+
effect="allow",
30+
),
31+
PermissionData(
32+
action=ActionData(external_key=permissions.PUBLISH_LIBRARY_CONTENT),
33+
effect="allow",
34+
),
35+
PermissionData(
36+
action=ActionData(external_key=permissions.REUSE_LIBRARY_CONTENT),
37+
effect="allow",
38+
),
39+
PermissionData(
40+
action=ActionData(external_key=permissions.VIEW_LIBRARY_TEAM),
41+
effect="allow",
42+
),
43+
PermissionData(
44+
action=ActionData(external_key=permissions.MANAGE_LIBRARY_TEAM),
45+
effect="allow",
46+
),
47+
PermissionData(
48+
action=ActionData(external_key=permissions.CREATE_LIBRARY_COLLECTION),
49+
effect="allow",
50+
),
51+
PermissionData(
52+
action=ActionData(external_key=permissions.EDIT_LIBRARY_COLLECTION),
53+
effect="allow",
54+
),
55+
PermissionData(
56+
action=ActionData(external_key=permissions.DELETE_LIBRARY_COLLECTION),
57+
effect="allow",
58+
),
59+
]
60+
61+
LIST_LIBRARY_AUTHOR_PERMISSIONS = [
62+
PermissionData(
63+
action=ActionData(external_key=permissions.VIEW_LIBRARY),
64+
effect="allow",
65+
),
66+
PermissionData(
67+
action=ActionData(external_key=permissions.MANAGE_LIBRARY_TAGS),
68+
effect="allow",
69+
),
70+
PermissionData(
71+
action=ActionData(external_key=permissions.EDIT_LIBRARY_CONTENT),
72+
effect="allow",
73+
),
74+
PermissionData(
75+
action=ActionData(external_key=permissions.PUBLISH_LIBRARY_CONTENT),
76+
effect="allow",
77+
),
78+
PermissionData(
79+
action=ActionData(external_key=permissions.REUSE_LIBRARY_CONTENT),
80+
effect="allow",
81+
),
82+
PermissionData(
83+
action=ActionData(external_key=permissions.VIEW_LIBRARY_TEAM),
84+
effect="allow",
85+
),
86+
PermissionData(
87+
action=ActionData(external_key=permissions.CREATE_LIBRARY_COLLECTION),
88+
effect="allow",
89+
),
90+
PermissionData(
91+
action=ActionData(external_key=permissions.EDIT_LIBRARY_COLLECTION),
92+
effect="allow",
93+
),
94+
PermissionData(
95+
action=ActionData(external_key=permissions.DELETE_LIBRARY_COLLECTION),
96+
effect="allow",
97+
),
98+
]
99+
100+
LIST_LIBRARY_CONTRIBUTOR_PERMISSIONS = [
101+
PermissionData(
102+
action=ActionData(external_key=permissions.VIEW_LIBRARY),
103+
effect="allow",
104+
),
105+
PermissionData(
106+
action=ActionData(external_key=permissions.MANAGE_LIBRARY_TAGS),
107+
effect="allow",
108+
),
109+
PermissionData(
110+
action=ActionData(external_key=permissions.EDIT_LIBRARY_CONTENT),
111+
effect="allow",
112+
),
113+
PermissionData(
114+
action=ActionData(external_key=permissions.REUSE_LIBRARY_CONTENT),
115+
effect="allow",
116+
),
117+
PermissionData(
118+
action=ActionData(external_key=permissions.VIEW_LIBRARY_TEAM),
119+
effect="allow",
120+
),
121+
PermissionData(
122+
action=ActionData(external_key=permissions.CREATE_LIBRARY_COLLECTION),
123+
effect="allow",
124+
),
125+
PermissionData(
126+
action=ActionData(external_key=permissions.EDIT_LIBRARY_COLLECTION),
127+
effect="allow",
128+
),
129+
PermissionData(
130+
action=ActionData(external_key=permissions.DELETE_LIBRARY_COLLECTION),
131+
effect="allow",
132+
),
133+
]
134+
135+
LIST_LIBRARY_USER_PERMISSIONS = [
136+
PermissionData(
137+
action=ActionData(external_key=permissions.VIEW_LIBRARY),
138+
effect="allow",
139+
),
140+
PermissionData(
141+
action=ActionData(external_key=permissions.REUSE_LIBRARY_CONTENT),
142+
effect="allow",
143+
),
144+
PermissionData(
145+
action=ActionData(external_key=permissions.VIEW_LIBRARY_TEAM),
146+
effect="allow",
147+
),
148+
]

openedx_authz/tests/api/test_roles.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import casbin
9+
import pkg_resources
910
from ddt import data as ddt_data
1011
from ddt import ddt, unpack
1112
from django.test import TestCase
@@ -25,14 +26,14 @@
2526
get_subjects_for_role_in_scope,
2627
unassign_role_from_subject_in_scope,
2728
)
28-
from openedx_authz.engine.enforcer import AuthzEnforcer
29-
from openedx_authz.engine.utils import migrate_policy_between_enforcers
30-
from openedx_authz.tests.constants import (
29+
from openedx_authz.constants.roles import (
3130
LIST_LIBRARY_ADMIN_PERMISSIONS,
3231
LIST_LIBRARY_AUTHOR_PERMISSIONS,
3332
LIST_LIBRARY_CONTRIBUTOR_PERMISSIONS,
3433
LIST_LIBRARY_USER_PERMISSIONS,
3534
)
35+
from openedx_authz.engine.enforcer import AuthzEnforcer
36+
from openedx_authz.engine.utils import migrate_policy_between_enforcers
3637

3738

3839
class BaseRolesTestCase(TestCase):
@@ -52,11 +53,11 @@ def _seed_database_with_policies(cls):
5253
"""
5354
global_enforcer = AuthzEnforcer.get_enforcer()
5455
global_enforcer.load_policy()
56+
model_path = pkg_resources.resource_filename("openedx_authz.engine", "config/model.conf")
57+
policy_path = pkg_resources.resource_filename("openedx_authz.engine", "config/authz.policy")
58+
5559
migrate_policy_between_enforcers(
56-
source_enforcer=casbin.Enforcer(
57-
"openedx_authz/engine/config/model.conf",
58-
"openedx_authz/engine/config/authz.policy",
59-
),
60+
source_enforcer=casbin.Enforcer(model_path, policy_path),
6061
target_enforcer=global_enforcer,
6162
)
6263
global_enforcer.clear_policy() # Clear to simulate fresh start for each test

openedx_authz/tests/api/test_users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
is_user_allowed,
1515
unassign_role_from_user,
1616
)
17+
from openedx_authz.constants.roles import LIST_LIBRARY_ADMIN_PERMISSIONS, LIST_LIBRARY_AUTHOR_PERMISSIONS
1718
from openedx_authz.tests.api.test_roles import RolesTestSetupMixin
18-
from openedx_authz.tests.constants import LIST_LIBRARY_ADMIN_PERMISSIONS, LIST_LIBRARY_AUTHOR_PERMISSIONS
1919

2020

2121
class UserAssignmentsSetupMixin(RolesTestSetupMixin):

openedx_authz/tests/constants.py

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)