-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_custom_css.py
More file actions
130 lines (97 loc) · 4.22 KB
/
Copy pathtest_custom_css.py
File metadata and controls
130 lines (97 loc) · 4.22 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Tests for custom CSS injection feature.
"""
from unittest.mock import patch
import pytest
from httpx import ASGITransport, AsyncClient
from docs_server.main import app
@pytest.fixture
def temp_docs(tmp_path):
"""Create temporary docs with required files."""
docs = tmp_path / "docs"
docs.mkdir()
(docs / "index.md").write_text("# Welcome\n\nHome page.")
(docs / "sidebar.md").write_text("# Nav\n\n* [Home](index.md)")
(docs / "topbar.md").write_text("# Top\n\n## right\n* [Link](index.md)")
return docs
@pytest.mark.asyncio
async def test_custom_css_endpoint_200_when_file_exists(temp_docs):
"""GET /custom.css returns 200 with text/css when custom.css exists."""
(temp_docs / "custom.css").write_text("/* custom */ body { color: red; }")
from docs_server.config import settings
with (
patch.object(settings, "DOCS_ROOT", temp_docs),
patch.object(settings, "CUSTOM_CSS", "custom.css"),
patch.object(settings, "DEBUG", False),
):
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://localhost:8080",
) as client:
response = await client.get("/custom.css")
assert response.status_code == 200
assert "text/css" in response.headers.get("content-type", "")
assert "body { color: red; }" in response.text
@pytest.mark.asyncio
async def test_custom_css_endpoint_404_when_file_missing(temp_docs):
"""GET /custom.css returns 404 when custom.css does not exist."""
from docs_server.config import settings
with (
patch.object(settings, "DOCS_ROOT", temp_docs),
patch.object(settings, "CUSTOM_CSS", "custom.css"),
):
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://localhost:8080",
) as client:
response = await client.get("/custom.css")
assert response.status_code == 404
@pytest.mark.asyncio
async def test_custom_css_serves_theme_css_when_configured(temp_docs):
"""CUSTOM_CSS=theme.css serves theme.css at /custom.css."""
(temp_docs / "theme.css").write_text("/* theme */ :root { --accent: blue; }")
from docs_server.config import settings
with (
patch.object(settings, "DOCS_ROOT", temp_docs),
patch.object(settings, "CUSTOM_CSS", "theme.css"),
):
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://localhost:8080",
) as client:
response = await client.get("/custom.css")
assert response.status_code == 200
assert "--accent: blue" in response.text
def test_template_includes_custom_css_link_when_passed():
"""Template includes link when custom_css_url is passed."""
from docs_server.templates import create_html_template
result = create_html_template("<p>Hi</p>", custom_css_url="/custom.css")
assert 'href="/custom.css"' in result
assert '<link rel="stylesheet"' in result
def test_template_no_custom_css_link_when_none():
"""Template has no custom CSS link when custom_css_url is None."""
from docs_server.templates import create_html_template
result = create_html_template("<p>Hi</p>", custom_css_url=None)
assert 'href="/custom.css"' not in result
def test_get_custom_css_path_returns_path_when_exists(tmp_path, monkeypatch):
"""get_custom_css_path returns Path when file exists."""
from docs_server.config import settings
from docs_server.helpers import get_custom_css_path
docs = tmp_path / "docs"
docs.mkdir()
(docs / "custom.css").write_text("/* test */")
monkeypatch.setattr(settings, "DOCS_ROOT", docs)
monkeypatch.setattr(settings, "CUSTOM_CSS", "custom.css")
path = get_custom_css_path()
assert path is not None
assert path.name == "custom.css"
def test_get_custom_css_path_returns_none_when_missing(tmp_path, monkeypatch):
"""get_custom_css_path returns None when file does not exist."""
from docs_server.config import settings
from docs_server.helpers import get_custom_css_path
docs = tmp_path / "docs"
docs.mkdir()
monkeypatch.setattr(settings, "DOCS_ROOT", docs)
monkeypatch.setattr(settings, "CUSTOM_CSS", "custom.css")
path = get_custom_css_path()
assert path is None