From 6eef76b77b68b4e4e60aa7054fb12d3feda189c3 Mon Sep 17 00:00:00 2001 From: natalia Date: Mon, 15 Jan 2024 14:20:50 +0100 Subject: [PATCH] wip: use fixtures from oauth_tools --- tests/integration/conftest.py | 90 ------------------------- tests/integration/test_grafana_oauth.py | 1 + tox.ini | 2 +- 3 files changed, 2 insertions(+), 91 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index aa71f06d..be8c0b1c 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -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__) @@ -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() diff --git a/tests/integration/test_grafana_oauth.py b/tests/integration/test_grafana_oauth.py index 5e0b2d56..4a0c4d77 100644 --- a/tests/integration/test_grafana_oauth.py +++ b/tests/integration/test_grafana_oauth.py @@ -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__) diff --git a/tox.ini b/tox.ini index 0264dd2c..b7cc1dbb 100644 --- a/tox.ini +++ b/tox.ini @@ -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