Skip to content

Commit 969f795

Browse files
test: add test cases for when assignment is false
1 parent 4ef4aac commit 969f795

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

openedx_authz/models/core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77

88
from typing import ClassVar, NamedTuple
99

10-
from django.contrib.auth import get_user_model
1110
from django.db import models, transaction
1211

1312
from openedx_authz.constants import AUTHZ_POLICY_ATTRIBUTES_SEPARATOR
1413
from openedx_authz.engine.filter import Filter
1514

16-
User = get_user_model()
17-
1815
class BaseRegistryModel(models.Model):
1916
"""Base model that supports automatic subclass registration.
2017

openedx_authz/tests/api/test_roles.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,40 @@ def test_unassign_and_reassign_subject(self):
13271327
new_roles = {r.external_key for assignment in new_assignments for r in assignment.roles}
13281328
self.assertIn("library_admin", new_roles)
13291329

1330+
def test_no_event_sent_when_role_already_assigned(self):
1331+
"""No lifecycle event is emitted when the role assignment already exists.
1332+
1333+
Expected result:
1334+
- ``assign_role_to_subject_in_scope`` returns False.
1335+
- ``transaction.on_commit`` is not called.
1336+
"""
1337+
subject = SubjectData(external_key="alice")
1338+
role = RoleData(external_key=roles.LIBRARY_ADMIN.external_key)
1339+
scope = ScopeData(external_key="lib:Org1:math_101")
1340+
1341+
with patch("openedx_authz.api.roles.transaction.on_commit") as mock_on_commit:
1342+
result = assign_role_to_subject_in_scope(subject, role, scope)
1343+
1344+
self.assertFalse(result)
1345+
mock_on_commit.assert_not_called()
1346+
1347+
def test_no_event_sent_when_role_not_present_for_removal(self):
1348+
"""No lifecycle event is emitted when the role to remove does not exist.
1349+
1350+
Expected result:
1351+
- ``unassign_role_from_subject_in_scope`` returns False.
1352+
- ``transaction.on_commit`` is not called.
1353+
"""
1354+
subject = SubjectData(external_key="alice")
1355+
role = RoleData(external_key=roles.LIBRARY_USER.external_key)
1356+
scope = ScopeData(external_key="lib:Org1:math_101")
1357+
1358+
with patch("openedx_authz.api.roles.transaction.on_commit") as mock_on_commit:
1359+
result = unassign_role_from_subject_in_scope(subject, role, scope)
1360+
1361+
self.assertFalse(result)
1362+
mock_on_commit.assert_not_called()
1363+
13301364

13311365
@ddt
13321366
class TestFieldIndexAndValues(TestCase):

0 commit comments

Comments
 (0)