@@ -89,3 +89,38 @@ def test_unraid_deploy_requires_dedicated_ssh_alias() -> None:
|
||||
assert "git ls-files --cached --others --exclude-standard -z" in deploy_script
|
||||
assert '[[ -e "$tracked_path" ]]' in deploy_script
|
||||
assert "/health/ready/" in deploy_script
|
||||
|
||||
|
||||
def test_public_https_origin_uses_external_port_not_internal_app_port() -> None:
|
||||
deploy_script = (ROOT / "scripts" / "deploy_docker.sh").read_text(encoding="utf-8")
|
||||
assert 'app_public_port="${APP_PUBLIC_PORT:-}"' in deploy_script
|
||||
assert 'app_public_port="443"' in deploy_script
|
||||
assert 'write_env "PUBLIC_BASE_URL" "$app_base_url" 1' in deploy_script
|
||||
assert 'https://$app_host:$app_port' not in deploy_script
|
||||
|
||||
|
||||
def test_health_endpoints_are_exempt_from_internal_https_redirect() -> None:
|
||||
assert r"^health/(?:live|ready)/$" in settings.SECURE_REDIRECT_EXEMPT
|
||||
|
||||
|
||||
def test_unraid_uses_dedicated_shared_cache_database() -> None:
|
||||
compose = yaml.safe_load((ROOT / "docker-compose.unraid.yml").read_text(encoding="utf-8"))
|
||||
environment = compose["services"]["app"]["environment"]
|
||||
assert environment["CACHE_URL"] == "redis://127.0.0.1:6379/1"
|
||||
assert environment["HEALTHCHECK_REQUIRE_CACHE"] == "1"
|
||||
|
||||
|
||||
def test_unraid_database_url_is_built_inside_the_container() -> None:
|
||||
compose = yaml.safe_load((ROOT / "docker-compose.unraid.yml").read_text(encoding="utf-8"))
|
||||
environment = compose["services"]["app"]["environment"]
|
||||
assert "DATABASE_URL" not in environment
|
||||
|
||||
|
||||
def test_deploy_uses_selected_environment_file_for_compose() -> None:
|
||||
compose = yaml.safe_load((ROOT / "docker-compose.yml").read_text(encoding="utf-8"))
|
||||
deploy_script = (ROOT / "scripts" / "deploy_docker.sh").read_text(encoding="utf-8")
|
||||
|
||||
assert compose["x-app"]["env_file"] == ["${VACATURERADAR_ENV_FILE:-.env}"]
|
||||
assert 'export VACATURERADAR_ENV_FILE="$env_file"' in deploy_script
|
||||
assert 'docker compose --env-file "$env_file" -f "$compose_file" up -d --build' in deploy_script
|
||||
assert 'waarde uit ${env_file}' in deploy_script
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from django.test import override_settings
|
||||
|
||||
from apps.core.health import readiness
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
@override_settings(HEALTHCHECK_REQUIRE_CACHE=True)
|
||||
def test_readiness_checks_database_and_shared_cache():
|
||||
result = readiness()
|
||||
assert result.ok is True
|
||||
assert result.checks == {"database": "ok", "cache": "ok"}
|
||||
|
||||
|
||||
@override_settings(HEALTHCHECK_REQUIRE_CACHE=True)
|
||||
def test_readiness_reports_cache_failure(monkeypatch):
|
||||
def fail_cache(*args, **kwargs):
|
||||
raise ConnectionError
|
||||
|
||||
monkeypatch.setattr("apps.core.health.cache.set", fail_cache)
|
||||
result = readiness()
|
||||
assert result.ok is False
|
||||
assert result.checks["database"] == "ok"
|
||||
assert result.checks["cache"] == "error:ConnectionError"
|
||||
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.test import RequestFactory, override_settings
|
||||
|
||||
from apps.core.network import client_ip
|
||||
|
||||
|
||||
def _request(remote: str, forwarded: str = ""):
|
||||
request = RequestFactory().get("/")
|
||||
request.META["REMOTE_ADDR"] = remote
|
||||
if forwarded:
|
||||
request.META["HTTP_X_FORWARDED_FOR"] = forwarded
|
||||
return request
|
||||
|
||||
|
||||
@override_settings(TRUSTED_PROXY_CIDRS=[])
|
||||
def test_client_ip_ignores_untrusted_forwarded_header():
|
||||
assert client_ip(_request("203.0.113.20", "198.51.100.7")) == "203.0.113.20"
|
||||
|
||||
|
||||
@override_settings(TRUSTED_PROXY_CIDRS=["172.18.0.0/16"])
|
||||
def test_client_ip_uses_forwarded_chain_only_from_trusted_proxy():
|
||||
request = _request("172.18.0.4", "198.51.100.7, 172.18.0.3")
|
||||
assert client_ip(request) == "198.51.100.7"
|
||||
|
||||
|
||||
@override_settings(TRUSTED_PROXY_CIDRS=["172.18.0.0/16"])
|
||||
def test_client_ip_skips_invalid_forwarded_values():
|
||||
request = _request("172.18.0.4", "invalid, 198.51.100.9")
|
||||
assert client_ip(request) == "198.51.100.9"
|
||||
@@ -0,0 +1,126 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from config.settings import normalize_public_base_url
|
||||
from scripts.configure_public_url import ConfigurationError, configure_env, normalize_public_url
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("raw", "origin", "host"),
|
||||
[
|
||||
(
|
||||
"https://vacatureradar.itworx.tech/",
|
||||
"https://vacatureradar.itworx.tech",
|
||||
"vacatureradar.itworx.tech",
|
||||
),
|
||||
("https://jobs.example.be:8443", "https://jobs.example.be:8443", "jobs.example.be"),
|
||||
("http://127.0.0.1:1226", "http://127.0.0.1:1226", "127.0.0.1"),
|
||||
],
|
||||
)
|
||||
def test_public_url_normalization(raw, origin, host):
|
||||
assert normalize_public_base_url(raw) == (origin, host)
|
||||
assert normalize_public_url(raw) == (origin, host)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw",
|
||||
[
|
||||
"vacatureradar.itworx.tech",
|
||||
"ftp://vacatureradar.itworx.tech",
|
||||
"https://user:secret@example.org",
|
||||
"https://example.org/app",
|
||||
"https://example.org?debug=1",
|
||||
],
|
||||
)
|
||||
def test_public_url_rejects_unsafe_or_unsupported_values(raw):
|
||||
with pytest.raises((ImproperlyConfigured, ConfigurationError)):
|
||||
normalize_public_base_url(raw)
|
||||
|
||||
def test_configure_public_url_updates_only_public_security_settings(tmp_path: Path):
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text(
|
||||
"DJANGO_SECRET_KEY=keep-this-secret\n"
|
||||
"POSTGRES_PASSWORD=keep-this-password\n"
|
||||
"DJANGO_ALLOWED_HOSTS=192.168.10.150,localhost\n"
|
||||
"DJANGO_CSRF_TRUSTED_ORIGINS=http://192.168.10.150:1226\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
changed = configure_env(
|
||||
env_file,
|
||||
"https://vacatureradar.itworx.tech/",
|
||||
hsts_seconds=300,
|
||||
cache_url="redis://127.0.0.1:6379/1",
|
||||
trusted_proxy_cidrs=["172.18.0.0/16"],
|
||||
)
|
||||
content = env_file.read_text(encoding="utf-8")
|
||||
|
||||
assert "PUBLIC_BASE_URL=https://vacatureradar.itworx.tech" in content
|
||||
assert "DJANGO_DEBUG=0" in content
|
||||
assert "vacatureradar.itworx.tech" in content
|
||||
assert (
|
||||
"DJANGO_CSRF_TRUSTED_ORIGINS="
|
||||
"http://192.168.10.150:1226,https://vacatureradar.itworx.tech"
|
||||
) in content
|
||||
assert "CACHE_URL=redis://127.0.0.1:6379/1" in content
|
||||
assert "TRUSTED_PROXY_CIDRS=172.18.0.0/16" in content
|
||||
assert "DJANGO_SECRET_KEY=keep-this-secret" in content
|
||||
assert "POSTGRES_PASSWORD=keep-this-password" in content
|
||||
assert env_file.stat().st_mode & 0o777 == 0o600
|
||||
assert "DJANGO_SECRET_KEY" not in changed
|
||||
|
||||
def test_configure_public_url_rejects_plain_http(tmp_path: Path):
|
||||
with pytest.raises(ConfigurationError, match="HTTPS"):
|
||||
configure_env(tmp_path / ".env", "http://vacatureradar.example.be")
|
||||
|
||||
def test_public_base_url_populates_django_host_and_csrf_settings():
|
||||
environment = os.environ.copy()
|
||||
environment.update(
|
||||
{
|
||||
"DJANGO_SECRET_KEY": "aB3!" * 16,
|
||||
"DJANGO_DEBUG": "0",
|
||||
"PUBLIC_BASE_URL": "https://vacatureradar.itworx.tech",
|
||||
"DEMO_MODE_ENABLED": "0",
|
||||
"SECURE_HSTS_SECONDS": "300",
|
||||
}
|
||||
)
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
"-c",
|
||||
(
|
||||
"import config.settings as settings; "
|
||||
"assert 'vacatureradar.itworx.tech' in settings.ALLOWED_HOSTS; "
|
||||
"assert 'https://vacatureradar.itworx.tech' "
|
||||
"in settings.CSRF_TRUSTED_ORIGINS; "
|
||||
"assert settings.DEBUG is False; "
|
||||
"assert settings.SESSION_COOKIE_SECURE is True; "
|
||||
"assert settings.CSRF_COOKIE_SECURE is True; "
|
||||
"assert settings.SECURE_SSL_REDIRECT is True"
|
||||
),
|
||||
],
|
||||
cwd=Path(__file__).resolve().parents[2],
|
||||
env=environment,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
) # noqa: S603 - fixed interpreter and static assertion program
|
||||
assert result.returncode == 0, result.stderr
|
||||
|
||||
def test_configure_public_url_is_idempotent(tmp_path: Path):
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text("DJANGO_ALLOWED_HOSTS=localhost\n", encoding="utf-8")
|
||||
|
||||
for _ in range(2):
|
||||
configure_env(env_file, "https://vacatureradar.example.be")
|
||||
|
||||
content = env_file.read_text(encoding="utf-8")
|
||||
assert content.count("PUBLIC_BASE_URL=") == 1
|
||||
assert content.count("vacatureradar.example.be") == 3
|
||||
@@ -45,6 +45,7 @@ def test_validate_production_security_skips_when_debug_mode_is_enabled():
|
||||
("change-me", ["example.org"], ["https://example.org"], True, True, True),
|
||||
("too-short", ["example.org"], ["https://example.org"], True, True, True),
|
||||
("a" * 64, [], ["https://example.org"], True, True, True),
|
||||
("a" * 64, ["*"], ["https://example.org"], True, True, True),
|
||||
("a" * 64, ["example.org"], [], True, True, True),
|
||||
("a" * 64, ["example.org"], ["http://example.org"], True, True, True),
|
||||
("a" * 64, ["example.org"], ["https://example.org"], False, True, True),
|
||||
@@ -70,3 +71,34 @@ def test_validate_production_security_requires_secure_settings_for_prod(
|
||||
csrf_cookie_secure=csrf_cookie_secure,
|
||||
secure_ssl_redirect=secure_ssl_redirect,
|
||||
)
|
||||
|
||||
|
||||
def test_validate_production_security_rejects_writable_public_demo():
|
||||
with pytest.raises(ImproperlyConfigured):
|
||||
_validate_production_security(
|
||||
debug=False,
|
||||
secret_key="a" * 64,
|
||||
allowed_hosts=["vacatureradar.itworx.tech"],
|
||||
csrf_trusted_origins=["https://vacatureradar.itworx.tech"],
|
||||
session_cookie_secure=True,
|
||||
csrf_cookie_secure=True,
|
||||
secure_ssl_redirect=True,
|
||||
public_base_url="https://vacatureradar.itworx.tech",
|
||||
demo_mode_enabled=True,
|
||||
demo_read_only=False,
|
||||
)
|
||||
|
||||
|
||||
def test_validate_production_security_accepts_read_only_https_demo():
|
||||
_validate_production_security(
|
||||
debug=False,
|
||||
secret_key="a" * 64,
|
||||
allowed_hosts=["vacatureradar.itworx.tech"],
|
||||
csrf_trusted_origins=["https://vacatureradar.itworx.tech"],
|
||||
session_cookie_secure=True,
|
||||
csrf_cookie_secure=True,
|
||||
secure_ssl_redirect=True,
|
||||
public_base_url="https://vacatureradar.itworx.tech",
|
||||
demo_mode_enabled=True,
|
||||
demo_read_only=True,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user