Initial Commit: Status Testing
This commit is contained in:
1 parent
1eff675f05
commit
42dd1f2d4a
286 files changed
+64800
-369
No files matched your search
@@ -0,0 +1,82 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("fastapi")
|
||||
pytest.importorskip("httpx")
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from surveybox import db
|
||||
from surveybox.settings import Settings
|
||||
from surveybox.upgrade import run_upgrade
|
||||
from surveybox.web.routes.pages import safe_next
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[
|
||||
(None, "/"),
|
||||
("", "/"),
|
||||
("/", "/"),
|
||||
("/forms/1?x=2", "/forms/1?x=2"),
|
||||
("https://evil.example", "/"),
|
||||
("//evil.example", "/"),
|
||||
("/\\evil.example", "/"),
|
||||
("/ok\r\nSet-Cookie: x=1", "/"),
|
||||
("relative", "/"),
|
||||
],
|
||||
)
|
||||
def test_safe_next(value: str | None, expected: str) -> None:
|
||||
assert safe_next(value) == expected
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(db_settings: Settings) -> TestClient:
|
||||
run_upgrade(db_settings, db.get_engine())
|
||||
from surveybox.web.app import create_app
|
||||
|
||||
return TestClient(create_app())
|
||||
|
||||
|
||||
@pytest.mark.db
|
||||
def test_health(client: TestClient) -> None:
|
||||
assert client.get("/health/live").json() == {"status": "ok"}
|
||||
ready = client.get("/health/ready")
|
||||
assert ready.status_code == 200
|
||||
assert ready.headers["cache-control"] == "no-store"
|
||||
|
||||
|
||||
@pytest.mark.db
|
||||
@pytest.mark.parametrize(
|
||||
("accept", "text"),
|
||||
[
|
||||
("de-DE,de;q=0.9", "Formulare und Abstimmungen, selbst gehostet."),
|
||||
("fr", "Formulaires et sondages, auto-hébergés."),
|
||||
("en-GB", "Forms and polls, self-hosted."),
|
||||
],
|
||||
)
|
||||
def test_index_in_three_languages(client: TestClient, accept: str, text: str) -> None:
|
||||
resp = client.get("/", headers={"Accept-Language": accept})
|
||||
assert resp.status_code == 200
|
||||
assert text in resp.text
|
||||
assert "http://testserver" not in resp.text # nur relative Links
|
||||
|
||||
|
||||
@pytest.mark.db
|
||||
def test_language_cookie_and_safe_redirect(client: TestClient) -> None:
|
||||
resp = client.get("/language/fr?next=//evil.example", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert resp.headers["location"] == "/"
|
||||
assert "surveybox_lang=fr" in resp.headers["set-cookie"]
|
||||
assert "httponly" in resp.headers["set-cookie"].lower()
|
||||
page = client.get("/", headers={"Accept-Language": "de"})
|
||||
assert "Formulaires et sondages, auto-hébergés." in page.text
|
||||
|
||||
|
||||
@pytest.mark.db
|
||||
def test_unknown_language_sets_no_cookie(client: TestClient) -> None:
|
||||
resp = client.get("/language/xx", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "set-cookie" not in resp.headers
|
||||
Reference in new issue
Block a user