turn check_if_user_exists into a fixture

This commit is contained in:
Daniel 2023-12-14 12:29:06 +01:00
parent 6be9fdc535
commit 359a18eae5
2 changed files with 35 additions and 22 deletions

View file

@ -1,9 +1,11 @@
from typing import Generator
import os
import re
from typing import Callable, Generator
import pytest
from playwright.sync_api import APIRequestContext, Playwright
from playwright.sync_api import APIRequestContext, BrowserContext, Playwright, TimeoutError
from pytest_abra import DirManager
from pytest_abra import BaseUrl, DirManager
@pytest.fixture(scope="session")
@ -15,3 +17,29 @@ def api_request_context(
request_context = playwright.request.new_context(storage_state=state_file)
yield request_context
request_context.dispose()
@pytest.fixture
def check_if_user_exists() -> Callable[[BrowserContext, dict[str, str], BaseUrl], bool]:
"""This is actually a normal function supplied as a fixture. This is because normal imports from tests_authentik is difficult.
tests_authentik is not part of the python environment, so
from ... import function
will most likely fail. However, pytest handles the loading of fixtures from conftest.py automatically, hence we use that to load functions too."""
def inner_check_if_user_exists(admin_context: BrowserContext, env_config: dict[str, str], URL: BaseUrl) -> bool:
# 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"Users|Benutzer")).click()
user = page.get_by_text(os.environ["TEST_USER"])
try:
user.wait_for(state="visible", timeout=5_000)
return True
except TimeoutError:
return False
return inner_check_if_user_exists

View file

@ -2,7 +2,7 @@ import json
import os
import re
from playwright.sync_api import BrowserContext, TimeoutError, expect
from playwright.sync_api import BrowserContext, expect
from pytest_abra import BaseUrl, DirManager
@ -32,23 +32,6 @@ def setup_admin_state(context: BrowserContext, env_config: dict[str, str], DIR:
context.storage_state(path=DIR.STATES / "authentik_admin_state.json")
def check_if_user_exists(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"Users|Benutzer")).click()
user = page.get_by_text(TEST_USER)
try:
user.wait_for(state="visible", timeout=5_000)
return True
except TimeoutError:
return False
def create_invite_link(admin_context: BrowserContext, env_config: dict[str, str], URL: BaseUrl):
# go to admin page
page = admin_context.new_page()
@ -99,7 +82,9 @@ def create_user(user_context: BrowserContext, invitelink):
expect(page.locator("ak-library")).to_be_visible()
def setup_user_state(context: BrowserContext, env_config: dict[str, str], DIR: DirManager, URL: BaseUrl):
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())