public-release phases 1-3: read-only public mode, gunicorn container, demo bootstrap, CI, README
- before_request guard: visitors browse + explore; writes need PHYSCOM_ADMIN_TOKEN login - SameSite=Lax/HttpOnly cookies, persisted secret key, debug off unless FLASK_DEBUG=1 - non-root gunicorn container with healthcheck; physcom demo --if-empty on first start - landing page Top Concepts, write controls hidden from visitors - CI workflow, pyproject metadata, compose Ollama parity, README rewrite - tests/test_web_access.py: access control + GET smoke test of every page Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
161
tests/test_web_access.py
Normal file
161
tests/test_web_access.py
Normal file
@@ -0,0 +1,161 @@
|
||||
"""Web UI: read-only public mode, admin login, and a GET smoke test of every page."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from physcom.db.repository import Repository
|
||||
from physcom.db.schema import init_db
|
||||
from physcom.engine.constraint_resolver import ConstraintResolver
|
||||
from physcom.engine.pipeline import Pipeline
|
||||
from physcom.engine.scorer import Scorer
|
||||
from physcom.seed.transport_example import load_transport_seed
|
||||
|
||||
TOKEN = "test-admin-token"
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def db_file(tmp_path_factory):
|
||||
path = tmp_path_factory.mktemp("web") / "physcom.db"
|
||||
repo = Repository(init_db(path))
|
||||
load_transport_seed(repo)
|
||||
domain = repo.get_domain("urban_commuting")
|
||||
Pipeline(repo, ConstraintResolver(), Scorer(domain)).run(
|
||||
domain, ["platform", "actuator", "energy_storage"],
|
||||
score_threshold=0.01, passes=[1, 2, 3],
|
||||
)
|
||||
ids = {
|
||||
"entity_id": repo.list_entities()[0].id,
|
||||
"domain_id": domain.id,
|
||||
"combo_id": next(
|
||||
r["combination"].id for r in repo.get_all_results("urban_commuting")
|
||||
if r["pass_reached"] >= 3
|
||||
),
|
||||
}
|
||||
repo.conn.close()
|
||||
return path, ids
|
||||
|
||||
|
||||
def _app(monkeypatch, db_file, token: str | None, open_access: bool = False):
|
||||
from physcom_web.app import create_app
|
||||
monkeypatch.setenv("PHYSCOM_DB", str(db_file[0]))
|
||||
monkeypatch.setenv("FLASK_SECRET_KEY", "test")
|
||||
if token:
|
||||
monkeypatch.setenv("PHYSCOM_ADMIN_TOKEN", token)
|
||||
else:
|
||||
monkeypatch.delenv("PHYSCOM_ADMIN_TOKEN", raising=False)
|
||||
return create_app(open_access=open_access)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def visitor(monkeypatch, db_file):
|
||||
return _app(monkeypatch, db_file, TOKEN).test_client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin(monkeypatch, db_file):
|
||||
client = _app(monkeypatch, db_file, TOKEN).test_client()
|
||||
assert client.post("/login", data={"token": TOKEN}).status_code == 302
|
||||
return client
|
||||
|
||||
|
||||
def _public_pages(ids):
|
||||
return [
|
||||
"/", "/entities/", f"/entities/{ids['entity_id']}",
|
||||
"/domains/", f"/domains/{ids['domain_id']}",
|
||||
"/results/", "/results/urban_commuting",
|
||||
f"/results/urban_commuting/{ids['combo_id']}",
|
||||
"/login",
|
||||
]
|
||||
|
||||
|
||||
def _admin_pages():
|
||||
return ["/admin/", "/pipeline/", "/entities/new", "/domains/new", "/admin/snapshot/export"]
|
||||
|
||||
|
||||
def test_visitor_can_browse(visitor, db_file):
|
||||
for url in _public_pages(db_file[1]):
|
||||
assert visitor.get(url).status_code == 200, url
|
||||
|
||||
|
||||
def test_visitor_cannot_reach_admin_pages(visitor):
|
||||
for url in _admin_pages():
|
||||
resp = visitor.get(url)
|
||||
assert resp.status_code == 302 and "/login" in resp.location, url
|
||||
|
||||
|
||||
def test_visitor_cannot_write(visitor, db_file):
|
||||
ids = db_file[1]
|
||||
for url in [
|
||||
"/admin/wipe", "/admin/reseed", "/pipeline/run",
|
||||
f"/entities/{ids['entity_id']}/delete", f"/domains/{ids['domain_id']}/delete",
|
||||
"/results/urban_commuting/reset",
|
||||
f"/results/urban_commuting/{ids['combo_id']}/review",
|
||||
]:
|
||||
assert visitor.post(url).status_code == 403, url
|
||||
|
||||
|
||||
def test_visitor_can_explore(visitor, db_file):
|
||||
url = f"/results/urban_commuting/{db_file[1]['combo_id']}/explore"
|
||||
assert visitor.post(url, data={"platform_mass": "inf"}).status_code == 200
|
||||
|
||||
|
||||
def test_home_lists_top_concept(visitor):
|
||||
html = visitor.get("/").get_data(as_text=True)
|
||||
assert "Top Concepts" in html and "urban_commuting" in html
|
||||
|
||||
|
||||
def test_visitor_pages_hide_write_controls(visitor):
|
||||
html = visitor.get("/").get_data(as_text=True)
|
||||
assert 'class="readonly"' in html
|
||||
assert "/admin/" not in html and "/pipeline/" not in html
|
||||
|
||||
|
||||
def test_wrong_token_rejected(visitor):
|
||||
assert visitor.post("/login", data={"token": "nope"}).status_code == 401
|
||||
assert visitor.get("/admin/").status_code == 302
|
||||
|
||||
|
||||
def test_no_token_means_no_login(monkeypatch, db_file):
|
||||
client = _app(monkeypatch, db_file, None).test_client()
|
||||
assert client.post("/login", data={"token": ""}).status_code == 401
|
||||
assert client.post("/admin/wipe").status_code == 403
|
||||
|
||||
|
||||
def test_login_redirect_stays_on_site(visitor):
|
||||
resp = visitor.post("/login?next=//evil.example", data={"token": TOKEN})
|
||||
assert resp.location == "/"
|
||||
resp = visitor.post("/login?next=/admin/", data={"token": TOKEN})
|
||||
assert resp.location == "/admin/"
|
||||
|
||||
|
||||
def test_admin_can_reach_everything(admin, db_file):
|
||||
for url in _public_pages(db_file[1]) + _admin_pages():
|
||||
assert admin.get(url).status_code == 200, url
|
||||
html = admin.get("/").get_data(as_text=True)
|
||||
assert 'class="readonly"' not in html
|
||||
|
||||
|
||||
def test_open_access_dev_server(monkeypatch, db_file):
|
||||
app = _app(monkeypatch, db_file, None, open_access=True)
|
||||
assert app.test_client().get("/admin/").status_code == 200
|
||||
# Setting a token locks even the dev server.
|
||||
app = _app(monkeypatch, db_file, TOKEN, open_access=True)
|
||||
assert app.test_client().get("/admin/").status_code == 302
|
||||
|
||||
|
||||
def test_debug_off_by_default(monkeypatch, db_file):
|
||||
import physcom_web.app as web
|
||||
seen = {}
|
||||
monkeypatch.delenv("FLASK_DEBUG", raising=False)
|
||||
monkeypatch.setenv("PHYSCOM_DB", str(db_file[0]))
|
||||
monkeypatch.setattr("flask.Flask.run", lambda self, **kw: seen.update(kw))
|
||||
web.run()
|
||||
assert seen["debug"] is False and seen["host"] == "127.0.0.1"
|
||||
|
||||
|
||||
def test_cli_demo_if_empty_skips_populated_db(db_file):
|
||||
from click.testing import CliRunner
|
||||
from physcom.cli import main
|
||||
result = CliRunner().invoke(main, ["--db", str(db_file[0]), "demo", "--if-empty"])
|
||||
assert result.exit_code == 0 and "skipping" in result.output
|
||||
Reference in New Issue
Block a user