-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[FC-0099] feat: assign library roles after successful library creation #37532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10ba620
feat: assign library roles after successful library creation
mariajgrimaldi ea33154
test: add unittest for role assignment after content library creation
mariajgrimaldi ede1472
refactor: address quality issues
mariajgrimaldi da472a0
build: update openedx-authz to latest release
mariajgrimaldi 6eae3ec
refactor: add more comprehensive test cases
mariajgrimaldi 43018bb
refactor: address quality issues and bump openedx-authz
mariajgrimaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,8 +28,10 @@ | |
| LIBRARY_COLLECTION_UPDATED, | ||
| LIBRARY_CONTAINER_UPDATED, | ||
| ) | ||
| from openedx_authz.api.users import get_user_role_assignments_in_scope | ||
| from openedx_learning.api import authoring as authoring_api | ||
|
|
||
| from common.djangoapps.student.tests.factories import UserFactory | ||
| from .. import api | ||
| from ..models import ContentLibrary | ||
| from .base import ContentLibrariesRestApiTest | ||
|
|
@@ -1479,3 +1481,126 @@ def test_get_backup_task_status_failed(self) -> None: | |
| assert status is not None | ||
| assert status['state'] == UserTaskStatus.FAILED | ||
| assert status['file'] is None | ||
|
|
||
|
|
||
| class ContentLibraryAuthZRoleAssignmentTest(ContentLibrariesRestApiTest): | ||
| """ | ||
| Tests for Content Library role assignment via the AuthZ Authorization Framework. | ||
|
|
||
| These tests verify that library roles are correctly assigned to users through | ||
| the openedx-authz (AuthZ) Authorization Framework when libraries are created or when | ||
| explicit role assignments are made. | ||
|
|
||
| See: https://github.com/openedx/openedx-authz/ | ||
| """ | ||
|
|
||
| def setUp(self) -> None: | ||
| super().setUp() | ||
|
|
||
| # Create Content Libraries | ||
| self._create_library("test-lib-role-1", "Test Library Role 1") | ||
|
|
||
| # Fetch the created ContentLibrary objects so we can access their learning_package.id | ||
| self.lib1 = ContentLibrary.objects.get(slug="test-lib-role-1") | ||
|
|
||
| def test_assign_library_admin_role_to_user_via_authz(self) -> None: | ||
| """ | ||
| Test assigning a library admin role to a user via the AuthZ Authorization Framework. | ||
|
|
||
| This test verifies that the openedx-authz Authorization Framework correctly | ||
| assigns the library_admin role to a user when explicitly called. | ||
| """ | ||
| api.assign_library_role_to_user(self.lib1.library_key, self.user, api.AccessLevel.ADMIN_LEVEL) | ||
|
|
||
| roles = get_user_role_assignments_in_scope(self.user.username, str(self.lib1.library_key)) | ||
| assert len(roles) == 1 | ||
| assert "library_admin" in repr(roles[0].roles[0]) | ||
|
|
||
| def test_assign_library_author_role_to_user_via_authz(self) -> None: | ||
| """ | ||
| Test assigning a library author role to a user via the AuthZ Authorization Framework. | ||
|
|
||
| This test verifies that the openedx-authz Authorization Framework correctly | ||
| assigns the library_author role to a user when explicitly called. | ||
| """ | ||
| # Create a new user to avoid conflicts with roles assigned during library creation | ||
| author_user = UserFactory.create(username="Author", email="[email protected]") | ||
|
|
||
| api.assign_library_role_to_user(self.lib1.library_key, author_user, api.AccessLevel.AUTHOR_LEVEL) | ||
|
|
||
| roles = get_user_role_assignments_in_scope(author_user.username, str(self.lib1.library_key)) | ||
| assert len(roles) == 1 | ||
| assert "library_author" in repr(roles[0].roles[0]) | ||
|
|
||
| @mock.patch("openedx.core.djangoapps.content_libraries.api.libraries.assign_role_to_user_in_scope") | ||
| def test_library_creation_assigns_admin_role_via_authz( | ||
| self, | ||
| mock_assign_role | ||
| ) -> None: | ||
| """ | ||
| Test that creating a library via REST API assigns admin role via AuthZ. | ||
|
|
||
| This test verifies that when a library is created via the REST API, | ||
| the creator is automatically assigned the library_admin role through | ||
| the openedx-authz Authorization Framework. | ||
| """ | ||
| mock_assign_role.return_value = True | ||
|
|
||
| # Create a new library (this should trigger role assignment in the REST API) | ||
| self._create_library("test-lib-role-2", "Test Library Role 2") | ||
|
|
||
| # Verify that assign_role_to_user_in_scope was called | ||
| mock_assign_role.assert_called_once() | ||
| call_args = mock_assign_role.call_args | ||
| assert call_args[0][0] == self.user.username # username | ||
| assert call_args[0][1] == "library_admin" # role | ||
| assert "test-lib-role-2" in call_args[0][2] # library_key (contains slug) | ||
|
|
||
| @mock.patch("openedx.core.djangoapps.content_libraries.api.libraries.assign_role_to_user_in_scope") | ||
| def test_library_creation_handles_authz_failure_gracefully( | ||
| self, | ||
| mock_assign_role | ||
| ) -> None: | ||
| """ | ||
| Test that library creation succeeds even if AuthZ role assignment fails. | ||
|
|
||
| This test verifies that if the openedx-authz Authorization Framework fails to assign | ||
| a role (returns False), the library creation still succeeds. This ensures that | ||
| the system degrades gracefully and doesn't break library creation if there are | ||
| issues with the Authorization Framework. | ||
| """ | ||
| # Simulate openedx-authz failing to assign the role | ||
| mock_assign_role.return_value = False | ||
|
|
||
| # Library creation should still succeed | ||
| result = self._create_library("test-lib-role-3", "Test Library Role 3") | ||
| assert result is not None | ||
| assert result["slug"] == "test-lib-role-3" | ||
|
|
||
| # Verify that the library was created successfully | ||
| lib3 = ContentLibrary.objects.get(slug="test-lib-role-3") | ||
| assert lib3 is not None | ||
| assert lib3.slug == "test-lib-role-3" | ||
|
|
||
| @mock.patch("openedx.core.djangoapps.content_libraries.api.libraries.assign_role_to_user_in_scope") | ||
| def test_library_creation_handles_authz_exception( | ||
| self, | ||
| mock_assign_role | ||
| ) -> None: | ||
| """ | ||
| Test that library creation succeeds even if AuthZ raises an exception. | ||
|
|
||
| This test verifies that if the openedx-authz Authorization Framework raises an | ||
| exception during role assignment, the library creation still succeeds. This ensures | ||
| robust error handling when the Authorization Framework is unavailable or misconfigured. | ||
| """ | ||
| # Simulate openedx-authz raising an exception for unknown issues | ||
| mock_assign_role.side_effect = Exception("AuthZ unavailable") | ||
|
|
||
| # Library creation should still succeed (the exception should be caught/handled) | ||
| # Note: Currently, the code doesn't catch this exception, so we expect it to propagate. | ||
| # This test documents the current behavior and can be updated if error handling is added. | ||
| with self.assertRaises(Exception) as context: | ||
| self._create_library("test-lib-role-4", "Test Library Role 4") | ||
|
|
||
| assert "AuthZ unavailable" in str(context.exception) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I don't lose this: https://openedx.atlassian.net/wiki/spaces/OEPM/pages/5252317270/Libraries+Roles+and+Permissions+Migration+Plan