@@ -840,6 +840,59 @@ def test_webhook_with_invalid_ci_signature(self, mock_github, mock_warning):
840840 data = json .dumps ({}), headers = self .generate_header ({}, 'workflow_run' , "1" ))
841841 mock_warning .assert_called_once ()
842842
843+ @mock .patch ('mod_ci.controllers.BlockedUsers' )
844+ @mock .patch ('mod_ci.controllers.queue_test' )
845+ @mock .patch ('requests.get' , side_effect = mock_api_request_github )
846+ def test_webhook_workflow_run_event_completed_action_successful_pull_request (self , mock_request ,
847+ mock_queue_test , mock_blocked ):
848+ """Test webhook triggered with workflow run event with action completed and status success for pull request."""
849+ data = {'action' : 'completed' ,
850+ 'workflow_run' : {'event' : 'pull_request' ,
851+ 'name' : 'Build CCExtractor on Linux' , 'head_sha' : '1' ,
852+ 'head_branch' : 'master' }, 'sender' : {'login' : 'test_owner' }}
853+ fakedata = {'workflow_runs' : [
854+ {'head_sha' : '1' , 'status' : 'completed' ,
855+ 'conclusion' : 'success' , 'name' : Workflow_builds .LINUX },
856+ {'head_sha' : '1' , 'status' : 'completed' ,
857+ 'conclusion' : 'success' , 'name' : Workflow_builds .WINDOWS }
858+ ]}
859+ pull_requests = [{'head' : {'sha' : '1' }, 'user' : {'id' : 1 }, 'number' : '1' }]
860+
861+ class MockedRepository :
862+ def statuses (self , * args ):
863+ class gh_status :
864+ def post (* args , ** kwargs ):
865+ return None
866+ return gh_status
867+
868+ class actions :
869+ class runs :
870+ def get (* args , ** kwargs ):
871+ return fakedata
872+
873+ class pulls :
874+ def get (* args , ** kwargs ):
875+ return pull_requests
876+
877+ class MockedGitHub :
878+ def repos (self , * args ):
879+ return MockedRepository
880+ with self .app .test_client () as c :
881+ from github import GitHub
882+ GitHub .repos = Mock (return_value = MockedGitHub .repos )
883+ mock_blocked .query .filter .return_value .first .return_value = None
884+ response = c .post (
885+ '/start-ci' , environ_overrides = WSGI_ENVIRONMENT ,
886+ data = json .dumps (data ), headers = self .generate_header (data , 'workflow_run' ))
887+ mock_queue_test .assert_called_once ()
888+ mock_queue_test .reset_mock ()
889+ mock_blocked .query .filter .return_value .first .return_value = 1
890+ response = c .post (
891+ '/start-ci' , environ_overrides = WSGI_ENVIRONMENT ,
892+ data = json .dumps (data ), headers = self .generate_header (data , 'workflow_run' ))
893+ mock_queue_test .assert_not_called ()
894+ self .assertEqual (response .data , b'ERROR' )
895+
843896 def test_start_ci_with_a_get_request (self ):
844897 """Test start_ci function with a request method other than post."""
845898 from mod_ci .controllers import start_ci
@@ -903,6 +956,18 @@ def test_webhook_issue_opened(self, mock_issue, mock_requests, mock_mailing):
903956 mock_issue .query .filter (mock_issue .issue_id == '1234' )
904957 mock_mailing .assert_called_once_with (mock .ANY , '1234' , 'testTitle' , 'testAuthor' , 'testing' )
905958
959+ @mock .patch ('run.log.critical' )
960+ def test_github_api_error (self , mock_critical ):
961+ """Test."""
962+ from github import GitHub
963+
964+ from mod_ci .controllers import deschedule_test , schedule_test
965+ schedule_test (GitHub ('1' ).repos ('1' )('1' ).statuses ('1' ), 1 , None )
966+ mock_critical .assert_called ()
967+ mock_critical .reset_mock ()
968+ deschedule_test (GitHub ('1' ).repos ('1' )('1' ).statuses ('1' ), 1 , None )
969+ mock_critical .assert_called ()
970+
906971 @mock .patch ('mod_ci.controllers.is_main_repo' )
907972 @mock .patch ('mod_ci.controllers.shutil' )
908973 def test_update_build_badge (self , mock_shutil , mock_check_repo ):
0 commit comments