From e7ded8b1e750d92d7325362efa6d91495228d64e Mon Sep 17 00:00:00 2001 From: Alejandro Rojas Date: Wed, 26 Jun 2019 15:53:55 +0200 Subject: [PATCH] [AIRFLOW-5387] Handle showPaused URL param properly --- airflow/www/utils.py | 17 +++++++++++++++-- tests/www/test_utils.py | 42 +++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/airflow/www/utils.py b/airflow/www/utils.py index 4902a3c1daca9..1422ac2f0187b 100644 --- a/airflow/www/utils.py +++ b/airflow/www/utils.py @@ -65,13 +65,26 @@ def should_hide_value_for_key(key_name): def get_params(**kwargs): + hide_paused_dags_by_default = conf.getboolean('webserver', + 'hide_paused_dags_by_default') if 'showPaused' in kwargs: - v = kwargs['showPaused'] - if v or v is None: + show_paused_dags_url_param = kwargs['showPaused'] + if _should_remove_show_paused_from_url_params( + show_paused_dags_url_param, + hide_paused_dags_by_default + ): kwargs.pop('showPaused') return urlencode({d: v if v is not None else '' for d, v in kwargs.items()}) +def _should_remove_show_paused_from_url_params(show_paused_dags_url_param, + hide_paused_dags_by_default): + return any([ + show_paused_dags_url_param != hide_paused_dags_by_default, + show_paused_dags_url_param is None + ]) + + def generate_pages(current_page, num_of_pages, search=None, showPaused=None, window=7): """ diff --git a/tests/www/test_utils.py b/tests/www/test_utils.py index 002d19eda03b6..a5e519b8ec775 100644 --- a/tests/www/test_utils.py +++ b/tests/www/test_utils.py @@ -23,8 +23,10 @@ from urllib.parse import parse_qs from bs4 import BeautifulSoup +from parameterized import parameterized from airflow.www import utils +from tests.test_utils.config import conf_vars class TestUtils(unittest.TestCase): @@ -103,14 +105,38 @@ def test_params_search(self): self.assertEqual('search=bash_', utils.get_params(search='bash_')) - def test_params_showPaused_true(self): - """Should detect True as default for showPaused""" - self.assertEqual('', - utils.get_params(showPaused=True)) - - def test_params_showPaused_false(self): - self.assertEqual('showPaused=False', - utils.get_params(showPaused=False)) + @parameterized.expand([ + (True, False, ''), + (False, True, ''), + (True, True, 'showPaused=True'), + (False, False, 'showPaused=False'), + (None, True, ''), + (None, False, ''), + ]) + def test_params_showPaused(self, show_paused, hide_by_default, expected_result): + with conf_vars({('webserver', 'hide_paused_dags_by_default'): str(hide_by_default)}): + self.assertEqual(expected_result, + utils.get_params(showPaused=show_paused)) + + @parameterized.expand([ + (True, False, True), + (False, True, True), + (True, True, False), + (False, False, False), + (None, True, True), + (None, False, True), + ]) + def test_should_remove_show_paused_from_url_params(self, show_paused, + hide_by_default, expected_result): + with conf_vars({('webserver', 'hide_paused_dags_by_default'): str(hide_by_default)}): + + self.assertEqual( + expected_result, + utils._should_remove_show_paused_from_url_params( + show_paused, + hide_by_default + ) + ) def test_params_none_and_zero(self): qs = utils.get_params(a=0, b=None)