import pytest import yaml from random import random from playwright.sync_api import Playwright, sync_playwright, BrowserContext, expect, Browser, Page, Locator # playwright = sync_playwright().start() # browser = playwright.chromium.launch(headless=False) with open("config.yaml") as file: CONFIG = yaml.safe_load(file) TIMEOUT = CONFIG['timeout'] def check_for(locator: Locator): expect(locator).to_be_visible(timeout=TIMEOUT) """ Test Authentik Login and DE Locale """ @pytest.fixture(scope="module", autouse=False) def admin_login(browser: Browser): context = browser.new_context() context.set_default_timeout(TIMEOUT) context.tracing.start(screenshots=True, snapshots=True, sources=True) 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="admin_state.json") page.close() context.tracing.stop(path="trace.zip") context.close() """ Create User """ @pytest.fixture(scope="module", autouse=False) def init_create_user(browser: Browser): admin_context = browser.new_context(storage_state="admin_state.json") admin_context.tracing.start(screenshots=True, snapshots=True, sources=True) admin_context.set_default_timeout(TIMEOUT) admin_page = admin_context.new_page() invitelink = create_invite_link(admin_page) admin_context.tracing.stop(path="create_invite_link.zip") admin_context.close() user_context = browser.new_context() user_context.tracing.start(screenshots=True, snapshots=True, sources=True) user_context.set_default_timeout(TIMEOUT) create_user(user_context, invitelink) user_context.tracing.stop(path="create_user.zip") user_context.close() yield """ Delete User """ @pytest.fixture(scope="module", autouse=True) def post_delete_user(browser: Browser): yield context = browser.new_context(storage_state="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="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") page.locator("input[name=\"name\"]").click() page.locator("input[name=\"name\"]").fill("Test User") page.locator("input[name=\"email\"]").click() page.locator("input[name=\"email\"]").fill("test@example.com") page.get_by_placeholder("Passwort", exact=True).click() page.get_by_placeholder("Passwort", exact=True).fill("test123") page.get_by_placeholder("Passwort (wiederholen)").click() page.get_by_placeholder("Passwort (wiederholen)").fill("test123") page.get_by_role("button", name="Weiter").click() check_for(page.locator("ak-library")) context.storage_state(path="user_state.json") """ Delete Nextcloud Account """ def delete_nextcloud_user(page): page.goto(CONFIG['domain']) with page.expect_popup() as nextcloud_info: page.get_by_role("link", name="Nextcloud").click() nextcloud = nextcloud_info.value nextcloud.get_by_role("link", name="Open settings menu").click() nextcloud.get_by_role("link", name="Users").click() nextcloud.locator("#app-content div").filter(has_text="testuser").get_by_role("button", name="Toggle user actions menu").click() nextcloud.get_by_role("button", name="Delete user").click() nextcloud.get_by_role("button", name="Delete authentik-testuser's account").click() """ 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')).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(scope="function") def admin_session(browser: Browser): context = browser.new_context(storage_state="admin_state.json") context.tracing.start(screenshots=True, snapshots=True, sources=True) context.set_default_timeout(TIMEOUT) page = context.new_page() page.goto(CONFIG['domain']) yield context, page context.close() """ Reuse Authentik User Session """ @pytest.fixture(scope="function") def user_session(browser: Browser): context = browser.new_context(storage_state="user_state.json") context.tracing.start(screenshots=True, snapshots=True, sources=True) context.set_default_timeout(TIMEOUT) page = context.new_page() page.goto(CONFIG['domain']) yield context, page context.close() def test_dummy(): return """ Test Changing Username in Authentik """ def test_authentik_change_username(user_session): context, page = user_session page.get_by_role("banner").get_by_role("link").nth(1).click() page.get_by_placeholder("testuser").click() page.get_by_placeholder("testuser").fill("renameduser") page.get_by_role("button", name="Speichern").click() check_for(page.get_by_role("heading", name="Not allowed to change username.")) context.tracing.stop(path="change_username.zip") """ Test Wordpress """ def test_wordpress(admin_session): context, page = admin_session with page.expect_popup() as info: page.get_by_role("link", name="Wordpress").click() wordpress = info.value check_for(wordpress.locator("#wpcontent")) if CONFIG['locale'] == 'de': check_for(wordpress.get_by_role("heading", name="Willkommen bei WordPress!")) context.tracing.stop(path="wordpress.zip") """ Test Wekan """ def test_wekan(admin_session): context, page = admin_session with page.expect_popup() as info: page.get_by_role("link", name="Wekan").click() wekan = info.value wekan.get_by_role("link", name="Member Settings").click() check_for(wekan.get_by_role("link", name=" Admin Panel")) context.tracing.stop(path="wekan.zip") """ Test Vikunja """ def test_vikunja(admin_session): context, page = admin_session with page.expect_popup() as info: page.get_by_role("link", name="Vikunja").click() vikunja = info.value vikunja.get_by_text("Log in with").click() check_for(vikunja.get_by_placeholder("Add a new task…")) context.tracing.stop(path="vikunja.zip") """ Test Nextcloud """ def test_nextcloud(user_session): context, page = user_session with page.expect_popup() as nextcloud_info: page.get_by_role("link", name="Nextcloud").click() nextcloud = nextcloud_info.value if nextcloud.query_selector('.close-icon'): nextcloud.get_by_role("button", name="Close modal").click() if CONFIG.get('default_quota'): quota = int(nextcloud.get_by_role("listitem", name="Storage informations").get_by_role("link").inner_text().split()[3]) assert quota == CONFIG['default_quota'] for app in CONFIG['nc_apps']: check_for(nextcloud.get_by_role("link", name=app)) context.tracing.stop(path="nextcloud.zip") """ Test Onlyoffice in Nextcloud """ def test_onlyoffice(user_session): context, page = user_session with page.expect_popup() as nextcloud_info: page.get_by_role("link", name="Nextcloud").click() nextcloud = nextcloud_info.value if nextcloud.query_selector('.close-icon'): nextcloud.get_by_role("button", name="Close modal").click() nextcloud.get_by_role("link", name="New file/folder menu").click() nextcloud.get_by_role("link", name="New document").click() nextcloud.locator("#view9-input-file").fill("test.docx") nextcloud.get_by_role("button", name="Submit").click() outer_frame = nextcloud.frame_locator('#onlyofficeFrame') check_for(outer_frame.locator('body')) inner_frame = outer_frame.frame_locator('#app > iframe') check_for(inner_frame.locator('body')) onlyoffice = nextcloud.frame("frameEditor") check_for(onlyoffice.locator('//*[@id="area_id"]')) onlyoffice.locator("#btn-goback").click() nextcloud.get_by_role("link", name="Not favorited test .docx Share Actions").get_by_role("link", name="Actions").click() nextcloud.get_by_role("link", name="Delete file").click() context.tracing.stop(path="onlyoffice.zip")