From 0cb1eb367dce855d2e4fe2f5afcec361b3548b2c Mon Sep 17 00:00:00 2001 From: deen13 Date: Tue, 25 Jun 2024 13:55:41 +0200 Subject: [PATCH] Setup playwright and implement login test --- .circleci/config.yml | 5 +++++ pyproject.toml | 4 ++++ tests/conftest.py | 9 ++++++++- tests/ui/__init__.py | 3 +++ tests/ui/test_login.py | 13 +++++++++++++ tools/test.sh | 2 ++ 6 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/ui/__init__.py create mode 100644 tests/ui/test_login.py diff --git a/.circleci/config.yml b/.circleci/config.yml index f2935e841d..f79e2fef8f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -284,6 +284,8 @@ jobs: test: docker: - image: cimg/python:3.11.7 + environment: + DJANGO_ALLOW_ASYNC_UNSAFE: True - image: cimg/postgres:14.1 environment: POSTGRES_USER: integreat @@ -298,6 +300,9 @@ jobs: - run: name: Enable virtual environment command: echo "source .venv/bin/activate" >> $BASH_ENV + - run: + name: Install playwright dependencies + command: playwright install --with-deps - run: name: Migrate database command: integreat-cms-cli migrate --settings=integreat_cms.core.circleci_settings diff --git a/pyproject.toml b/pyproject.toml index 16599a2e3e..42ea899fb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -112,6 +112,8 @@ dev = [ "types-python-dateutil", "types-PyYAML", "types-requests", + "pytest-playwright", + "playwright" ] pinned = [ "aiohttp==3.9.5", @@ -330,6 +332,8 @@ dev-pinned = [ "virtualenv==20.26.2", "Werkzeug==3.0.3", "zipp==3.19.2", + "pytest-playwright==0.5.0", + "playwright==1.44.0" ] [project.urls] diff --git a/tests/conftest.py b/tests/conftest.py index 684ad687ab..15d9b8832a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,8 @@ from django.contrib.auth import get_user_model from django.core.management import call_command from django.test.client import AsyncClient, Client +from playwright.sync_api import Page +from pytest_django.live_server_helper import LiveServer from pytest_httpserver.httpserver import HTTPServer from integreat_cms.cms.constants.roles import ( @@ -31,7 +33,6 @@ from _pytest.fixtures import SubRequest from pytest_django.plugin import _DatabaseBlocker # type: ignore[attr-defined] - #: A role identifier for superusers ROOT: Final = "ROOT" #: A role identifier for anonymous users @@ -132,3 +133,9 @@ def login_role_user_async( @pytest.fixture(scope="function") def mock_server(httpserver: HTTPServer) -> MockServer: return MockServer(httpserver) + + +@pytest.fixture() +def test_server(page: Page, live_server: LiveServer, load_test_data: None) -> Page: + page.goto(live_server.url) + return page diff --git a/tests/ui/__init__.py b/tests/ui/__init__.py new file mode 100644 index 0000000000..5ce75e731e --- /dev/null +++ b/tests/ui/__init__.py @@ -0,0 +1,3 @@ +""" +This package contains tests of the :mod:`integreat_cms` app +""" diff --git a/tests/ui/test_login.py b/tests/ui/test_login.py new file mode 100644 index 0000000000..edd583253e --- /dev/null +++ b/tests/ui/test_login.py @@ -0,0 +1,13 @@ +from playwright.sync_api import expect, Page +from pytest_django.live_server_helper import LiveServer + + +def test_login(page: Page, test_server: LiveServer) -> None: + page.goto(test_server.url) + + page.fill("input[name='username']", "root") + page.fill("input[type='password']", "root1234") + + page.get_by_text("Anmelden").click() + + expect(page.get_by_role("heading", name="Admin Dashboard")).to_be_visible() diff --git a/tools/test.sh b/tools/test.sh index 8f936a7d69..a7fa1e11ef 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -34,6 +34,8 @@ export INTEGREAT_CMS_LINKCHECK_DISABLE_LISTENERS=1 # Disable background tasks during testing export INTEGREAT_CMS_BACKGROUND_TASKS_ENABLED=0 +export DJANGO_ALLOW_ASYNC_UNSAFE="True" + TESTS=() # Parse given command line arguments