@@ -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
13321366class TestFieldIndexAndValues (TestCase ):
0 commit comments