From e4a9a84646052f037c535f44ba5486f3c5c3fac3 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 20 Jun 2020 23:52:06 +0200 Subject: [PATCH] e2e: cleanup, use USER function instead of typing static strings --- e2e/test_enroll_2_step.py | 10 +++------- e2e/test_login_default.py | 10 ++++------ e2e/test_provider_oidc.py | 34 +++++++++++++++------------------- e2e/utils.py | 12 ++++++++++++ 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/e2e/test_enroll_2_step.py b/e2e/test_enroll_2_step.py index 111284475..c85915290 100644 --- a/e2e/test_enroll_2_step.py +++ b/e2e/test_enroll_2_step.py @@ -1,12 +1,8 @@ """Test 2-step enroll flow""" -from django.contrib.staticfiles.testing import StaticLiveServerTestCase -from selenium import webdriver from selenium.webdriver.common.by import By -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support import expected_conditions as ec -from selenium.webdriver.support.ui import WebDriverWait -from e2e.utils import SeleniumTestCase +from e2e.utils import USER, SeleniumTestCase from passbook.flows.models import Flow, FlowDesignation, FlowStageBinding from passbook.policies.expression.models import ExpressionPolicy from passbook.policies.models import PolicyBinding @@ -80,8 +76,8 @@ class TestEnroll2Step(SeleniumTestCase): self.driver.get(self.live_server_url) self.driver.find_element(By.CSS_SELECTOR, "[role=enroll]").click() self.driver.find_element(By.ID, "id_username").send_keys("foo") - self.driver.find_element(By.ID, "id_password").send_keys("pbadmin") - self.driver.find_element(By.ID, "id_password_repeat").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_password").send_keys(USER().username) + self.driver.find_element(By.ID, "id_password_repeat").send_keys(USER().username) self.driver.find_element(By.CSS_SELECTOR, ".pf-c-button").click() self.driver.find_element(By.ID, "id_name").send_keys("some name") self.driver.find_element(By.ID, "id_email").send_keys("foo@bar.baz") diff --git a/e2e/test_login_default.py b/e2e/test_login_default.py index 0925d7b3f..5b0d8bcad 100644 --- a/e2e/test_login_default.py +++ b/e2e/test_login_default.py @@ -1,10 +1,8 @@ """test default login flow""" -from selenium import webdriver from selenium.webdriver.common.by import By -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.keys import Keys -from e2e.utils import SeleniumTestCase +from e2e.utils import USER, SeleniumTestCase class TestLogin(SeleniumTestCase): @@ -14,11 +12,11 @@ class TestLogin(SeleniumTestCase): """test default login flow""" self.driver.get(f"{self.live_server_url}/flows/default-authentication-flow/") self.driver.find_element(By.ID, "id_uid_field").click() - self.driver.find_element(By.ID, "id_uid_field").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username) self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER) - self.driver.find_element(By.ID, "id_password").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_password").send_keys(USER().username) self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER) self.assertEqual( self.driver.find_element(By.XPATH, "//a[contains(@href, '/-/user/')]").text, - "pbadmin", + USER().username, ) diff --git a/e2e/test_provider_oidc.py b/e2e/test_provider_oidc.py index 9a3d4e598..82f37dfe5 100644 --- a/e2e/test_provider_oidc.py +++ b/e2e/test_provider_oidc.py @@ -1,19 +1,16 @@ """test OpenID Provider flow""" from time import sleep -from django.contrib.staticfiles.testing import StaticLiveServerTestCase from django.shortcuts import reverse from oauth2_provider.generators import generate_client_id, generate_client_secret from oidc_provider.models import Client, ResponseType -from selenium import webdriver from selenium.webdriver.common.by import By -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.keys import Keys from docker import DockerClient, from_env from docker.models.containers import Container from docker.types import Healthcheck -from e2e.utils import SeleniumTestCase, ensure_rsa_key +from e2e.utils import USER, SeleniumTestCase, ensure_rsa_key from passbook.core.models import Application from passbook.flows.models import Flow from passbook.providers.oidc.models import OpenIDProvider @@ -34,7 +31,6 @@ class TestProviderOIDC(SeleniumTestCase): container = client.containers.run( image="grafana/grafana:latest", detach=True, - name="passbook-e2e-grafana-client", network_mode="host", auto_remove=True, healthcheck=Healthcheck( @@ -101,9 +97,9 @@ class TestProviderOIDC(SeleniumTestCase): self.driver.get("http://localhost:3000") self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click() self.driver.find_element(By.ID, "id_uid_field").click() - self.driver.find_element(By.ID, "id_uid_field").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username) self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER) - self.driver.find_element(By.ID, "id_password").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_password").send_keys(USER().username) self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER) sleep(2) self.assertEqual( @@ -144,35 +140,35 @@ class TestProviderOIDC(SeleniumTestCase): self.driver.get("http://localhost:3000") self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click() self.driver.find_element(By.ID, "id_uid_field").click() - self.driver.find_element(By.ID, "id_uid_field").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username) self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER) - self.driver.find_element(By.ID, "id_password").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_password").send_keys(USER().username) self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER) self.driver.find_element(By.XPATH, "//a[contains(@href, '/profile')]").click() self.assertEqual( self.driver.find_element(By.CLASS_NAME, "page-header__title").text, - "passbook Default Admin", + USER().name, ) self.assertEqual( self.driver.find_element( By.XPATH, "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[1]/div/input", ).get_attribute("value"), - "passbook Default Admin", + USER().name, ) self.assertEqual( self.driver.find_element( By.XPATH, "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[2]/div/input", ).get_attribute("value"), - "root@localhost", + USER().email, ) self.assertEqual( self.driver.find_element( By.XPATH, "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[3]/div/input", ).get_attribute("value"), - "root@localhost", + USER().email, ) def test_authorization_consent_explicit(self): @@ -208,9 +204,9 @@ class TestProviderOIDC(SeleniumTestCase): self.driver.get("http://localhost:3000") self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click() self.driver.find_element(By.ID, "id_uid_field").click() - self.driver.find_element(By.ID, "id_uid_field").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username) self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER) - self.driver.find_element(By.ID, "id_password").send_keys("pbadmin") + self.driver.find_element(By.ID, "id_password").send_keys(USER().username) self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER) self.assertIn( @@ -224,26 +220,26 @@ class TestProviderOIDC(SeleniumTestCase): self.driver.find_element(By.XPATH, "//a[contains(@href, '/profile')]").click() self.assertEqual( self.driver.find_element(By.CLASS_NAME, "page-header__title").text, - "passbook Default Admin", + USER().name, ) self.assertEqual( self.driver.find_element( By.XPATH, "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[1]/div/input", ).get_attribute("value"), - "passbook Default Admin", + USER().name, ) self.assertEqual( self.driver.find_element( By.XPATH, "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[2]/div/input", ).get_attribute("value"), - "root@localhost", + USER().email, ) self.assertEqual( self.driver.find_element( By.XPATH, "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[3]/div/input", ).get_attribute("value"), - "root@localhost", + USER().email, ) diff --git a/e2e/utils.py b/e2e/utils.py index 7577ac581..d01c894c3 100644 --- a/e2e/utils.py +++ b/e2e/utils.py @@ -1,4 +1,5 @@ """passbook e2e testing utilities""" +from functools import lru_cache from glob import glob from importlib.util import module_from_spec, spec_from_file_location from inspect import getmembers, isfunction @@ -13,6 +14,15 @@ from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.ui import WebDriverWait +from passbook.core.models import User + + +@lru_cache +# pylint: disable=invalid-name +def USER() -> User: + """Cached function that always returns pbadmin""" + return User.objects.get(username="pbadmin") + def ensure_rsa_key(): """Ensure that at least one RSAKey Object exists, create one if none exist""" @@ -25,6 +35,8 @@ def ensure_rsa_key(): class SeleniumTestCase(StaticLiveServerTestCase): + """StaticLiveServerTestCase which automatically creates a Webdriver instance""" + def setUp(self): super().setUp() self.driver = self._get_driver()