File size: 1,840 Bytes
0109502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import pytest
from pipeline import secret_scan


def test_ensure_ready_raises_when_gitleaks_missing_and_no_override(monkeypatch):
    monkeypatch.setattr(secret_scan, "gitleaks_available", lambda: False)
    monkeypatch.delenv("ALLOW_MISSING_GITLEAKS", raising=False)
    with pytest.raises(RuntimeError, match="Gitleaks"):
        secret_scan.ensure_secret_scanner_ready()


def test_ensure_ready_passes_when_override_set(monkeypatch):
    monkeypatch.setattr(secret_scan, "gitleaks_available", lambda: False)
    monkeypatch.setenv("ALLOW_MISSING_GITLEAKS", "1")
    secret_scan.ensure_secret_scanner_ready()  # must not raise


def test_ensure_ready_passes_when_gitleaks_available(monkeypatch):
    monkeypatch.setattr(secret_scan, "gitleaks_available", lambda: True)
    monkeypatch.delenv("ALLOW_MISSING_GITLEAKS", raising=False)
    secret_scan.ensure_secret_scanner_ready()  # must not raise


def test_scan_code_raises_when_gitleaks_missing_and_no_override(monkeypatch):
    monkeypatch.setattr(secret_scan, "shutil", secret_scan.shutil)
    monkeypatch.setattr(secret_scan.shutil, "which", lambda name: None)
    monkeypatch.delenv("ALLOW_MISSING_GITLEAKS", raising=False)
    with pytest.raises(RuntimeError, match="fail-closed"):
        secret_scan.scan_code("x = 1")


def test_scan_code_returns_warning_result_when_override_set(monkeypatch):
    monkeypatch.setattr(secret_scan.shutil, "which", lambda name: None)
    monkeypatch.setenv("ALLOW_MISSING_GITLEAKS", "1")
    result = secret_scan.scan_code("x = 1")
    assert result["passed"] is True
    assert result["warning"] == "gitleaks_not_installed_override_allowed"


def test_truthy_variants():
    for v in ("1", "true", "True", "yes", "on"):
        assert secret_scan._truthy(v)
    for v in ("0", "false", "", None, "no"):
        assert not secret_scan._truthy(v)