fat-catTW commented on code in PR #76748:
URL: https://github.com/apache/airflow/pull/76748#discussion_r4599131578
##########
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py:
##########
@@ -241,6 +242,117 @@ def test_deserialize_user(self, flask_app,
auth_manager_with_appbuilder):
assert user.get_id() == result.get_id()
+ @pytest.mark.asyncio
+ async def test_deserialize_user_async(self, flask_app,
auth_manager_with_appbuilder):
+ user = create_user(flask_app, "async-test")
+ auth_manager_with_appbuilder.cache.clear()
+
+ result = await
auth_manager_with_appbuilder.deserialize_user_async({"sub": str(user.id)})
+
+ assert result.get_id() == user.get_id()
+ with assert_queries_count(0):
+ result = await
auth_manager_with_appbuilder.deserialize_user_async({"sub": str(user.id)})
+
+ assert result.get_id() == user.get_id()
+
+ @pytest.mark.asyncio
+ async def test_deserialize_user_async_rejects_unknown_user(self,
auth_manager_with_appbuilder):
+ with pytest.raises(ValueError, match="User with id 99999 not found"):
+ await auth_manager_with_appbuilder.deserialize_user_async({"sub":
"99999"})
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("active", [False, None])
+ async def test_deserialize_user_async_rejects_inactive_user(
+ self, flask_app, auth_manager_with_appbuilder, active
+ ):
+ user = create_user(flask_app, f"async-inactive-{active}")
+ user.active = active
+ auth_manager_with_appbuilder.session.commit()
+ auth_manager_with_appbuilder.cache.clear()
+
+ with pytest.raises(ValueError, match=f"User with id {user.id} is not
active"):
+ await auth_manager_with_appbuilder.deserialize_user_async({"sub":
str(user.id)})
+
+ @pytest.mark.skipif(
+ not hasattr(BaseAuthManager, "deserialize_user_async"),
+ reason="This Airflow version has no async user deserialization hook",
+ )
+ @mock.patch(
+
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager._get_token_validator",
+ autospec=True,
+ )
+ @pytest.mark.asyncio
+ async def test_get_user_from_token_uses_async_deserialization(
+ self, mock_get_token_validator, flask_app, auth_manager_with_appbuilder
+ ):
+ user = create_user(flask_app, "jwt-async-test")
+ auth_manager_with_appbuilder.cache.clear()
+ mock_get_token_validator.return_value.avalidated_claims =
mock.AsyncMock(
+ return_value={"sub": str(user.id)}
+ )
+
+ with mock.patch.object(auth_manager_with_appbuilder,
"deserialize_user") as mock_deserialize_user:
+ result = await
auth_manager_with_appbuilder.get_user_from_token("token")
+
+ assert result.get_id() == user.get_id()
+ mock_deserialize_user.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_deserialize_user_async_retries_once_after_db_error(self,
auth_manager_with_appbuilder):
+ user = Mock()
+ user.id = 99996
+ failed_session = MagicMock()
+ failed_session.scalars = mock.AsyncMock(
+ side_effect=OperationalError("connection dropped", None,
Exception())
+ )
+ retry_session = MagicMock()
+ retry_query_result = Mock()
+ retry_query_result.one.return_value = user
+ retry_session.scalars = mock.AsyncMock(return_value=retry_query_result)
+ auth_manager_with_appbuilder.cache.pop(user.id, None)
+
+ @asynccontextmanager
+ async def create_session(session):
+ yield session
+
+ with mock.patch(
Review Comment:
Updated.
Changed to decorator.
--
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]