from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent


def test_dockerfile_does_not_run_as_root():
    content = (REPO_ROOT / "Dockerfile").read_text()
    assert "USER appuser" in content


def test_compose_base_file_does_not_publish_db_or_redis_ports():
    content = (REPO_ROOT / "docker-compose.yml").read_text()
    assert "5432:5432" not in content
    assert "6379:6379" not in content


def test_compose_override_restores_ports_for_local_dev():
    content = (REPO_ROOT / "docker-compose.override.yml").read_text()
    assert "5432:5432" in content
    assert "6379:6379" in content


def test_deploy_workflow_pins_base_compose_file_only():
    content = (REPO_ROOT / ".github" / "workflows" / "deploy.yml").read_text()
    assert "-f docker-compose.yml" in content


def test_dockerfile_pins_base_image_by_digest():
    content = (REPO_ROOT / "Dockerfile").read_text()
    assert "FROM python:3.11-slim@sha256:" in content


def test_dependabot_config_covers_pip_docker_and_actions():
    content = (REPO_ROOT / ".github" / "dependabot.yml").read_text()
    assert 'package-ecosystem: "pip"' in content
    assert 'package-ecosystem: "docker"' in content
    assert 'package-ecosystem: "github-actions"' in content


def test_ci_workflow_runs_dependency_vulnerability_scan():
    content = (REPO_ROOT / ".github" / "workflows" / "ci.yml").read_text()
    assert "pip-audit" in content


def test_ci_workflow_runs_sast():
    content = (REPO_ROOT / ".github" / "workflows" / "ci.yml").read_text()
    assert "bandit" in content


def test_ci_workflow_runs_secret_scan_with_full_history():
    content = (REPO_ROOT / ".github" / "workflows" / "ci.yml").read_text()
    assert "gitleaks" in content
    assert "fetch-depth: 0" in content
