|
3 | 3 | from unittest import mock |
4 | 4 |
|
5 | 5 | import pytest |
6 | | -from django.test import TestCase |
| 6 | +from django.test import TestCase, override_settings |
7 | 7 |
|
8 | 8 |
|
9 | 9 | try: |
@@ -276,3 +276,128 @@ def test_push_payload_with_content_available_not_set(self, mock_apns): |
276 | 276 | req = args[0] |
277 | 277 |
|
278 | 278 | assert "content-available" not in req.message["aps"] |
| 279 | + |
| 280 | + |
| 281 | +class APNSErrorTimeoutTests(TestCase): |
| 282 | + |
| 283 | + @mock.patch("push_notifications.apns_async.asyncio.wait_for") |
| 284 | + @mock.patch("push_notifications.apns_async.APNS", autospec=True) |
| 285 | + @override_settings( |
| 286 | + PUSH_NOTIFICATION_SETTINGS={ |
| 287 | + 'APNS_ERROR_TIMEOUT': 15 |
| 288 | + } |
| 289 | + ) |
| 290 | + def test_test_timeout_value_passed_to_wait_for(self, mock_apns, mock_wait_for): |
| 291 | + mock_wait_for.return_value = mock.AsyncMock( |
| 292 | + return_value=NotificationResult("123", "200") |
| 293 | + ) |
| 294 | + apns_send_message( |
| 295 | + "123", |
| 296 | + "Test message", |
| 297 | + creds=TokenCredentials( |
| 298 | + key="aaa", |
| 299 | + key_id="bbb", |
| 300 | + team_id="ccc", |
| 301 | + ), |
| 302 | + ) |
| 303 | + |
| 304 | + mock_wait_for.assert_called_once() |
| 305 | + _, kwargs = mock_wait_for.call_args |
| 306 | + assert kwargs["timeout"] == 15 |
| 307 | + |
| 308 | + @mock.patch("push_notifications.apns_async.asyncio.wait_for") |
| 309 | + @mock.patch("push_notifications.apns_async.APNs", autospec=True) |
| 310 | + def test_default_timeout_is_5_seconds(self, mock_apns, mock_wait_for): |
| 311 | + mock_wait_for.return_value = mock.AsyncMock( |
| 312 | + return_value=NotificationResult("123", "200") |
| 313 | + ) |
| 314 | + |
| 315 | + apns_send_message( |
| 316 | + "123", |
| 317 | + "Test message", |
| 318 | + creds=TokenCredentials( |
| 319 | + key="aaa", |
| 320 | + key_id="bbb", |
| 321 | + team_id="ccc", |
| 322 | + ), |
| 323 | + ) |
| 324 | + |
| 325 | + mock_wait_for.assert_called_once() |
| 326 | + _, kwargs = mock_wait_for.call_args |
| 327 | + assert kwargs["timeout"] == 5 |
| 328 | + |
| 329 | + @mock.patch("push_notifications.apns_async.asyncio.wait_for") |
| 330 | + @mock.patch("push_notifications.apns_async.APNs", autospec=True) |
| 331 | + @override_settings( |
| 332 | + PUSH_NOTIFICATIONS_SETTINGS={ |
| 333 | + 'APNS_ERROR_TIMEOUT': 1 |
| 334 | + } |
| 335 | + ) |
| 336 | + def test_short_timeout_value_is_respected(self, mock_apns, mock_wait_for): |
| 337 | + mock_wait_for.return_value = mock.AsyncMock( |
| 338 | + return_value=NotificationResult("123", "200") |
| 339 | + ) |
| 340 | + |
| 341 | + apns_send_message( |
| 342 | + "123", |
| 343 | + "Test message", |
| 344 | + creds=TokenCredentials( |
| 345 | + key="aaa", |
| 346 | + key_id="bbb", |
| 347 | + team_id="ccc", |
| 348 | + ), |
| 349 | + ) |
| 350 | + |
| 351 | + mock_wait_for.assert_called_once() |
| 352 | + _, kwargs = mock_wait_for.call_args |
| 353 | + assert kwargs["timeout"] == 1 |
| 354 | + |
| 355 | + @mock.patch("push_notifications.apns_async.asyncio.wait_for") |
| 356 | + @mock.patch("push_notifications.apns_async.APNs", autospec=True) |
| 357 | + @override_settings( |
| 358 | + PUSH_NOTIFICATIONS_SETTINGS={ |
| 359 | + 'APNS_ERROR_TIMEOUT': 30 |
| 360 | + } |
| 361 | + ) |
| 362 | + def test_long_timeout_value_is_respected(self, mock_apns, mock_wait_for): |
| 363 | + mock_wait_for.return_value = mock.AsyncMock( |
| 364 | + return_value=NotificationResult("123", "200") |
| 365 | + ) |
| 366 | + |
| 367 | + apns_send_message( |
| 368 | + "123", |
| 369 | + "Test message", |
| 370 | + creds=TokenCredentials( |
| 371 | + key="aaa", |
| 372 | + key_id="bbb", |
| 373 | + team_id="ccc", |
| 374 | + ), |
| 375 | + ) |
| 376 | + |
| 377 | + mock_wait_for.assert_called_once() |
| 378 | + _, kwargs = mock_wait_for.call_args |
| 379 | + assert kwargs["timeout"] == 30 |
| 380 | + |
| 381 | + @mock.patch("push_notifications.apns_async.asyncio.wait_for") |
| 382 | + @mock.patch("push_notifications.apns_async.APNs", autospec=True) |
| 383 | + @override_settings( |
| 384 | + PUSH_NOTIFICATIONS_SETTINGS={} |
| 385 | + ) |
| 386 | + def test_empty_settings_uses_default_timeout(self, mock_apns, mock_wait_for): |
| 387 | + mock_wait_for.return_value = mock.AsyncMock( |
| 388 | + return_value=NotificationResult("123", "200") |
| 389 | + ) |
| 390 | + |
| 391 | + apns_send_message( |
| 392 | + "123", |
| 393 | + "Test message", |
| 394 | + creds=TokenCredentials( |
| 395 | + key="aaa", |
| 396 | + key_id="bbb", |
| 397 | + team_id="ccc", |
| 398 | + ), |
| 399 | + ) |
| 400 | + |
| 401 | + mock_wait_for.assert_called_once() |
| 402 | + _, kwargs = mock_wait_for.call_args |
| 403 | + assert kwargs["timeout"] == 5 |
0 commit comments