vincbeck commented on code in PR #76746:
URL: https://github.com/apache/airflow/pull/76746#discussion_r4591159865


##########
providers/fab/tests/unit/fab/auth_manager/security_manager/test_override.py:
##########
@@ -189,6 +189,99 @@ def 
test_add_role_returns_existing_on_concurrent_insert(self, mock_log):
         assert mock_session.rollback.called
         assert mock_log.error.call_count == 0
 
+    
@mock.patch("airflow.providers.fab.auth_manager.security_manager.override.log")
+    def test_add_user_returns_none_when_commit_fails(self, mock_log):
+        sm = EmptySecurityManager()
+        sm._hash_password = Mock(return_value="hashed")
+
+        mock_session = Mock(spec=Session)
+        mock_session.commit.side_effect = IntegrityError("stmt", {}, 
Exception("Duplicate entry"))
+
+        with mock.patch.object(EmptySecurityManager, "session", mock_session):
+            result = sm.add_user("alice", "Alice", "Smith", 
"[email protected]", password="secret")
+
+        assert result is None
+        mock_session.rollback.assert_called_once()
+        mock_log.error.assert_called_once()
+
+    @mock.patch.object(EmptySecurityManager, "auth_user_registration", 
new_callable=mock.PropertyMock)
+    def test_auth_user_oauth_returns_none_when_registration_fails(self, 
mock_auth_user_registration):
+        mock_auth_user_registration.return_value = True
+        sm = EmptySecurityManager()
+        sm.find_user = Mock(return_value=None)
+        sm._oauth_calculate_user_roles = Mock(return_value=[])
+        sm._hash_password = Mock(return_value="hashed")
+
+        mock_session = Mock(spec=Session)
+        mock_session.commit.side_effect = IntegrityError("stmt", {}, 
Exception("Duplicate entry"))
+
+        with mock.patch.object(EmptySecurityManager, "session", mock_session):
+            result = sm.auth_user_oauth({"username": "alice", "email": 
"[email protected]"})
+
+        assert result is None
+        mock_session.rollback.assert_called_once()
+
+    @mock.patch.object(
+        EmptySecurityManager,
+        "auth_user_registration_role",
+        new_callable=mock.PropertyMock,
+        return_value="User",
+    )
+    @mock.patch.object(
+        EmptySecurityManager, "auth_user_registration", 
new_callable=mock.PropertyMock, return_value=True
+    )
+    def test_auth_user_remote_user_returns_none_when_registration_fails(self, 
_, __):

Review Comment:
   nit: this test and 
`test_auth_user_remote_user_uses_user_created_by_parallel_request` only differ 
by what the second `find_user` returns, can you merge them into one 
parametrized test?



##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -2559,6 +2559,12 @@ def auth_user_remote_user(self, username) -> User | None:
                 email=username + "@email.notfound",
                 role=self.find_role(self.auth_user_registration_role),
             )
+            if user is None:
+                # a parallel first request for the same REMOTE_USER can win 
the insert

Review Comment:
   LDAP and OAuth auto-registration have the same race, e.g. a script making 
parallel basic auth API calls for a new LDAP user gets a 401 on all but one. 
Should we do the same there? Fine as a follow-up too.



##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -2559,6 +2559,12 @@ def auth_user_remote_user(self, username) -> User | None:
                 email=username + "@email.notfound",
                 role=self.find_role(self.auth_user_registration_role),
             )
+            if user is None:

Review Comment:
   nit: this duplicates the check below. You could do `user = 
self.add_user(...) or self.find_user(username=username)` and turn the `elif` 
into a plain `if user is None or not user.is_active:`, so one check covers both 
cases.



-- 
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]

Reply via email to