@@ -31,70 +31,84 @@ def _make_gitlab_config(**overrides):
3131class TestSetCommitStatus :
3232 """Test Gitlab.set_commit_status()"""
3333
34- def test_calls_correct_api_path (self ):
34+ @patch ("socketsecurity.core.scm.gitlab.requests.post" )
35+ def test_calls_correct_url_and_json_payload (self , mock_post ):
36+ mock_post .return_value = MagicMock (status_code = 200 )
3537 config = _make_gitlab_config ()
36- client = MagicMock ()
37- gl = Gitlab (client = client , config = config )
38- gl ._request_with_fallback = MagicMock ()
38+ gl = Gitlab (client = MagicMock (), config = config )
3939
4040 gl .set_commit_status ("success" , "No blocking issues" , "https://app.socket.dev/report/123" )
4141
42- gl . _request_with_fallback .assert_called_once_with (
43- path = " projects/99/statuses/abc123def456" ,
44- payload = {
42+ mock_post .assert_called_once_with (
43+ "https://gitlab.example.com/api/v4/ projects/99/statuses/abc123def456" ,
44+ json = {
4545 "state" : "success" ,
4646 "name" : "socket-security" ,
4747 "description" : "No blocking issues" ,
4848 "target_url" : "https://app.socket.dev/report/123" ,
4949 },
50- method = "POST" ,
5150 headers = config .headers ,
52- base_url = config .api_url ,
5351 )
5452
55- def test_failed_state_payload (self ):
53+ @patch ("socketsecurity.core.scm.gitlab.requests.post" )
54+ def test_failed_state_payload (self , mock_post ):
55+ mock_post .return_value = MagicMock (status_code = 200 )
5656 config = _make_gitlab_config ()
57- client = MagicMock ()
58- gl = Gitlab (client = client , config = config )
59- gl ._request_with_fallback = MagicMock ()
57+ gl = Gitlab (client = MagicMock (), config = config )
6058
6159 gl .set_commit_status ("failed" , "3 blocking alert(s) found" )
6260
63- args = gl . _request_with_fallback . call_args
64- assert args . kwargs [ " payload" ] ["state" ] == "failed"
65- assert args . kwargs [ " payload" ] ["description" ] == "3 blocking alert(s) found"
66- assert "target_url" not in args . kwargs [ " payload" ]
61+ payload = mock_post . call_args . kwargs [ "json" ]
62+ assert payload ["state" ] == "failed"
63+ assert payload ["description" ] == "3 blocking alert(s) found"
64+ assert "target_url" not in payload
6765
68- def test_skipped_when_no_mr_project_id (self ):
66+ @patch ("socketsecurity.core.scm.gitlab.requests.post" )
67+ def test_skipped_when_no_mr_project_id (self , mock_post ):
6968 config = _make_gitlab_config (mr_project_id = None )
70- client = MagicMock ()
71- gl = Gitlab (client = client , config = config )
72- gl ._request_with_fallback = MagicMock ()
69+ gl = Gitlab (client = MagicMock (), config = config )
7370
7471 gl .set_commit_status ("success" , "No blocking issues" )
7572
76- gl . _request_with_fallback .assert_not_called ()
73+ mock_post .assert_not_called ()
7774
78- def test_graceful_error_handling (self ):
75+ @patch ("socketsecurity.core.scm.gitlab.requests.post" )
76+ def test_graceful_error_handling (self , mock_post ):
77+ mock_post .side_effect = Exception ("connection error" )
7978 config = _make_gitlab_config ()
80- client = MagicMock ()
81- gl = Gitlab (client = client , config = config )
82- gl ._request_with_fallback = MagicMock (side_effect = Exception ("API error" ))
79+ gl = Gitlab (client = MagicMock (), config = config )
8380
8481 # Should not raise
8582 gl .set_commit_status ("success" , "No blocking issues" )
8683
87- def test_no_target_url_omitted_from_payload (self ):
84+ @patch ("socketsecurity.core.scm.gitlab.requests.post" )
85+ def test_no_target_url_omitted_from_payload (self , mock_post ):
86+ mock_post .return_value = MagicMock (status_code = 200 )
8887 config = _make_gitlab_config ()
89- client = MagicMock ()
90- gl = Gitlab (client = client , config = config )
91- gl ._request_with_fallback = MagicMock ()
88+ gl = Gitlab (client = MagicMock (), config = config )
9289
9390 gl .set_commit_status ("success" , "No blocking issues" , target_url = "" )
9491
95- payload = gl . _request_with_fallback . call_args .kwargs ["payload " ]
92+ payload = mock_post . call_args .kwargs ["json " ]
9693 assert "target_url" not in payload
9794
95+ @patch ("socketsecurity.core.scm.gitlab.requests.post" )
96+ def test_auth_fallback_on_401 (self , mock_post ):
97+ resp_401 = MagicMock (status_code = 401 )
98+ resp_401 .raise_for_status .side_effect = Exception ("401" )
99+ resp_200 = MagicMock (status_code = 200 )
100+ mock_post .side_effect = [resp_401 , resp_200 ]
101+
102+ config = _make_gitlab_config ()
103+ gl = Gitlab (client = MagicMock (), config = config )
104+
105+ gl .set_commit_status ("success" , "No blocking issues" )
106+
107+ assert mock_post .call_count == 2
108+ # Second call should use fallback headers (PRIVATE-TOKEN)
109+ fallback_headers = mock_post .call_args_list [1 ].kwargs ["headers" ]
110+ assert "PRIVATE-TOKEN" in fallback_headers
111+
98112
99113class TestEnableCommitStatusCliArg :
100114 """Test --enable-commit-status CLI argument parsing"""
0 commit comments