aboutsummaryrefslogtreecommitdiff
path: root/lib/project/tests/test_gui_assets.py
blob: 961f15edae6692ed995d02a1d805097663cf7144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Packaged, CSP-compatible frontend assets do not require a bundler or CDN."""
from html.parser import HTMLParser

import pytest

pytest.importorskip("fastapi")
from fastapi.testclient import TestClient

from producer.gui import create_app


class AssetReferences(HTMLParser):
    def __init__(self):
        super().__init__()
        self.scripts = []
        self.stylesheets = []
        self.inline_attributes = []

    def handle_starttag(self, tag, attrs):
        attrs = dict(attrs)
        self.inline_attributes.extend(name for name in attrs if name == "style" or name.startswith("on"))
        if tag == "script":
            self.scripts.append(attrs.get("src"))
        if tag == "link" and attrs.get("rel") == "stylesheet":
            self.stylesheets.append(attrs.get("href"))


def test_gui_packaged_external_assets_and_condensed_workflow(tmp_path):
    with TestClient(create_app(8765, output_dir=tmp_path), base_url="http://127.0.0.1:8765") as client:
        page = client.get("/")
        assert page.status_code == 200
        parser = AssetReferences()
        parser.feed(page.text)
        assert parser.scripts == ["/assets/app.js"]
        assert parser.stylesheets == ["/assets/style.css"]
        assert not parser.inline_attributes
        assert "unsafe-inline" not in page.headers["Content-Security-Policy"]
        assert "approximate, not an exact export preview" in page.text
        for name in ("Recording", "Settings", "Compare", "Export", "Activity"):
            assert f">{name}</h2>" in page.text or name in page.text
        for banned in ("#token", "private by design", "Make it sound like you"):
            assert banned not in page.text
        for path in parser.scripts + parser.stylesheets + ["/assets/state.js"]:
            response = client.get(path)
            assert response.status_code == 200, path
            assert response.headers["Cache-Control"] == "no-store"
            assert len(response.content) > 100
        assert client.get("/api/schema").status_code == 200