Skip to content

Commit

Permalink
wip: use fixtures from oauth_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
natalian98 authored and nsklikas committed Jan 15, 2024
1 parent 2fe19be commit 6eef76b
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 91 deletions.
90 changes: 0 additions & 90 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
import pytest
from pytest_operator.plugin import OpsTest

# Dependencies for the oauth integration test
import os
from typing import Any, AsyncGenerator, Callable, Coroutine, Dict
from playwright.async_api import async_playwright
from playwright.async_api._generated import Browser, BrowserContext, BrowserType, Page
from playwright.async_api._generated import Playwright as AsyncPlaywright


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -97,85 +89,3 @@ async def grafana_tester_charm(ops_test: OpsTest) -> Path:
@pytest.fixture(scope="module")
def temp_dir(tmp_path_factory):
return tmp_path_factory.mktemp("data")


# To learn more about playwright for python see https://github.com/microsoft/playwright-python.
# Fixtures are accessible from https://github.com/microsoft/playwright-python/blob/main/tests/async/conftest.py.
@pytest.fixture(scope="module")
def launch_arguments(pytestconfig: Any) -> Dict:
return {
"headless": not (pytestconfig.getoption("--headed") or os.getenv("HEADFUL", False)),
"channel": pytestconfig.getoption("--browser-channel"),
}


@pytest.fixture(scope="module")
async def playwright() -> AsyncGenerator[AsyncPlaywright, None]:
async with async_playwright() as playwright_object:
yield playwright_object


@pytest.fixture(scope="module")
def browser_type(playwright: AsyncPlaywright, browser_name: str) -> BrowserType:
if browser_name == "firefox":
return playwright.firefox
if browser_name == "webkit":
return playwright.webkit
return playwright.chromium


@pytest.fixture(scope="module")
async def browser_factory(
launch_arguments: Dict, browser_type: BrowserType
) -> AsyncGenerator[Callable[..., Coroutine[Any, Any, Browser]], None]:
browsers = []

async def launch(**kwargs: Any) -> Browser:
browser = await browser_type.launch(**launch_arguments, **kwargs)
browsers.append(browser)
return browser

yield launch
for browser in browsers:
await browser.close()


@pytest.fixture(scope="module")
async def browser(
browser_factory: Callable[..., Coroutine[Any, Any, Browser]]
) -> AsyncGenerator[Browser, None]:
browser = await browser_factory()
yield browser
await browser.close()


@pytest.fixture
async def context_factory(
browser: Browser,
) -> AsyncGenerator[Callable[..., Coroutine[Any, Any, BrowserContext]], None]:
contexts = []

async def launch(**kwargs: Any) -> BrowserContext:
context = await browser.new_context(**kwargs)
contexts.append(context)
return context

yield launch
for context in contexts:
await context.close()


@pytest.fixture
async def context(
context_factory: Callable[..., Coroutine[Any, Any, BrowserContext]]
) -> AsyncGenerator[BrowserContext, None]:
context = await context_factory(ignore_https_errors=True)
yield context
await context.close()


@pytest.fixture
async def page(context: BrowserContext) -> AsyncGenerator[Page, None]:
page = await context.new_page()
yield page
await page.close()
1 change: 1 addition & 0 deletions tests/integration/test_grafana_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
verify_page_loads,
get_cookie_from_browser_by_name,
)
from oauth_tools.conftest import * # noqa
from oauth_tools.constants import EXTERNAL_USER_EMAIL, APPS

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ deps =
pytest-operator
pytest-playwright
lightkube
git+https://github.com/canonical/iam-bundle@IAM-640-outsource-oauth-tools-package#egg=oauth_tools
git+https://github.com/canonical/iam-bundle@main#egg=oauth_tools
commands =
playwright install
pytest -vv --tb native --log-cli-level=INFO --color=yes -s {posargs} {toxinidir}/tests/integration

0 comments on commit 6eef76b

Please sign in to comment.