From 387977a54adcdf3f8a60ae57de198e54b3bdbbdb Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Nov 2023 15:23:18 +0100 Subject: [PATCH] authentic setup is now handled via pytest plugin --- src/conftest.py | 4 +++ src/setup/setup_authentik.py | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/setup/setup_authentik.py diff --git a/src/conftest.py b/src/conftest.py index 43b4a2a..86ad681 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -4,6 +4,10 @@ import pytest from dirmanager import DirManager from dotenv import dotenv_values +pytest_plugins = [ + "setup.setup_authentik", +] + def pytest_addoption(parser): parser.addoption( diff --git a/src/setup/setup_authentik.py b/src/setup/setup_authentik.py new file mode 100644 index 0000000..0c7307b --- /dev/null +++ b/src/setup/setup_authentik.py @@ -0,0 +1,47 @@ +import json +from pathlib import Path + +import pytest +from icecream import ic +from playwright.sync_api import Browser, Locator, expect + +cred_file = Path("../credentials.json") +with open(cred_file, "r") as f: + CREDENTIALS = json.load(f) + +print(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) + + +@pytest.fixture(scope="session", autouse=True) +def admin_login(browser: Browser, dotenv_config, STATES): + # ic(dotenv_config) + + # go to page + context = browser.new_context() + context.set_default_timeout(TIMEOUT) + 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()