add authentik setup, this is base for all other testing

This commit is contained in:
Daniel 2023-11-22 12:30:36 +01:00
parent 65aa461d5a
commit 25a087db9d

View file

@ -0,0 +1,165 @@
import pytest
from playwright.sync_api import Browser, Locator, expect
# playwright = sync_playwright().start()
# browser = playwright.chromium.launch(headless=False)
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_context(browser, state_file=None):
if state_file:
context = browser.new_context(storage_state=state_file)
else:
context = browser.new_context()
context.set_default_timeout(TIMEOUT)
return context
""" Test Authentik Login and DE Locale """
@pytest.fixture(scope="session", autouse=True)
def admin_login(browser: Browser, dotenv_config, STATES):
CONFIG = dotenv_config
context = setup_context(browser)
page = context.new_page()
page.goto(CONFIG["domain"])
welcome_message = CONFIG.get("welcome_message")
if welcome_message:
check_for(page.get_by_text(welcome_message))
if CONFIG["locale"] == "de":
check_for(page.get_by_text("Benutzername oder Passwort vergessen?"))
check_for(page.get_by_text("E-Mail or Anmeldename"))
check_for(page.get_by_text("Passwort", exact=True))
page.locator('input[name="uidField"]').fill(CONFIG["admin"])
page.locator('ak-stage-identification input[name="password"]').fill(CONFIG["admin_pw"])
page.get_by_role("button", name="Log In").click()
check_for(page.locator("ak-library"))
if CONFIG["locale"] == "de":
check_for(page.get_by_text("Meine Anwendungen"))
context.storage_state(path=f"{STATES}/admin_state.json")
page.close()
context.close()
""" Create User """
@pytest.fixture(scope="session", autouse=True)
def init_create_user(browser: Browser, dotenv_config, STATES):
admin_context = setup_context(browser, f"{STATES}/admin_state.json")
admin_page = admin_context.new_page()
invitelink = create_invite_link(admin_page, dotenv_config)
admin_context.close()
user_context = setup_context(browser)
create_user(user_context, invitelink)
user_context.close()
""" Delete User """
@pytest.fixture(scope="session", autouse=True)
def post_delete_user(browser: Browser, dotenv_config, RECORDS, STATES):
yield
context = browser.new_context(storage_state=f"{STATES}/admin_state.json")
context.tracing.start(screenshots=True, snapshots=True, sources=True)
context.set_default_timeout(TIMEOUT)
page = context.new_page()
# delete_nextcloud_user(page)
delete_authentik_user(page, dotenv_config)
context.tracing.stop(path=f"{RECORDS}/delete_user.zip")
""" Create Invite Link """
def create_invite_link(page, dotenv_config):
CONFIG = dotenv_config
page.goto(CONFIG["domain"])
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
""" Create User from invitelink """
def create_user(context, invitelink, STATES):
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")
""" Delete Authentik Account """
def delete_authentik_user(page, dotenv_config):
CONFIG = dotenv_config
page.goto(CONFIG["domain"])
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_role("row").filter(has=page.get_by_text(testuser["username"])).get_by_role("checkbox").click()
page.get_by_role("button", name="Löschen").click()
page.get_by_role("dialog").get_by_role("button", name="Löschen").click()
check_for(page.get_by_text("1 Benutzer erfolgreich gelöscht"))
""" Reuse Authentik Admin Session """
@pytest.fixture
def admin_session(browser: Browser, dotenv_config, STATES):
CONFIG = dotenv_config
context = setup_context(browser, f"{STATES}/admin_state.json")
page = context.new_page()
page.goto(CONFIG["domain"])
yield context, page
context.close()
""" Reuse Authentik User Session """
@pytest.fixture
def user_session(browser: Browser, dotenv_config, STATES):
CONFIG = dotenv_config
context = setup_context(browser, f"{STATES}/user_state.json")
page = context.new_page()
page.goto(CONFIG["domain"])
yield context, page
context.close()