import http.client import shutil import threading from pathlib import Path from urllib.parse import urlencode import pytest import yaml from rtut import ui, user ROOT = Path(__file__).resolve().parent.parent def write(path: Path, data): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(yaml.safe_dump(data, sort_keys=False)) @pytest.fixture def srv(tmp_path): shutil.copy(ROOT / "runbooks/showcase-lighthouse/pack.yaml", tmp_path / "runbooks/show/pack.yaml") if (tmp_path / "runbooks/show").mkdir(parents=True) is None else None for f in (ROOT / "attribute-sets").glob("*.yaml"): write(tmp_path / "attribute-sets" / f.name, yaml.safe_load(f.read_text())) (tmp_path / "project-templates").mkdir() shutil.copy(ROOT / "project-templates/lighthouse-watch.yaml", tmp_path / "project-templates/lighthouse-watch.yaml") write(tmp_path / "projects/watch/project.yaml", { "spec": "project/v0.1", "id": "watch", "title": "Watch project", "description": "Old description", "environment": "rehearsal", "attribute_sets": ["stormy-coast", "user-attributes"], "attributes": {"logdir": "/tmp/rtut-shell-x", "keeper": "Ada"}, "runbooks": [{"pack": "rtut.showcase-lighthouse"}]}) s = ui.serve([tmp_path], 0) threading.Thread(target=s.serve_forever, daemon=True).start() s.repo = tmp_path yield s s.shutdown() def req(s, method, path, form=None): c = http.client.HTTPConnection("127.0.0.1", s.server_address[1]) body = urlencode(form) if form is not None else None c.request(method, path, body, {"Host": "127.0.0.1", "Content-Type": "application/x-www-form-urlencoded"}) r = c.getresponse() return r.status, r.getheader("Location"), r.read().decode() def post(s, path, **form): return req(s, "POST", path, {"_t": s.app.token, **form}) # ---- shell: sidebar, user, theme --------------------------------------------------------------------------------- PAGES = ["/", "/projects", "/runbooks", "/activity", "/attribute-sets", "/user", "/pack/0", "/project/watch", "/template/lighthouse-watch", "/attribute-set/harbour-defaults"] @pytest.mark.parametrize("path", PAGES) def test_every_page_has_the_sidebar_with_sections_user_and_theme_switch(srv, path): st, _, body = req(srv, "GET", path) assert st == 200 assert "