Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

push_notifications: Fix for aioapns 2.1.

Anders Kaseorg0ba0620000bc
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
index f08973a5643..d427ac95a38 100644
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -85,13 +85,16 @@ def get_apns_context() -> Optional[APNsContext]:
# That's a little sloppy, but harmless unless a server gets
# hammered with a ton of these all at once after startup.
loop = asyncio.new_event_loop()
- apns = aioapns.APNs(
- client_cert=settings.APNS_CERT_FILE,
- topic=settings.APNS_TOPIC,
- max_connection_attempts=APNS_MAX_RETRIES,
- loop=loop,
- use_sandbox=settings.APNS_SANDBOX,
- )
+
+ async def make_apns() -> aioapns.APNs:
+ return aioapns.APNs(
+ client_cert=settings.APNS_CERT_FILE,
+ topic=settings.APNS_TOPIC,
+ max_connection_attempts=APNS_MAX_RETRIES,
+ use_sandbox=settings.APNS_SANDBOX,
+ )
+
+ apns = loop.run_until_complete(make_apns())
return APNsContext(apns=apns, loop=loop)
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index 822f1412ce4..fd58ca6d021 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -8,6 +8,7 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
from unittest import mock, skipUnless
from urllib import parse
+import aioapns
import orjson
import responses
from django.conf import settings
@@ -1478,11 +1479,14 @@ class TestAPNs(PushNotificationTest):
zerver.lib.push_notifications.get_apns_context.cache_clear()
try:
- with self.settings(APNS_CERT_FILE="/foo.pem"), mock.patch("aioapns.APNs") as mock_apns:
+ with self.settings(APNS_CERT_FILE="/foo.pem"), mock.patch(
+ "ssl.SSLContext.load_cert_chain"
+ ) as mock_load_cert_chain:
apns_context = get_apns_context()
assert apns_context is not None
try:
- self.assertEqual(mock_apns.return_value, apns_context.apns)
+ mock_load_cert_chain.assert_called_once_with("/foo.pem")
+ assert apns_context.apns.pool.loop == apns_context.loop
finally:
apns_context.loop.close()
finally:
@@ -1535,8 +1539,6 @@ class TestAPNs(PushNotificationTest):
)
def test_http_retry_eventually_fails(self) -> None:
- import aioapns
-
self.setup_apns_tokens()
with self.mock_apns() as apns_context, self.assertLogs(
"zerver.lib.push_notifications", level="INFO"