-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_output.py
More file actions
159 lines (122 loc) · 5.35 KB
/
test_output.py
File metadata and controls
159 lines (122 loc) · 5.35 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pytest
from socketsecurity.output import OutputHandler
from socketsecurity.core.classes import Diff, Issue
import json
class TestOutputHandler:
@pytest.fixture
def handler(self):
return OutputHandler(blocking_disabled=False)
def test_report_pass_with_blocking_issues(self, handler):
diff = Diff()
diff.new_alerts = [Issue(error=True)]
assert not handler.report_pass(diff)
def test_report_pass_with_blocking_disabled(self):
handler = OutputHandler(blocking_disabled=True)
diff = Diff()
diff.new_alerts = [Issue(error=True)]
assert handler.report_pass(diff)
def test_json_output_format(self, handler, capsys):
diff = Diff()
test_issue = Issue(
title="Test",
severity="high",
description="Test description",
error=True,
key="test-key",
type="test-type",
pkg_type="npm",
pkg_name="test-package",
pkg_version="1.0.0",
purl="pkg:npm/test-package@1.0.0"
)
diff.new_alerts = [test_issue]
handler.output_console_json(diff)
captured = capsys.readouterr()
# Parse the JSON output and verify structure
output = json.loads(captured.out)
assert output["issues"][0]["title"] == "Test"
assert output["issues"][0]["severity"] == "high"
assert output["issues"][0]["blocking"] is True
assert output["issues"][0]["description"] == "Test description"
def test_sbom_file_saving(self, handler, tmp_path):
# Test SBOM file is created correctly
diff = Diff()
diff.sbom = {"test": "data"}
sbom_path = tmp_path / "test.json"
handler.save_sbom_file(diff, str(sbom_path))
assert sbom_path.exists()
def test_report_pass_with_strict_blocking_new_alerts(self):
"""Test that strict-blocking fails on new blocking alerts"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
# Create config with strict_blocking
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True, warn=False)]
diff.unchanged_alerts = []
assert not handler.report_pass(diff)
def test_report_pass_with_strict_blocking_unchanged_alerts(self):
"""Test that strict-blocking fails on unchanged blocking alerts"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = []
diff.unchanged_alerts = [Issue(error=True, warn=False)]
assert not handler.report_pass(diff)
def test_report_pass_with_strict_blocking_both_alerts(self):
"""Test that strict-blocking fails when both new and unchanged alerts exist"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True, warn=False)]
diff.unchanged_alerts = [Issue(error=True, warn=False)]
assert not handler.report_pass(diff)
def test_report_pass_with_strict_blocking_only_warnings(self):
"""Test that strict-blocking passes when only warnings (not errors) exist"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=False, warn=True)]
diff.unchanged_alerts = [Issue(error=False, warn=True)]
assert handler.report_pass(diff)
def test_report_pass_strict_blocking_disabled(self):
"""Test that strict-blocking without the flag passes with unchanged alerts"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = False # Flag not set
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = []
diff.unchanged_alerts = [Issue(error=True, warn=False)]
# Should pass because strict_blocking is False
assert handler.report_pass(diff)
def test_disable_blocking_overrides_strict_blocking(self):
"""Test that disable-blocking takes precedence over strict-blocking"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = True
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True, warn=False)]
diff.unchanged_alerts = [Issue(error=True, warn=False)]
# Should pass because disable_blocking takes precedence
assert handler.report_pass(diff)