-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_gitlab_commit_status.py
More file actions
184 lines (142 loc) · 6.72 KB
/
test_gitlab_commit_status.py
File metadata and controls
184 lines (142 loc) · 6.72 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Tests for GitLab commit status integration"""
import os
import pytest
from unittest.mock import patch, MagicMock, call
from socketsecurity.core.scm.gitlab import Gitlab, GitlabConfig
def _make_gitlab_config(**overrides):
defaults = dict(
commit_sha="abc123def456",
api_url="https://gitlab.example.com/api/v4",
project_dir="/builds/test",
mr_source_branch="feature",
mr_iid="42",
mr_project_id="99",
commit_message="test commit",
default_branch="main",
project_name="test-project",
pipeline_source="merge_request_event",
commit_author="dev@example.com",
token="glpat-test",
repository="test-project",
is_default_branch=False,
headers={"Authorization": "Bearer glpat-test", "accept": "application/json"},
)
defaults.update(overrides)
return GitlabConfig(**defaults)
class TestSetCommitStatus:
"""Test Gitlab.set_commit_status()"""
@patch("socketsecurity.core.scm.gitlab.requests.post")
def test_calls_correct_url_and_json_payload(self, mock_post):
mock_post.return_value = MagicMock(status_code=200)
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
gl.set_commit_status("success", "No blocking issues", "https://app.socket.dev/report/123")
mock_post.assert_called_once_with(
"https://gitlab.example.com/api/v4/projects/99/statuses/abc123def456",
json={
"state": "success",
"context": "socket-security-commit-status",
"description": "No blocking issues",
"ref": "feature",
"target_url": "https://app.socket.dev/report/123",
},
headers=config.headers,
)
@patch("socketsecurity.core.scm.gitlab.requests.post")
def test_failed_state_payload(self, mock_post):
mock_post.return_value = MagicMock(status_code=200)
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
gl.set_commit_status("failed", "3 blocking alert(s) found")
payload = mock_post.call_args.kwargs["json"]
assert payload["state"] == "failed"
assert payload["description"] == "3 blocking alert(s) found"
assert "target_url" not in payload
@patch("socketsecurity.core.scm.gitlab.requests.post")
def test_skipped_when_no_mr_project_id(self, mock_post):
config = _make_gitlab_config(mr_project_id=None)
gl = Gitlab(client=MagicMock(), config=config)
gl.set_commit_status("success", "No blocking issues")
mock_post.assert_not_called()
@patch("socketsecurity.core.scm.gitlab.requests.post")
def test_graceful_error_handling(self, mock_post):
mock_post.side_effect = Exception("connection error")
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
# Should not raise
gl.set_commit_status("success", "No blocking issues")
@patch("socketsecurity.core.scm.gitlab.requests.post")
def test_no_target_url_omitted_from_payload(self, mock_post):
mock_post.return_value = MagicMock(status_code=200)
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
gl.set_commit_status("success", "No blocking issues", target_url="")
payload = mock_post.call_args.kwargs["json"]
assert "target_url" not in payload
@patch("socketsecurity.core.scm.gitlab.requests.post")
def test_auth_fallback_on_401(self, mock_post):
resp_401 = MagicMock(status_code=401)
resp_401.raise_for_status.side_effect = Exception("401")
resp_200 = MagicMock(status_code=200)
mock_post.side_effect = [resp_401, resp_200]
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
gl.set_commit_status("success", "No blocking issues")
assert mock_post.call_count == 2
# Second call should use fallback headers (PRIVATE-TOKEN)
fallback_headers = mock_post.call_args_list[1].kwargs["headers"]
assert "PRIVATE-TOKEN" in fallback_headers
class TestEnableMergePipelineCheck:
"""Test Gitlab.enable_merge_pipeline_check()"""
@patch("socketsecurity.core.scm.gitlab.requests.put")
def test_calls_correct_url_and_payload(self, mock_put):
mock_put.return_value = MagicMock(status_code=200)
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
gl.enable_merge_pipeline_check()
mock_put.assert_called_once_with(
"https://gitlab.example.com/api/v4/projects/99",
json={"only_allow_merge_if_pipeline_succeeds": True},
headers=config.headers,
)
@patch("socketsecurity.core.scm.gitlab.requests.put")
def test_skipped_when_no_mr_project_id(self, mock_put):
config = _make_gitlab_config(mr_project_id=None)
gl = Gitlab(client=MagicMock(), config=config)
gl.enable_merge_pipeline_check()
mock_put.assert_not_called()
@patch("socketsecurity.core.scm.gitlab.requests.put")
def test_auth_fallback_on_401(self, mock_put):
resp_401 = MagicMock(status_code=401)
resp_200 = MagicMock(status_code=200)
mock_put.side_effect = [resp_401, resp_200]
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
gl.enable_merge_pipeline_check()
assert mock_put.call_count == 2
fallback_headers = mock_put.call_args_list[1].kwargs["headers"]
assert "PRIVATE-TOKEN" in fallback_headers
@patch("socketsecurity.core.scm.gitlab.requests.put")
def test_graceful_error_handling(self, mock_put):
mock_put.side_effect = Exception("connection error")
config = _make_gitlab_config()
gl = Gitlab(client=MagicMock(), config=config)
# Should not raise
gl.enable_merge_pipeline_check()
class TestEnableCommitStatusCliArg:
"""Test --enable-commit-status CLI argument parsing"""
def test_default_is_false(self):
from socketsecurity.config import create_argument_parser
parser = create_argument_parser()
args = parser.parse_args([])
assert args.enable_commit_status is False
def test_flag_sets_true(self):
from socketsecurity.config import create_argument_parser
parser = create_argument_parser()
args = parser.parse_args(["--enable-commit-status"])
assert args.enable_commit_status is True
def test_underscore_alias(self):
from socketsecurity.config import create_argument_parser
parser = create_argument_parser()
args = parser.parse_args(["--enable_commit_status"])
assert args.enable_commit_status is True