57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
import logging
|
|
|
|
import yaml
|
|
|
|
from app.services.mitmproxy.templates import list_file_templates
|
|
|
|
|
|
def test_file_templates_use_four_space_indentation():
|
|
for template in list_file_templates():
|
|
for line in template.code.splitlines():
|
|
assert "\t" not in line, f"{template.id} contains a tab: {line!r}"
|
|
if not line.strip():
|
|
continue
|
|
indent = len(line) - len(line.lstrip(" "))
|
|
assert indent % 4 == 0, (
|
|
f"{template.id} has non-4-space indentation: {line!r}"
|
|
)
|
|
|
|
|
|
def test_list_file_templates_loads_yaml_backed_templates():
|
|
templates = list_file_templates()
|
|
|
|
assert templates
|
|
assert all(template.origin == "file" for template in templates)
|
|
assert all(template.built_in is True for template in templates)
|
|
assert all(template.protocol in {"http", "tcp"} for template in templates)
|
|
|
|
|
|
def test_list_file_templates_skips_invalid_files(tmp_path, caplog):
|
|
path = tmp_path / "invalid.yaml"
|
|
path.write_text("[]\n", encoding="utf-8")
|
|
|
|
with caplog.at_level(logging.WARNING):
|
|
templates = list_file_templates(tmp_path)
|
|
|
|
assert templates == []
|
|
assert "Skipping template file" in caplog.text
|
|
|
|
|
|
def test_list_file_templates_skips_duplicate_ids(tmp_path, caplog):
|
|
payload = {
|
|
"id": "duplicate",
|
|
"name": "Duplicate",
|
|
"protocol": "http",
|
|
"description": "Duplicate template",
|
|
"code": "from mitmproxy import http\n",
|
|
}
|
|
document = yaml.safe_dump(payload, sort_keys=False)
|
|
(tmp_path / "one.yaml").write_text(document, encoding="utf-8")
|
|
(tmp_path / "two.yaml").write_text(document, encoding="utf-8")
|
|
|
|
with caplog.at_level(logging.WARNING):
|
|
templates = list_file_templates(tmp_path)
|
|
|
|
assert templates == []
|
|
assert "duplicate file-backed template id" in caplog.text
|