jason810496 commented on code in PR #62737:
URL: https://github.com/apache/airflow/pull/62737#discussion_r2887378593
##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -1739,10 +1740,34 @@ def add_permission_to_role(self, role: Role,
permission: Permission | None) -> N
self.session.merge(role)
self.session.commit()
log.info(const.LOGMSG_INF_SEC_ADD_PERMROLE, permission,
role.name)
+ except IntegrityError as e:
+ self.session.rollback()
+ if self._is_permission_assigned_to_role(role_id=role.id,
permission_view_id=permission.id):
+ log.debug("Permission '%s' already assigned to role '%s'",
permission, role.name)
Review Comment:
Perhaps `info` level logging would be better?
##########
providers/fab/tests/unit/fab/auth_manager/security_manager/test_override.py:
##########
@@ -32,6 +34,46 @@ def __init__(self):
class TestFabAirflowSecurityManagerOverride:
+
@mock.patch("airflow.providers.fab.auth_manager.security_manager.override.log")
+ def
test_add_permission_to_role_ignores_duplicate_from_concurrent_worker(self,
mock_log):
+ sm = EmptySecurityManager()
+ role = Mock(id=1, name="test_admin", permissions=[])
+ permission = Mock(id=2)
+
+ mock_session = Mock()
+ mock_session.commit.side_effect = IntegrityError("stmt", {},
Exception("Duplicate entry"))
+
+ sm._is_permission_assigned_to_role = Mock(return_value=True)
+
+ with mock.patch.object(EmptySecurityManager, "session", mock_session):
+ sm.add_permission_to_role(role, permission)
+
+ assert mock_session.rollback.mock_calls == [call()]
+ assert sm._is_permission_assigned_to_role.mock_calls ==
[call(role_id=1, permission_view_id=2)]
+ assert mock_log.error.mock_calls == []
+
Review Comment:
> The new tests use several unspecced `Mock()` instances (e.g., `role`,
`permission`, `mock_session`). Using `spec`/`autospec` (especially for the
session and role objects) would prevent the test from passing with invalid
attribute/method usage and aligns better with Airflow's testing expectations.
We haven't resolved this comment by Copilot yet
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]