You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by js...@apache.org on 2024/10/01 19:23:49 UTC

(airflow) branch main updated: Prevent redirect loop on /home with tags/lastrun filters (#42607) (#42609)

This is an automated email from the ASF dual-hosted git repository.

jscheffl pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new be2e48ef41 Prevent redirect loop on /home with tags/lastrun filters (#42607) (#42609)
be2e48ef41 is described below

commit be2e48ef41327c86474e91b4283826bf2c27ebfc
Author: Julian Maicher <jm...@users.noreply.github.com>
AuthorDate: Tue Oct 1 21:23:40 2024 +0200

    Prevent redirect loop on /home with tags/lastrun filters (#42607) (#42609)
    
    Closes #42607
---
 airflow/www/views.py               | 17 ++++++++++-------
 tests/www/views/test_views_home.py | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/airflow/www/views.py b/airflow/www/views.py
index b3300b517e..a361b9bd50 100644
--- a/airflow/www/views.py
+++ b/airflow/www/views.py
@@ -814,19 +814,22 @@ class Airflow(AirflowBaseView):
             return redirect(url_for("Airflow.index"))
 
         filter_tags_cookie_val = flask_session.get(FILTER_TAGS_COOKIE)
+        filter_lastrun_cookie_val = flask_session.get(FILTER_LASTRUN_COOKIE)
+
+        # update filter args in url from session values if needed
+        if (not arg_tags_filter and filter_tags_cookie_val) or (
+            not arg_lastrun_filter and filter_lastrun_cookie_val
+        ):
+            tags = arg_tags_filter or (filter_tags_cookie_val and filter_tags_cookie_val.split(","))
+            lastrun = arg_lastrun_filter or filter_lastrun_cookie_val
+            return redirect(url_for("Airflow.index", tags=tags, lastrun=lastrun))
+
         if arg_tags_filter:
             flask_session[FILTER_TAGS_COOKIE] = ",".join(arg_tags_filter)
-        elif filter_tags_cookie_val:
-            # If tags exist in cookie, but not URL, add them to the URL
-            return redirect(url_for("Airflow.index", tags=filter_tags_cookie_val.split(",")))
 
-        filter_lastrun_cookie_val = flask_session.get(FILTER_LASTRUN_COOKIE)
         if arg_lastrun_filter:
             arg_lastrun_filter = arg_lastrun_filter.strip().lower()
             flask_session[FILTER_LASTRUN_COOKIE] = arg_lastrun_filter
-        elif filter_lastrun_cookie_val:
-            # If tags exist in cookie, but not URL, add them to the URL
-            return redirect(url_for("Airflow.index", lastrun=filter_lastrun_cookie_val))
 
         if arg_status_filter is None:
             filter_status_cookie_val = flask_session.get(FILTER_STATUS_COOKIE)
diff --git a/tests/www/views/test_views_home.py b/tests/www/views/test_views_home.py
index ddec0c0bcf..44dda24fee 100644
--- a/tests/www/views/test_views_home.py
+++ b/tests/www/views/test_views_home.py
@@ -466,3 +466,41 @@ def test_analytics_pixel(user_client, is_enabled, should_have_pixel):
         check_content_in_response("apacheairflow.gateway.scarf.sh", resp)
     else:
         check_content_not_in_response("apacheairflow.gateway.scarf.sh", resp)
+
+
+@pytest.mark.parametrize(
+    "url, filter_tags_cookie_val, filter_lastrun_cookie_val, expected_filter_tags, expected_filter_lastrun",
+    [
+        ("home", None, None, [], None),
+        # from url only
+        ("home?tags=example&tags=test", None, None, ["example", "test"], None),
+        ("home?lastrun=running", None, None, [], "running"),
+        ("home?tags=example&tags=test&lastrun=running", None, None, ["example", "test"], "running"),
+        # from cookie only
+        ("home", "example,test", None, ["example", "test"], None),
+        ("home", None, "running", [], "running"),
+        ("home", "example,test", "running", ["example", "test"], "running"),
+        # from url and cookie
+        ("home?tags=example", "example,test", None, ["example"], None),
+        ("home?lastrun=failed", None, "running", [], "failed"),
+        ("home?tags=example", None, "running", ["example"], "running"),
+        ("home?lastrun=running", "example,test", None, ["example", "test"], "running"),
+        ("home?tags=example&lastrun=running", "example,test", "failed", ["example"], "running"),
+    ],
+)
+def test_filter_cookie_eval(
+    working_dags,
+    admin_client,
+    url,
+    filter_tags_cookie_val,
+    filter_lastrun_cookie_val,
+    expected_filter_tags,
+    expected_filter_lastrun,
+):
+    with admin_client.session_transaction() as flask_session:
+        flask_session[FILTER_TAGS_COOKIE] = filter_tags_cookie_val
+        flask_session[FILTER_LASTRUN_COOKIE] = filter_lastrun_cookie_val
+
+    resp = admin_client.get(url, follow_redirects=True)
+    assert resp.request.args.getlist("tags") == expected_filter_tags
+    assert resp.request.args.get("lastrun") == expected_filter_lastrun