Skip to content

Commit 1d9d664

Browse files
authored
Merge pull request #194 from akquinet/192
tests for index & author quote
2 parents 27171e3 + 7ff6c7a commit 1d9d664

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,18 @@ The index page can be deactivated with the `index_enabled` option and customized
300300

301301
```yaml
302302
index_enabled: false # default is true
303+
304+
# Single line HTML
303305
index_html: "<html><body><h1>PowerDNS API Proxy</h1></body></html>"
306+
307+
# Or multiline HTML
308+
index_html: |
309+
<html>
310+
<body>
311+
<h1>PowerDNS API Proxy</h1>
312+
<p>Custom content here</p>
313+
</body>
314+
</html>
304315
```
305316

306317
## Development

powerdns_api_proxy/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class ProxyConfig(BaseModel):
157157
<p><a href="/docs">Swagger Docs</a></p>
158158
<q>The Domain Name Server (DNS) is the Achilles heel of the Web.<br>
159159
The important thing is that it's managed responsibly.</q>
160+
<p style="margin-top: 1em; font-size: 0.9em; color: #666;">— Tim Berners-Lee</p>
160161
</center>
161162
</body>
162163
</html>

tests/unit/config_parsing_test.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import tempfile
2+
from pathlib import Path
3+
4+
from powerdns_api_proxy.config import load_config
5+
6+
7+
def test_index_html_from_yaml_single_line():
8+
config_content = """
9+
pdns_api_url: "https://test.example.com"
10+
pdns_api_token: "testtoken123"
11+
index_html: "<html><body><h1>PowerDNS API Proxy</h1></body></html>"
12+
environments:
13+
- name: "Test"
14+
token_sha512: "127aab81f4caab9c00e72f26e4c5c4b20146201a1548a787494d999febf1b9422c1711932117f38d9be9efe46f78aa72d8f6a391101bedd6e200014f6738450d"
15+
zones:
16+
- name: "example.com"
17+
"""
18+
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml") as f:
19+
f.write(config_content)
20+
f.flush()
21+
22+
config_path = Path(f.name)
23+
config = load_config(config_path)
24+
assert (
25+
config.index_html == "<html><body><h1>PowerDNS API Proxy</h1></body></html>"
26+
)
27+
assert config.index_enabled is True
28+
29+
30+
def test_index_html_from_yaml_multiline():
31+
config_content = """
32+
pdns_api_url: "https://test.example.com"
33+
pdns_api_token: "testtoken123"
34+
index_html: |
35+
<html>
36+
<body>
37+
<h1>Custom Page</h1>
38+
</body>
39+
</html>
40+
environments:
41+
- name: "Test"
42+
token_sha512: "127aab81f4caab9c00e72f26e4c5c4b20146201a1548a787494d999febf1b9422c1711932117f38d9be9efe46f78aa72d8f6a391101bedd6e200014f6738450d"
43+
zones:
44+
- name: "example.com"
45+
"""
46+
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml") as f:
47+
f.write(config_content)
48+
f.flush()
49+
50+
config_path = Path(f.name)
51+
config = load_config(config_path)
52+
assert "<h1>Custom Page</h1>" in config.index_html
53+
assert config.index_enabled is True
54+
55+
56+
def test_index_disabled_from_yaml():
57+
config_content = """
58+
pdns_api_url: "https://test.example.com"
59+
pdns_api_token: "testtoken123"
60+
index_enabled: false
61+
environments:
62+
- name: "Test"
63+
token_sha512: "127aab81f4caab9c00e72f26e4c5c4b20146201a1548a787494d999febf1b9422c1711932117f38d9be9efe46f78aa72d8f6a391101bedd6e200014f6738450d"
64+
zones:
65+
- name: "example.com"
66+
"""
67+
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml") as f:
68+
f.write(config_content)
69+
f.flush()
70+
71+
config_path = Path(f.name)
72+
config = load_config(config_path)
73+
assert config.index_enabled is False

tests/unit/proxy_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,42 @@ def fixture_patch_pdns() -> Generator[AsyncMock, None, None]:
5656
yield pdns_patch
5757

5858

59+
def test_index_default_html():
60+
response = client.get("/")
61+
assert response.status_code == 200
62+
assert "text/html" in response.headers["content-type"]
63+
assert "PowerDNS API Proxy" in response.text
64+
65+
66+
def test_index_custom_html():
67+
custom_html = "<html><body><h1>Custom Page</h1></body></html>"
68+
custom_config = ProxyConfig(
69+
pdns_api_token="blaaa",
70+
pdns_api_url="bluub",
71+
environments=[dummy_proxy_environment],
72+
index_html=custom_html,
73+
)
74+
75+
with patch("powerdns_api_proxy.proxy.config", custom_config):
76+
response = client.get("/")
77+
assert response.status_code == 200
78+
assert response.text == custom_html
79+
assert "<h1>Custom Page</h1>" in response.text
80+
81+
82+
def test_index_disabled():
83+
custom_config = ProxyConfig(
84+
pdns_api_token="blaaa",
85+
pdns_api_url="bluub",
86+
environments=[dummy_proxy_environment],
87+
index_enabled=False,
88+
)
89+
90+
with patch("powerdns_api_proxy.proxy.config", custom_config):
91+
response = client.get("/")
92+
assert response.status_code == 404
93+
94+
5995
def test_api_root(fixture_patch_dummy_config):
6096
answer = client.get("/api", headers={"X-API-Key": dummy_proxy_environment_token})
6197
data = answer.json()

0 commit comments

Comments
 (0)