Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions openedx_authz/api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ class ScopeData(AuthZData, metaclass=ScopeMeta):
# Subclasses like ContentLibraryData ('lib') represent concrete resource types with their own namespaces.
NAMESPACE: ClassVar[str] = "global"

def __eq__(self, other: "ScopeData") -> bool:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but I'm not sure why the codecov report indicates that this method is uncovered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:o thanks for bringing this up! I'll look into it

Copy link
Copy Markdown
Member Author

@mariajgrimaldi mariajgrimaldi Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So @define already creates the correct eq, and since this is the parent of ContentLibraryData it gets overridden. I dropped it since we don't need it in this case :)

"""Compare scopes based on their external_key."""
return self.external_key == other.external_key

@classmethod
def validate_external_key(cls, _: str) -> bool:
"""Validate the external_key format for ScopeData.
Expand Down
2 changes: 1 addition & 1 deletion openedx_authz/api/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,6 @@ def get_scopes_for_subject_and_permission(
scopes = []
for role_assignment in roles_for_subject:
for role in role_assignment.roles:
if permission in role.permissions:
if permission in role.permissions and role_assignment.scope not in scopes:
scopes.append(role_assignment.scope)
return scopes
35 changes: 35 additions & 0 deletions openedx_authz/tests/api/test_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,41 @@ def test_get_scopes_for_subject_and_permission(self, subject_name, action_name,
for expected_scope in expected_scope_names:
self.assertIn(expected_scope, actual_scope_names)

def test_get_scopes_for_subject_and_permission_no_duplicates(self):
"""Test that get_scopes_for_subject_and_permission returns no duplicate scopes.

This test verifies that when a subject has multiple roles in the same scope
that grant the same permission, the scope appears only once in the result.

Expected result:
- Each scope appears exactly once in the returned list
- No duplicate scopes even when multiple roles grant the same permission
"""
test_scope = "lib:TestOrg:duplicate_test"
test_subject = "test_user_duplicates"

assign_role_to_subject_in_scope(
SubjectData(external_key=test_subject),
RoleData(external_key=roles.LIBRARY_ADMIN.external_key),
ScopeData(external_key=test_scope),
)

assign_role_to_subject_in_scope(
SubjectData(external_key=test_subject),
RoleData(external_key=roles.LIBRARY_AUTHOR.external_key),
ScopeData(external_key=test_scope),
)

subject = SubjectData(external_key=test_subject)
permission = PermissionData(action=ActionData(external_key="view_library"))

scopes = get_scopes_for_subject_and_permission(subject, permission)
scope_external_keys = [scope.external_key for scope in scopes]

self.assertEqual(len(scope_external_keys), 1)
self.assertEqual(scope_external_keys[0], test_scope)
self.assertEqual(len(scope_external_keys), len(set(scope_external_keys)))

@ddt_data(
(roles.LIBRARY_AUTHOR.external_key, "lib:Org4:art_101", {"liam"}),
(roles.LIBRARY_AUTHOR.external_key, "lib:Org4:art_201", {"liam"}),
Expand Down