2020-06-07 18:30:56 +01:00
|
|
|
"""passbook e2e testing utilities"""
|
2020-06-20 22:52:06 +01:00
|
|
|
from functools import lru_cache
|
2020-06-07 18:30:56 +01:00
|
|
|
from glob import glob
|
2020-06-19 18:34:27 +01:00
|
|
|
from importlib.util import module_from_spec, spec_from_file_location
|
2020-06-07 18:30:56 +01:00
|
|
|
from inspect import getmembers, isfunction
|
2020-06-21 14:09:01 +01:00
|
|
|
from os import makedirs
|
2020-06-21 19:46:38 +01:00
|
|
|
from time import time
|
2020-06-19 18:34:27 +01:00
|
|
|
|
|
|
|
from Cryptodome.PublicKey import RSA
|
2020-06-07 18:30:56 +01:00
|
|
|
from django.apps import apps
|
2020-06-20 16:06:00 +01:00
|
|
|
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
2020-06-07 18:30:56 +01:00
|
|
|
from django.db import connection, transaction
|
|
|
|
from django.db.utils import IntegrityError
|
2020-06-20 22:56:35 +01:00
|
|
|
from django.shortcuts import reverse
|
2020-06-20 16:06:00 +01:00
|
|
|
from selenium import webdriver
|
|
|
|
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
|
|
from selenium.webdriver.remote.webdriver import WebDriver
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
2020-06-26 14:06:46 +01:00
|
|
|
from structlog import get_logger
|
2020-06-19 18:34:27 +01:00
|
|
|
|
2020-06-20 22:52:06 +01:00
|
|
|
from passbook.core.models import User
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache
|
|
|
|
# pylint: disable=invalid-name
|
2020-06-20 23:26:29 +01:00
|
|
|
def USER() -> User: # noqa
|
2020-06-20 22:52:06 +01:00
|
|
|
"""Cached function that always returns pbadmin"""
|
|
|
|
return User.objects.get(username="pbadmin")
|
|
|
|
|
2020-06-19 18:34:27 +01:00
|
|
|
|
|
|
|
def ensure_rsa_key():
|
|
|
|
"""Ensure that at least one RSAKey Object exists, create one if none exist"""
|
|
|
|
from oidc_provider.models import RSAKey
|
|
|
|
|
|
|
|
if not RSAKey.objects.exists():
|
|
|
|
key = RSA.generate(2048)
|
|
|
|
rsakey = RSAKey(key=key.exportKey("PEM").decode("utf8"))
|
|
|
|
rsakey.save()
|
2020-06-20 16:06:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SeleniumTestCase(StaticLiveServerTestCase):
|
2020-06-20 22:52:06 +01:00
|
|
|
"""StaticLiveServerTestCase which automatically creates a Webdriver instance"""
|
|
|
|
|
2020-06-20 16:06:00 +01:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2020-07-02 20:55:02 +01:00
|
|
|
makedirs("selenium_screenshots/", exist_ok=True)
|
2020-06-20 16:06:00 +01:00
|
|
|
self.driver = self._get_driver()
|
2020-06-20 23:26:29 +01:00
|
|
|
self.driver.maximize_window()
|
2020-06-30 15:36:30 +01:00
|
|
|
self.driver.implicitly_wait(300)
|
|
|
|
self.wait = WebDriverWait(self.driver, 500)
|
2020-06-20 16:06:00 +01:00
|
|
|
self.apply_default_data()
|
2020-06-26 14:06:46 +01:00
|
|
|
self.logger = get_logger()
|
2020-06-20 16:06:00 +01:00
|
|
|
|
|
|
|
def _get_driver(self) -> WebDriver:
|
|
|
|
return webdriver.Remote(
|
|
|
|
command_executor="http://localhost:4444/wd/hub",
|
|
|
|
desired_capabilities=DesiredCapabilities.CHROME,
|
|
|
|
)
|
|
|
|
|
|
|
|
def tearDown(self):
|
2020-07-02 20:55:02 +01:00
|
|
|
self.driver.save_screenshot(f"selenium_screenshots/{self.__class__.__name__}_{time()}.png")
|
2020-06-26 14:06:46 +01:00
|
|
|
for line in self.driver.get_log("browser"):
|
|
|
|
self.logger.warning(
|
|
|
|
line["message"], source=line["source"], level=line["level"]
|
|
|
|
)
|
2020-06-20 16:06:00 +01:00
|
|
|
self.driver.quit()
|
2020-06-20 16:06:15 +01:00
|
|
|
super().tearDown()
|
2020-06-20 16:06:00 +01:00
|
|
|
|
2020-06-26 15:21:59 +01:00
|
|
|
def wait_for_url(self, desired_url):
|
|
|
|
"""Wait until URL is `desired_url`."""
|
2020-06-30 15:36:30 +01:00
|
|
|
self.wait.until(
|
|
|
|
lambda driver: driver.current_url == desired_url,
|
|
|
|
f"URL {self.driver.current_url} doesn't match expected URL {desired_url}",
|
|
|
|
)
|
2020-06-26 15:21:59 +01:00
|
|
|
|
2020-06-20 22:56:35 +01:00
|
|
|
def url(self, view, **kwargs) -> str:
|
|
|
|
"""reverse `view` with `**kwargs` into full URL using live_server_url"""
|
|
|
|
return self.live_server_url + reverse(view, kwargs=kwargs)
|
|
|
|
|
2020-06-20 16:06:00 +01:00
|
|
|
def apply_default_data(self):
|
|
|
|
"""apply objects created by migrations after tables have been truncated"""
|
|
|
|
# Find all migration files
|
|
|
|
# load all functions
|
|
|
|
migration_files = glob("**/migrations/*.py", recursive=True)
|
|
|
|
matches = []
|
|
|
|
for migration in migration_files:
|
|
|
|
with open(migration, "r+") as migration_file:
|
|
|
|
# Check if they have a `RunPython`
|
|
|
|
if "RunPython" in migration_file.read():
|
|
|
|
matches.append(migration)
|
|
|
|
|
|
|
|
with connection.schema_editor() as schema_editor:
|
|
|
|
for match in matches:
|
|
|
|
# Load module from file path
|
|
|
|
spec = spec_from_file_location("", match)
|
|
|
|
migration_module = module_from_spec(spec)
|
|
|
|
# pyright: reportGeneralTypeIssues=false
|
|
|
|
spec.loader.exec_module(migration_module)
|
|
|
|
# Call all functions from module
|
|
|
|
for _, func in getmembers(migration_module, isfunction):
|
|
|
|
with transaction.atomic():
|
|
|
|
try:
|
|
|
|
func(apps, schema_editor)
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|