-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathtest_unit_webhooks.py
347 lines (298 loc) · 10.3 KB
/
test_unit_webhooks.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import hashlib
import hmac
import json
from typing import Type
from unittest import mock
from unittest.mock import MagicMock
import pytest
import responses
from pytest_django.fixtures import SettingsWrapper
from pytest_mock import MockerFixture
from requests.exceptions import ConnectionError, Timeout
from core.constants import FLAGSMITH_SIGNATURE_HEADER
from environments.models import Environment, Webhook
from organisations.models import Organisation, OrganisationWebhook
from webhooks.sample_webhook_data import (
environment_webhook_data,
organisation_webhook_data,
)
from webhooks.webhooks import (
WebhookEventType,
WebhookType,
call_environment_webhooks,
call_integration_webhook,
call_organisation_webhooks,
call_webhook_with_failure_mail_after_retries,
trigger_sample_webhook,
)
@mock.patch("webhooks.webhooks.requests")
def test_webhooks_requests_made_to_all_urls_for_environment(
mock_requests: MagicMock,
environment: Environment,
) -> None:
# Given
webhook_1 = Webhook.objects.create(
url="http://url.1.com", enabled=True, environment=environment
)
webhook_2 = Webhook.objects.create(
url="http://url.2.com", enabled=True, environment=environment
)
# When
call_environment_webhooks(
environment_id=environment.id,
data={},
event_type=WebhookEventType.FLAG_UPDATED.value,
)
# Then
assert len(mock_requests.post.call_args_list) == 2
# and
call_1_args, _ = mock_requests.post.call_args_list[0]
call_2_args, _ = mock_requests.post.call_args_list[1]
all_call_args = call_1_args + call_2_args
assert all(str(webhook.url) in all_call_args for webhook in (webhook_1, webhook_2))
@mock.patch("webhooks.webhooks.requests")
def test_webhooks_request_not_made_to_disabled_webhook(
mock_requests: MagicMock,
environment: Environment,
) -> None:
# Given
Webhook.objects.create(
url="http://url.1.com", enabled=False, environment=environment
)
# When
call_environment_webhooks(
environment_id=environment.id,
data={},
event_type=WebhookEventType.FLAG_UPDATED.value,
)
# Then
mock_requests.post.assert_not_called()
@mock.patch("webhooks.webhooks.requests")
def test_trigger_sample_webhook_makes_correct_post_request_for_environment(
mock_request: MagicMock,
) -> None:
url = "http://test.test"
webhook = Webhook(url=url)
trigger_sample_webhook(webhook, WebhookType.ENVIRONMENT)
args, kwargs = mock_request.post.call_args
assert json.loads(kwargs["data"]) == environment_webhook_data
assert args[0] == url
@mock.patch("webhooks.webhooks.requests")
def test_trigger_sample_webhook_makes_correct_post_request_for_organisation(
mock_request: MagicMock,
) -> None:
url = "http://test.test"
webhook = OrganisationWebhook(url=url)
trigger_sample_webhook(webhook, WebhookType.ORGANISATION)
args, kwargs = mock_request.post.call_args
assert json.loads(kwargs["data"]) == organisation_webhook_data
assert args[0] == url
@mock.patch("webhooks.webhooks.WebhookSerializer")
@mock.patch("webhooks.webhooks.requests")
def test_request_made_with_correct_signature(
mock_requests: MagicMock,
webhook_serializer: MagicMock,
environment: Environment,
) -> None:
# Given
payload = {"key": "value"}
webhook_serializer.return_value.data = payload
secret = "random_key"
Webhook.objects.create(
url="http://url.1.com",
enabled=True,
environment=environment,
secret=secret,
)
expected_signature = hmac.new(
key=secret.encode(),
msg=json.dumps(payload).encode(),
digestmod=hashlib.sha256,
).hexdigest()
call_environment_webhooks(
environment_id=environment.id,
data={},
event_type=WebhookEventType.FLAG_UPDATED.value,
)
# When
_, kwargs = mock_requests.post.call_args_list[0]
# Then
received_signature = kwargs["headers"][FLAGSMITH_SIGNATURE_HEADER]
assert hmac.compare_digest(expected_signature, received_signature) is True
@mock.patch("webhooks.webhooks.requests")
def test_request_does_not_have_signature_header_if_secret_is_not_set(
mock_requests: MagicMock,
environment: Environment,
) -> None:
# Given
Webhook.objects.create(
url="http://url.1.com", enabled=True, environment=environment
)
# When
call_environment_webhooks(
environment_id=environment.id,
data={},
event_type=WebhookEventType.FLAG_UPDATED.value,
)
# Then
_, kwargs = mock_requests.post.call_args_list[0]
assert FLAGSMITH_SIGNATURE_HEADER not in kwargs["headers"]
@pytest.mark.parametrize("expected_error", [ConnectionError, Timeout])
@pytest.mark.django_db
def test_call_environment_webhooks__multiple_webhooks__failure__calls_expected(
mocker: MockerFixture,
expected_error: Type[Exception],
environment: Environment,
) -> None:
# Given
requests_post_mock = mocker.patch("webhooks.webhooks.requests.post")
requests_post_mock.side_effect = expected_error()
send_failure_email_mock: mock.Mock = mocker.patch(
"webhooks.webhooks.send_failure_email"
)
webhook_1 = Webhook.objects.create(
url="http://url.1.com",
enabled=True,
environment=environment,
)
webhook_2 = Webhook.objects.create(
url="http://url.2.com",
enabled=True,
environment=environment,
)
expected_data = {} # type: ignore[var-annotated]
expected_event_type = WebhookEventType.FLAG_UPDATED.value
expected_send_failure_email_data = {
"event_type": expected_event_type,
"data": expected_data,
}
expected_send_failure_status_code = f"N/A ({expected_error.__name__})"
retries = 4
# When
call_environment_webhooks(
environment_id=environment.id,
data=expected_data,
event_type=expected_event_type,
retries=retries,
)
# Then
assert requests_post_mock.call_count == 2 * retries
assert send_failure_email_mock.call_count == 2
send_failure_email_mock.assert_has_calls(
[
mocker.call(
webhook_2,
expected_send_failure_email_data,
WebhookType.ENVIRONMENT.value,
expected_send_failure_status_code,
),
mocker.call(
webhook_1,
expected_send_failure_email_data,
WebhookType.ENVIRONMENT.value,
expected_send_failure_status_code,
),
],
any_order=True,
)
@pytest.mark.parametrize("expected_error", [ConnectionError, Timeout])
@pytest.mark.django_db
def test_call_organisation_webhooks__multiple_webhooks__failure__calls_expected(
mocker: MockerFixture,
expected_error: Type[Exception],
organisation: Organisation,
settings: SettingsWrapper,
) -> None:
# Given
requests_post_mock = mocker.patch("webhooks.webhooks.requests.post")
requests_post_mock.side_effect = expected_error()
send_failure_email_mock: mock.Mock = mocker.patch(
"webhooks.webhooks.send_failure_email"
)
webhook_1 = OrganisationWebhook.objects.create(
url="http://url.1.com", enabled=True, organisation=organisation
)
webhook_2 = OrganisationWebhook.objects.create(
url="http://url.2.com", enabled=True, organisation=organisation
)
expected_data = {} # type: ignore[var-annotated]
expected_event_type = WebhookEventType.FLAG_UPDATED.value
expected_send_failure_email_data = {
"event_type": expected_event_type,
"data": expected_data,
}
expected_send_failure_status_code = f"N/A ({expected_error.__name__})"
retries = 5
# When
call_organisation_webhooks(
organisation_id=organisation.id,
data=expected_data,
event_type=expected_event_type,
retries=retries,
)
# Then
assert requests_post_mock.call_count == 2 * retries
assert send_failure_email_mock.call_count == 2
send_failure_email_mock.assert_has_calls(
[
mocker.call(
webhook_2,
expected_send_failure_email_data,
WebhookType.ORGANISATION.value,
expected_send_failure_status_code,
),
mocker.call(
webhook_1,
expected_send_failure_email_data,
WebhookType.ORGANISATION.value,
expected_send_failure_status_code,
),
],
any_order=True,
)
def test_call_webhook_with_failure_mail_after_retries_raises_error_on_invalid_args(): # type: ignore[no-untyped-def]
try_count = 10
with pytest.raises(ValueError):
call_webhook_with_failure_mail_after_retries(0, {}, "", try_count=try_count)
def test_call_webhook_with_failure_mail_after_retries_does_not_retry_if_not_using_processor( # type: ignore[no-untyped-def] # noqa: E501
mocker: MockerFixture, organisation: Organisation, settings: SettingsWrapper
):
# Given
requests_post_mock = mocker.patch("webhooks.webhooks.requests.post")
requests_post_mock.side_effect = ConnectionError
send_failure_email_mock: mock.Mock = mocker.patch(
"webhooks.webhooks.send_failure_email"
)
settings.RETRY_WEBHOOKS = False
webhook = OrganisationWebhook.objects.create(
url="http://url.1.com", enabled=True, organisation=organisation
)
# When
call_webhook_with_failure_mail_after_retries(
webhook.id,
data={},
webhook_type=WebhookType.ORGANISATION.value,
send_failure_mail=True,
)
# Then
assert requests_post_mock.call_count == 1
send_failure_email_mock.assert_called_once()
@responses.activate()
def test_call_integration_webhook_does_not_raise_error_on_backoff_give_up(
mocker: MockerFixture,
) -> None:
"""
This test is essentially verifying that the `raise_on_giveup` argument
passed to the backoff decorator on _call_webhook is working as we
expect it to.
"""
# Given
url = "https://test.com/webhook"
config = mocker.MagicMock(secret=None, url=url)
responses.add(url=url, method="POST", body=json.dumps({}), status=400)
# When
result = call_integration_webhook(config, data={})
# Then
# we don't get a result from the function (as expected), and no exception is
# raised
assert result is None