Skip to content

Commit

Permalink
push_notification: Delete obsolete DBX_IOS_APP_ID code.
Browse files Browse the repository at this point in the history
I'm not sure that this was ever actually used, but it's definitely
just clutter for Zulip today.
  • Loading branch information
timabbott committed May 16, 2017
1 parent 286f9a4 commit 9d63a5a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
40 changes: 14 additions & 26 deletions zerver/lib/push_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@
# for each request
connection = None

# We maintain an additional APNS connection for pushing to Zulip apps that have been signed
# by the Dropbox certs (and have an app id of com.dropbox.zulip)
dbx_connection = None

# `APNS_SANDBOX` should be a bool
assert isinstance(settings.APNS_SANDBOX, bool)

Expand Down Expand Up @@ -147,10 +143,6 @@ def get_connection(cert_file, key_file):
connection = get_connection(settings.APNS_CERT_FILE,
settings.APNS_KEY_FILE)

if settings.DBX_APNS_CERT_FILE is not None and os.path.exists(settings.DBX_APNS_CERT_FILE): # nocoverage
dbx_connection = get_connection(settings.DBX_APNS_CERT_FILE,
settings.DBX_APNS_KEY_FILE)

def num_push_devices_for_user(user_profile, kind = None):
# type: (UserProfile, Optional[int]) -> PushDeviceToken
if kind is None:
Expand All @@ -169,7 +161,7 @@ def hex_to_b64(data):

def _do_push_to_apns_service(user_id, message, apns_connection):
# type: (int, APNsMessage, APNs) -> None
if not apns_connection:
if not apns_connection: # nocoverage
logging.info("Not delivering APNS message %s to user %s due to missing connection" % (message, user_id))
return

Expand All @@ -188,7 +180,7 @@ def send_apple_push_notification_to_user(user, alert, **extra_data):
@statsd_increment("apple_push_notification")
def send_apple_push_notification(user_id, devices, **extra_data):
# type: (int, List[DeviceToken], **Any) -> None
if not connection and not dbx_connection:
if not connection:
logging.warning("Attempting to send push notification, but no connection was found. "
"This may be because we could not find the APNS Certificate file.")
return
Expand All @@ -197,22 +189,18 @@ def send_apple_push_notification(user_id, devices, **extra_data):
tokens = [(b64_to_hex(device.token), device.ios_app_id, device.token)
for device in devices]

for conn, app_ids in [
(connection, [settings.ZULIP_IOS_APP_ID, None]),
(dbx_connection, [settings.DBX_IOS_APP_ID])]:

valid_devices = [device for device in tokens if device[1] in app_ids]
valid_tokens = [device[0] for device in valid_devices]
if valid_tokens:
logging.info("APNS: Sending apple push notification "
"to devices: %s" % (valid_devices,))
zulip_message = APNsMessage(user_id, valid_tokens,
alert=extra_data['zulip']['alert'],
**extra_data)
_do_push_to_apns_service(user_id, zulip_message, conn)
else:
logging.warn("APNS: Not sending notification because "
"tokens didn't match devices: %s" % (app_ids,))
valid_devices = [device for device in tokens if device[1] in [settings.ZULIP_IOS_APP_ID, None]]
valid_tokens = [device[0] for device in valid_devices]
if valid_tokens:
logging.info("APNS: Sending apple push notification "
"to devices: %s" % (valid_devices,))
zulip_message = APNsMessage(user_id, valid_tokens,
alert=extra_data['zulip']['alert'],
**extra_data)
_do_push_to_apns_service(user_id, zulip_message, connection)
else: # nocoverage
logging.warn("APNS: Not sending notification because "
"tokens didn't match devices: %s/%s" % (tokens, settings.ZULIP_IOS_APP_ID,))

# NOTE: This is used by the check_apns_tokens manage.py command. Do not call it otherwise, as the
# feedback() call can take up to 15s
Expand Down
18 changes: 2 additions & 16 deletions zerver/tests/test_push_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ def setUp(self):
self.user_profile = self.example_user('hamlet')
apn.connection = apn.get_connection('fake-cert', 'fake-key')
self.redis_client = apn.redis_client = MockRedis() # type: ignore
apn.dbx_connection = apn.get_connection('fake-cert', 'fake-key')
self.tokens = [u'aaaa', u'bbbb']
for token in self.tokens:
PushDeviceToken.objects.create(
Expand Down Expand Up @@ -306,19 +305,14 @@ def test_end_to_end(self):
mock.patch('zerver.lib.push_notifications.requests.request',
side_effect=self.bounce_request), \
mock.patch('zerver.lib.push_notifications._do_push_to_apns_service'), \
mock.patch('logging.info') as mock_info, \
mock.patch('logging.warn') as mock_warn:
mock.patch('logging.info') as mock_info:
apn.handle_push_notification(self.user_profile.id, missed_message)
devices = [
(apn.b64_to_hex(device.token), device.ios_app_id, device.token)
for device in RemotePushDeviceToken.objects.all()
]
mock_info.assert_called_with("APNS: Sending apple push "
"notification to devices: %s" % (devices,))
mock_warn.assert_called_with("APNS: Not sending "
"notification because tokens "
"didn't match devices: "
"['com.dropbox.Zulip']")

def test_disabled_notifications(self):
# type: () -> None
Expand Down Expand Up @@ -723,19 +717,11 @@ def test_push(message):
@mock.patch('logging.warn')
@mock.patch('logging.info')
@mock.patch('apns.GatewayConnection.send_notification_multiple')
def test_connection_single_none(self, mock_push, mock_info, mock_warn):
def test_connection_none(self, mock_push, mock_info, mock_warn):
# type: (mock.MagicMock, mock.MagicMock, mock.MagicMock) -> None
apn.connection = None
apn.send_apple_push_notification_to_user(self.user_profile, "test alert")

@mock.patch('logging.error')
@mock.patch('apns.GatewayConnection.send_notification_multiple')
def test_connection_both_none(self, mock_push, mock_error):
# type: (mock.MagicMock, mock.MagicMock) -> None
apn.connection = None
apn.dbx_connection = None
apn.send_apple_push_notification_to_user(self.user_profile, "test alert")

class APNsFeedbackTest(PushNotificationTest):
@mock.patch('logging.info')
@mock.patch('apns.FeedbackConnection.items')
Expand Down
1 change: 0 additions & 1 deletion zproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,6 @@ def get_secret(key):

# iOS App IDs
ZULIP_IOS_APP_ID = 'com.zulip.Zulip'
DBX_IOS_APP_ID = 'com.dropbox.Zulip'

########################################################################
# SSO AND LDAP SETTINGS
Expand Down

0 comments on commit 9d63a5a

Please sign in to comment.