dns-blocklist-mirror/tests/test_config.py
XLion 7d99d7e4e8
All checks were successful
Checks / checks (push) Successful in 45s
Deploy Pages / deploy (push) Successful in 20s
Split tests by responsibility
2026-08-15 22:34:31 +08:00

214 lines
4.3 KiB
Python

from __future__ import annotations
from pathlib import Path
import pytest
from dns_blocklist_mirror.config import parse_config
from tests._support import run_cli
@pytest.mark.parametrize(
("config", "expected_error"),
[
(
"""\
version: 2
mirrors: {}
""",
"version: expected 1",
),
(
"""\
version: 1
mirrors: {}
unexpected: true
""",
'root: unknown key "unexpected"',
),
(
"""\
version: 1
mirrors:
hagezi:
- destnation: pro.txt
sources:
- https://example.com/pro.txt
""",
'mirrors.hagezi[0]: unknown key "destnation"',
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources: []
""",
"mirrors.hagezi[0].sources: expected at least one HTTPS URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- http://example.com/pro.txt
""",
"mirrors.hagezi[0].sources[0]: expected an HTTPS URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- https://user:[email protected]/pro.txt
""",
"mirrors.hagezi[0].sources[0]: expected an HTTPS URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- https://example.com:65536/pro.txt
""",
"mirrors.hagezi[0].sources[0]: expected an HTTPS URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- "https://example.com/pro.txt#fragment"
""",
"mirrors.hagezi[0].sources[0]: expected an HTTPS URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- "https://example.com/pro.txt "
""",
"mirrors.hagezi[0].sources[0]: expected an HTTPS URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: ../pro.txt
sources:
- https://example.com/pro.txt
""",
"mirrors.hagezi[0].destination: expected a safe relative path",
),
(
"""\
version: 1
mirrors:
HaGeZi:
- destination: pro.txt
sources:
- https://example.com/pro.txt
""",
'mirrors: invalid provider name "HaGeZi"',
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- https://example.com/pro.txt
- https://example.com/pro.txt
""",
"mirrors.hagezi[0].sources: duplicate URL",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- https://example.com/pro.txt
- destination: pro.txt
sources:
- https://example.com/other.txt
""",
'mirrors.hagezi[1].destination: duplicate output "hagezi/pro.txt"',
),
(
"""\
version: 1
version: 1
mirrors: {}
""",
'YAML contains duplicate key "version"',
),
(
"""\
? [version]
: 1
mirrors: {}
""",
"YAML mapping keys must be scalar values",
),
(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- "https://\\0/list.txt"
""",
"mirrors.hagezi[0].sources[0]: expected an HTTPS URL",
),
],
)
def test_cli_rejects_invalid_configuration(
tmp_path: Path,
config: str,
expected_error: str,
) -> None:
config_path = tmp_path / "mirrors.yaml"
config_path.write_text(config, encoding="utf-8")
output_path = tmp_path / "public"
result = run_cli(config_path, output_path)
assert result.returncode == 2
assert expected_error in result.stderr
assert "Traceback" not in result.stderr
assert not output_path.exists()
def test_config_accepts_a_valid_https_port() -> None:
config = parse_config(
{
"version": 1,
"mirrors": {
"hagezi": [
{
"destination": "pro.txt",
"sources": ["https://example.com:443/pro.txt"],
}
],
},
}
)
assert config.mirrors[0].sources == ("https://example.com:443/pro.txt",)