From 0cdda11e9183c78f37710bc3bc01d9cf85476a25 Mon Sep 17 00:00:00 2001 From: Cory Francis Myers Date: Fri, 11 Oct 2024 15:25:53 -0700 Subject: [PATCH] refactor: move locale parameterization from tests to the firefox_web_driver fixture --- securedrop/tests/functional/conftest.py | 12 +++--- .../functional/pageslayout/test_journalist.py | 33 ++++++++--------- .../pageslayout/test_journalist_account.py | 17 ++++----- .../pageslayout/test_journalist_admin.py | 28 +++++++------- .../pageslayout/test_journalist_col.py | 15 ++++---- .../pageslayout/test_journalist_delete.py | 37 ++++++++----------- .../test_submit_and_retrieve_file.py | 9 ++--- securedrop/tests/functional/web_drivers.py | 4 ++ 8 files changed, 74 insertions(+), 81 deletions(-) diff --git a/securedrop/tests/functional/conftest.py b/securedrop/tests/functional/conftest.py index 7395a23a0cc..ecd72d33fc7 100644 --- a/securedrop/tests/functional/conftest.py +++ b/securedrop/tests/functional/conftest.py @@ -21,12 +21,14 @@ from tests.utils.i18n import get_test_locales -# Function-scoped so that tests can be run in parallel if needed -@pytest.fixture -def firefox_web_driver(locale: str = "en_US") -> WebDriver: # type: ignore +# Function-scoped so that tests can be run in parallel if needed. The fixture +# needs to know the locale at setup time, so we do that parameterization here +# rather than at the test level. +@pytest.fixture(params=get_test_locales()) +def firefox_web_driver(request) -> WebDriver: # type: ignore + locale = request.param.replace("_", "-") with get_web_driver( - web_driver_type=WebDriverTypeEnum.FIREFOX, - accept_languages=locale.replace("_", "-"), + web_driver_type=WebDriverTypeEnum.FIREFOX, accept_languages=locale ) as web_driver: yield web_driver diff --git a/securedrop/tests/functional/pageslayout/test_journalist.py b/securedrop/tests/functional/pageslayout/test_journalist.py index f05a91c73bd..cce48d35e0d 100644 --- a/securedrop/tests/functional/pageslayout/test_journalist.py +++ b/securedrop/tests/functional/pageslayout/test_journalist.py @@ -17,20 +17,19 @@ # import pytest from tests.functional.app_navigators.journalist_app_nav import JournalistAppNavigator -from tests.functional.pageslayout.utils import list_locales, save_static_data +from tests.functional.pageslayout.utils import save_static_data -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestJournalistLayout: - def test_login_index_and_edit(self, locale, sd_servers, firefox_web_driver): + def test_login_index_and_edit(self, sd_servers, firefox_web_driver, request): + locale = firefox_web_driver.locale # Given an SD server # And a journalist accessing the journalist interface - locale_with_commas = locale.replace("_", "-") journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.driver.get(f"{sd_servers.journalist_app_base_url}/login") journ_app_nav.got_expected_language(locale) @@ -55,14 +54,14 @@ def test_login_index_and_edit(self, locale, sd_servers, firefox_web_driver): journ_app_nav.journalist_visits_edit_account() save_static_data(journ_app_nav.driver, locale, "journalist-edit_account_user") - def test_index_entered_text(self, locale, sd_servers, firefox_web_driver): + def test_index_entered_text(self, sd_servers, firefox_web_driver): # Given an SD server # And a journalist accessing the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) # Take a screenshot of the login page with the form completed @@ -75,15 +74,15 @@ def test_index_entered_text(self, locale, sd_servers, firefox_web_driver): save_static_data(journ_app_nav.driver, locale, "journalist-index_with_text") def test_index_with_submission_and_select_documents( - self, locale, sd_servers_with_submitted_file, firefox_web_driver + self, sd_servers_with_submitted_file, firefox_web_driver ): # Given an SD server with an already-submitted file # And a journalist logging into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) # Take a screenshot of the index page when there is a source and submission @@ -116,27 +115,27 @@ def test_index_with_submission_and_select_documents( ) save_static_data(journ_app_nav.driver, locale, "journalist-composes_reply") - def test_fail_to_visit_admin(self, locale, sd_servers, firefox_web_driver): + def test_fail_to_visit_admin(self, sd_servers, firefox_web_driver): # Given an SD server # And someone accessing the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) # Take a screenshot of them trying to force-browse to the admin interface journ_app_nav.driver.get(f"{sd_servers.journalist_app_base_url}/admin") save_static_data(journ_app_nav.driver, locale, "journalist-code-fail_to_visit_admin") - def test_fail_login(self, locale, sd_servers, firefox_web_driver): + def test_fail_login(self, sd_servers, firefox_web_driver): # Given an SD server # And someone accessing the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) # Take a screenshot of trying to log in using invalid credentials diff --git a/securedrop/tests/functional/pageslayout/test_journalist_account.py b/securedrop/tests/functional/pageslayout/test_journalist_account.py index 887ff0ba380..4e18a3449ed 100644 --- a/securedrop/tests/functional/pageslayout/test_journalist_account.py +++ b/securedrop/tests/functional/pageslayout/test_journalist_account.py @@ -21,22 +21,21 @@ from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from tests.functional.app_navigators.journalist_app_nav import JournalistAppNavigator -from tests.functional.pageslayout.utils import list_locales, save_static_data +from tests.functional.pageslayout.utils import save_static_data -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestJournalistLayoutAccount: def test_account_edit_and_set_hotp_secret( - self, locale, sd_servers_with_clean_state, firefox_web_driver + self, sd_servers_with_clean_state, firefox_web_driver ): # Given an SD server # And a journalist logging into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_clean_state.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_clean_state.journalist_username, @@ -97,16 +96,14 @@ def explanatory_tooltip_is_correct() -> None: alert = journ_app_nav.driver.switch_to.alert alert.accept() - def test_account_new_two_factor_totp( - self, locale, sd_servers_with_clean_state, firefox_web_driver - ): + def test_account_new_two_factor_totp(self, sd_servers_with_clean_state, firefox_web_driver): # Given an SD server # And a journalist logging into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_clean_state.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_clean_state.journalist_username, diff --git a/securedrop/tests/functional/pageslayout/test_journalist_admin.py b/securedrop/tests/functional/pageslayout/test_journalist_admin.py index d2c8e7f188c..bccabcd1f01 100644 --- a/securedrop/tests/functional/pageslayout/test_journalist_admin.py +++ b/securedrop/tests/functional/pageslayout/test_journalist_admin.py @@ -25,23 +25,22 @@ from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from tests.functional.app_navigators.journalist_app_nav import JournalistAppNavigator -from tests.functional.pageslayout.utils import list_locales, save_static_data +from tests.functional.pageslayout.utils import save_static_data -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestAdminLayoutAddAndEditUser: def test_admin_adds_user_hotp_and_edits_hotp( - self, locale, sd_servers_with_clean_state, firefox_web_driver + self, sd_servers_with_clean_state, firefox_web_driver ): # Given an SD server # And a journalist logging into the journalist interface as an admin assert sd_servers_with_clean_state.journalist_is_admin - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_clean_state.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_clean_state.journalist_username, @@ -118,16 +117,16 @@ def _admin_visits_reset_2fa_hotp_step() -> None: ) def test_admin_adds_user_totp_and_edits_totp( - self, locale, sd_servers_with_clean_state, firefox_web_driver + self, sd_servers_with_clean_state, firefox_web_driver ): # Given an SD server # And a journalist logging into the journalist interface as an admin assert sd_servers_with_clean_state.journalist_is_admin - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_clean_state.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_clean_state.journalist_username, @@ -223,18 +222,17 @@ def _retry_2fa_pop_ups( logging.info("Selenium has failed to click; retrying.") -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestAdminLayoutEditConfig: - def test_admin_changes_logo(self, locale, sd_servers_with_clean_state, firefox_web_driver): + def test_admin_changes_logo(self, sd_servers_with_clean_state, firefox_web_driver): # Given an SD server # And a journalist logging into the journalist interface as an admin assert sd_servers_with_clean_state.journalist_is_admin - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_clean_state.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_clean_state.journalist_username, @@ -264,15 +262,15 @@ def updated_image() -> None: # Take a screenshot save_static_data(journ_app_nav.driver, locale, "journalist-admin_changes_logo_image") - def test_ossec_alert_button(self, locale, sd_servers, firefox_web_driver): + def test_ossec_alert_button(self, sd_servers, firefox_web_driver): # Given an SD server # And a journalist logging into the journalist interface as an admin assert sd_servers.journalist_is_admin - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers.journalist_username, diff --git a/securedrop/tests/functional/pageslayout/test_journalist_col.py b/securedrop/tests/functional/pageslayout/test_journalist_col.py index fd39240ff89..b8e5e00e849 100644 --- a/securedrop/tests/functional/pageslayout/test_journalist_col.py +++ b/securedrop/tests/functional/pageslayout/test_journalist_col.py @@ -24,7 +24,7 @@ from tests.factories import SecureDropConfigFactory from tests.functional.app_navigators.journalist_app_nav import JournalistAppNavigator from tests.functional.conftest import SdServersFixtureResult, spawn_sd_servers -from tests.functional.pageslayout.utils import list_locales, save_static_data +from tests.functional.pageslayout.utils import save_static_data def _create_source_and_submission_and_delete_source_key(config_in_use: SecureDropConfig) -> None: @@ -63,19 +63,18 @@ def sd_servers_with_deleted_source_key( yield sd_servers_result -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestJournalistLayoutCol: def test_col_with_and_without_documents( - self, locale, sd_servers_with_submitted_file, firefox_web_driver + self, sd_servers_with_submitted_file, firefox_web_driver ): # Given an SD server with an already-submitted file # And a journalist logging into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_submitted_file.journalist_username, @@ -105,14 +104,14 @@ def submission_deleted() -> None: journ_app_nav.nav_helper.wait_for(submission_deleted) save_static_data(journ_app_nav.driver, locale, "journalist-col_no_document") - def test_col_has_no_key(self, locale, sd_servers_with_deleted_source_key, firefox_web_driver): + def test_col_has_no_key(self, sd_servers_with_deleted_source_key, firefox_web_driver): # Given an SD server with an already-submitted file, but the source's key was deleted # And a journalist logging into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_deleted_source_key.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_deleted_source_key.journalist_username, diff --git a/securedrop/tests/functional/pageslayout/test_journalist_delete.py b/securedrop/tests/functional/pageslayout/test_journalist_delete.py index 43b52494623..4f0cc487c35 100644 --- a/securedrop/tests/functional/pageslayout/test_journalist_delete.py +++ b/securedrop/tests/functional/pageslayout/test_journalist_delete.py @@ -17,20 +17,19 @@ # import pytest from tests.functional.app_navigators.journalist_app_nav import JournalistAppNavigator -from tests.functional.pageslayout.utils import list_locales, save_static_data +from tests.functional.pageslayout.utils import save_static_data -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestJournalistLayoutDelete: - def test_delete_none(self, locale, sd_servers_with_submitted_file, firefox_web_driver): + def test_delete_none(self, sd_servers_with_submitted_file, firefox_web_driver): # Given an SD server with a file submitted by a source # And a journalist logged into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_submitted_file.journalist_username, @@ -46,16 +45,14 @@ def test_delete_none(self, locale, sd_servers_with_submitted_file, firefox_web_d journ_app_nav.journalist_confirm_delete_selected() save_static_data(journ_app_nav.driver, locale, "journalist-delete_none") - def test_delete_one_confirmation( - self, locale, sd_servers_with_submitted_file, firefox_web_driver - ): + def test_delete_one_confirmation(self, sd_servers_with_submitted_file, firefox_web_driver): # Given an SD server with a file submitted by a source # And a journalist logged into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_submitted_file.journalist_username, @@ -73,16 +70,14 @@ def test_delete_one_confirmation( journ_app_nav.journalist_clicks_delete_selected_link() save_static_data(journ_app_nav.driver, locale, "journalist-delete_one_confirmation") - def test_delete_all_confirmation( - self, locale, sd_servers_with_submitted_file, firefox_web_driver - ): + def test_delete_all_confirmation(self, sd_servers_with_submitted_file, firefox_web_driver): # Given an SD server with a file submitted by a source # And a journalist logged into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_submitted_file.journalist_username, @@ -97,14 +92,14 @@ def test_delete_all_confirmation( journ_app_nav.journalist_clicks_delete_all_and_sees_confirmation() save_static_data(journ_app_nav.driver, locale, "journalist-delete_all_confirmation") - def test_delete_one(self, locale, sd_servers_with_submitted_file, firefox_web_driver): + def test_delete_one(self, sd_servers_with_submitted_file, firefox_web_driver): # Given an SD server with a file submitted by a source # And a journalist logged into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_submitted_file.journalist_username, @@ -124,14 +119,14 @@ def test_delete_one(self, locale, sd_servers_with_submitted_file, firefox_web_dr # Take a screenshot save_static_data(journ_app_nav.driver, locale, "journalist-delete_one") - def test_delete_all(self, locale, sd_servers_with_submitted_file, firefox_web_driver): + def test_delete_all(self, sd_servers_with_submitted_file, firefox_web_driver): # Given an SD server with a file submitted by a source # And a journalist logged into the journalist interface - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale journ_app_nav = JournalistAppNavigator( journalist_app_base_url=sd_servers_with_submitted_file.journalist_app_base_url, web_driver=firefox_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) journ_app_nav.journalist_logs_in( username=sd_servers_with_submitted_file.journalist_username, diff --git a/securedrop/tests/functional/pageslayout/test_submit_and_retrieve_file.py b/securedrop/tests/functional/pageslayout/test_submit_and_retrieve_file.py index 563254c0d40..425ec1d0d0e 100644 --- a/securedrop/tests/functional/pageslayout/test_submit_and_retrieve_file.py +++ b/securedrop/tests/functional/pageslayout/test_submit_and_retrieve_file.py @@ -3,21 +3,20 @@ from selenium.webdriver.common.by import By from tests.functional.app_navigators.journalist_app_nav import JournalistAppNavigator from tests.functional.app_navigators.source_app_nav import SourceAppNavigator -from tests.functional.pageslayout.utils import list_locales, save_static_data +from tests.functional.pageslayout.utils import save_static_data -@pytest.mark.parametrize("locale", list_locales()) @pytest.mark.pagelayout class TestSubmitAndRetrieveFile: def test_submit_and_retrieve_happy_path( - self, locale, sd_servers_with_clean_state, tor_browser_web_driver, firefox_web_driver + self, sd_servers_with_clean_state, tor_browser_web_driver, firefox_web_driver ): # Given a source user accessing the app from their browser - locale_with_commas = locale.replace("_", "-") + locale = firefox_web_driver.locale source_app_nav = SourceAppNavigator( source_app_base_url=sd_servers_with_clean_state.source_app_base_url, web_driver=tor_browser_web_driver, - accept_languages=locale_with_commas, + accept_languages=locale, ) # And they created an account diff --git a/securedrop/tests/functional/web_drivers.py b/securedrop/tests/functional/web_drivers.py index b7685b418e5..7af6f352d97 100644 --- a/securedrop/tests/functional/web_drivers.py +++ b/securedrop/tests/functional/web_drivers.py @@ -112,6 +112,10 @@ def _create_firefox_driver( if not firefox_driver: raise Exception("Could not create Firefox web driver") + # Add this attribute to the returned driver object so that tests using this + # fixture can know what locale it's parameterized with. + firefox_driver.locale = accept_languages or "en_US" # type: ignore[attr-defined] + return firefox_driver