1
1
import json
2
2
import os
3
3
from datetime import datetime
4
+ from unittest import mock
4
5
5
6
from fastapi .testclient import TestClient
6
7
7
8
from jbi .app import app
8
9
from jbi .environment import get_settings
9
10
from jbi .models import BugzillaWebhook , BugzillaWebhookRequest
10
-
11
- EXAMPLE_WEBHOOK = BugzillaWebhook (
12
- id = 0 ,
13
- creator = "" ,
14
- name = "" ,
15
- url = "http://server/bugzilla_webhook" ,
16
- event = "create,change,comment" ,
17
- product = "Any" ,
18
- component = "Any" ,
19
- enabled = True ,
20
- errors = 0 ,
21
- )
11
+ from tests .fixtures .factories import bugzilla_webhook_factory
22
12
23
13
24
14
def test_read_root (anon_client ):
@@ -69,6 +59,22 @@ def test_powered_by_jbi_filtered(exclude_middleware, anon_client):
69
59
assert "DevTest" not in html
70
60
71
61
62
+ def test_webhooks_details (anon_client , mocked_bugzilla ):
63
+ mocked_bugzilla .list_webhooks .return_value = [
64
+ bugzilla_webhook_factory (),
65
+ bugzilla_webhook_factory (errors = 42 , enabled = False ),
66
+ ]
67
+ resp = anon_client .get ("/bugzilla_webhooks/" )
68
+
69
+ wh1 , wh2 = resp .json ()
70
+
71
+ assert "creator" not in wh1
72
+ assert wh1 ["enabled" ]
73
+ assert wh1 ["errors" ] == 0
74
+ assert not wh2 ["enabled" ]
75
+ assert wh2 ["errors" ] == 42
76
+
77
+
72
78
def test_statics_are_served (anon_client ):
73
79
resp = anon_client .get ("/static/styles.css" )
74
80
assert resp .status_code == 200
@@ -195,7 +201,7 @@ def test_read_heartbeat_bugzilla_webhooks_fails(
195
201
):
196
202
mocked_bugzilla .logged_in .return_value = True
197
203
mocked_bugzilla .list_webhooks .return_value = [
198
- EXAMPLE_WEBHOOK . copy ( update = { " enabled" : False } )
204
+ bugzilla_webhook_factory ( enabled = False )
199
205
]
200
206
201
207
resp = anon_client .get ("/__heartbeat__" )
@@ -207,6 +213,26 @@ def test_read_heartbeat_bugzilla_webhooks_fails(
207
213
}
208
214
209
215
216
+ def test_heartbeat_bugzilla_reports_webhooks_errors (
217
+ anon_client , mocked_jira , mocked_bugzilla
218
+ ):
219
+ mocked_bugzilla .logged_in .return_value = True
220
+ mocked_bugzilla .list_webhooks .return_value = [
221
+ bugzilla_webhook_factory (id = 1 , errors = 0 , product = "Remote Settings" ),
222
+ bugzilla_webhook_factory (id = 2 , errors = 3 , name = "Search Toolbar" ),
223
+ ]
224
+
225
+ with mock .patch ("jbi.services.bugzilla.statsd" ) as mocked :
226
+ anon_client .get ("/__heartbeat__" )
227
+
228
+ mocked .gauge .assert_any_call (
229
+ "jbi.bugzilla.webhooks.1-test-webhooks-remote-settings.errors" , 0
230
+ )
231
+ mocked .gauge .assert_any_call (
232
+ "jbi.bugzilla.webhooks.2-search-toolbar-firefox.errors" , 3
233
+ )
234
+
235
+
210
236
def test_read_heartbeat_bugzilla_services_fails (
211
237
anon_client , mocked_jira , mocked_bugzilla
212
238
):
@@ -227,7 +253,7 @@ def test_read_heartbeat_bugzilla_services_fails(
227
253
def test_read_heartbeat_success (anon_client , mocked_jira , mocked_bugzilla ):
228
254
"""/__heartbeat__ returns 200 when checks succeed."""
229
255
mocked_bugzilla .logged_in .return_value = True
230
- mocked_bugzilla .list_webhooks .return_value = [EXAMPLE_WEBHOOK ]
256
+ mocked_bugzilla .list_webhooks .return_value = [bugzilla_webhook_factory () ]
231
257
mocked_jira .get_server_info .return_value = {}
232
258
mocked_jira .projects .return_value = [{"key" : "DevTest" }]
233
259
mocked_jira .get_project_components .return_value = [{"name" : "Main" }]
@@ -298,7 +324,7 @@ def test_jira_heartbeat_missing_permissions(anon_client, mocked_jira, mocked_bug
298
324
299
325
def test_jira_heartbeat_unknown_components (anon_client , mocked_jira , mocked_bugzilla ):
300
326
mocked_bugzilla .logged_in .return_value = True
301
- mocked_bugzilla .list_webhooks .return_value = [EXAMPLE_WEBHOOK ]
327
+ mocked_bugzilla .list_webhooks .return_value = [bugzilla_webhook_factory () ]
302
328
mocked_jira .get_server_info .return_value = {}
303
329
304
330
resp = anon_client .get ("/__heartbeat__" )
@@ -310,7 +336,7 @@ def test_jira_heartbeat_unknown_components(anon_client, mocked_jira, mocked_bugz
310
336
def test_head_heartbeat_success (anon_client , mocked_jira , mocked_bugzilla ):
311
337
"""/__heartbeat__ support head requests"""
312
338
mocked_bugzilla .logged_in .return_value = True
313
- mocked_bugzilla .list_webhooks .return_value = [EXAMPLE_WEBHOOK ]
339
+ mocked_bugzilla .list_webhooks .return_value = [bugzilla_webhook_factory () ]
314
340
mocked_jira .get_server_info .return_value = {}
315
341
mocked_jira .projects .return_value = [{"key" : "DevTest" }]
316
342
mocked_jira .get_project_components .return_value = [{"name" : "Main" }]
0 commit comments