authentik setup and tracing (#2)
* authentik sessions created successfully during setup without breaking tracing * setup works on EN and DE localization by using regex patterns * automated tracing with pytest --trace option, manual hook no longer needed Reviewed-on: local-it-infrastructure/e2e_tests#2 Co-authored-by: Daniel <d.brummerloh@gmail.com> Co-committed-by: Daniel <d.brummerloh@gmail.com>
This commit is contained in:
parent
97ed87c79f
commit
d2cd6ba47f
22 changed files with 519 additions and 304 deletions
122
src/tests_authentik/setup_authentik.py
Normal file
122
src/tests_authentik/setup_authentik.py
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
from icecream import ic
|
||||
from playwright.sync_api import BrowserContext, expect
|
||||
|
||||
from src.dirmanager import DirManager
|
||||
|
||||
ADMIN_USER = os.environ["ADMIN_USER"]
|
||||
ADMIN_PASS = os.environ["ADMIN_PASS"]
|
||||
|
||||
|
||||
TESTUSER = {"username": "testuser", "name": "Test User", "password": "test123", "email": "test@example.com"}
|
||||
TIMEOUT = 10000
|
||||
|
||||
|
||||
def test_create_admin_login(context: BrowserContext, dotenv_config: dict[str, str], DIR: DirManager):
|
||||
# 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:
|
||||
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=f"{DIR.STATES}/admin_state.json")
|
||||
|
||||
|
||||
def check_if_user_exists(admin_context: BrowserContext, dotenv_config: dict[str, str]):
|
||||
# go to admin page
|
||||
page = admin_context.new_page()
|
||||
url = "https://" + dotenv_config["DOMAIN"]
|
||||
page.goto(url)
|
||||
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"Users|Benutzer")).click()
|
||||
result = page.get_by_text(TESTUSER["username"]).is_visible(timeout=TIMEOUT)
|
||||
return result
|
||||
|
||||
|
||||
def create_invite_link(admin_context: BrowserContext, dotenv_config: dict[str, str]):
|
||||
# go to admin page
|
||||
page = admin_context.new_page()
|
||||
url = "https://" + dotenv_config["DOMAIN"]
|
||||
page.goto(url)
|
||||
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(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()
|
||||
expect(page.locator("ak-library")).to_be_visible()
|
||||
|
||||
|
||||
def test_create_user_session(context: BrowserContext, dotenv_config: dict[str, str], DIR: DirManager):
|
||||
context.set_default_timeout(TIMEOUT)
|
||||
|
||||
# load admin cookies
|
||||
state_file = DIR.STATES / "admin_state.json"
|
||||
storage_state = json.loads(state_file.read_bytes())
|
||||
context.add_cookies(storage_state["cookies"])
|
||||
|
||||
if check_if_user_exists(context, dotenv_config):
|
||||
# just login with user
|
||||
pass
|
||||
context.clear_cookies()
|
||||
else:
|
||||
## create user
|
||||
# create invite_link
|
||||
invite_link = create_invite_link(context, dotenv_config)
|
||||
# create user
|
||||
context.clear_cookies()
|
||||
create_user(context, invite_link)
|
||||
|
||||
context.storage_state(path=f"{DIR.STATES}/user_state.json")
|
||||
Loading…
Add table
Add a link
Reference in a new issue