ashb commented on code in PR #68261:
URL: https://github.com/apache/airflow/pull/68261#discussion_r3383120634


##########
airflow-core/tests/unit/api_fastapi/conftest.py:
##########
@@ -64,35 +74,72 @@ def test_client(request):
             ): 
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager",
         }
     ):
-        app = create_app()
-        auth_manager: SimpleAuthManager = app.state.auth_manager
-        # set time_very_before to 2014-01-01 00:00:00 and time_very_after to 
tomorrow
-        # to make the JWT token always valid for all test cases with 
time_machine
-        time_very_before = datetime.datetime(2014, 1, 1, 0, 0, 0)
-        time_after = datetime.datetime.now() + datetime.timedelta(days=1)
-        with time_machine.travel(time_very_before, tick=False):
-            token = auth_manager._get_token_signer(
-                expiration_time_in_seconds=(time_after - 
time_very_before).total_seconds()
-            ).generate(
-                auth_manager.serialize_user(
-                    SimpleAuthManagerUser(username="test", role="admin", 
teams=["team1"])
-                ),
-            )
-        with 
mock.patch("airflow.models.revoked_token.RevokedToken.is_revoked", 
return_value=False):
-            yield TestClient(
-                app,
-                headers={"Authorization": f"Bearer {token}"},
-                base_url=f"{BASE_URL}{get_api_path(request)}",
-            )
+        return create_app()
+
+
+def _reset_shared_app(app: FastAPI) -> None:
+    """
+    Reset mutable state on the session-shared app before each test.
+
+    The app object is reused for the whole session, so any per-test mutation 
of its ``state`` or
+    ``dependency_overrides`` would leak into later tests. These mutations 
exist today and were
+    harmless only because each test used to build its own app:
+
+    * the auth endpoint tests swap ``app.state.auth_manager`` for a mock 
without restoring it;
+    * several route tests (extra links, tasks, logs) install a 
``dag_bag_from_app`` dependency
+      override in their per-test setup and never pop it;
+    * ``app.state.dag_bag`` carries an LRU/TTL cache of deserialized Dags 
keyed by
+      ``dag_version_id``. A warm entry from an earlier test would let a later 
test skip the
+      serialized-Dag DB read, which silently breaks query-count assertions 
(e.g. the grid
+      ``ti_summaries`` stream tests) depending on execution order.
+
+    Restore the canonical auth manager, drop leftover overrides (including on 
mounted sub-apps),
+    and empty the Dag cache so each test starts from the pristine app and 
re-applies its own state.
+    """
+    app.state.auth_manager = create_auth_manager()

Review Comment:
   Make this a yield fixture, capture the keys and values in state at start and 
restore them after? That should make it more resilient. 
   
   I think we might also need to look at the app.state of sun mounted apps



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