e2e: cleanup, use USER function instead of typing static strings

This commit is contained in:
Jens Langhammer 2020-06-20 23:52:06 +02:00
parent 4d81172a48
commit e4a9a84646
4 changed files with 34 additions and 32 deletions

View file

@ -1,12 +1,8 @@
"""Test 2-step enroll flow""" """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.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as ec 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.flows.models import Flow, FlowDesignation, FlowStageBinding
from passbook.policies.expression.models import ExpressionPolicy from passbook.policies.expression.models import ExpressionPolicy
from passbook.policies.models import PolicyBinding from passbook.policies.models import PolicyBinding
@ -80,8 +76,8 @@ class TestEnroll2Step(SeleniumTestCase):
self.driver.get(self.live_server_url) self.driver.get(self.live_server_url)
self.driver.find_element(By.CSS_SELECTOR, "[role=enroll]").click() 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_username").send_keys("foo")
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_repeat").send_keys("pbadmin") 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.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_name").send_keys("some name")
self.driver.find_element(By.ID, "id_email").send_keys("foo@bar.baz") self.driver.find_element(By.ID, "id_email").send_keys("foo@bar.baz")

View file

@ -1,10 +1,8 @@
"""test default login flow""" """test default login flow"""
from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from e2e.utils import SeleniumTestCase from e2e.utils import USER, SeleniumTestCase
class TestLogin(SeleniumTestCase): class TestLogin(SeleniumTestCase):
@ -14,11 +12,11 @@ class TestLogin(SeleniumTestCase):
"""test default login flow""" """test default login flow"""
self.driver.get(f"{self.live_server_url}/flows/default-authentication-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").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_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.ID, "id_password").send_keys(Keys.ENTER)
self.assertEqual( self.assertEqual(
self.driver.find_element(By.XPATH, "//a[contains(@href, '/-/user/')]").text, self.driver.find_element(By.XPATH, "//a[contains(@href, '/-/user/')]").text,
"pbadmin", USER().username,
) )

View file

@ -1,19 +1,16 @@
"""test OpenID Provider flow""" """test OpenID Provider flow"""
from time import sleep from time import sleep
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.shortcuts import reverse from django.shortcuts import reverse
from oauth2_provider.generators import generate_client_id, generate_client_secret from oauth2_provider.generators import generate_client_id, generate_client_secret
from oidc_provider.models import Client, ResponseType from oidc_provider.models import Client, ResponseType
from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from docker import DockerClient, from_env from docker import DockerClient, from_env
from docker.models.containers import Container from docker.models.containers import Container
from docker.types import Healthcheck 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.core.models import Application
from passbook.flows.models import Flow from passbook.flows.models import Flow
from passbook.providers.oidc.models import OpenIDProvider from passbook.providers.oidc.models import OpenIDProvider
@ -34,7 +31,6 @@ class TestProviderOIDC(SeleniumTestCase):
container = client.containers.run( container = client.containers.run(
image="grafana/grafana:latest", image="grafana/grafana:latest",
detach=True, detach=True,
name="passbook-e2e-grafana-client",
network_mode="host", network_mode="host",
auto_remove=True, auto_remove=True,
healthcheck=Healthcheck( healthcheck=Healthcheck(
@ -101,9 +97,9 @@ class TestProviderOIDC(SeleniumTestCase):
self.driver.get("http://localhost:3000") self.driver.get("http://localhost:3000")
self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click() 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").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_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.ID, "id_password").send_keys(Keys.ENTER)
sleep(2) sleep(2)
self.assertEqual( self.assertEqual(
@ -144,35 +140,35 @@ class TestProviderOIDC(SeleniumTestCase):
self.driver.get("http://localhost:3000") self.driver.get("http://localhost:3000")
self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click() 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").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_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.ID, "id_password").send_keys(Keys.ENTER)
self.driver.find_element(By.XPATH, "//a[contains(@href, '/profile')]").click() self.driver.find_element(By.XPATH, "//a[contains(@href, '/profile')]").click()
self.assertEqual( self.assertEqual(
self.driver.find_element(By.CLASS_NAME, "page-header__title").text, self.driver.find_element(By.CLASS_NAME, "page-header__title").text,
"passbook Default Admin", USER().name,
) )
self.assertEqual( self.assertEqual(
self.driver.find_element( self.driver.find_element(
By.XPATH, By.XPATH,
"/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[1]/div/input", "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[1]/div/input",
).get_attribute("value"), ).get_attribute("value"),
"passbook Default Admin", USER().name,
) )
self.assertEqual( self.assertEqual(
self.driver.find_element( self.driver.find_element(
By.XPATH, By.XPATH,
"/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[2]/div/input", "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[2]/div/input",
).get_attribute("value"), ).get_attribute("value"),
"root@localhost", USER().email,
) )
self.assertEqual( self.assertEqual(
self.driver.find_element( self.driver.find_element(
By.XPATH, By.XPATH,
"/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[3]/div/input", "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[3]/div/input",
).get_attribute("value"), ).get_attribute("value"),
"root@localhost", USER().email,
) )
def test_authorization_consent_explicit(self): def test_authorization_consent_explicit(self):
@ -208,9 +204,9 @@ class TestProviderOIDC(SeleniumTestCase):
self.driver.get("http://localhost:3000") self.driver.get("http://localhost:3000")
self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click() 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").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_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.ID, "id_password").send_keys(Keys.ENTER)
self.assertIn( self.assertIn(
@ -224,26 +220,26 @@ class TestProviderOIDC(SeleniumTestCase):
self.driver.find_element(By.XPATH, "//a[contains(@href, '/profile')]").click() self.driver.find_element(By.XPATH, "//a[contains(@href, '/profile')]").click()
self.assertEqual( self.assertEqual(
self.driver.find_element(By.CLASS_NAME, "page-header__title").text, self.driver.find_element(By.CLASS_NAME, "page-header__title").text,
"passbook Default Admin", USER().name,
) )
self.assertEqual( self.assertEqual(
self.driver.find_element( self.driver.find_element(
By.XPATH, By.XPATH,
"/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[1]/div/input", "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[1]/div/input",
).get_attribute("value"), ).get_attribute("value"),
"passbook Default Admin", USER().name,
) )
self.assertEqual( self.assertEqual(
self.driver.find_element( self.driver.find_element(
By.XPATH, By.XPATH,
"/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[2]/div/input", "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[2]/div/input",
).get_attribute("value"), ).get_attribute("value"),
"root@localhost", USER().email,
) )
self.assertEqual( self.assertEqual(
self.driver.find_element( self.driver.find_element(
By.XPATH, By.XPATH,
"/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[3]/div/input", "/html/body/grafana-app/div/div/div/react-profile-wrapper/form[1]/div[3]/div/input",
).get_attribute("value"), ).get_attribute("value"),
"root@localhost", USER().email,
) )

View file

@ -1,4 +1,5 @@
"""passbook e2e testing utilities""" """passbook e2e testing utilities"""
from functools import lru_cache
from glob import glob from glob import glob
from importlib.util import module_from_spec, spec_from_file_location from importlib.util import module_from_spec, spec_from_file_location
from inspect import getmembers, isfunction 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.remote.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait 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(): def ensure_rsa_key():
"""Ensure that at least one RSAKey Object exists, create one if none exist""" """Ensure that at least one RSAKey Object exists, create one if none exist"""
@ -25,6 +35,8 @@ def ensure_rsa_key():
class SeleniumTestCase(StaticLiveServerTestCase): class SeleniumTestCase(StaticLiveServerTestCase):
"""StaticLiveServerTestCase which automatically creates a Webdriver instance"""
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.driver = self._get_driver() self.driver = self._get_driver()