Skip to content

Commit aa620bd

Browse files
committed
refactor: use the new constants
1 parent b843695 commit aa620bd

8 files changed

Lines changed: 234 additions & 224 deletions

File tree

openedx_authz/rest_api/v1/views.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from rest_framework.views import APIView
1616

1717
from openedx_authz import api
18+
from openedx_authz.constants import permissions
1819
from openedx_authz.rest_api.data import RoleOperationError, RoleOperationStatus
1920
from openedx_authz.rest_api.decorators import authz_permissions, view_auth_classes
2021
from openedx_authz.rest_api.utils import (
@@ -182,13 +183,13 @@ class RoleUserAPIView(APIView):
182183
"username": "john_doe",
183184
"email": "[email protected]",
184185
"full_name": "John Doe"
185-
"roles": ["library_admin", "library_user"]
186+
"roles": [roles.LIBRARY_ADMIN, roles.LIBRARY_USER]
186187
},
187188
{
188189
"username": "jane_doe",
189190
"email": "[email protected]",
190191
"full_name": "Jane Doe"
191-
"roles": ["library_user"]
192+
"roles": [roles.LIBRARY_USER]
192193
}
193194
]
194195
}
@@ -223,7 +224,7 @@ class RoleUserAPIView(APIView):
223224
PUT /api/authz/v1/roles/users/ ::
224225
225226
{
226-
"role": "library_admin",
227+
"role": roles.LIBRARY_ADMIN,
227228
"scope": "lib:DemoX:CSPROB",
228229
"users": ["[email protected]", "username2"]
229230
}
@@ -250,7 +251,7 @@ class RoleUserAPIView(APIView):
250251
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
251252
},
252253
)
253-
@authz_permissions(["view_library_team"])
254+
@authz_permissions([permissions.VIEW_LIBRARY_TEAM])
254255
def get(self, request: HttpRequest) -> Response:
255256
"""Retrieve all users with role assignments within a specific scope."""
256257
serializer = ListUsersInRoleWithScopeSerializer(data=request.query_params)
@@ -277,7 +278,7 @@ def get(self, request: HttpRequest) -> Response:
277278
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
278279
},
279280
)
280-
@authz_permissions(["manage_library_team"])
281+
@authz_permissions([permissions.MANAGE_LIBRARY_TEAM])
281282
def put(self, request: HttpRequest) -> Response:
282283
"""Assign multiple users to a specific role within a scope."""
283284
serializer = AddUsersToRoleWithScopeSerializer(data=request.data)
@@ -324,7 +325,7 @@ def put(self, request: HttpRequest) -> Response:
324325
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
325326
},
326327
)
327-
@authz_permissions(["manage_library_team"])
328+
@authz_permissions([permissions.MANAGE_LIBRARY_TEAM])
328329
def delete(self, request: HttpRequest) -> Response:
329330
"""Remove multiple users from a specific role within a scope."""
330331
serializer = RemoveUsersFromRoleWithScopeSerializer(data=request.query_params)
@@ -399,13 +400,15 @@ class RoleListView(APIView):
399400
"previous": null,
400401
"results": [
401402
{
402-
"role": "library_author",
403+
"role": roles.LIBRARY_AUTHOR,
403404
"permissions": ["delete_library_content", "edit_library"],
404405
"user_count": 5
405406
},
406407
{
407-
"role": "library_user",
408-
"permissions": ["view_library", "view_library_team", "reuse_library_content"],
408+
"role": roles.LIBRARY_USER,
409+
"permissions": [permissions.VIEW_LIBRARY,
410+
permissions.VIEW_LIBRARY_TEAM,
411+
permissions.REUSE_LIBRARY_CONTENT],
409412
"user_count": 12
410413
}
411414
]
@@ -427,7 +430,7 @@ class RoleListView(APIView):
427430
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
428431
},
429432
)
430-
@authz_permissions(["view_library_team"])
433+
@authz_permissions([permissions.VIEW_LIBRARY_TEAM])
431434
def get(self, request: HttpRequest) -> Response:
432435
"""Retrieve all roles and their permissions for a specific scope."""
433436
serializer = ListRolesWithScopeSerializer(data=request.query_params)

openedx_authz/tests/api/test_data.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
SubjectData,
1818
UserData,
1919
)
20+
from openedx_authz.constants import permissions, roles
2021

2122

2223
@ddt
@@ -372,7 +373,7 @@ def test_user_data_str_and_repr(self, external_key, expected_str, expected_repr)
372373
@data(
373374
("read", "Read", "act^read"),
374375
("write", "Write", "act^write"),
375-
("delete_library", "Delete Library", "act^delete_library"),
376+
(permissions.DELETE_LIBRARY, "Delete Library", "act^delete_library"),
376377
("edit_content", "Edit Content", "act^edit_content"),
377378
)
378379
@unpack
@@ -413,7 +414,7 @@ def test_scope_data_str_and_repr(self, external_key, expected_str, expected_repr
413414

414415
@data(
415416
("instructor", "Instructor", "role^instructor"),
416-
("library_admin", "Library Admin", "role^library_admin"),
417+
(roles.LIBRARY_ADMIN, "Library Admin", "role^library_admin"),
417418
("course_staff", "Course Staff", "role^course_staff"),
418419
)
419420
@unpack
@@ -454,7 +455,7 @@ def test_role_data_str_with_permissions(self):
454455
("read", "allow", "Read - allow", "act^read => allow"),
455456
("write", "deny", "Write - deny", "act^write => deny"),
456457
(
457-
"delete_library",
458+
permissions.DELETE_LIBRARY,
458459
"allow",
459460
"Delete Library - allow",
460461
"act^delete_library => allow",
@@ -485,7 +486,7 @@ def test_role_assignment_data_str(self):
485486
"""
486487
user = UserData(external_key="john_doe")
487488
role1 = RoleData(external_key="instructor")
488-
role2 = RoleData(external_key="library_admin")
489+
role2 = RoleData(external_key=roles.LIBRARY_ADMIN)
489490
scope = ContentLibraryData(external_key="lib:DemoX:CSPROB")
490491
assignment = RoleAssignmentData(subject=user, roles=[role1, role2], scope=scope)
491492

@@ -502,7 +503,7 @@ def test_role_assignment_data_repr(self):
502503
"""
503504
user = UserData(external_key="john_doe")
504505
role1 = RoleData(external_key="instructor")
505-
role2 = RoleData(external_key="library_admin")
506+
role2 = RoleData(external_key=roles.LIBRARY_ADMIN)
506507
scope = ContentLibraryData(external_key="lib:DemoX:CSPROB")
507508
assignment = RoleAssignmentData(subject=user, roles=[role1, role2], scope=scope)
508509

0 commit comments

Comments
 (0)