/
test_firebase_api_client.py
293 lines (250 loc) · 11 KB
/
test_firebase_api_client.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
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import patch
if TYPE_CHECKING:
from typing import Any
from pytest_django.fixtures import SettingsWrapper
from requests_mock.mocker import Mocker
from requests_mock.request import _RequestObjectProxy
from requests_mock.response import _Context
from _pytest.logging import LogCaptureFixture
import pytest
from django.core.exceptions import ImproperlyConfigured
from integreat_cms.cms.models import PushNotification, Region
from integreat_cms.firebase_api.firebase_api_client import FirebaseApiClient
class TestFirebaseApiClient:
"""
Test for :class:`~integreat_cms.firebase_api.firebase_api_client.FirebaseApiClient`
Only tests the correct result (``True``/``False``) and logging output depending on the firebase-HTTP response.
It does not test if firebase-api-server works/answers as expected.
"""
#: A set of response-data for the mocking-function
#: Google does not really document what errors are possible
response_mock_data = {
"200_success": {
"name": "projects/integreat-2020/messages/1",
"status_code": 200,
},
"200_no_name": {"status_code": 200},
"401_invalid_key": {
"error": {
"message": "Request had invalid authentication credentials.",
"code": 401,
},
"status_code": 401,
},
"404": {"error": {"code": 404}, "status_code": 404},
}
def __init__(self) -> None:
self.patch: Any = None
def setup_method(self) -> None:
self.patch = patch.object(
FirebaseApiClient, "_get_access_token", return_value="secret access token"
)
self.patch.start()
def teardown_method(self) -> None:
self.patch.stop()
self.patch = None
@pytest.mark.django_db
def test_client_throws_exception_when_fcm_disabled(
self, settings: SettingsWrapper, load_test_data: None
) -> None:
"""
Tests that an ImproperlyConfigured exception is thrown, if firebase API is disabled in settings
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
"""
settings.FCM_ENABLED = False
notification = PushNotification.objects.first()
with pytest.raises(ImproperlyConfigured):
FirebaseApiClient(notification)
@pytest.mark.django_db
def test_is_valid(self, settings: SettingsWrapper, load_test_data: None) -> None:
"""
Tests that :meth:`~integreat_cms.firebase_api.firebase_api_client.FirebaseApiClient.is_valid` is ``True``,
when FCM_ENABLED and pushNotification valid (with title and translation)
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
"""
settings.FCM_ENABLED = True
notification = PushNotification.objects.first()
assert FirebaseApiClient(notification).is_valid()
@pytest.mark.django_db
def test_is_invalid_when_no_translation(
self, settings: SettingsWrapper, load_test_data: None
) -> None:
"""
Tests that :meth:`~integreat_cms.firebase_api.firebase_api_client.FirebaseApiClient.is_valid` is ``False``,
when translation missing
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
"""
settings.FCM_ENABLED = True
notification = PushNotification.objects.first()
pns = FirebaseApiClient(notification)
pns.prepared_pnts = []
assert not pns.is_valid()
@pytest.mark.django_db
def test_is_invalid_when_no_title(
self, settings: SettingsWrapper, load_test_data: None
) -> None:
"""
Tests that :meth:`~integreat_cms.firebase_api.firebase_api_client.FirebaseApiClient.is_valid` is ``False``,
when pushNotification-title is missing
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
"""
settings.FCM_ENABLED = True
notification = PushNotification.objects.first()
pns = FirebaseApiClient(notification)
pns.prepared_pnts[0].title = ""
assert not pns.is_valid()
@pytest.mark.django_db
def test_firebase_api_200_success(
self,
settings: SettingsWrapper,
load_test_data: None,
requests_mock: Mocker,
caplog: LogCaptureFixture,
) -> None:
"""
Tests firebase-api-response handling, test a successful API call
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:param requests_mock: Fixture for mocking requests, see :doc:`requests-mock:pytest`
:param caplog: Fixture for asserting log messages in tests (see :fixture:`pytest:caplog`)
"""
status = self.send_all_with_mocked_response(
settings, requests_mock, self.response_mock_data["200_success"]
)
for record in caplog.records:
assert (
"sent, FCM id: 'projects/integreat-2020/messages/1'" in record.message
)
assert status
@pytest.mark.django_db
def test_firebase_api_200_unexpected_api_response(
self,
settings: SettingsWrapper,
load_test_data: None,
requests_mock: Mocker,
caplog: LogCaptureFixture,
) -> None:
"""
Tests firebase-api-response handling,
test a partial successful api call - HTTP 200 but no message name in response
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:param requests_mock: Fixture for mocking requests, see :doc:`requests-mock:pytest`
:param caplog: Fixture for asserting log messages in tests (see :fixture:`pytest:caplog`)
"""
status = self.send_all_with_mocked_response(
settings, requests_mock, self.response_mock_data["200_no_name"]
)
for record in caplog.records:
assert "sent, but unexpected API response" in record.message
assert status
@pytest.mark.django_db
def test_firebase_api_403_wrong_token(
self,
settings: SettingsWrapper,
load_test_data: None,
requests_mock: Mocker,
caplog: LogCaptureFixture,
) -> None:
"""
Tests firebase-api-response handling,
test a denied call - HTTP 403 because of wrong auth-token
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:param requests_mock: Fixture for mocking requests, see :doc:`requests-mock:pytest`
:param caplog: Fixture for asserting log messages in tests (see :fixture:`pytest:caplog`)
"""
status = self.send_all_with_mocked_response(
settings, requests_mock, self.response_mock_data["401_invalid_key"]
)
for record in caplog.records:
assert "Received invalid response from FCM for" in record.message
assert not status
@pytest.mark.django_db
def test_firebase_api_404(
self,
settings: SettingsWrapper,
load_test_data: None,
requests_mock: Mocker,
caplog: LogCaptureFixture,
) -> None:
"""
Tests firebase-api-response handling, test 404 response
:param settings: The Django settings
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:param requests_mock: Fixture for mocking requests, see :doc:`requests-mock:pytest`
:param caplog: Fixture for asserting log messages in tests (see :fixture:`pytest:caplog`)
"""
status = self.send_all_with_mocked_response(
settings, requests_mock, self.response_mock_data["404"]
)
for record in caplog.records:
assert "Received invalid response from FCM for" in record.message
assert not status
@pytest.mark.django_db
def send_all_with_mocked_response(
self,
settings: SettingsWrapper,
requests_mock: Mocker,
mocked_response: dict[str, Any],
) -> bool:
"""
Fires a :meth:`~integreat_cms.firebase_api.firebase_api_client.FirebaseApiClient.send_all`
with mocked post
:param settings: The Django settings
:param requests_mock: Fixture for mocking requests, see :doc:`requests-mock:pytest`
:param mocked_response: Data for the mocked response, depending on test-scenario (given by
:attr:`~tests.firebase_api.test_firebase_api_client.TestFirebaseApiClient.response_mock_data`)
:return: Success status of the notification sending
"""
status_code = mocked_response.pop("status_code")
settings.FCM_ENABLED = True
requests_mock.post(
settings.FCM_URL, json=mocked_response, status_code=status_code
)
notification = PushNotification.objects.first()
pns = FirebaseApiClient(notification)
return pns.send_all()
@pytest.mark.django_db
def test_region_notification_send(
self, settings: SettingsWrapper, load_test_data: None, requests_mock: Mocker
) -> None:
targets = set()
def evaluate_request(request: _RequestObjectProxy, context: _Context) -> object:
targets.add(request.json()["message"]["topic"])
return self.response_mock_data["200_success"]
requests_mock.post(settings.FCM_URL, json=evaluate_request, status_code=200)
settings.FCM_ENABLED = True
notification = PushNotification.objects.get(pk=1)
pns = FirebaseApiClient(notification)
pns.send_all()
assert targets == {
"augsburg-en-news",
"augsburg-de-news",
}
@pytest.mark.django_db
def test_multiple_regions_notification_send(
self, settings: SettingsWrapper, load_test_data: None, requests_mock: Mocker
) -> None:
targets = set()
def evaluate_request(request: _RequestObjectProxy, context: _Context) -> object:
targets.add(request.json()["message"]["topic"])
return self.response_mock_data["200_success"]
requests_mock.post(settings.FCM_URL, json=evaluate_request, status_code=200)
settings.FCM_ENABLED = True
notification = PushNotification.objects.get(pk=1)
notification.regions.add(Region.objects.get(slug="nurnberg"))
pns = FirebaseApiClient(notification)
pns.send_all()
assert targets == {
"nurnberg-en-news",
"nurnberg-de-news",
"augsburg-en-news",
"augsburg-de-news",
}