e2e_tests/recipes/authentik/tests_authentik/setup_authentik.py
Daniel 2dd765a974 various (#16)
* add full integration test of cli / pytest_abra with all tests

* save path of runner_*.py in runner subclass to improve test discovery -> allows for same test name in two different runners

* reorganize output dir names

* use URL fixture everywhere

* rework coordinator interface

* add --session_id to cli args

* add log results table

* plenty of refactoring

* add assert messages

* add plenty of tests

* add /docs dir with plenty of documentation

* fix authentik setup

* add authentik cleanup, remove test user

* add random test user credential generation and integrate into test routine. random creds are saved to STATES

Reviewed-on: local-it-infrastructure/e2e_tests#16
Co-authored-by: Daniel <d.brummerloh@gmail.com>
Co-committed-by: Daniel <d.brummerloh@gmail.com>
2023-12-14 14:03:58 +01:00

104 lines
3.9 KiB
Python

import json
import os
import re
from playwright.sync_api import BrowserContext, expect
from pytest_abra import BaseUrl, DirManager
ADMIN_USER = os.environ["ADMIN_USER"]
ADMIN_PASS = os.environ["ADMIN_PASS"]
TEST_USER = os.environ["TEST_USER"]
TEST_PASS = os.environ["TEST_PASS"]
def setup_admin_state(context: BrowserContext, env_config: dict[str, str], DIR: DirManager, URL: BaseUrl):
# go to page
page = context.new_page()
page.goto(URL.get())
# check welcome message
welcome_message = env_config.get("welcome_message")
if welcome_message:
expect(page.get_by_text(welcome_message)).to_be_visible()
# login
page.locator("input[name='uidField']").fill(ADMIN_USER)
page.locator("ak-stage-identification input[name='password']").fill(ADMIN_PASS)
page.get_by_role("button", name="Log In").click()
expect(page.locator("ak-library")).to_be_visible()
# save state
context.storage_state(path=DIR.STATES / "authentik_admin_state.json")
def create_invite_link(admin_context: BrowserContext, env_config: dict[str, str], URL: BaseUrl):
# go to admin page
page = admin_context.new_page()
page.goto(URL.get())
page.get_by_role("link", name="Admin Interface").click()
nav = page.locator("ak-sidebar-item", has_text=re.compile(r"Directory|Verzeichnis"))
nav.click()
nav.get_by_role("link", name=re.compile(r"Invitations|Einladungen")).click()
# todo: only works if no links have been created yet (empty list)
page.get_by_role("cell", name=re.compile(r"Keine Objekte|objects")).get_by_role(
"button"
).click() # todo: confirm "objects" for en lang
page.locator('input[name="name"]').click()
linkname = "test_link_123"
page.locator('input[name="name"]').fill(linkname)
page.get_by_placeholder("Wählen Sie ein Objekt aus.").click()
page.get_by_role("option", name=re.compile(r"invitation-enrollment-flow")).click()
# force, because else we get "intercepts pointer events"
page.locator("footer").locator("ak-spinner-button").first.click(force=True)
linklocator = page.get_by_role("rowgroup").filter(has=page.get_by_text(linkname))
linklocator.locator(".fa-angle-down").click()
# page.get_by_text(linkname).click()
invitelink = linklocator.get_by_role("textbox").get_attribute(name="value")
return invitelink
def create_user(user_context: BrowserContext, invitelink):
# warning: only works on german site
page = user_context.new_page()
page.goto(invitelink)
page.get_by_placeholder("Benutzername").click()
page.get_by_placeholder("Benutzername").fill(TEST_USER)
page.locator('input[name="name"]').click()
page.locator('input[name="name"]').fill("name")
page.locator('input[name="email"]').click()
email = os.environ["IMAP_EMAIL"] if "IMAP_EMAIL" in os.environ else "test@domain.com"
page.locator('input[name="email"]').fill(email)
page.get_by_placeholder("Passwort", exact=True).click()
page.get_by_placeholder("Passwort", exact=True).fill(TEST_PASS)
page.get_by_placeholder("Passwort (wiederholen)").click()
page.get_by_placeholder("Passwort (wiederholen)").fill(TEST_PASS)
page.get_by_role("button", name="Weiter").click()
expect(page.locator("ak-library")).to_be_visible()
def setup_user_state(
context: BrowserContext, env_config: dict[str, str], DIR: DirManager, URL: BaseUrl, check_if_user_exists
):
# load admin cookies to context
state_file = DIR.STATES / "authentik_admin_state.json"
storage_state = json.loads(state_file.read_bytes())
context.add_cookies(storage_state["cookies"])
if check_if_user_exists(context, env_config, URL):
# just login with user
pass
context.clear_cookies()
else:
# get invite_link
invite_link = create_invite_link(context, env_config, URL)
# create user
context.clear_cookies()
create_user(context, invite_link)
context.storage_state(path=DIR.STATES / "authentik_user_state.json")