77import pytest
88from freezegun import freeze_time
99from rest_framework import status
10- from rest_framework .response import Response
1110from rest_framework .reverse import reverse
1211from rest_framework .test import APITestCase
1312from shared .django_apps .core .tests .factories import (
1716 PullFactory ,
1817 RepositoryFactory ,
1918)
20- from shared .utils .test_utils import mock_config_helper , mock_metrics
19+ from shared .utils .test_utils import mock_config_helper
2120
2221from codecov_auth .models import (
2322 GITHUB_APP_INSTALLATION_DEFAULT_NAME ,
@@ -86,113 +85,6 @@ def setUp(self):
8685 active = True ,
8786 )
8887
89- @patch (
90- "webhook_handlers.views.github.GithubWebhookHandler._handle_installation_events" ,
91- lambda self , request , * args , ** kwargs : Response (),
92- )
93- @patch (
94- "webhook_handlers.views.github.GithubWebhookHandler._handle_marketplace_events" ,
95- lambda self , request , * args , ** kwargs : Response (),
96- )
97- @patch (
98- "webhook_handlers.views.github.GithubWebhookHandler._handle_installation_repository_events" ,
99- lambda self , request , * args , ** kwargs : Response (),
100- )
101- @patch (
102- "webhook_handlers.views.github.GithubWebhookHandler._handle_installation_events" ,
103- lambda self , request , * args , ** kwargs : Response (),
104- )
105- def test_webhook_counters (self ):
106- metrics = mock_metrics (self .mocker )
107- # Simple events
108- for event in [
109- "unhandled" ,
110- GitHubWebhookEvents .PING ,
111- GitHubWebhookEvents .PUBLIC ,
112- GitHubWebhookEvents .STATUS ,
113- GitHubWebhookEvents .PULL_REQUEST ,
114- GitHubWebhookEvents .INSTALLATION ,
115- GitHubWebhookEvents .INSTALLATION_REPOSITORIES ,
116- GitHubWebhookEvents .MARKETPLACE_PURCHASE ,
117- ]:
118- with self .subTest ("with event " + event ):
119- _ = self ._post_event_data (
120- event = event ,
121- data = {},
122- )
123- assert metrics .data ["webhooks.github.received." + event ] == 1
124-
125- # Repository event + actions
126- for action in ["publicized" , "privatized" , "deleted" ]:
127- with self .subTest ("repository " + action ):
128- _ = self ._post_event_data (
129- event = GitHubWebhookEvents .REPOSITORY ,
130- data = {
131- "action" : action ,
132- "repository" : {
133- "id" : self .repo .service_id ,
134- "owner" : {"id" : - 1 },
135- },
136- },
137- )
138- assert (
139- metrics .data ["webhooks.github.received.repository." + action ] == 1
140- )
141-
142- # Delete event + ref_types
143- for ref_type in ["branch" , "other" ]:
144- with self .subTest ("delete " + ref_type ):
145- branch = BranchFactory (repository = self .repo )
146- _ = self ._post_event_data (
147- event = GitHubWebhookEvents .DELETE ,
148- data = {
149- "ref" : "refs/heads/" + branch .name ,
150- "ref_type" : ref_type ,
151- "repository" : {"id" : self .repo .service_id },
152- },
153- )
154- assert metrics .data ["webhooks.github.received.delete." + ref_type ] == 1
155-
156- # Push event + ref_types
157- for ref_type , uri in [("branch" , "refs/heads/" ), ("tag" , "refs/tags/" )]:
158- with self .subTest ("push " + ref_type ):
159- _ = self ._post_event_data (
160- event = GitHubWebhookEvents .PUSH ,
161- data = {
162- "ref" : uri + "unmerged" ,
163- "repository" : {"id" : self .repo .service_id },
164- "commits" : [],
165- },
166- )
167- assert metrics .data ["webhooks.github.received.push." + ref_type ] == 1
168-
169- # Organization event + actions
170- for action in ["member_removed" , "other" ]:
171- with self .subTest ("organization " + action ):
172- _ = self ._post_event_data (
173- event = GitHubWebhookEvents .ORGANIZATION ,
174- data = {
175- "action" : action ,
176- "membership" : {"user" : {"id" : - 1 }},
177- "organization" : {"id" : - 1 },
178- },
179- )
180- assert (
181- metrics .data ["webhooks.github.received.organization." + action ] == 1
182- )
183-
184- # Member event + actions
185- for action in ["removed" , "other" ]:
186- with self .subTest ("member " + action ):
187- _ = self ._post_event_data (
188- event = GitHubWebhookEvents .MEMBER ,
189- data = {
190- "action" : action ,
191- "member" : {"id" : - 1 },
192- "repository" : {"id" : self .repo .service_id },
193- },
194- )
195-
19688 def test_get_repo_paths_dont_crash (self ):
19789 with self .subTest ("with ownerid success" ):
19890 self ._post_event_data (
@@ -1275,7 +1167,6 @@ def test_marketplace_purchase_but_user_has_stripe_subscription(
12751167 )
12761168
12771169 def test_signature_validation (self ):
1278- metrics = mock_metrics (self .mocker )
12791170 response = self .client .post (
12801171 reverse ("github-webhook" ),
12811172 ** {
@@ -1288,7 +1179,6 @@ def test_signature_validation(self):
12881179 )
12891180
12901181 assert response .status_code == status .HTTP_403_FORBIDDEN
1291- assert metrics .data ["webhooks.github.invalid_signature" ] == 1
12921182
12931183 response = self .client .post (
12941184 reverse ("github-webhook" ),
@@ -1307,7 +1197,6 @@ def test_signature_validation(self):
13071197 )
13081198
13091199 assert response .status_code == status .HTTP_200_OK
1310- assert metrics .data ["webhooks.github.received.total" ] == 1
13111200
13121201 response = self .client .post (
13131202 reverse ("github-webhook" ),
@@ -1326,7 +1215,6 @@ def test_signature_validation(self):
13261215 )
13271216
13281217 assert response .status_code == status .HTTP_200_OK
1329- assert metrics .data ["webhooks.github.received.total" ] == 2
13301218
13311219 @patch ("webhook_handlers.views.github.get_config" )
13321220 def test_signature_validation_with_string_key (self , get_config_mock ):
0 commit comments