pages.py
python
sha256:f7413c1abec6d6a8e5bdabcd4cc7ad5fcd059ce6a6f50f566e2b0118a0a8b578
tests: make tests/docs_contract/ database-free by directory…
Opus 5.5
10 days ago
| 1 | """Shared page inventory for the /muse docs contract tests (musehub#242). |
| 2 | |
| 3 | ``ui_docs._PHASES`` decides which pages exist; ``ticket`` maps a page to the docs-audit |
| 4 | ticket that owns its fixes, so a known finding can be strict-xfailed against it. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from unittest.mock import patch |
| 10 | |
| 11 | import pytest |
| 12 | from fastapi.testclient import TestClient |
| 13 | from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware |
| 14 | |
| 15 | from musehub.api.routes.musehub import install, ui_docs |
| 16 | from musehub.api.routes.musehub.ui_docs import _PHASES |
| 17 | from musehub.main import app |
| 18 | from tests.docs_contract.registry_check import DOCS_ROOT, phase_path |
| 19 | |
| 20 | SECTION_PAGES = [phase_path(p[0]) for p in _PHASES] |
| 21 | PAGES = [DOCS_ROOT] + SECTION_PAGES |
| 22 | |
| 23 | |
| 24 | def ticket(path: str) -> str: |
| 25 | """Owning docs-audit page ticket: #243 is the landing page, #244 + NN is section NN.""" |
| 26 | if path == DOCS_ROOT: |
| 27 | return "musehub#243" |
| 28 | num = next(p[1] for p in _PHASES if phase_path(p[0]) == path) |
| 29 | return f"musehub#{244 + int(num)}" |
| 30 | |
| 31 | |
| 32 | def xfail_known(path: str, known: set[str], what: str) -> object: |
| 33 | """``path`` as a test parameter, strict-xfailed against its page ticket when it is a known finding.""" |
| 34 | if path in known: |
| 35 | # raises=: only the finding's own assert is expected; a setup error stays an error (musehub#273). |
| 36 | return pytest.param(path, marks=pytest.mark.xfail(strict=True, raises=AssertionError, reason=f"{ticket(path)}: {what}")) |
| 37 | return path |
| 38 | |
| 39 | |
| 40 | def _render(client: TestClient, paths: list[str], headers: dict[str, str] | None = None) -> dict[str, str]: |
| 41 | pin = "0.0.0-docs-contract" # no release pointer: pin the version both docs and install.sh would read |
| 42 | with patch.object(ui_docs, "_latest_published_muse_version", return_value=pin), \ |
| 43 | patch.object(install, "_latest_published_muse_version", return_value=pin): |
| 44 | responses = {path: client.get(path, headers=headers) for path in paths} |
| 45 | bad = {p: r.status_code for p, r in responses.items() if r.status_code != 200} |
| 46 | assert not bad, f"routes did not render: {bad}" |
| 47 | return {p: r.text for p, r in responses.items()} |
| 48 | |
| 49 | |
| 50 | def render_all() -> dict[str, str]: |
| 51 | """Every docs page's HTML, rendered in-process: no lifespan, no database, no release pointer.""" |
| 52 | return _render(TestClient(app), PAGES) # no context manager → no lifespan → no database |
| 53 | |
| 54 | |
| 55 | # Production topology (deploy/nginx-cf.conf, deploy/deploy.sh, entrypoint.sh): nginx terminates TLS |
| 56 | # and proxies plain HTTP to the slot's published port 127.0.0.1:<port>; Docker forwards that into the |
| 57 | # container, where uvicorn runs with --proxy-headers and so honours X-Forwarded-Proto only from its |
| 58 | # --forwarded-allow-ips addresses. Inside the container the proxy's connection arrives from the |
| 59 | # bridge network's gateway, not from loopback. |
| 60 | PUBLIC_HOST = "musehub.ai" |
| 61 | BRIDGE_GATEWAY_PEER = ("172.18.0.1", 40000) |
| 62 | LOOPBACK_PEER = ("127.0.0.1", 40000) |
| 63 | |
| 64 | |
| 65 | def render_behind_proxy(paths: list[str], trusted_hosts: str, peer: tuple[str, int]) -> dict[str, str]: |
| 66 | """``paths`` as a visitor on ``https://musehub.ai`` receives them through nginx → uvicorn.""" |
| 67 | client = TestClient( |
| 68 | ProxyHeadersMiddleware(app, trusted_hosts=trusted_hosts), |
| 69 | base_url=f"http://{PUBLIC_HOST}", # nginx forwards `Host: $host` over plain HTTP |
| 70 | client=peer, |
| 71 | ) |
| 72 | return _render(client, paths, headers={"X-Forwarded-Proto": "https", "X-Forwarded-For": "203.0.113.10"}) |
File History
4 commits
sha256:f7413c1abec6d6a8e5bdabcd4cc7ad5fcd059ce6a6f50f566e2b0118a0a8b578
tests: make tests/docs_contract/ database-free by directory…
Opus 5.5
10 days ago
sha256:eb10be5b0151404c767863448333d0cf0ad1b63219b5113abb902cb0132ae67e
Merge dev (#242 DOC_INF_04) into task/243-docs-landing (#243)
Opus 5.5
10 days ago
sha256:e8b5a50b0a352faa913ea7d5e72e615527564b52466009c56171d1d54f15b7ec
test(docs): check that /muse pages served over HTTPS hand o…
Opus 5.5
10 days ago
sha256:0e4f9453dfdeb2c10fb31581a5b195aa7af7d349f90e5904bd38e1c1f7d19cc0
test(docs): check every documented muse command against the…
Opus 5.5
10 days ago