make authentic setup a test a gain

This commit is contained in:
Daniel 2023-11-24 14:15:46 +01:00
parent dfe63687a3
commit d6b4feb9b6

View file

@ -1,9 +1,8 @@
# 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 Browser, BrowserContext, Locator, Page, expect, sync_playwright
from playwright.sync_api import Browser, BrowserContext, expect
cred_file = Path("credentials.json")
with open(cred_file, "r") as f:
@ -16,35 +15,7 @@ TESTUSER = {"username": "testuser", "name": "Test User", "password": "test123",
TIMEOUT = 5000
def check_for(locator: Locator):
expect(locator).to_be_visible(timeout=TIMEOUT)
def test_setup_authentik(browser): # WIP
"""run with pytest"""
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, dotenv_config, STATES)
def setup_authentik(dotenv_config: dict[str, str], STATES: Path):
"""run directly as function from RUNNER"""
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, dotenv_config, STATES)
def create_admin_login(context: BrowserContext, dotenv_config: dict[str, str], STATES: Path):
def test_create_admin_login(context: BrowserContext, dotenv_config: dict[str, str], STATES: Path):
# go to page
page = context.new_page()
url = "https://" + dotenv_config["DOMAIN"]
@ -53,18 +24,16 @@ def create_admin_login(context: BrowserContext, dotenv_config: dict[str, str], S
# check welcome message
welcome_message = dotenv_config.get("welcome_message")
if welcome_message:
check_for(page.get_by_text(welcome_message))
expect(page.get_by_text(welcome_message)).to_be_visible(timeout=TIMEOUT)
# 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"))
expect(page.locator("ak-library")).to_be_visible(timeout=TIMEOUT)
# save state
context.storage_state(path=f"{STATES}/admin_state.json")
page.close()
# context.close()
def create_invite_link(admin_context: BrowserContext, dotenv_config: dict[str, str]):
@ -103,14 +72,17 @@ def create_user(context: BrowserContext, invitelink, STATES: Path):
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")
expect(page.locator("ak-library")).to_be_visible()
def create_user_session(
admin_context: BrowserContext, user_context: BrowserContext, dotenv_config: dict[str, str], STATES: Path
):
invitelink = create_invite_link(admin_context, dotenv_config)
def test_create_user_session(browser: Browser, dotenv_config: dict[str, str], STATES: Path):
# create invite_link
admin_context = browser.new_context(storage_state=f"{STATES}/admin_state.json")
admin_context.set_default_timeout(TIMEOUT)
invite_link = create_invite_link(admin_context, dotenv_config)
# create user
create_user(user_context, invitelink, STATES)
user_context = browser.new_context()
user_context.set_default_timeout(TIMEOUT)
create_user(user_context, invite_link, STATES)
user_context.storage_state(path=f"{STATES}/user_state.json")