aboutsummaryrefslogtreecommitdiff
path: root/tests/playwright/conftest.py
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2022-12-22 18:40:26 +0100
committerDaniel Schadt <kingdread@gmx.de>2022-12-22 18:40:26 +0100
commitc0c8e764a8819ba10066beb4ca829488ceed4e9d (patch)
treee37c3161d0315ec6ce942f340ee6f92f318b8fbd /tests/playwright/conftest.py
parentc1a8f099199e3b70da6ff569d8aaa72df5eaf809 (diff)
downloadfietsboek-c0c8e764a8819ba10066beb4ca829488ceed4e9d.tar.gz
fietsboek-c0c8e764a8819ba10066beb4ca829488ceed4e9d.tar.bz2
fietsboek-c0c8e764a8819ba10066beb4ca829488ceed4e9d.zip
reorder playwright helper structure
To add more playwright tests and to cut down on the duplicated code, the helper methods have been put into a small class. This way, we don't have to add 100 pytest fixtures.
Diffstat (limited to 'tests/playwright/conftest.py')
-rw-r--r--tests/playwright/conftest.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/playwright/conftest.py b/tests/playwright/conftest.py
index dedaf22..1130df2 100644
--- a/tests/playwright/conftest.py
+++ b/tests/playwright/conftest.py
@@ -1,7 +1,17 @@
+import datetime
import socket
import threading
+from typing import Optional
from wsgiref import simple_server
+from pyramid.authentication import AuthTktCookieHelper
+from pyramid.testing import DummyRequest
+
+from testutils import load_gpx_asset
+from fietsboek import models
+from fietsboek.models.track import Visibility, TrackType
+from fietsboek.config import Config
+
import pytest
@@ -46,3 +56,79 @@ def dbaccess(app):
"""
session_factory = app.registry["dbsession_factory"]
return session_factory()
+
+
+class Helper:
+ """Helper to insert objects for testing into the database and similar."""
+
+ def __init__(self, dbaccess, app_settings, page, data_manager):
+ self.dbaccess = dbaccess
+ self.app_settings = app_settings
+ self.page = page
+ self.data_manager = data_manager
+ self._johnny = None
+
+ def john_doe(self) -> models.User:
+ """Provides a test user (John Doe).
+
+ This fixture either returns the existing John or creates a new one.
+ """
+ if self._johnny:
+ return self._johnny
+
+ with self.dbaccess:
+ user = models.User(name="John Doe", email="john@doe.com", is_verified=True)
+ user.set_password("password")
+ self.dbaccess.add(user)
+ self.dbaccess.commit()
+ self.dbaccess.refresh(user, ["id"])
+ self.dbaccess.expunge(user)
+ self._johnny = user
+ return user
+
+ def login(self, user: Optional[models.User] = None):
+ """Logs the given user in by setting the auth cookie."""
+ if user is None:
+ user = self.john_doe()
+ config = Config.construct(session_key=self.app_settings["session_key"])
+ secret = config.derive_secret("auth-cookie")
+ helper = AuthTktCookieHelper(secret)
+ headers = helper.remember(DummyRequest(), str(user.id))
+ for _, header_val in headers:
+ cookie = header_val.split(";")[0]
+ name, value = cookie.split("=", 1)
+ self.page.context.add_cookies(
+ [
+ {"name": name, "value": value, "domain": "localhost", "path": "/"},
+ ]
+ )
+
+ def add_track(
+ self, user: Optional[models.User] = None, track_name: str = "Teasi_1.gpx.gz"
+ ) -> models.Track:
+ """Add a track to the given user.
+
+ If the user is None, John Doe is used.
+ """
+ if user is None:
+ user = self.john_doe()
+ with self.dbaccess:
+ user = self.dbaccess.merge(user)
+ track = models.Track(
+ title="Another awesome track",
+ visibility=Visibility.PRIVATE,
+ description="Another description",
+ )
+ track.date = datetime.datetime.now(datetime.timezone.utc)
+ user.tracks.append(track)
+ self.dbaccess.flush()
+ self.dbaccess.refresh(track, ["id"])
+ self.dbaccess.expunge(track)
+ self.data_manager.initialize(track.id).compress_gpx(load_gpx_asset(track_name))
+ self.dbaccess.commit()
+ return track
+
+
+@pytest.fixture
+def playwright_helper(dbaccess, app_settings, page, data_manager):
+ return Helper(dbaccess, app_settings, page, data_manager)