e2e_tests/previous-work/conftest.py
2023-11-21 15:19:33 +01:00

149 lines
5.9 KiB
Python

import pytest
import yaml
from pathlib import Path
from playwright.sync_api import sync_playwright, expect, Browser, Locator
# playwright = sync_playwright().start()
# browser = playwright.chromium.launch(headless=False)
with open("config.yaml") as file:
CONFIG = yaml.safe_load(file)
testuser = {'username': 'testuser',
'name': "Test User",
'password': 'test123',
'email': 'test@example.com'}
RECORDS = Path("records")
RECORDS.mkdir(exist_ok=True)
STATES = Path("states")
STATES.mkdir(exist_ok=True)
TIMEOUT = CONFIG['timeout']
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.tracing.start(screenshots=True, snapshots=True, sources=True)
context.set_default_timeout(TIMEOUT)
return context
""" Test Authentik Login and DE Locale """
@pytest.fixture(scope="session", autouse=True)
def admin_login(browser: Browser):
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.tracing.stop(path=f"{RECORDS}/admin_login.zip")
context.close()
""" Create User """
@pytest.fixture(scope="session", autouse=True)
def init_create_user(browser: Browser, admin_login):
admin_context = setup_context(browser, f"{STATES}/admin_state.json")
admin_page = admin_context.new_page()
invitelink = create_invite_link(admin_page)
admin_context.tracing.stop(path=f"{RECORDS}/create_invite_link.zip")
admin_context.close()
user_context = setup_context(browser)
create_user(user_context, invitelink)
user_context.tracing.stop(path=f"{RECORDS}/create_user.zip")
user_context.close()
""" Delete User """
@pytest.fixture(scope="session", autouse=True)
def post_delete_user(browser: Browser):
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)
context.tracing.stop(path=f"{RECORDS}/delete_user.zip")
""" Create Invite Link """
def create_invite_link(page):
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):
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):
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):
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):
context = setup_context(browser, f"{STATES}/user_state.json")
page = context.new_page()
page.goto(CONFIG['domain'])
yield context, page
context.close()