|
| 1 | +""" |
| 2 | +Pytest configuration for geometadata E2E tests. |
| 3 | +
|
| 4 | +Provides fixtures for Django live server and test data setup. |
| 5 | +Uses pytest-playwright for browser automation. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +import pytest |
| 11 | +from django.contrib.staticfiles.testing import StaticLiveServerTestCase |
| 12 | +from playwright.sync_api import Page |
| 13 | + |
| 14 | +from plugins.geometadata import plugin_settings |
| 15 | +from plugins.geometadata.models import ArticleGeometadata |
| 16 | +from utils.testing import helpers |
| 17 | + |
| 18 | + |
| 19 | +# Allow async unsafe operations for Django ORM in Playwright tests |
| 20 | +os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE", "true") |
| 21 | + |
| 22 | + |
| 23 | +class GeometadataLiveServerTestCase(StaticLiveServerTestCase): |
| 24 | + """ |
| 25 | + Live server test case with geometadata fixtures. |
| 26 | +
|
| 27 | + Provides a running Django server and pre-populated test data |
| 28 | + for E2E testing with Playwright. |
| 29 | + """ |
| 30 | + |
| 31 | + host = "localhost" |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def setUpClass(cls): |
| 35 | + super().setUpClass() |
| 36 | + # Install plugin settings |
| 37 | + plugin_settings.install() |
| 38 | + |
| 39 | + # Create core fixtures |
| 40 | + cls.press = helpers.create_press() |
| 41 | + cls.journal, _ = helpers.create_journals() |
| 42 | + |
| 43 | + # Create editor user |
| 44 | + cls.editor = helpers.create_user( |
| 45 | + "editor@test.com", |
| 46 | + ["editor"], |
| 47 | + cls.journal, |
| 48 | + is_staff=True, |
| 49 | + is_active=True, |
| 50 | + ) |
| 51 | + cls.editor.set_password("testpass123") |
| 52 | + cls.editor.save() |
| 53 | + |
| 54 | + # Create published article with geometadata |
| 55 | + cls.article = helpers.create_article(cls.journal, with_author=True) |
| 56 | + cls.article.stage = "Published" |
| 57 | + cls.article.date_published = "2024-01-15" |
| 58 | + cls.article.save() |
| 59 | + |
| 60 | + cls.geometadata = ArticleGeometadata.objects.create( |
| 61 | + article=cls.article, |
| 62 | + geometry_wkt="POINT(13.4 52.5)", |
| 63 | + place_name="Berlin", |
| 64 | + admin_units="Berlin, Germany", |
| 65 | + temporal_periods=[["2020-01-01", "2021-12-31"]], |
| 66 | + ) |
| 67 | + |
| 68 | + # Create issue with the article |
| 69 | + cls.issue = helpers.create_issue( |
| 70 | + cls.journal, vol=1, number=1, articles=[cls.article] |
| 71 | + ) |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def tearDownClass(cls): |
| 75 | + super().tearDownClass() |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture(scope="module") |
| 79 | +def live_server(): |
| 80 | + """ |
| 81 | + Fixture providing a live Django server for E2E tests. |
| 82 | +
|
| 83 | + Uses Django's StaticLiveServerTestCase to serve static files. |
| 84 | + """ |
| 85 | + server = GeometadataLiveServerTestCase("run") |
| 86 | + server.setUpClass() |
| 87 | + try: |
| 88 | + yield server |
| 89 | + finally: |
| 90 | + server.tearDownClass() |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +def base_url(live_server): |
| 95 | + """Return the base URL of the live server.""" |
| 96 | + return live_server.live_server_url |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def journal(live_server): |
| 101 | + """Return the test journal.""" |
| 102 | + return live_server.journal |
| 103 | + |
| 104 | + |
| 105 | +@pytest.fixture |
| 106 | +def article(live_server): |
| 107 | + """Return the test article with geometadata.""" |
| 108 | + return live_server.article |
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture |
| 112 | +def geometadata(live_server): |
| 113 | + """Return the test geometadata.""" |
| 114 | + return live_server.geometadata |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +def issue(live_server): |
| 119 | + """Return the test issue.""" |
| 120 | + return live_server.issue |
| 121 | + |
| 122 | + |
| 123 | +@pytest.fixture |
| 124 | +def editor(live_server): |
| 125 | + """Return the editor user.""" |
| 126 | + return live_server.editor |
| 127 | + |
| 128 | + |
| 129 | +@pytest.fixture |
| 130 | +def authenticated_page(page: Page, base_url: str, editor, journal) -> Page: |
| 131 | + """ |
| 132 | + Return a page with an authenticated editor session. |
| 133 | +
|
| 134 | + Logs in via the Django login page. |
| 135 | + """ |
| 136 | + # Navigate to login page |
| 137 | + login_url = f"{base_url}/login/" |
| 138 | + page.goto(login_url) |
| 139 | + |
| 140 | + # Fill in credentials |
| 141 | + page.fill('input[name="user_name"]', editor.email) |
| 142 | + page.fill('input[name="user_pass"]', "testpass123") |
| 143 | + page.click('button[type="submit"], input[type="submit"]') |
| 144 | + |
| 145 | + # Wait for redirect |
| 146 | + page.wait_for_load_state("networkidle") |
| 147 | + |
| 148 | + return page |
| 149 | + |
| 150 | + |
| 151 | +@pytest.fixture |
| 152 | +def map_selectors(): |
| 153 | + """ |
| 154 | + Return CSS selectors for map elements. |
| 155 | +
|
| 156 | + Centralizes selector definitions for maintainability. |
| 157 | + """ |
| 158 | + return { |
| 159 | + "map_container": "#geometadata-map, .geometadata-map, [data-geometadata-map]", |
| 160 | + "leaflet_map": ".leaflet-container", |
| 161 | + "leaflet_marker": ".leaflet-marker-icon", |
| 162 | + "leaflet_popup": ".leaflet-popup", |
| 163 | + "leaflet_tile": ".leaflet-tile", |
| 164 | + "loading_indicator": ".geometadata-loading", |
| 165 | + "error_message": ".geometadata-error", |
| 166 | + "download_button": 'a[href*="geojson"], button[data-download="geojson"]', |
| 167 | + } |
0 commit comments