Files

61 lines
2.6 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
from pathlib import Path
import pytest
from pshelpers import fake_visual, make_settings
from visualkit.config import Settings as VisualSettings
from packetstation import guard
from packetstation.main import create_app
@pytest.mark.parametrize("stack", ["test", "ci", "dev"])
def test_non_prod_stack_refuses_production_target(tmp_path: Path, stack: str) -> None:
s = make_settings(tmp_path, stack=stack)
with pytest.raises(guard.StackGuardError, match="production"):
guard.check_stack(s, fake_visual(production=True, explicit=True))
def test_non_prod_stack_with_dev_target_ok(tmp_path: Path) -> None:
r = guard.check_stack(make_settings(tmp_path, stack="test"), fake_visual(False))
assert not r.production and r.warnings == []
def test_prod_stack_refuses_implicit_dev(tmp_path: Path) -> None:
with pytest.raises(guard.StackGuardError, match="default to DEV"):
guard.check_stack(make_settings(tmp_path, stack="prod"), fake_visual(False, explicit=False))
def test_prod_stack_explicit_dev_warns(tmp_path: Path) -> None:
r = guard.check_stack(make_settings(tmp_path, stack="prod"), fake_visual(False, explicit=True))
assert r.warnings and "non-production" in r.warnings[0]
def test_prod_stack_production_ok(tmp_path: Path) -> None:
r = guard.check_stack(make_settings(tmp_path, stack="prod"), fake_visual(True, explicit=True))
assert r.production and r.warnings == []
def test_with_real_visualkit_settings(tmp_path: Path) -> None:
# No connection is made: only VisualKit's settings object is built.
prod = VisualSettings(_env_file=None, profile="prod")
dev_default = VisualSettings(_env_file=None)
assert prod.is_production and not dev_default.is_production
with pytest.raises(guard.StackGuardError):
guard.check_stack(make_settings(tmp_path, stack="ci"), prod)
with pytest.raises(guard.StackGuardError):
guard.check_stack(make_settings(tmp_path, stack="prod"), dev_default)
assert guard.check_stack(make_settings(tmp_path, stack="prod"), prod).production
def test_ldap_insecure_warns_off_prod(tmp_path: Path) -> None:
r = guard.check_stack(make_settings(tmp_path, stack="test", ldap_allow_insecure=True), fake_visual(False))
assert any("LDAP_ALLOW_INSECURE" in w for w in r.warnings)
def test_app_refuses_to_start(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
monkeypatch.setattr(guard, "load_visual_settings", lambda: fake_visual(True, explicit=True))
with pytest.raises(guard.StackGuardError):
create_app(make_settings(tmp_path, stack="ci"))