aboutsummaryrefslogtreecommitdiff
path: root/tests/playwright/test_basic.py
blob: 4c6f9d69e09e360e013735b807fe7a28e5b7116c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import datetime

import pytest
from pyramid.authentication import AuthTktCookieHelper
from pyramid.testing import DummyRequest
from playwright.sync_api import Page, expect
from sqlalchemy import select

from testutils import load_gpx_asset
from fietsboek import models
from fietsboek.models.track import Visibility
from fietsboek.config import Config


@pytest.fixture
def john_doe(dbaccess):
    """Provides a test user (John Doe).

    This fixture either returns the existing John or creates a new one.
    """
    query = models.User.query_by_email("john@doe.com")
    result = dbaccess.execute(query).scalar_one_or_none()
    if result:
        return result
    with dbaccess:
        user = models.User(name="John Doe", email="john@doe.com", is_verified=True)
        user.set_password("password")
        dbaccess.add(user)
        dbaccess.commit()
        return user


def do_login(settings: dict, page: Page, user: models.User):
    """Logs the given user in by setting the auth cookie."""
    config = Config.construct(session_key=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)
        page.context.add_cookies([
            {"name": name, "value": value, "domain": "localhost", "path": "/"},
        ])


def test_homepage(page: Page):
    page.goto("/")
    assert "Welcome to Fietsboek!" in page.content()
    assert "Here you can …" in page.content()


def test_login_failed(page: Page):
    page.goto("/")
    page.get_by_role("button", name="User").click()
    page.get_by_text("Login").click()

    page.get_by_label("E-Mail").fill("not-john@doe.com")
    page.get_by_label("Password").fill("password")
    page.get_by_role("button", name="Login").click()

    expect(page.locator(".alert")).to_have_text("Invalid login credentials")


def test_login_success(page: Page, john_doe):

    page.goto("/")
    page.get_by_role("button", name="User").click()
    page.get_by_text("Login").click()

    page.get_by_label("E-Mail").fill("john@doe.com")
    page.get_by_label("Password").fill("password")
    page.get_by_role("button", name="Login").click()

    expect(page.locator(".alert")).to_have_text("You are now logged in")


def test_upload(page: Page, john_doe, app_settings, tmp_path, dbaccess):
    do_login(app_settings, page, john_doe)
    page.goto("/")
    page.get_by_text("Upload").click()

    # We unpack one of the test GPX files
    gpx_data = load_gpx_asset("Teasi_1.gpx.gz")
    gpx_path = tmp_path / "Upload.gpx"
    with open(gpx_path, "wb") as gpx_fobj:
        gpx_fobj.write(gpx_data)

    page.get_by_label("GPX file").set_input_files(gpx_path)
    page.locator(".bi-upload").click()

    # We now fill in most of the data
    page.get_by_label("Title").fill("An awesome track!")
    page.get_by_label("Date").type("07302022")
    page.get_by_label("Date").press("Tab")
    page.get_by_label("Date").type("12:41")
    page.get_by_label("Visibility").select_option(label="Public")
    page.get_by_label("Tags").fill("Tolle Tour")
    page.get_by_role("button", name="Add Tag").click()
    page.get_by_label("Description").fill("Beschreibung der tollen Tour")

    page.locator(".btn", has_text="Upload").click()

    # Once we have finished the upload, extract the ID of the track and check
    # the properties
    new_track_id = int(page.url.rsplit("/", 1)[1])
    track = dbaccess.execute(select(models.Track).filter_by(id=new_track_id)).scalar_one()

    assert track.title == "An awesome track!"
    assert track.date.replace(tzinfo=None) == datetime.datetime(2022, 7, 30, 12, 41)
    assert track.visibility == Visibility.PUBLIC
    assert track.text_tags() == {"Tolle Tour"}
    assert track.description == "Beschreibung der tollen Tour"