From 702791e5cdf60d92577b62c90ce8a354ee6dcccb Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 24 Nov 2023 10:27:54 +0100 Subject: [PATCH] runner is executed authentik setup als function, without pytest --- src/runner.py | 2 + .../setup_authentik.py | 2 + .../authentik_setup_nonpytest.py | 1 - ...uthentik_plugin.py => plugin_authentik.py} | 0 src/tests_authentik/runner_authentik.py | 5 + src/tests_authentik/setup_authentik.py | 102 ++++++++++++++++++ ...thentik_setup.py => test_authentik_old.py} | 0 7 files changed, 111 insertions(+), 1 deletion(-) rename src/{setup => setup-deprecated}/setup_authentik.py (98%) delete mode 100644 src/tests_authentik/authentik_setup_nonpytest.py rename src/tests_authentik/{authentik_plugin.py => plugin_authentik.py} (100%) create mode 100644 src/tests_authentik/setup_authentik.py rename src/tests_authentik/{test_authentik_setup.py => test_authentik_old.py} (100%) diff --git a/src/runner.py b/src/runner.py index fc7b40f..b987e60 100644 --- a/src/runner.py +++ b/src/runner.py @@ -3,6 +3,7 @@ from typing import Callable, Optional, TypedDict import pytest from dirmanager import DirManager +from dotenv import dotenv_values from icecream import ic @@ -19,6 +20,7 @@ class Runner: def __init__(self, dotenv_path: Path, tests_dir: Path, session_id: str): self.dotenv_path = dotenv_path + self.config: dict[str, str] = dotenv_values(dotenv_path) self.tests_dir = tests_dir self.session_id = session_id self.dir_manager = DirManager(tests_dir, session_id) diff --git a/src/setup/setup_authentik.py b/src/setup-deprecated/setup_authentik.py similarity index 98% rename from src/setup/setup_authentik.py rename to src/setup-deprecated/setup_authentik.py index f3acd9c..acb7e1b 100644 --- a/src/setup/setup_authentik.py +++ b/src/setup-deprecated/setup_authentik.py @@ -19,6 +19,8 @@ def check_for(locator: Locator): expect(locator).to_be_visible(timeout=TIMEOUT) + +# todo: why is this a fixture? to get dotenv_config @pytest.fixture(scope="session", autouse=True) def create_admin_login(browser: Browser, dotenv_config, STATES): # ic(dotenv_config) diff --git a/src/tests_authentik/authentik_setup_nonpytest.py b/src/tests_authentik/authentik_setup_nonpytest.py deleted file mode 100644 index a4d8b5c..0000000 --- a/src/tests_authentik/authentik_setup_nonpytest.py +++ /dev/null @@ -1 +0,0 @@ -# put authentik setup here and run at beginning of runner diff --git a/src/tests_authentik/authentik_plugin.py b/src/tests_authentik/plugin_authentik.py similarity index 100% rename from src/tests_authentik/authentik_plugin.py rename to src/tests_authentik/plugin_authentik.py diff --git a/src/tests_authentik/runner_authentik.py b/src/tests_authentik/runner_authentik.py index 1ef97b8..13292b3 100644 --- a/src/tests_authentik/runner_authentik.py +++ b/src/tests_authentik/runner_authentik.py @@ -1,6 +1,7 @@ from pathlib import Path from runner import Runner, SubTest +from tests_authentik.setup_authentik import setup_authentik def condition_always_true(dotenv_path: Path) -> bool: @@ -14,3 +15,7 @@ def condition_always_false(dotenv_path: Path) -> bool: class RunnerAuthentik(Runner): test_dir_name = "tests_authentik" main_test_name = "test_authentik_dummy.py" + + def run_setup(self): + # run setup + setup_authentik(self.config, self.dir_manager.dirs["states"]) diff --git a/src/tests_authentik/setup_authentik.py b/src/tests_authentik/setup_authentik.py new file mode 100644 index 0000000..986a9e1 --- /dev/null +++ b/src/tests_authentik/setup_authentik.py @@ -0,0 +1,102 @@ +# put authentik setup here and run at beginning of runner +import json +from pathlib import Path + +from icecream import ic +from playwright.sync_api import BrowserContext, Locator, Page, expect, sync_playwright + +cred_file = Path("../credentials.json") +with open(cred_file, "r") as f: + CREDENTIALS = json.load(f) + +ic("RUNNING SETUP") +ic(CREDENTIALS) + +TESTUSER = {"username": "testuser", "name": "Test User", "password": "test123", "email": "test@example.com"} +TIMEOUT = 5000 + + +def check_for(locator: Locator): + expect(locator).to_be_visible(timeout=TIMEOUT) + + +def setup_authentik(dotenv_config: dict[str, str], STATES: Path): + with sync_playwright() as p: + browser = p.chromium.launch() + admin_context = browser.new_context() + admin_context.set_default_timeout(TIMEOUT) + user_context = browser.new_context() + user_context.set_default_timeout(TIMEOUT) + create_admin_login(admin_context, dotenv_config, STATES) + create_user_session(admin_context, user_context, STATES) + + +def create_admin_login(context: BrowserContext, dotenv_config: dict[str, str], STATES: Path): + # go to page + page = context.new_page() + url = "https://" + dotenv_config["DOMAIN"] + page.goto(url) + + # check welcome message + welcome_message = dotenv_config.get("welcome_message") + if welcome_message: + check_for(page.get_by_text(welcome_message)) + + # login + page.locator('input[name="uidField"]').fill(CREDENTIALS["admin"]) + page.locator('ak-stage-identification input[name="password"]').fill(CREDENTIALS["admin_pw"]) + page.get_by_role("button", name="Log In").click() + check_for(page.locator("ak-library")) + + # save state + context.storage_state(path=f"{STATES}/admin_state.json") + page.close() + context.close() + + +def create_invite_link(page: Page, dotenv_config: dict[str, str]): + url = "https://" + dotenv_config["DOMAIN"] + page.goto(url) + page.get_by_role("link", name="Admin Interface").click() + page.get_by_text("Verzeichnis").click() + page.get_by_text("Benutzer").nth(2).click() + page.get_by_text("Einladungen").click() + page.get_by_role("button", name="Erstellen").first.click() + page.locator('input[name="name"]').click() + linkname = "testlink9433" + page.locator('input[name="name"]').fill(linkname) + page.get_by_placeholder("Wählen Sie ein Objekt aus.").click() + page.get_by_role("option", name="invitation-enrollment-flow invitation-enrollment-flow").click() + page.get_by_text("Erstellen", exact=True).first.click() + linklocator = page.get_by_role("rowgroup").filter(has=page.get_by_text(linkname)) + linklocator.locator(".fa-angle-down").click() + invitelink = linklocator.get_by_role("textbox").get_attribute(name="value") + return invitelink + + +def create_user(context: BrowserContext, invitelink, STATES: Path): + page = context.new_page() + page.goto(invitelink) + page.get_by_placeholder("Benutzername").click() + page.get_by_placeholder("Benutzername").fill(TESTUSER["username"]) + page.locator('input[name="name"]').click() + page.locator('input[name="name"]').fill(TESTUSER["name"]) + page.locator('input[name="email"]').click() + page.locator('input[name="email"]').fill(TESTUSER["email"]) + page.get_by_placeholder("Passwort", exact=True).click() + page.get_by_placeholder("Passwort", exact=True).fill(TESTUSER["password"]) + page.get_by_placeholder("Passwort (wiederholen)").click() + page.get_by_placeholder("Passwort (wiederholen)").fill(TESTUSER["password"]) + page.get_by_role("button", name="Weiter").click() + check_for(page.locator("ak-library")) + context.storage_state(path=f"{STATES}/user_state.json") + + +def create_user_session(admin_context: BrowserContext, user_context: BrowserContext, STATES: Path): + # admin_context = browser.new_context(storage_state=f"{STATES}/admin_state.json")) + # admin_context = setup_context(browser, f"{STATES}/admin_state.json") + admin_page = admin_context.new_page() + invitelink = create_invite_link(admin_page) + + # create user + create_user(user_context, invitelink, STATES) diff --git a/src/tests_authentik/test_authentik_setup.py b/src/tests_authentik/test_authentik_old.py similarity index 100% rename from src/tests_authentik/test_authentik_setup.py rename to src/tests_authentik/test_authentik_old.py