2020-12-05 21:08:42 +00:00
|
|
|
"""authentik e2e testing utilities"""
|
2020-11-23 13:24:42 +00:00
|
|
|
import json
|
2020-10-20 16:42:26 +00:00
|
|
|
from functools import wraps
|
2020-06-07 17:30:56 +00:00
|
|
|
from glob import glob
|
2020-06-19 17:34:27 +00:00
|
|
|
from importlib.util import module_from_spec, spec_from_file_location
|
2020-06-07 17:30:56 +00:00
|
|
|
from inspect import getmembers, isfunction
|
2020-07-12 14:17:04 +00:00
|
|
|
from os import environ, makedirs
|
2020-09-11 21:21:11 +00:00
|
|
|
from time import sleep, time
|
2020-10-20 16:42:26 +00:00
|
|
|
from typing import Any, Callable, Dict, Optional
|
2020-06-19 17:34:27 +00:00
|
|
|
|
2020-06-07 17:30:56 +00:00
|
|
|
from django.apps import apps
|
2020-06-20 15:06:00 +00:00
|
|
|
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
2020-06-07 17:30:56 +00:00
|
|
|
from django.db import connection, transaction
|
|
|
|
from django.db.utils import IntegrityError
|
2020-06-20 21:56:35 +00:00
|
|
|
from django.shortcuts import reverse
|
2020-10-22 12:05:29 +00:00
|
|
|
from django.test.testcases import TransactionTestCase
|
2020-09-11 21:21:11 +00:00
|
|
|
from docker import DockerClient, from_env
|
|
|
|
from docker.models.containers import Container
|
2020-06-20 15:06:00 +00:00
|
|
|
from selenium import webdriver
|
2020-12-06 15:17:51 +00:00
|
|
|
from selenium.common.exceptions import (
|
|
|
|
NoSuchElementException,
|
|
|
|
TimeoutException,
|
|
|
|
WebDriverException,
|
|
|
|
)
|
2020-11-23 13:24:42 +00:00
|
|
|
from selenium.webdriver.common.by import By
|
2020-06-20 15:06:00 +00:00
|
|
|
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
|
|
from selenium.webdriver.remote.webdriver import WebDriver
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
2021-01-01 14:39:43 +00:00
|
|
|
from structlog.stdlib import get_logger
|
2020-06-19 17:34:27 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.core.api.users import UserSerializer
|
|
|
|
from authentik.core.models import User
|
2021-02-03 20:18:31 +00:00
|
|
|
from authentik.managed.manager import ObjectManager
|
2020-06-20 21:52:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
2020-06-20 22:26:29 +00:00
|
|
|
def USER() -> User: # noqa
|
2020-12-05 21:08:42 +00:00
|
|
|
"""Cached function that always returns akadmin"""
|
|
|
|
return User.objects.get(username="akadmin")
|
2020-06-20 21:52:06 +00:00
|
|
|
|
2020-06-19 17:34:27 +00:00
|
|
|
|
2020-06-20 15:06:00 +00:00
|
|
|
class SeleniumTestCase(StaticLiveServerTestCase):
|
2020-06-20 21:52:06 +00:00
|
|
|
"""StaticLiveServerTestCase which automatically creates a Webdriver instance"""
|
|
|
|
|
2020-09-11 21:21:11 +00:00
|
|
|
container: Optional[Container] = None
|
|
|
|
|
2020-06-20 15:06:00 +00:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2020-07-02 19:55:02 +00:00
|
|
|
makedirs("selenium_screenshots/", exist_ok=True)
|
2020-06-20 15:06:00 +00:00
|
|
|
self.driver = self._get_driver()
|
2020-06-20 22:26:29 +00:00
|
|
|
self.driver.maximize_window()
|
2020-09-29 13:01:01 +00:00
|
|
|
self.driver.implicitly_wait(30)
|
|
|
|
self.wait = WebDriverWait(self.driver, 60)
|
2020-06-20 15:06:00 +00:00
|
|
|
self.apply_default_data()
|
2020-06-26 13:06:46 +00:00
|
|
|
self.logger = get_logger()
|
2020-09-11 21:21:11 +00:00
|
|
|
if specs := self.get_container_specs():
|
|
|
|
self.container = self._start_container(specs)
|
|
|
|
|
|
|
|
def _start_container(self, specs: Dict[str, Any]) -> Container:
|
|
|
|
client: DockerClient = from_env()
|
2020-09-29 12:04:23 +00:00
|
|
|
client.images.pull(specs["image"])
|
2020-09-11 21:21:11 +00:00
|
|
|
container = client.containers.run(**specs)
|
2020-09-16 19:54:35 +00:00
|
|
|
if "healthcheck" not in specs:
|
|
|
|
return container
|
2020-09-11 21:21:11 +00:00
|
|
|
while True:
|
|
|
|
container.reload()
|
|
|
|
status = container.attrs.get("State", {}).get("Health", {}).get("Status")
|
|
|
|
if status == "healthy":
|
|
|
|
return container
|
|
|
|
self.logger.info("Container failed healthcheck")
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
def get_container_specs(self) -> Optional[Dict[str, Any]]:
|
|
|
|
"""Optionally get container specs which will launched on setup, wait for the container to
|
|
|
|
be healthy, and deleted again on tearDown"""
|
|
|
|
return None
|
2020-06-20 15:06:00 +00: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-23 18:03:35 +00:00
|
|
|
if "TF_BUILD" in environ:
|
2020-07-12 14:17:04 +00:00
|
|
|
screenshot_file = (
|
|
|
|
f"selenium_screenshots/{self.__class__.__name__}_{time()}.png"
|
|
|
|
)
|
|
|
|
self.driver.save_screenshot(screenshot_file)
|
|
|
|
self.logger.warning("Saved screenshot", file=screenshot_file)
|
2020-06-26 13:06:46 +00:00
|
|
|
for line in self.driver.get_log("browser"):
|
|
|
|
self.logger.warning(
|
|
|
|
line["message"], source=line["source"], level=line["level"]
|
|
|
|
)
|
2020-09-11 21:21:11 +00:00
|
|
|
if self.container:
|
|
|
|
self.container.kill()
|
2020-06-20 15:06:00 +00:00
|
|
|
self.driver.quit()
|
2020-06-20 15:06:15 +00:00
|
|
|
super().tearDown()
|
2020-06-20 15:06:00 +00:00
|
|
|
|
2020-06-26 14:21:59 +00:00
|
|
|
def wait_for_url(self, desired_url):
|
|
|
|
"""Wait until URL is `desired_url`."""
|
2020-06-30 14:36:30 +00:00
|
|
|
self.wait.until(
|
2020-09-16 21:31:16 +00:00
|
|
|
lambda driver: driver.current_url == desired_url,
|
2020-06-30 14:36:30 +00:00
|
|
|
f"URL {self.driver.current_url} doesn't match expected URL {desired_url}",
|
|
|
|
)
|
2020-06-26 14:21:59 +00:00
|
|
|
|
2020-06-20 21:56:35 +00: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-11-23 13:24:42 +00:00
|
|
|
def shell_url(self, view, **kwargs) -> str:
|
|
|
|
"""same as self.url() but show URL in shell"""
|
|
|
|
return f"{self.live_server_url}/#{reverse(view, kwargs=kwargs)}"
|
|
|
|
|
|
|
|
def assert_user(self, expected_user: User):
|
|
|
|
"""Check users/me API and assert it matches expected_user"""
|
2020-12-05 21:08:42 +00:00
|
|
|
self.driver.get(self.url("authentik_api:user-me") + "?format=json")
|
2020-11-23 13:24:42 +00:00
|
|
|
user_json = self.driver.find_element(By.CSS_SELECTOR, "pre").text
|
|
|
|
user = UserSerializer(data=json.loads(user_json))
|
|
|
|
user.is_valid()
|
|
|
|
self.assertEqual(user["username"].value, expected_user.username)
|
|
|
|
self.assertEqual(user["name"].value, expected_user.name)
|
|
|
|
self.assertEqual(user["email"].value, expected_user.email)
|
|
|
|
|
2020-06-20 15:06:00 +00:00
|
|
|
def apply_default_data(self):
|
|
|
|
"""apply objects created by migrations after tables have been truncated"""
|
2021-02-03 20:18:31 +00:00
|
|
|
# Not all default objects are managed, like users for example
|
|
|
|
# Hence we still have to load all migrations and apply them, then run the ObjectManager
|
2020-06-20 15:06:00 +00:00
|
|
|
# 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
|
2021-02-03 20:18:31 +00:00
|
|
|
ObjectManager().run()
|
2020-10-20 16:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def retry(max_retires=3, exceptions=None):
|
|
|
|
"""Retry test multiple times. Default to catching Selenium Timeout Exception"""
|
|
|
|
|
|
|
|
if not exceptions:
|
2020-12-06 15:17:51 +00:00
|
|
|
exceptions = [WebDriverException, TimeoutException, NoSuchElementException]
|
2020-10-20 16:42:26 +00:00
|
|
|
|
2020-10-22 12:05:29 +00:00
|
|
|
logger = get_logger()
|
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
def retry_actual(func: Callable):
|
|
|
|
"""Retry test multiple times"""
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
@wraps(func)
|
2020-10-22 12:05:29 +00:00
|
|
|
def wrapper(self: TransactionTestCase, *args, **kwargs):
|
2020-10-20 16:42:26 +00:00
|
|
|
"""Run test again if we're below max_retries, including tearDown and
|
|
|
|
setUp. Otherwise raise the error"""
|
|
|
|
nonlocal count
|
|
|
|
try:
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
# pylint: disable=catching-non-exception
|
|
|
|
except tuple(exceptions) as exc:
|
|
|
|
count += 1
|
|
|
|
if count > max_retires:
|
2020-10-22 12:05:29 +00:00
|
|
|
logger.debug("Exceeded retry count", exc=exc, test=self)
|
2020-10-20 16:42:26 +00:00
|
|
|
# pylint: disable=raising-non-exception
|
|
|
|
raise exc
|
2020-10-22 12:05:29 +00:00
|
|
|
logger.debug("Retrying on error", exc=exc, test=self)
|
2020-10-20 16:42:26 +00:00
|
|
|
self.tearDown()
|
2020-11-15 19:46:53 +00:00
|
|
|
self._post_teardown() # noqa
|
2020-10-20 16:42:26 +00:00
|
|
|
self.setUp()
|
|
|
|
return wrapper(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return retry_actual
|