2016-10-27 23:55:31 +02:00
|
|
|
|
2018-02-09 23:19:00 +01:00
|
|
|
from contextlib import contextmanager
|
2017-08-29 01:05:20 +02:00
|
|
|
import itertools
|
2017-08-18 09:04:52 +02:00
|
|
|
import requests
|
2016-08-03 11:11:25 +02:00
|
|
|
import mock
|
2016-08-08 14:20:41 +02:00
|
|
|
from mock import call
|
2016-08-04 13:15:49 +02:00
|
|
|
import time
|
2018-05-11 01:39:38 +02:00
|
|
|
from typing import Any, Dict, List, Optional, Union, SupportsInt
|
2016-08-03 11:11:25 +02:00
|
|
|
|
2018-04-26 06:50:37 +02:00
|
|
|
import base64
|
2016-12-13 08:41:48 +01:00
|
|
|
import gcm
|
2018-05-04 01:40:46 +02:00
|
|
|
import json
|
2017-10-06 23:16:29 +02:00
|
|
|
import os
|
2016-10-27 23:55:31 +02:00
|
|
|
import ujson
|
2018-05-04 01:40:46 +02:00
|
|
|
import uuid
|
2016-08-08 14:20:41 +02:00
|
|
|
|
2016-10-27 23:55:31 +02:00
|
|
|
from django.test import TestCase, override_settings
|
2016-08-03 11:11:25 +02:00
|
|
|
from django.conf import settings
|
2016-10-27 23:55:31 +02:00
|
|
|
from django.http import HttpResponse
|
2018-05-04 01:40:46 +02:00
|
|
|
from django.utils.crypto import get_random_string
|
2016-08-03 11:11:25 +02:00
|
|
|
|
2017-05-08 17:54:11 +02:00
|
|
|
from zerver.models import (
|
2017-05-11 09:12:21 +02:00
|
|
|
PushDeviceToken,
|
|
|
|
UserProfile,
|
|
|
|
Message,
|
2017-05-11 10:55:05 +02:00
|
|
|
UserMessage,
|
2017-11-29 02:49:11 +01:00
|
|
|
receives_offline_email_notifications,
|
|
|
|
receives_offline_push_notifications,
|
2017-05-08 17:54:11 +02:00
|
|
|
receives_online_notifications,
|
2017-08-17 16:55:32 +02:00
|
|
|
receives_stream_notifications,
|
2017-05-11 09:12:21 +02:00
|
|
|
get_client,
|
2017-08-29 06:28:30 +02:00
|
|
|
get_realm,
|
2017-05-11 09:12:21 +02:00
|
|
|
Recipient,
|
2017-05-11 10:15:00 +02:00
|
|
|
Stream,
|
2018-02-16 23:18:47 +01:00
|
|
|
Subscription,
|
2017-05-08 17:54:11 +02:00
|
|
|
)
|
2017-11-10 00:51:06 +01:00
|
|
|
from zerver.lib.soft_deactivation import do_soft_deactivate_users
|
2016-08-03 11:11:25 +02:00
|
|
|
from zerver.lib import push_notifications as apn
|
2017-10-06 23:16:29 +02:00
|
|
|
from zerver.lib.push_notifications import get_mobile_push_content, \
|
2017-10-12 03:56:50 +02:00
|
|
|
DeviceToken, PushNotificationBouncerException, get_apns_client
|
2016-10-27 23:55:31 +02:00
|
|
|
from zerver.lib.response import json_success
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2016-08-03 11:11:25 +02:00
|
|
|
|
2016-10-27 23:55:31 +02:00
|
|
|
from zilencer.models import RemoteZulipServer, RemotePushDeviceToken
|
|
|
|
from django.utils.timezone import now
|
|
|
|
|
2017-10-06 23:16:29 +02:00
|
|
|
ZERVER_DIR = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
|
2017-05-16 08:05:31 +02:00
|
|
|
class BouncerTestCase(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-05-16 08:05:31 +02:00
|
|
|
self.server_uuid = "1234-abcd"
|
2016-10-27 23:55:31 +02:00
|
|
|
server = RemoteZulipServer(uuid=self.server_uuid,
|
|
|
|
api_key="magic_secret_api_key",
|
|
|
|
hostname="demo.example.com",
|
|
|
|
last_updated=now())
|
|
|
|
server.save()
|
2017-10-27 08:28:23 +02:00
|
|
|
super().setUp()
|
2016-10-27 23:55:31 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def tearDown(self) -> None:
|
2016-10-27 23:55:31 +02:00
|
|
|
RemoteZulipServer.objects.filter(uuid=self.server_uuid).delete()
|
2017-10-27 08:28:23 +02:00
|
|
|
super().tearDown()
|
2016-10-27 23:55:31 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def bounce_request(self, *args: Any, **kwargs: Any) -> HttpResponse:
|
2016-10-27 23:55:31 +02:00
|
|
|
"""This method is used to carry out the push notification bouncer
|
|
|
|
requests using the Django test browser, rather than python-requests.
|
|
|
|
"""
|
|
|
|
# args[0] is method, args[1] is URL.
|
|
|
|
local_url = args[1].replace(settings.PUSH_NOTIFICATION_BOUNCER_URL, "")
|
|
|
|
if args[0] == "POST":
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid,
|
|
|
|
local_url,
|
|
|
|
kwargs['data'],
|
|
|
|
subdomain="")
|
2016-10-27 23:55:31 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError("Unsupported method for bounce_request")
|
|
|
|
return result
|
|
|
|
|
2018-05-11 01:39:38 +02:00
|
|
|
def get_generic_payload(self, method: str='register') -> Dict[str, Any]:
|
2017-05-16 08:05:31 +02:00
|
|
|
user_id = 10
|
|
|
|
token = "111222"
|
|
|
|
token_kind = PushDeviceToken.GCM
|
|
|
|
|
|
|
|
return {'user_id': user_id,
|
|
|
|
'token': token,
|
|
|
|
'token_kind': token_kind}
|
|
|
|
|
|
|
|
class PushBouncerNotificationTest(BouncerTestCase):
|
2017-08-29 06:28:30 +02:00
|
|
|
DEFAULT_SUBDOMAIN = ""
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unregister_remote_push_user_params(self) -> None:
|
2016-10-27 23:55:31 +02:00
|
|
|
token = "111222"
|
|
|
|
token_kind = PushDeviceToken.GCM
|
|
|
|
|
|
|
|
endpoint = '/api/v1/remotes/push/unregister'
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'token_kind': token_kind})
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, "Missing 'token' argument")
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'token': token})
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, "Missing 'token_kind' argument")
|
2017-08-29 06:28:30 +02:00
|
|
|
|
|
|
|
# We need the root ('') subdomain to be in use for this next
|
|
|
|
# test, since the push bouncer API is only available there:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
realm.string_id = ""
|
|
|
|
realm.save()
|
|
|
|
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(self.example_email("hamlet"), endpoint, {'token': token,
|
2018-10-12 20:18:07 +02:00
|
|
|
'user_id': 15,
|
2017-12-14 19:02:31 +01:00
|
|
|
'token_kind': token_kind},
|
|
|
|
realm="")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, "Must validate with valid Zulip server API key")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_register_remote_push_user_paramas(self) -> None:
|
2016-10-27 23:55:31 +02:00
|
|
|
token = "111222"
|
|
|
|
user_id = 11
|
|
|
|
token_kind = PushDeviceToken.GCM
|
|
|
|
|
|
|
|
endpoint = '/api/v1/remotes/push/register'
|
|
|
|
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id, 'token_kind': token_kind})
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, "Missing 'token' argument")
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id, 'token': token})
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, "Missing 'token_kind' argument")
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'token': token, 'token_kind': token_kind})
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, "Missing 'user_id' argument")
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id, 'token': token, 'token_kind': 17})
|
2017-07-07 18:29:45 +02:00
|
|
|
self.assert_json_error(result, "Invalid token type")
|
2016-10-27 23:55:31 +02:00
|
|
|
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(self.example_email("hamlet"), endpoint, {'user_id': user_id,
|
|
|
|
'token_kind': token_kind,
|
|
|
|
'token': token})
|
2017-08-29 06:28:30 +02:00
|
|
|
self.assert_json_error(result, "Account is not associated with this subdomain",
|
|
|
|
status_code=401)
|
|
|
|
|
|
|
|
# We need the root ('') subdomain to be in use for this next
|
|
|
|
# test, since the push bouncer API is only available there:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
realm.string_id = ""
|
|
|
|
realm.save()
|
|
|
|
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(self.example_email("hamlet"), endpoint, {'user_id': user_id,
|
|
|
|
'token_kind': token_kind,
|
|
|
|
'token': token})
|
2017-08-29 06:28:30 +02:00
|
|
|
self.assert_json_error(result, "Must validate with valid Zulip server API key")
|
|
|
|
|
2018-04-26 07:08:44 +02:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id,
|
|
|
|
'token_kind': token_kind,
|
|
|
|
'token': token},
|
|
|
|
subdomain="zulip")
|
|
|
|
self.assert_json_error(result, "Invalid subdomain for push notifications bouncer",
|
|
|
|
status_code=401)
|
|
|
|
|
2018-04-26 06:36:34 +02:00
|
|
|
# We do a bit of hackery here to the API_KEYS cache just to
|
|
|
|
# make the code simple for sending an incorrect API key.
|
|
|
|
from zerver.lib.test_classes import API_KEYS
|
|
|
|
API_KEYS[self.server_uuid] = 'invalid'
|
|
|
|
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id,
|
|
|
|
'token_kind': token_kind,
|
|
|
|
'token': token})
|
|
|
|
self.assert_json_error(result, "Zulip server auth failure: key does not match role 1234-abcd",
|
|
|
|
status_code=401)
|
|
|
|
|
|
|
|
del API_KEYS[self.server_uuid]
|
|
|
|
|
2018-04-26 06:50:37 +02:00
|
|
|
credentials = "%s:%s" % ("5678-efgh", 'invalid')
|
|
|
|
api_auth = 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
|
|
|
result = self.client_post(endpoint, {'user_id': user_id,
|
|
|
|
'token_kind': token_kind,
|
|
|
|
'token': token},
|
|
|
|
HTTP_AUTHORIZATION = api_auth)
|
|
|
|
self.assert_json_error(result, "Zulip server auth failure: 5678-efgh is not registered",
|
|
|
|
status_code=401)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_remote_push_user_endpoints(self) -> None:
|
2016-10-27 23:55:31 +02:00
|
|
|
endpoints = [
|
|
|
|
('/api/v1/remotes/push/register', 'register'),
|
|
|
|
('/api/v1/remotes/push/unregister', 'unregister'),
|
|
|
|
]
|
|
|
|
|
|
|
|
for endpoint, method in endpoints:
|
|
|
|
payload = self.get_generic_payload(method)
|
|
|
|
|
|
|
|
# Verify correct results are success
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, payload)
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
remote_tokens = RemotePushDeviceToken.objects.filter(token=payload['token'])
|
|
|
|
token_count = 1 if method == 'register' else 0
|
|
|
|
self.assertEqual(len(remote_tokens), token_count)
|
|
|
|
|
|
|
|
# Try adding/removing tokens that are too big...
|
2017-05-07 20:00:17 +02:00
|
|
|
broken_token = "x" * 5000 # too big
|
2016-10-27 23:55:31 +02:00
|
|
|
payload['token'] = broken_token
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, payload)
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, 'Empty or invalid length token')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_invalid_apns_token(self) -> None:
|
2017-07-07 18:18:37 +02:00
|
|
|
endpoints = [
|
2017-07-31 20:36:49 +02:00
|
|
|
('/api/v1/remotes/push/register', 'apple-token'),
|
2017-07-07 18:18:37 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
for endpoint, method in endpoints:
|
|
|
|
payload = {
|
|
|
|
'user_id': 10,
|
|
|
|
'token': 'xyz uses non-hex characters',
|
|
|
|
'token_kind': PushDeviceToken.APNS,
|
|
|
|
}
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid, endpoint, payload)
|
2017-07-07 18:18:37 +02:00
|
|
|
self.assert_json_error(result, 'Invalid APNS token')
|
|
|
|
|
2016-10-27 23:55:31 +02:00
|
|
|
@override_settings(PUSH_NOTIFICATION_BOUNCER_URL='https://push.zulip.org.example.com')
|
|
|
|
@mock.patch('zerver.lib.push_notifications.requests.request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_push_bouncer_api(self, mock: Any) -> None:
|
2016-10-27 23:55:31 +02:00
|
|
|
"""This is a variant of the below test_push_api, but using the full
|
|
|
|
push notification bouncer flow
|
|
|
|
"""
|
|
|
|
mock.side_effect = self.bounce_request
|
2017-05-07 19:39:30 +02:00
|
|
|
user = self.example_user('cordelia')
|
|
|
|
email = user.email
|
2016-10-27 23:55:31 +02:00
|
|
|
self.login(email)
|
|
|
|
server = RemoteZulipServer.objects.get(uuid=self.server_uuid)
|
|
|
|
|
|
|
|
endpoints = [
|
2017-07-07 18:29:45 +02:00
|
|
|
('/json/users/me/apns_device_token', 'apple-tokenaz', RemotePushDeviceToken.APNS),
|
|
|
|
('/json/users/me/android_gcm_reg_id', 'android-token', RemotePushDeviceToken.GCM),
|
2016-10-27 23:55:31 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
# Test error handling
|
2017-07-07 18:29:45 +02:00
|
|
|
for endpoint, _, kind in endpoints:
|
2016-10-27 23:55:31 +02:00
|
|
|
# Try adding/removing tokens that are too big...
|
2017-07-07 18:18:37 +02:00
|
|
|
broken_token = "a" * 5000 # too big
|
2017-07-07 18:29:45 +02:00
|
|
|
result = self.client_post(endpoint, {'token': broken_token,
|
2017-08-29 06:28:30 +02:00
|
|
|
'token_kind': kind},
|
|
|
|
subdomain="zulip")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, 'Empty or invalid length token')
|
|
|
|
|
2017-07-07 18:29:45 +02:00
|
|
|
result = self.client_delete(endpoint, {'token': broken_token,
|
2017-08-29 06:28:30 +02:00
|
|
|
'token_kind': kind},
|
|
|
|
subdomain="zulip")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, 'Empty or invalid length token')
|
|
|
|
|
|
|
|
# Try to remove a non-existent token...
|
2017-07-07 18:29:45 +02:00
|
|
|
result = self.client_delete(endpoint, {'token': 'abcd1234',
|
2017-08-29 06:28:30 +02:00
|
|
|
'token_kind': kind},
|
|
|
|
subdomain="zulip")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_error(result, 'Token does not exist')
|
|
|
|
|
|
|
|
# Add tokens
|
2017-07-07 18:29:45 +02:00
|
|
|
for endpoint, token, kind in endpoints:
|
2016-10-27 23:55:31 +02:00
|
|
|
# Test that we can push twice
|
2017-08-29 06:28:30 +02:00
|
|
|
result = self.client_post(endpoint, {'token': token},
|
|
|
|
subdomain="zulip")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-29 06:28:30 +02:00
|
|
|
result = self.client_post(endpoint, {'token': token},
|
|
|
|
subdomain="zulip")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id, token=token,
|
|
|
|
server=server))
|
|
|
|
self.assertEqual(len(tokens), 1)
|
|
|
|
self.assertEqual(tokens[0].token, token)
|
|
|
|
|
|
|
|
# User should have tokens for both devices now.
|
|
|
|
tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id,
|
|
|
|
server=server))
|
|
|
|
self.assertEqual(len(tokens), 2)
|
|
|
|
|
|
|
|
# Remove tokens
|
2017-07-07 18:29:45 +02:00
|
|
|
for endpoint, token, kind in endpoints:
|
|
|
|
result = self.client_delete(endpoint, {'token': token,
|
2017-08-29 06:28:30 +02:00
|
|
|
'token_kind': kind},
|
|
|
|
subdomain="zulip")
|
2016-10-27 23:55:31 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id, token=token,
|
|
|
|
server=server))
|
|
|
|
self.assertEqual(len(tokens), 0)
|
|
|
|
|
2017-05-11 10:55:05 +02:00
|
|
|
class PushNotificationTest(BouncerTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().setUp()
|
2017-05-07 21:25:59 +02:00
|
|
|
self.user_profile = self.example_user('hamlet')
|
2016-08-08 12:02:22 +02:00
|
|
|
self.tokens = [u'aaaa', u'bbbb']
|
2016-08-03 11:11:25 +02:00
|
|
|
for token in self.tokens:
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
kind=PushDeviceToken.APNS,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user=self.user_profile,
|
|
|
|
ios_app_id=settings.ZULIP_IOS_APP_ID)
|
|
|
|
|
2017-05-11 08:32:11 +02:00
|
|
|
self.remote_tokens = [u'cccc']
|
|
|
|
for token in self.remote_tokens:
|
|
|
|
RemotePushDeviceToken.objects.create(
|
|
|
|
kind=RemotePushDeviceToken.APNS,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user_id=self.user_profile.id,
|
2017-05-11 10:55:05 +02:00
|
|
|
server=RemoteZulipServer.objects.get(uuid=self.server_uuid),
|
2017-05-11 08:32:11 +02:00
|
|
|
)
|
|
|
|
|
2017-05-11 09:12:21 +02:00
|
|
|
self.sending_client = get_client('test')
|
2017-05-24 02:42:31 +02:00
|
|
|
self.sender = self.example_user('hamlet')
|
2017-05-11 09:12:21 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_message(self, type: int, type_id: int=100) -> Message:
|
2017-05-11 10:15:00 +02:00
|
|
|
recipient, _ = Recipient.objects.get_or_create(
|
|
|
|
type_id=type_id,
|
2017-05-11 09:12:21 +02:00
|
|
|
type=type,
|
|
|
|
)
|
|
|
|
|
|
|
|
return Message.objects.create(
|
|
|
|
sender=self.sender,
|
|
|
|
recipient=recipient,
|
|
|
|
subject='Test Message',
|
|
|
|
content='This is test content',
|
2017-10-07 00:19:01 +02:00
|
|
|
rendered_content='This is test content',
|
2017-05-11 09:12:21 +02:00
|
|
|
pub_date=now(),
|
|
|
|
sending_client=self.sending_client,
|
|
|
|
)
|
|
|
|
|
2018-02-09 23:19:00 +01:00
|
|
|
@contextmanager
|
|
|
|
def mock_apns(self) -> mock.MagicMock:
|
|
|
|
mock_apns = mock.Mock()
|
|
|
|
with mock.patch('zerver.lib.push_notifications.get_apns_client') as mock_get:
|
|
|
|
mock_get.return_value = mock_apns
|
|
|
|
yield mock_apns
|
|
|
|
|
2017-05-11 10:55:05 +02:00
|
|
|
class HandlePushNotificationTest(PushNotificationTest):
|
2017-08-29 06:28:30 +02:00
|
|
|
DEFAULT_SUBDOMAIN = ""
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def bounce_request(self, *args: Any, **kwargs: Any) -> HttpResponse:
|
2017-05-11 10:55:05 +02:00
|
|
|
"""This method is used to carry out the push notification bouncer
|
|
|
|
requests using the Django test browser, rather than python-requests.
|
|
|
|
"""
|
|
|
|
# args[0] is method, args[1] is URL.
|
|
|
|
local_url = args[1].replace(settings.PUSH_NOTIFICATION_BOUNCER_URL, "")
|
|
|
|
if args[0] == "POST":
|
2017-12-14 22:52:22 +01:00
|
|
|
result = self.api_post(self.server_uuid,
|
|
|
|
local_url,
|
|
|
|
kwargs['data'],
|
|
|
|
content_type="application/json")
|
2017-05-11 10:55:05 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError("Unsupported method for bounce_request")
|
|
|
|
return result
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_end_to_end(self) -> None:
|
2017-05-17 07:16:20 +02:00
|
|
|
remote_gcm_tokens = [u'dddd']
|
|
|
|
for token in remote_gcm_tokens:
|
|
|
|
RemotePushDeviceToken.objects.create(
|
|
|
|
kind=RemotePushDeviceToken.GCM,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user_id=self.user_profile.id,
|
|
|
|
server=RemoteZulipServer.objects.get(uuid=self.server_uuid),
|
|
|
|
)
|
|
|
|
|
2017-05-11 10:55:05 +02:00
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=self.user_profile,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
2017-09-10 00:47:36 +02:00
|
|
|
missed_message = {
|
|
|
|
'message_id': message.id,
|
2017-10-19 06:37:35 +02:00
|
|
|
'trigger': 'private_message',
|
2017-09-10 00:47:36 +02:00
|
|
|
}
|
2017-05-11 10:55:05 +02:00
|
|
|
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.requests.request',
|
|
|
|
side_effect=self.bounce_request), \
|
2017-05-17 07:16:20 +02:00
|
|
|
mock.patch('zerver.lib.push_notifications.gcm') as mock_gcm, \
|
2018-02-09 23:19:00 +01:00
|
|
|
self.mock_apns() as mock_apns, \
|
2017-08-19 00:09:26 +02:00
|
|
|
mock.patch('logging.info') as mock_info, \
|
2017-10-02 11:11:42 +02:00
|
|
|
mock.patch('logging.warning'):
|
2017-05-17 07:16:20 +02:00
|
|
|
apns_devices = [
|
2017-05-11 10:55:05 +02:00
|
|
|
(apn.b64_to_hex(device.token), device.ios_app_id, device.token)
|
2017-05-17 07:16:20 +02:00
|
|
|
for device in RemotePushDeviceToken.objects.filter(
|
|
|
|
kind=PushDeviceToken.APNS)
|
2017-05-11 10:55:05 +02:00
|
|
|
]
|
2017-05-17 07:16:20 +02:00
|
|
|
gcm_devices = [
|
|
|
|
(apn.b64_to_hex(device.token), device.ios_app_id, device.token)
|
|
|
|
for device in RemotePushDeviceToken.objects.filter(
|
|
|
|
kind=PushDeviceToken.GCM)
|
|
|
|
]
|
|
|
|
mock_gcm.json_request.return_value = {
|
|
|
|
'success': {gcm_devices[0][2]: message.id}}
|
2017-08-19 01:38:11 +02:00
|
|
|
mock_apns.get_notification_result.return_value = 'Success'
|
2017-05-17 07:16:20 +02:00
|
|
|
apn.handle_push_notification(self.user_profile.id, missed_message)
|
2017-08-19 01:38:11 +02:00
|
|
|
for _, _, token in apns_devices:
|
|
|
|
mock_info.assert_any_call(
|
|
|
|
"APNs: Success sending for user %d to device %s",
|
|
|
|
self.user_profile.id, token)
|
2017-05-17 07:16:20 +02:00
|
|
|
for _, _, token in gcm_devices:
|
|
|
|
mock_info.assert_any_call(
|
|
|
|
"GCM: Sent %s as %s" % (token, message.id))
|
2017-05-11 10:55:05 +02:00
|
|
|
|
2018-05-21 20:20:23 +02:00
|
|
|
# Now test the unregistered case
|
2018-08-28 21:11:49 +02:00
|
|
|
mock_apns.get_notification_result.return_value = ('Unregistered', 1234567)
|
2018-05-21 20:20:23 +02:00
|
|
|
apn.handle_push_notification(self.user_profile.id, missed_message)
|
|
|
|
for _, _, token in apns_devices:
|
|
|
|
mock_info.assert_any_call(
|
|
|
|
"APNs: Removing invalid/expired token %s (%s)" %
|
|
|
|
(token, "Unregistered"))
|
|
|
|
self.assertEqual(RemotePushDeviceToken.objects.filter(kind=PushDeviceToken.APNS).count(), 0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_end_to_end_connection_error(self) -> None:
|
2017-08-18 09:04:52 +02:00
|
|
|
remote_gcm_tokens = [u'dddd']
|
|
|
|
for token in remote_gcm_tokens:
|
|
|
|
RemotePushDeviceToken.objects.create(
|
|
|
|
kind=RemotePushDeviceToken.GCM,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user_id=self.user_profile.id,
|
|
|
|
server=RemoteZulipServer.objects.get(uuid=self.server_uuid),
|
|
|
|
)
|
|
|
|
|
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=self.user_profile,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def retry(queue_name: Any, event: Any, processor: Any) -> None:
|
2017-08-18 09:04:52 +02:00
|
|
|
apn.handle_push_notification(event['user_profile_id'], event)
|
|
|
|
|
2017-09-10 00:47:36 +02:00
|
|
|
missed_message = {
|
|
|
|
'user_profile_id': self.user_profile.id,
|
|
|
|
'message_id': message.id,
|
2017-10-19 06:37:35 +02:00
|
|
|
'trigger': 'private_message',
|
2017-09-10 00:47:36 +02:00
|
|
|
}
|
2017-08-18 09:04:52 +02:00
|
|
|
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.requests.request',
|
|
|
|
side_effect=self.bounce_request), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.gcm') as mock_gcm, \
|
|
|
|
mock.patch('zerver.lib.push_notifications.send_notifications_to_bouncer',
|
|
|
|
side_effect=requests.ConnectionError), \
|
|
|
|
mock.patch('zerver.lib.queue.queue_json_publish',
|
|
|
|
side_effect=retry) as mock_retry, \
|
|
|
|
mock.patch('logging.warning') as mock_warn:
|
|
|
|
gcm_devices = [
|
|
|
|
(apn.b64_to_hex(device.token), device.ios_app_id, device.token)
|
|
|
|
for device in RemotePushDeviceToken.objects.filter(
|
|
|
|
kind=PushDeviceToken.GCM)
|
|
|
|
]
|
|
|
|
mock_gcm.json_request.return_value = {
|
|
|
|
'success': {gcm_devices[0][2]: message.id}}
|
|
|
|
apn.handle_push_notification(self.user_profile.id, missed_message)
|
|
|
|
self.assertEqual(mock_retry.call_count, 3)
|
|
|
|
mock_warn.assert_called_with("Maximum retries exceeded for "
|
|
|
|
"trigger:%s event:"
|
|
|
|
"push_notification" % (self.user_profile.id,))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_disabled_notifications(self) -> None:
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-05-11 10:55:05 +02:00
|
|
|
user_profile.enable_online_email_notifications = False
|
|
|
|
user_profile.enable_online_push_notifications = False
|
|
|
|
user_profile.enable_offline_email_notifications = False
|
|
|
|
user_profile.enable_offline_push_notifications = False
|
2017-08-17 16:55:32 +02:00
|
|
|
user_profile.enable_stream_push_notifications = False
|
2017-05-11 10:55:05 +02:00
|
|
|
user_profile.save()
|
|
|
|
apn.handle_push_notification(user_profile.id, {})
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_read_message(self) -> None:
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-05-11 10:55:05 +02:00
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
flags=UserMessage.flags.read,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
2017-09-10 00:47:36 +02:00
|
|
|
missed_message = {
|
|
|
|
'message_id': message.id,
|
2017-10-19 06:37:35 +02:00
|
|
|
'trigger': 'private_message',
|
2017-09-10 00:47:36 +02:00
|
|
|
}
|
2017-05-11 10:55:05 +02:00
|
|
|
apn.handle_push_notification(user_profile.id, missed_message)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_notifications_to_bouncer(self) -> None:
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-05-11 10:55:05 +02:00
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
2017-09-10 00:47:36 +02:00
|
|
|
missed_message = {
|
|
|
|
'message_id': message.id,
|
2017-10-19 06:37:35 +02:00
|
|
|
'trigger': 'private_message',
|
2017-09-10 00:47:36 +02:00
|
|
|
}
|
2017-05-11 10:55:05 +02:00
|
|
|
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=True), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.get_apns_payload',
|
|
|
|
return_value={'apns': True}), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.get_gcm_payload',
|
|
|
|
return_value={'gcm': True}), \
|
|
|
|
mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_notifications_to_bouncer') as mock_send:
|
|
|
|
apn.handle_push_notification(user_profile.id, missed_message)
|
|
|
|
mock_send.assert_called_with(user_profile.id,
|
|
|
|
{'apns': True},
|
|
|
|
{'gcm': True},
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_non_bouncer_push(self) -> None:
|
2017-05-11 10:55:05 +02:00
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=self.user_profile,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
|
|
|
for token in [u'dddd']:
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
kind=PushDeviceToken.GCM,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user=self.user_profile)
|
|
|
|
|
|
|
|
android_devices = list(
|
|
|
|
PushDeviceToken.objects.filter(user=self.user_profile,
|
|
|
|
kind=PushDeviceToken.GCM))
|
|
|
|
|
|
|
|
apple_devices = list(
|
|
|
|
PushDeviceToken.objects.filter(user=self.user_profile,
|
|
|
|
kind=PushDeviceToken.APNS))
|
|
|
|
|
2017-09-10 00:47:36 +02:00
|
|
|
missed_message = {
|
|
|
|
'message_id': message.id,
|
2017-10-19 06:37:35 +02:00
|
|
|
'trigger': 'private_message',
|
2017-09-10 00:47:36 +02:00
|
|
|
}
|
2017-05-11 10:55:05 +02:00
|
|
|
with mock.patch('zerver.lib.push_notifications.get_apns_payload',
|
|
|
|
return_value={'apns': True}), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.get_gcm_payload',
|
|
|
|
return_value={'gcm': True}), \
|
|
|
|
mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_apple_push_notification') as mock_send_apple, \
|
|
|
|
mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_android_push_notification') as mock_send_android:
|
|
|
|
|
|
|
|
apn.handle_push_notification(self.user_profile.id, missed_message)
|
|
|
|
mock_send_apple.assert_called_with(self.user_profile.id,
|
|
|
|
apple_devices,
|
2017-08-19 01:38:11 +02:00
|
|
|
{'apns': True})
|
2017-05-11 10:55:05 +02:00
|
|
|
mock_send_android.assert_called_with(android_devices,
|
|
|
|
{'gcm': True})
|
|
|
|
|
2018-08-02 01:29:06 +02:00
|
|
|
@override_settings(SEND_REMOVE_PUSH_NOTIFICATIONS=True)
|
2018-07-28 14:31:45 +02:00
|
|
|
def test_send_remove_notifications_to_bouncer(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
|
|
|
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=True), \
|
|
|
|
mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_notifications_to_bouncer') as mock_send_android, \
|
|
|
|
mock.patch('zerver.lib.push_notifications.get_common_payload',
|
|
|
|
return_value={'gcm': True}):
|
2018-08-02 01:28:28 +02:00
|
|
|
apn.handle_remove_push_notification(user_profile.id, message.id)
|
2018-07-28 14:31:45 +02:00
|
|
|
mock_send_android.assert_called_with(user_profile.id, {},
|
|
|
|
{'gcm': True,
|
|
|
|
'event': 'remove',
|
|
|
|
'zulip_message_id': message.id})
|
|
|
|
|
2018-08-02 01:29:06 +02:00
|
|
|
@override_settings(SEND_REMOVE_PUSH_NOTIFICATIONS=True)
|
2018-07-28 14:31:45 +02:00
|
|
|
def test_non_bouncer_push_remove(self) -> None:
|
|
|
|
message = self.get_message(Recipient.PERSONAL, type_id=1)
|
|
|
|
UserMessage.objects.create(
|
|
|
|
user_profile=self.user_profile,
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
|
|
|
|
for token in [u'dddd']:
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
kind=PushDeviceToken.GCM,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user=self.user_profile)
|
|
|
|
|
|
|
|
android_devices = list(
|
|
|
|
PushDeviceToken.objects.filter(user=self.user_profile,
|
|
|
|
kind=PushDeviceToken.GCM))
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_android_push_notification') as mock_send_android, \
|
|
|
|
mock.patch('zerver.lib.push_notifications.get_common_payload',
|
|
|
|
return_value={'gcm': True}):
|
2018-08-02 01:28:28 +02:00
|
|
|
apn.handle_remove_push_notification(self.user_profile.id, message.id)
|
2018-07-28 14:31:45 +02:00
|
|
|
mock_send_android.assert_called_with(android_devices,
|
|
|
|
{'gcm': True,
|
|
|
|
'event': 'remove',
|
|
|
|
'zulip_message_id': message.id})
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_message_does_not_exist(self) -> None:
|
2017-11-10 00:51:06 +01:00
|
|
|
"""This simulates a condition that should only be an error if the user is
|
|
|
|
not long-term idle; we fake it, though, in the sense that the user should
|
|
|
|
not have received the message in the first place"""
|
|
|
|
self.make_stream('public_stream')
|
|
|
|
message_id = self.send_stream_message("iago@zulip.com", "public_stream", "test")
|
|
|
|
missed_message = {'message_id': message_id}
|
2017-05-11 10:55:05 +02:00
|
|
|
with mock.patch('logging.error') as mock_logger:
|
|
|
|
apn.handle_push_notification(self.user_profile.id, missed_message)
|
|
|
|
mock_logger.assert_called_with("Could not find UserMessage with "
|
2017-11-10 00:51:06 +01:00
|
|
|
"message_id %s and user_id %s" %
|
|
|
|
(message_id, self.user_profile.id,))
|
|
|
|
|
2017-11-17 10:47:43 +01:00
|
|
|
def test_user_message_soft_deactivated(self) -> None:
|
2017-11-10 00:51:06 +01:00
|
|
|
"""This simulates a condition that should only be an error if the user is
|
|
|
|
not long-term idle; we fake it, though, in the sense that the user should
|
|
|
|
not have received the message in the first place"""
|
|
|
|
self.make_stream('public_stream')
|
|
|
|
self.subscribe(self.user_profile, 'public_stream')
|
|
|
|
do_soft_deactivate_users([self.user_profile])
|
|
|
|
|
|
|
|
message_id = self.send_stream_message("iago@zulip.com", "public_stream", "test")
|
|
|
|
missed_message = {
|
|
|
|
'message_id': message_id,
|
|
|
|
'trigger': 'stream_push_notify',
|
|
|
|
}
|
|
|
|
|
|
|
|
for token in [u'dddd']:
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
kind=PushDeviceToken.GCM,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user=self.user_profile)
|
|
|
|
|
|
|
|
android_devices = list(
|
|
|
|
PushDeviceToken.objects.filter(user=self.user_profile,
|
|
|
|
kind=PushDeviceToken.GCM))
|
|
|
|
|
|
|
|
apple_devices = list(
|
|
|
|
PushDeviceToken.objects.filter(user=self.user_profile,
|
|
|
|
kind=PushDeviceToken.APNS))
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.push_notifications.get_apns_payload',
|
|
|
|
return_value={'apns': True}), \
|
|
|
|
mock.patch('zerver.lib.push_notifications.get_gcm_payload',
|
|
|
|
return_value={'gcm': True}), \
|
|
|
|
mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_apple_push_notification') as mock_send_apple, \
|
|
|
|
mock.patch('zerver.lib.push_notifications'
|
|
|
|
'.send_android_push_notification') as mock_send_android, \
|
|
|
|
mock.patch('logging.error') as mock_logger:
|
|
|
|
apn.handle_push_notification(self.user_profile.id, missed_message)
|
|
|
|
mock_logger.assert_not_called()
|
|
|
|
mock_send_apple.assert_called_with(self.user_profile.id,
|
|
|
|
apple_devices,
|
|
|
|
{'apns': True})
|
|
|
|
mock_send_android.assert_called_with(android_devices,
|
|
|
|
{'gcm': True})
|
2017-05-11 10:55:05 +02:00
|
|
|
|
2017-08-29 01:03:29 +02:00
|
|
|
class TestAPNs(PushNotificationTest):
|
2017-11-05 10:51:25 +01:00
|
|
|
def devices(self) -> List[DeviceToken]:
|
2017-08-29 01:03:29 +02:00
|
|
|
return list(PushDeviceToken.objects.filter(
|
|
|
|
user=self.user_profile, kind=PushDeviceToken.APNS))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def send(self, devices: Optional[List[PushDeviceToken]]=None,
|
|
|
|
payload_data: Dict[str, Any]={}) -> None:
|
2017-10-12 03:56:50 +02:00
|
|
|
if devices is None:
|
|
|
|
devices = self.devices()
|
2017-08-29 01:03:29 +02:00
|
|
|
apn.send_apple_push_notification(
|
2017-10-12 03:56:50 +02:00
|
|
|
self.user_profile.id, devices, payload_data)
|
|
|
|
|
2018-02-10 03:22:26 +01:00
|
|
|
def test_get_apns_client(self) -> None:
|
2018-02-12 23:34:59 +01:00
|
|
|
import zerver.lib.push_notifications
|
|
|
|
zerver.lib.push_notifications._apns_client_initialized = False
|
2018-02-10 03:22:26 +01:00
|
|
|
with self.settings(APNS_CERT_FILE='/foo.pem'), \
|
2018-08-08 18:16:57 +02:00
|
|
|
mock.patch('apns2.client.APNsClient') as mock_client:
|
2018-02-10 03:22:26 +01:00
|
|
|
client = get_apns_client()
|
|
|
|
self.assertEqual(mock_client.return_value, client)
|
|
|
|
|
2018-02-09 23:19:00 +01:00
|
|
|
def test_not_configured(self) -> None:
|
|
|
|
with mock.patch('zerver.lib.push_notifications.get_apns_client') as mock_get, \
|
|
|
|
mock.patch('zerver.lib.push_notifications.logging') as mock_logging:
|
|
|
|
mock_get.return_value = None
|
|
|
|
self.send()
|
|
|
|
mock_logging.warning.assert_called_once_with(
|
|
|
|
"APNs: Dropping a notification because nothing configured. "
|
|
|
|
"Set PUSH_NOTIFICATION_BOUNCER_URL (or APNS_CERT_FILE).")
|
|
|
|
mock_logging.info.assert_not_called()
|
2017-08-29 01:03:29 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_success(self) -> None:
|
2018-02-09 23:19:00 +01:00
|
|
|
with self.mock_apns() as mock_apns, \
|
2017-08-29 01:03:29 +02:00
|
|
|
mock.patch('zerver.lib.push_notifications.logging') as mock_logging:
|
|
|
|
mock_apns.get_notification_result.return_value = 'Success'
|
|
|
|
self.send()
|
2017-10-02 11:11:42 +02:00
|
|
|
mock_logging.warning.assert_not_called()
|
2017-08-29 01:03:29 +02:00
|
|
|
for device in self.devices():
|
|
|
|
mock_logging.info.assert_any_call(
|
|
|
|
"APNs: Success sending for user %d to device %s",
|
|
|
|
self.user_profile.id, device.token)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_http_retry(self) -> None:
|
2017-08-29 01:05:20 +02:00
|
|
|
import hyper
|
2018-02-09 23:19:00 +01:00
|
|
|
with self.mock_apns() as mock_apns, \
|
2017-08-29 01:05:20 +02:00
|
|
|
mock.patch('zerver.lib.push_notifications.logging') as mock_logging:
|
|
|
|
mock_apns.get_notification_result.side_effect = itertools.chain(
|
|
|
|
[hyper.http20.exceptions.StreamResetError()],
|
|
|
|
itertools.repeat('Success'))
|
|
|
|
self.send()
|
2017-10-02 11:11:42 +02:00
|
|
|
mock_logging.warning.assert_called_once_with(
|
2017-08-29 01:05:20 +02:00
|
|
|
"APNs: HTTP error sending for user %d to device %s: %s",
|
|
|
|
self.user_profile.id, self.devices()[0].token, "StreamResetError")
|
|
|
|
for device in self.devices():
|
|
|
|
mock_logging.info.assert_any_call(
|
|
|
|
"APNs: Success sending for user %d to device %s",
|
|
|
|
self.user_profile.id, device.token)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_http_retry_eventually_fails(self) -> None:
|
2017-10-12 03:56:50 +02:00
|
|
|
import hyper
|
2018-02-09 23:19:00 +01:00
|
|
|
with self.mock_apns() as mock_apns, \
|
2017-10-12 03:56:50 +02:00
|
|
|
mock.patch('zerver.lib.push_notifications.logging') as mock_logging:
|
|
|
|
mock_apns.get_notification_result.side_effect = itertools.chain(
|
|
|
|
[hyper.http20.exceptions.StreamResetError()],
|
|
|
|
[hyper.http20.exceptions.StreamResetError()],
|
|
|
|
[hyper.http20.exceptions.StreamResetError()],
|
|
|
|
[hyper.http20.exceptions.StreamResetError()],
|
|
|
|
[hyper.http20.exceptions.StreamResetError()],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.send(devices=self.devices()[0:1])
|
|
|
|
self.assertEqual(mock_logging.warning.call_count, 5)
|
|
|
|
mock_logging.warning.assert_called_with(
|
|
|
|
'APNs: Failed to send for user %d to device %s: %s',
|
|
|
|
self.user_profile.id, self.devices()[0].token, 'HTTP error, retries exhausted')
|
|
|
|
self.assertEqual(mock_logging.info.call_count, 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_modernize_apns_payload(self) -> None:
|
2017-09-28 03:08:37 +02:00
|
|
|
payload = {'alert': 'Message from Hamlet',
|
2017-10-02 09:00:25 +02:00
|
|
|
'badge': 0,
|
2017-09-28 03:08:37 +02:00
|
|
|
'custom': {'zulip': {'message_ids': [3]}}}
|
|
|
|
self.assertEqual(
|
|
|
|
apn.modernize_apns_payload(
|
|
|
|
{'alert': 'Message from Hamlet',
|
|
|
|
'message_ids': [3]}),
|
|
|
|
payload)
|
|
|
|
self.assertEqual(
|
|
|
|
apn.modernize_apns_payload(payload),
|
|
|
|
payload)
|
|
|
|
|
2017-05-11 09:12:21 +02:00
|
|
|
class TestGetAlertFromMessage(PushNotificationTest):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_alert_from_private_group_message(self) -> None:
|
2017-09-10 00:47:36 +02:00
|
|
|
message = self.get_message(Recipient.HUDDLE)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'private_message'
|
2017-09-10 00:47:36 +02:00
|
|
|
alert = apn.get_alert_from_message(message)
|
2017-05-11 09:12:21 +02:00
|
|
|
self.assertEqual(alert, "New private group message from King Hamlet")
|
2017-09-10 00:47:36 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_alert_from_private_message(self) -> None:
|
2017-09-10 00:47:36 +02:00
|
|
|
message = self.get_message(Recipient.PERSONAL)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'private_message'
|
2017-09-10 00:47:36 +02:00
|
|
|
alert = apn.get_alert_from_message(message)
|
2017-05-11 09:12:21 +02:00
|
|
|
self.assertEqual(alert, "New private message from King Hamlet")
|
2017-09-10 00:47:36 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_alert_from_mention(self) -> None:
|
2017-09-10 00:47:36 +02:00
|
|
|
message = self.get_message(Recipient.STREAM)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'mentioned'
|
2017-09-10 00:47:36 +02:00
|
|
|
alert = apn.get_alert_from_message(message)
|
2017-05-11 09:12:21 +02:00
|
|
|
self.assertEqual(alert, "New mention from King Hamlet")
|
2017-09-10 00:47:36 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_alert_from_stream_message(self) -> None:
|
2017-09-10 00:47:36 +02:00
|
|
|
message = self.get_message(Recipient.STREAM)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'stream_push_notify'
|
2017-09-10 00:47:36 +02:00
|
|
|
message.stream_name = 'Denmark'
|
|
|
|
alert = apn.get_alert_from_message(message)
|
|
|
|
self.assertEqual(alert, "New stream message from King Hamlet in Denmark")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_alert_from_other_message(self) -> None:
|
2017-09-10 00:47:36 +02:00
|
|
|
message = self.get_message(0)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'stream_push_notify'
|
2017-09-10 00:47:36 +02:00
|
|
|
alert = apn.get_alert_from_message(message)
|
2017-05-11 09:12:21 +02:00
|
|
|
alert = apn.get_alert_from_message(self.get_message(0))
|
|
|
|
self.assertEqual(alert,
|
|
|
|
"New Zulip mentions and private messages from King "
|
|
|
|
"Hamlet")
|
|
|
|
|
2017-05-11 09:26:00 +02:00
|
|
|
class TestGetAPNsPayload(PushNotificationTest):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_apns_payload(self) -> None:
|
2018-10-19 00:09:18 +02:00
|
|
|
user_profile = self.example_user("othello")
|
2018-02-16 23:18:47 +01:00
|
|
|
message_id = self.send_huddle_message(
|
|
|
|
self.sender.email,
|
|
|
|
[self.example_email('othello'), self.example_email('cordelia')])
|
|
|
|
message = Message.objects.get(id=message_id)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'private_message'
|
2018-10-19 00:09:18 +02:00
|
|
|
payload = apn.get_apns_payload(user_profile, message)
|
2017-05-11 09:26:00 +02:00
|
|
|
expected = {
|
2017-08-31 22:27:46 +02:00
|
|
|
'alert': {
|
|
|
|
'title': "New private group message from King Hamlet",
|
|
|
|
'body': message.content,
|
|
|
|
},
|
2017-10-02 09:00:25 +02:00
|
|
|
'badge': 0,
|
2017-08-19 01:38:11 +02:00
|
|
|
'custom': {
|
|
|
|
'zulip': {
|
|
|
|
'message_ids': [message.id],
|
2017-12-12 05:40:11 +01:00
|
|
|
'recipient_type': 'private',
|
2018-02-16 23:18:47 +01:00
|
|
|
'pm_users': ','.join(
|
|
|
|
str(s.user_profile_id)
|
|
|
|
for s in Subscription.objects.filter(
|
|
|
|
recipient=message.recipient)),
|
2017-12-12 05:40:11 +01:00
|
|
|
'sender_email': 'hamlet@zulip.com',
|
|
|
|
'sender_id': 4,
|
|
|
|
'server': settings.EXTERNAL_HOST,
|
|
|
|
'realm_id': message.sender.realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
'realm_uri': message.sender.realm.uri,
|
2017-12-12 05:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
|
|
|
def test_get_apns_payload_stream(self):
|
|
|
|
# type: () -> None
|
2018-10-19 00:09:18 +02:00
|
|
|
user_profile = self.example_user("othello")
|
2017-12-12 05:40:11 +01:00
|
|
|
stream = Stream.objects.filter(name='Verona').get()
|
|
|
|
message = self.get_message(Recipient.STREAM, stream.id)
|
|
|
|
message.trigger = 'mentioned'
|
|
|
|
message.stream_name = 'Verona'
|
2018-10-19 00:09:18 +02:00
|
|
|
payload = apn.get_apns_payload(user_profile, message)
|
2017-12-12 05:40:11 +01:00
|
|
|
expected = {
|
|
|
|
'alert': {
|
|
|
|
'title': "New mention from King Hamlet",
|
|
|
|
'body': message.content,
|
|
|
|
},
|
|
|
|
'badge': 0,
|
|
|
|
'custom': {
|
|
|
|
'zulip': {
|
|
|
|
'message_ids': [message.id],
|
|
|
|
'recipient_type': 'stream',
|
|
|
|
'sender_email': 'hamlet@zulip.com',
|
|
|
|
'sender_id': 4,
|
|
|
|
"stream": apn.get_display_recipient(message.recipient),
|
|
|
|
"topic": message.subject,
|
|
|
|
'server': settings.EXTERNAL_HOST,
|
|
|
|
'realm_id': message.sender.realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
'realm_uri': message.sender.realm.uri,
|
2017-08-19 01:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-11 09:26:00 +02:00
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
2017-10-10 11:14:10 +02:00
|
|
|
@override_settings(PUSH_NOTIFICATION_REDACT_CONTENT = True)
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_apns_payload_redacted_content(self) -> None:
|
2018-10-19 00:09:18 +02:00
|
|
|
user_profile = self.example_user("othello")
|
2018-02-16 23:18:47 +01:00
|
|
|
message_id = self.send_huddle_message(
|
|
|
|
self.sender.email,
|
|
|
|
[self.example_email('othello'), self.example_email('cordelia')])
|
|
|
|
message = Message.objects.get(id=message_id)
|
2017-10-20 03:34:28 +02:00
|
|
|
message.trigger = 'private_message'
|
2018-10-19 00:09:18 +02:00
|
|
|
payload = apn.get_apns_payload(user_profile, message)
|
2017-10-10 11:14:10 +02:00
|
|
|
expected = {
|
|
|
|
'alert': {
|
|
|
|
'title': "New private group message from King Hamlet",
|
|
|
|
'body': "***REDACTED***",
|
|
|
|
},
|
|
|
|
'badge': 0,
|
|
|
|
'custom': {
|
|
|
|
'zulip': {
|
|
|
|
'message_ids': [message.id],
|
2017-12-12 05:40:11 +01:00
|
|
|
'recipient_type': 'private',
|
2018-02-16 23:18:47 +01:00
|
|
|
'pm_users': ','.join(
|
|
|
|
str(s.user_profile_id)
|
|
|
|
for s in Subscription.objects.filter(
|
|
|
|
recipient=message.recipient)),
|
2017-12-12 05:40:11 +01:00
|
|
|
'sender_email': self.example_email("hamlet"),
|
|
|
|
'sender_id': 4,
|
|
|
|
'server': settings.EXTERNAL_HOST,
|
|
|
|
'realm_id': message.sender.realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
'realm_uri': message.sender.realm.uri,
|
2017-10-10 11:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
2017-05-11 10:15:00 +02:00
|
|
|
class TestGetGCMPayload(PushNotificationTest):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_gcm_payload(self) -> None:
|
2017-05-11 10:15:00 +02:00
|
|
|
stream = Stream.objects.filter(name='Verona').get()
|
|
|
|
message = self.get_message(Recipient.STREAM, stream.id)
|
|
|
|
message.content = 'a' * 210
|
2017-10-07 00:19:01 +02:00
|
|
|
message.rendered_content = 'a' * 210
|
2017-05-11 10:15:00 +02:00
|
|
|
message.save()
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'mentioned'
|
2017-05-11 10:15:00 +02:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-05-11 10:15:00 +02:00
|
|
|
payload = apn.get_gcm_payload(user_profile, message)
|
|
|
|
expected = {
|
2017-05-24 02:42:31 +02:00
|
|
|
"user": user_profile.email,
|
2017-05-11 10:15:00 +02:00
|
|
|
"event": "message",
|
|
|
|
"alert": "New mention from King Hamlet",
|
|
|
|
"zulip_message_id": message.id,
|
|
|
|
"time": apn.datetime_to_timestamp(message.pub_date),
|
2017-10-07 00:19:01 +02:00
|
|
|
"content": 'a' * 200 + '…',
|
2017-05-11 10:15:00 +02:00
|
|
|
"content_truncated": True,
|
2017-12-16 03:01:49 +01:00
|
|
|
"server": settings.EXTERNAL_HOST,
|
|
|
|
"realm_id": self.example_user("hamlet").realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
"realm_uri": self.example_user("hamlet").realm.uri,
|
2017-12-16 03:01:49 +01:00
|
|
|
"sender_id": self.example_user("hamlet").id,
|
2017-05-25 01:40:26 +02:00
|
|
|
"sender_email": self.example_email("hamlet"),
|
2017-05-11 10:15:00 +02:00
|
|
|
"sender_full_name": "King Hamlet",
|
2017-06-15 07:44:00 +02:00
|
|
|
"sender_avatar_url": apn.absolute_avatar_url(message.sender),
|
2017-05-11 10:15:00 +02:00
|
|
|
"recipient_type": "stream",
|
|
|
|
"stream": apn.get_display_recipient(message.recipient),
|
|
|
|
"topic": message.subject,
|
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_gcm_payload_personal(self) -> None:
|
2017-05-11 10:15:00 +02:00
|
|
|
message = self.get_message(Recipient.PERSONAL, 1)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'private_message'
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-05-11 10:15:00 +02:00
|
|
|
payload = apn.get_gcm_payload(user_profile, message)
|
|
|
|
expected = {
|
2017-05-24 02:42:31 +02:00
|
|
|
"user": user_profile.email,
|
2017-05-11 10:15:00 +02:00
|
|
|
"event": "message",
|
|
|
|
"alert": "New private message from King Hamlet",
|
|
|
|
"zulip_message_id": message.id,
|
|
|
|
"time": apn.datetime_to_timestamp(message.pub_date),
|
|
|
|
"content": message.content,
|
|
|
|
"content_truncated": False,
|
2017-12-16 03:01:49 +01:00
|
|
|
"server": settings.EXTERNAL_HOST,
|
|
|
|
"realm_id": self.example_user("hamlet").realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
"realm_uri": self.example_user("hamlet").realm.uri,
|
2017-12-16 03:01:49 +01:00
|
|
|
"sender_id": self.example_user("hamlet").id,
|
2017-05-25 01:40:26 +02:00
|
|
|
"sender_email": self.example_email("hamlet"),
|
2017-05-11 10:15:00 +02:00
|
|
|
"sender_full_name": "King Hamlet",
|
2017-06-15 07:44:00 +02:00
|
|
|
"sender_avatar_url": apn.absolute_avatar_url(message.sender),
|
2017-05-11 10:15:00 +02:00
|
|
|
"recipient_type": "private",
|
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_gcm_payload_stream_notifications(self) -> None:
|
2017-09-10 00:47:36 +02:00
|
|
|
message = self.get_message(Recipient.STREAM, 1)
|
2017-10-19 06:37:35 +02:00
|
|
|
message.trigger = 'stream_push_notify'
|
2017-09-10 00:47:36 +02:00
|
|
|
message.stream_name = 'Denmark'
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
payload = apn.get_gcm_payload(user_profile, message)
|
|
|
|
expected = {
|
|
|
|
"user": user_profile.email,
|
|
|
|
"event": "message",
|
|
|
|
"alert": "New stream message from King Hamlet in Denmark",
|
|
|
|
"zulip_message_id": message.id,
|
|
|
|
"time": apn.datetime_to_timestamp(message.pub_date),
|
|
|
|
"content": message.content,
|
|
|
|
"content_truncated": False,
|
2017-12-16 03:01:49 +01:00
|
|
|
"server": settings.EXTERNAL_HOST,
|
|
|
|
"realm_id": self.example_user("hamlet").realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
"realm_uri": self.example_user("hamlet").realm.uri,
|
2017-12-16 03:01:49 +01:00
|
|
|
"sender_id": self.example_user("hamlet").id,
|
2017-09-10 00:47:36 +02:00
|
|
|
"sender_email": self.example_email("hamlet"),
|
|
|
|
"sender_full_name": "King Hamlet",
|
|
|
|
"sender_avatar_url": apn.absolute_avatar_url(message.sender),
|
|
|
|
"recipient_type": "stream",
|
|
|
|
"topic": "Test Message",
|
|
|
|
"stream": "Denmark"
|
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
2017-10-10 11:14:10 +02:00
|
|
|
@override_settings(PUSH_NOTIFICATION_REDACT_CONTENT = True)
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_gcm_payload_redacted_content(self) -> None:
|
2017-10-10 11:14:10 +02:00
|
|
|
message = self.get_message(Recipient.STREAM, 1)
|
2017-10-20 03:34:28 +02:00
|
|
|
message.trigger = 'stream_push_notify'
|
2017-10-10 11:14:10 +02:00
|
|
|
message.stream_name = 'Denmark'
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
payload = apn.get_gcm_payload(user_profile, message)
|
|
|
|
expected = {
|
|
|
|
"user": user_profile.email,
|
|
|
|
"event": "message",
|
|
|
|
"alert": "New stream message from King Hamlet in Denmark",
|
|
|
|
"zulip_message_id": message.id,
|
|
|
|
"time": apn.datetime_to_timestamp(message.pub_date),
|
|
|
|
"content": "***REDACTED***",
|
|
|
|
"content_truncated": False,
|
2017-12-16 03:01:49 +01:00
|
|
|
"server": settings.EXTERNAL_HOST,
|
|
|
|
"realm_id": self.example_user("hamlet").realm.id,
|
2018-08-02 00:44:40 +02:00
|
|
|
"realm_uri": self.example_user("hamlet").realm.uri,
|
2017-12-16 03:01:49 +01:00
|
|
|
"sender_id": self.example_user("hamlet").id,
|
2017-10-10 11:14:10 +02:00
|
|
|
"sender_email": self.example_email("hamlet"),
|
|
|
|
"sender_full_name": "King Hamlet",
|
|
|
|
"sender_avatar_url": apn.absolute_avatar_url(message.sender),
|
|
|
|
"recipient_type": "stream",
|
|
|
|
"topic": "Test Message",
|
|
|
|
"stream": "Denmark"
|
|
|
|
}
|
|
|
|
self.assertDictEqual(payload, expected)
|
|
|
|
|
|
|
|
|
2017-05-11 10:31:31 +02:00
|
|
|
class TestSendNotificationsToBouncer(ZulipTestCase):
|
|
|
|
@mock.patch('zerver.lib.push_notifications.send_to_push_bouncer')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_notifications_to_bouncer(self, mock_send: mock.MagicMock) -> None:
|
2017-05-11 10:31:31 +02:00
|
|
|
apn.send_notifications_to_bouncer(1, {'apns': True}, {'gcm': True})
|
|
|
|
post_data = {
|
|
|
|
'user_id': 1,
|
|
|
|
'apns_payload': {'apns': True},
|
|
|
|
'gcm_payload': {'gcm': True},
|
|
|
|
}
|
|
|
|
mock_send.assert_called_with('POST',
|
|
|
|
'notify',
|
|
|
|
ujson.dumps(post_data),
|
|
|
|
extra_headers={'Content-type':
|
|
|
|
'application/json'})
|
|
|
|
|
2017-11-05 11:49:43 +01:00
|
|
|
class Result:
|
2017-11-05 10:51:25 +01:00
|
|
|
def __init__(self, status: int=200, content: str=ujson.dumps({'msg': 'error'})) -> None:
|
2017-10-12 03:02:35 +02:00
|
|
|
self.status_code = status
|
|
|
|
self.content = content
|
2017-05-12 09:47:31 +02:00
|
|
|
|
2017-10-12 03:02:35 +02:00
|
|
|
class TestSendToPushBouncer(PushNotificationTest):
|
2017-05-12 09:47:31 +02:00
|
|
|
@mock.patch('requests.request', return_value=Result(status=500))
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_500_error(self, mock_request: mock.MagicMock) -> None:
|
2017-10-12 03:02:35 +02:00
|
|
|
with self.assertRaises(PushNotificationBouncerException) as exc:
|
2017-05-12 09:47:31 +02:00
|
|
|
apn.send_to_push_bouncer('register', 'register', {'data': True})
|
2017-10-12 03:02:35 +02:00
|
|
|
self.assertEqual(str(exc.exception),
|
|
|
|
'Received 500 from push notification bouncer')
|
2017-05-12 09:47:31 +02:00
|
|
|
|
|
|
|
@mock.patch('requests.request', return_value=Result(status=400))
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_400_error(self, mock_request: mock.MagicMock) -> None:
|
2017-05-12 09:47:31 +02:00
|
|
|
with self.assertRaises(apn.JsonableError) as exc:
|
|
|
|
apn.send_to_push_bouncer('register', 'register', {'msg': True})
|
2017-07-20 00:22:36 +02:00
|
|
|
self.assertEqual(exc.exception.msg, 'error')
|
2017-05-12 09:47:31 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_400_error_invalid_server_key(self) -> None:
|
2017-10-12 03:02:35 +02:00
|
|
|
from zerver.decorator import InvalidZulipServerError
|
|
|
|
# This is the exception our decorator uses for an invalid Zulip server
|
|
|
|
error_obj = InvalidZulipServerError("testRole")
|
|
|
|
with mock.patch('requests.request',
|
|
|
|
return_value=Result(status=400,
|
|
|
|
content=ujson.dumps(error_obj.to_json()))):
|
|
|
|
with self.assertRaises(PushNotificationBouncerException) as exc:
|
|
|
|
apn.send_to_push_bouncer('register', 'register', {'msg': True})
|
|
|
|
self.assertEqual(str(exc.exception),
|
|
|
|
'Push notifications bouncer error: '
|
|
|
|
'Zulip server auth failure: testRole is not registered')
|
|
|
|
|
2017-05-12 09:47:31 +02:00
|
|
|
@mock.patch('requests.request', return_value=Result(status=400, content='/'))
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_400_error_when_content_is_not_serializable(self, mock_request: mock.MagicMock) -> None:
|
2017-10-12 03:02:35 +02:00
|
|
|
with self.assertRaises(ValueError) as exc:
|
2017-05-12 09:47:31 +02:00
|
|
|
apn.send_to_push_bouncer('register', 'register', {'msg': True})
|
2017-10-12 03:02:35 +02:00
|
|
|
self.assertEqual(str(exc.exception),
|
|
|
|
'Expected object or value')
|
2017-05-12 09:47:31 +02:00
|
|
|
|
|
|
|
@mock.patch('requests.request', return_value=Result(status=300, content='/'))
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_300_error(self, mock_request: mock.MagicMock) -> None:
|
2017-10-12 03:02:35 +02:00
|
|
|
with self.assertRaises(PushNotificationBouncerException) as exc:
|
2017-05-12 09:47:31 +02:00
|
|
|
apn.send_to_push_bouncer('register', 'register', {'msg': True})
|
2017-10-12 03:02:35 +02:00
|
|
|
self.assertEqual(str(exc.exception),
|
|
|
|
'Push notification bouncer returned unexpected status code 300')
|
2017-05-12 09:47:31 +02:00
|
|
|
|
2017-05-12 09:55:12 +02:00
|
|
|
class TestNumPushDevicesForUser(PushNotificationTest):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_when_kind_is_none(self) -> None:
|
2017-05-12 09:55:12 +02:00
|
|
|
self.assertEqual(apn.num_push_devices_for_user(self.user_profile), 2)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_when_kind_is_not_none(self) -> None:
|
2017-05-12 09:55:12 +02:00
|
|
|
count = apn.num_push_devices_for_user(self.user_profile,
|
|
|
|
kind=PushDeviceToken.APNS)
|
|
|
|
self.assertEqual(count, 2)
|
|
|
|
|
2016-09-15 15:19:46 +02:00
|
|
|
class TestPushApi(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_push_api(self) -> None:
|
2017-05-07 19:39:30 +02:00
|
|
|
user = self.example_user('cordelia')
|
|
|
|
email = user.email
|
2016-09-15 15:19:46 +02:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
endpoints = [
|
2017-07-07 18:18:37 +02:00
|
|
|
('/json/users/me/apns_device_token', 'apple-tokenaz'),
|
2016-09-15 15:19:46 +02:00
|
|
|
('/json/users/me/android_gcm_reg_id', 'android-token'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Test error handling
|
2017-07-07 18:18:37 +02:00
|
|
|
for endpoint, label in endpoints:
|
2016-09-15 15:19:46 +02:00
|
|
|
# Try adding/removing tokens that are too big...
|
2017-07-07 18:18:37 +02:00
|
|
|
broken_token = "a" * 5000 # too big
|
2016-09-15 15:19:46 +02:00
|
|
|
result = self.client_post(endpoint, {'token': broken_token})
|
|
|
|
self.assert_json_error(result, 'Empty or invalid length token')
|
|
|
|
|
2017-07-07 18:18:37 +02:00
|
|
|
if label == 'apple-tokenaz':
|
|
|
|
result = self.client_post(endpoint, {'token': 'xyz has non-hex characters'})
|
|
|
|
self.assert_json_error(result, 'Invalid APNS token')
|
|
|
|
|
2016-09-15 15:19:46 +02:00
|
|
|
result = self.client_delete(endpoint, {'token': broken_token})
|
|
|
|
self.assert_json_error(result, 'Empty or invalid length token')
|
|
|
|
|
|
|
|
# Try to remove a non-existent token...
|
2017-07-07 18:18:37 +02:00
|
|
|
result = self.client_delete(endpoint, {'token': 'abcd1234'})
|
2016-09-15 15:19:46 +02:00
|
|
|
self.assert_json_error(result, 'Token does not exist')
|
|
|
|
|
|
|
|
# Add tokens
|
|
|
|
for endpoint, token in endpoints:
|
|
|
|
# Test that we can push twice
|
|
|
|
result = self.client_post(endpoint, {'token': token})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_post(endpoint, {'token': token})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
tokens = list(PushDeviceToken.objects.filter(user=user, token=token))
|
|
|
|
self.assertEqual(len(tokens), 1)
|
|
|
|
self.assertEqual(tokens[0].token, token)
|
|
|
|
|
|
|
|
# User should have tokens for both devices now.
|
|
|
|
tokens = list(PushDeviceToken.objects.filter(user=user))
|
|
|
|
self.assertEqual(len(tokens), 2)
|
|
|
|
|
|
|
|
# Remove tokens
|
|
|
|
for endpoint, token in endpoints:
|
|
|
|
result = self.client_delete(endpoint, {'token': token})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
tokens = list(PushDeviceToken.objects.filter(user=user, token=token))
|
|
|
|
self.assertEqual(len(tokens), 0)
|
|
|
|
|
2016-08-08 14:20:41 +02:00
|
|
|
class GCMTest(PushNotificationTest):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().setUp()
|
2016-12-13 08:41:48 +01:00
|
|
|
apn.gcm = gcm.GCM('fake key')
|
2016-08-08 14:20:41 +02:00
|
|
|
self.gcm_tokens = [u'1111', u'2222']
|
|
|
|
for token in self.gcm_tokens:
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
kind=PushDeviceToken.GCM,
|
|
|
|
token=apn.hex_to_b64(token),
|
|
|
|
user=self.user_profile,
|
|
|
|
ios_app_id=None)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_gcm_data(self, **kwargs: Any) -> Dict[str, Any]:
|
2016-08-08 14:20:41 +02:00
|
|
|
data = {
|
|
|
|
'key 1': 'Data 1',
|
|
|
|
'key 2': 'Data 2',
|
|
|
|
}
|
|
|
|
data.update(kwargs)
|
|
|
|
return data
|
|
|
|
|
|
|
|
class GCMNotSetTest(GCMTest):
|
2017-04-25 22:51:27 +02:00
|
|
|
@mock.patch('logging.warning')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_gcm_is_none(self, mock_warning: mock.MagicMock) -> None:
|
2016-08-08 14:20:41 +02:00
|
|
|
apn.gcm = None
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, {})
|
2017-10-14 02:21:38 +02:00
|
|
|
mock_warning.assert_called_with(
|
|
|
|
"Skipping sending a GCM push notification since PUSH_NOTIFICATION_BOUNCER_URL "
|
|
|
|
"and ANDROID_GCM_API_KEY are both unset")
|
2016-08-08 14:20:41 +02:00
|
|
|
|
2017-05-17 09:58:27 +02:00
|
|
|
class GCMIOErrorTest(GCMTest):
|
|
|
|
@mock.patch('zerver.lib.push_notifications.gcm.json_request')
|
|
|
|
@mock.patch('logging.warning')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_json_request_raises_ioerror(self, mock_warn: mock.MagicMock,
|
|
|
|
mock_json_request: mock.MagicMock) -> None:
|
2017-05-17 09:58:27 +02:00
|
|
|
mock_json_request.side_effect = IOError('error')
|
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, {})
|
|
|
|
mock_warn.assert_called_with('error')
|
|
|
|
|
2016-08-08 14:20:41 +02:00
|
|
|
class GCMSuccessTest(GCMTest):
|
|
|
|
@mock.patch('logging.warning')
|
|
|
|
@mock.patch('logging.info')
|
2016-12-13 08:41:48 +01:00
|
|
|
@mock.patch('gcm.GCM.json_request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_success(self, mock_send: mock.MagicMock, mock_info: mock.MagicMock,
|
|
|
|
mock_warning: mock.MagicMock) -> None:
|
2016-12-13 08:41:48 +01:00
|
|
|
res = {}
|
|
|
|
res['success'] = {token: ind for ind, token in enumerate(self.gcm_tokens)}
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_send.return_value = res
|
|
|
|
|
|
|
|
data = self.get_gcm_data()
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, data)
|
2016-08-08 14:20:41 +02:00
|
|
|
self.assertEqual(mock_info.call_count, 2)
|
|
|
|
c1 = call("GCM: Sent 1111 as 0")
|
|
|
|
c2 = call("GCM: Sent 2222 as 1")
|
2016-08-09 19:02:31 +02:00
|
|
|
mock_info.assert_has_calls([c1, c2], any_order=True)
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_warning.assert_not_called()
|
|
|
|
|
|
|
|
class GCMCanonicalTest(GCMTest):
|
|
|
|
@mock.patch('logging.warning')
|
2016-12-13 08:41:48 +01:00
|
|
|
@mock.patch('gcm.GCM.json_request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_equal(self, mock_send: mock.MagicMock, mock_warning: mock.MagicMock) -> None:
|
2016-12-13 08:41:48 +01:00
|
|
|
res = {}
|
|
|
|
res['canonical'] = {1: 1}
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_send.return_value = res
|
|
|
|
|
|
|
|
data = self.get_gcm_data()
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, data)
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_warning.assert_called_once_with("GCM: Got canonical ref but it "
|
|
|
|
"already matches our ID 1!")
|
|
|
|
|
|
|
|
@mock.patch('logging.warning')
|
2016-12-13 08:41:48 +01:00
|
|
|
@mock.patch('gcm.GCM.json_request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_pushdevice_not_present(self, mock_send: mock.MagicMock,
|
|
|
|
mock_warning: mock.MagicMock) -> None:
|
2016-12-13 08:41:48 +01:00
|
|
|
res = {}
|
2016-08-08 14:20:41 +02:00
|
|
|
t1 = apn.hex_to_b64(u'1111')
|
|
|
|
t2 = apn.hex_to_b64(u'3333')
|
2016-12-13 08:41:48 +01:00
|
|
|
res['canonical'] = {t1: t2}
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_send.return_value = res
|
|
|
|
|
2018-05-11 01:39:38 +02:00
|
|
|
def get_count(hex_token: str) -> int:
|
2016-09-12 08:09:43 +02:00
|
|
|
token = apn.hex_to_b64(hex_token)
|
2016-08-08 14:20:41 +02:00
|
|
|
return PushDeviceToken.objects.filter(
|
|
|
|
token=token, kind=PushDeviceToken.GCM).count()
|
|
|
|
|
|
|
|
self.assertEqual(get_count(u'1111'), 1)
|
|
|
|
self.assertEqual(get_count(u'3333'), 0)
|
|
|
|
|
|
|
|
data = self.get_gcm_data()
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, data)
|
2016-08-08 14:20:41 +02:00
|
|
|
msg = ("GCM: Got canonical ref %s "
|
|
|
|
"replacing %s but new ID not "
|
|
|
|
"registered! Updating.")
|
|
|
|
mock_warning.assert_called_once_with(msg % (t2, t1))
|
|
|
|
|
|
|
|
self.assertEqual(get_count(u'1111'), 0)
|
|
|
|
self.assertEqual(get_count(u'3333'), 1)
|
|
|
|
|
|
|
|
@mock.patch('logging.info')
|
2016-12-13 08:41:48 +01:00
|
|
|
@mock.patch('gcm.GCM.json_request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_pushdevice_different(self, mock_send: mock.MagicMock,
|
|
|
|
mock_info: mock.MagicMock) -> None:
|
2016-12-13 08:41:48 +01:00
|
|
|
res = {}
|
2016-08-08 14:20:41 +02:00
|
|
|
old_token = apn.hex_to_b64(u'1111')
|
|
|
|
new_token = apn.hex_to_b64(u'2222')
|
2016-12-13 08:41:48 +01:00
|
|
|
res['canonical'] = {old_token: new_token}
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_send.return_value = res
|
|
|
|
|
2018-05-11 01:39:38 +02:00
|
|
|
def get_count(hex_token: str) -> int:
|
2016-09-12 08:09:43 +02:00
|
|
|
token = apn.hex_to_b64(hex_token)
|
2016-08-08 14:20:41 +02:00
|
|
|
return PushDeviceToken.objects.filter(
|
|
|
|
token=token, kind=PushDeviceToken.GCM).count()
|
|
|
|
|
|
|
|
self.assertEqual(get_count(u'1111'), 1)
|
|
|
|
self.assertEqual(get_count(u'2222'), 1)
|
|
|
|
|
|
|
|
data = self.get_gcm_data()
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, data)
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_info.assert_called_once_with(
|
|
|
|
"GCM: Got canonical ref %s, dropping %s" % (new_token, old_token))
|
|
|
|
|
|
|
|
self.assertEqual(get_count(u'1111'), 0)
|
|
|
|
self.assertEqual(get_count(u'2222'), 1)
|
|
|
|
|
|
|
|
class GCMNotRegisteredTest(GCMTest):
|
|
|
|
@mock.patch('logging.info')
|
2016-12-13 08:41:48 +01:00
|
|
|
@mock.patch('gcm.GCM.json_request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_not_registered(self, mock_send: mock.MagicMock, mock_info: mock.MagicMock) -> None:
|
2016-12-13 08:41:48 +01:00
|
|
|
res = {}
|
2016-08-08 14:20:41 +02:00
|
|
|
token = apn.hex_to_b64(u'1111')
|
2016-12-13 08:41:48 +01:00
|
|
|
res['errors'] = {'NotRegistered': [token]}
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_send.return_value = res
|
|
|
|
|
2018-05-11 01:39:38 +02:00
|
|
|
def get_count(hex_token: str) -> int:
|
2016-09-12 08:09:43 +02:00
|
|
|
token = apn.hex_to_b64(hex_token)
|
2016-08-08 14:20:41 +02:00
|
|
|
return PushDeviceToken.objects.filter(
|
|
|
|
token=token, kind=PushDeviceToken.GCM).count()
|
|
|
|
|
|
|
|
self.assertEqual(get_count(u'1111'), 1)
|
|
|
|
|
|
|
|
data = self.get_gcm_data()
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, data)
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_info.assert_called_once_with("GCM: Removing %s" % (token,))
|
|
|
|
self.assertEqual(get_count(u'1111'), 0)
|
|
|
|
|
|
|
|
class GCMFailureTest(GCMTest):
|
|
|
|
@mock.patch('logging.warning')
|
2016-12-13 08:41:48 +01:00
|
|
|
@mock.patch('gcm.GCM.json_request')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_failure(self, mock_send: mock.MagicMock, mock_warn: mock.MagicMock) -> None:
|
2016-12-13 08:41:48 +01:00
|
|
|
res = {}
|
2016-08-08 14:20:41 +02:00
|
|
|
token = apn.hex_to_b64(u'1111')
|
2016-12-13 08:41:48 +01:00
|
|
|
res['errors'] = {'Failed': [token]}
|
2016-08-08 14:20:41 +02:00
|
|
|
mock_send.return_value = res
|
|
|
|
|
|
|
|
data = self.get_gcm_data()
|
2017-03-06 03:11:44 +01:00
|
|
|
apn.send_android_push_notification_to_user(self.user_profile, data)
|
2016-12-13 08:41:48 +01:00
|
|
|
c1 = call("GCM: Delivery to %s failed: Failed" % (token,))
|
|
|
|
mock_warn.assert_has_calls([c1], any_order=True)
|
2016-12-14 13:23:05 +01:00
|
|
|
|
2016-12-14 13:29:42 +01:00
|
|
|
class TestReceivesNotificationsFunctions(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-05-07 21:25:59 +02:00
|
|
|
self.user = self.example_user('cordelia')
|
2016-12-14 13:23:05 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receivers_online_notifications_when_user_is_a_bot(self) -> None:
|
2016-12-14 13:23:05 +01:00
|
|
|
self.user.is_bot = True
|
|
|
|
|
|
|
|
self.user.enable_online_push_notifications = True
|
|
|
|
self.assertFalse(receives_online_notifications(self.user))
|
|
|
|
|
|
|
|
self.user.enable_online_push_notifications = False
|
|
|
|
self.assertFalse(receives_online_notifications(self.user))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receivers_online_notifications_when_user_is_not_a_bot(self) -> None:
|
2016-12-14 13:23:05 +01:00
|
|
|
self.user.is_bot = False
|
|
|
|
|
|
|
|
self.user.enable_online_push_notifications = True
|
|
|
|
self.assertTrue(receives_online_notifications(self.user))
|
|
|
|
|
|
|
|
self.user.enable_online_push_notifications = False
|
|
|
|
self.assertFalse(receives_online_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receivers_offline_notifications_when_user_is_a_bot(self) -> None:
|
2016-12-14 13:29:42 +01:00
|
|
|
self.user.is_bot = True
|
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = True
|
|
|
|
self.user.enable_offline_push_notifications = True
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertFalse(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertFalse(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = False
|
|
|
|
self.user.enable_offline_push_notifications = False
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertFalse(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertFalse(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = True
|
|
|
|
self.user.enable_offline_push_notifications = False
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertFalse(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertFalse(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = False
|
|
|
|
self.user.enable_offline_push_notifications = True
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertFalse(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertFalse(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receivers_offline_notifications_when_user_is_not_a_bot(self) -> None:
|
2016-12-14 13:29:42 +01:00
|
|
|
self.user.is_bot = False
|
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = True
|
|
|
|
self.user.enable_offline_push_notifications = True
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertTrue(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertTrue(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = False
|
|
|
|
self.user.enable_offline_push_notifications = False
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertFalse(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertFalse(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = True
|
|
|
|
self.user.enable_offline_push_notifications = False
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertFalse(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertTrue(receives_offline_email_notifications(self.user))
|
2016-12-14 13:29:42 +01:00
|
|
|
|
|
|
|
self.user.enable_offline_email_notifications = False
|
|
|
|
self.user.enable_offline_push_notifications = True
|
2017-11-29 02:49:11 +01:00
|
|
|
self.assertTrue(receives_offline_push_notifications(self.user))
|
|
|
|
self.assertFalse(receives_offline_email_notifications(self.user))
|
2017-08-17 16:55:32 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receivers_stream_notifications_when_user_is_a_bot(self) -> None:
|
2017-08-17 16:55:32 +02:00
|
|
|
self.user.is_bot = True
|
|
|
|
|
|
|
|
self.user.enable_stream_push_notifications = True
|
|
|
|
self.assertFalse(receives_stream_notifications(self.user))
|
|
|
|
|
|
|
|
self.user.enable_stream_push_notifications = False
|
|
|
|
self.assertFalse(receives_stream_notifications(self.user))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receivers_stream_notifications_when_user_is_not_a_bot(self) -> None:
|
2017-08-17 16:55:32 +02:00
|
|
|
self.user.is_bot = False
|
|
|
|
|
|
|
|
self.user.enable_stream_push_notifications = True
|
|
|
|
self.assertTrue(receives_stream_notifications(self.user))
|
|
|
|
|
|
|
|
self.user.enable_stream_push_notifications = False
|
|
|
|
self.assertFalse(receives_stream_notifications(self.user))
|
2017-10-06 23:16:29 +02:00
|
|
|
|
|
|
|
class TestPushNotificationsContent(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_fixtures(self) -> None:
|
2018-04-20 03:57:21 +02:00
|
|
|
fixtures = ujson.loads(self.fixture_data("markdown_test_cases.json"))
|
2017-10-06 23:16:29 +02:00
|
|
|
tests = fixtures["regular_tests"]
|
|
|
|
for test in tests:
|
|
|
|
if "text_content" in test:
|
|
|
|
output = get_mobile_push_content(test["expected_output"])
|
|
|
|
self.assertEqual(output, test["text_content"])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_backend_only_fixtures(self) -> None:
|
2017-10-06 23:16:29 +02:00
|
|
|
fixtures = [
|
|
|
|
{
|
|
|
|
'name': 'realm_emoji',
|
|
|
|
'rendered_content': '<p>Testing <img alt=":green_tick:" class="emoji" src="/user_avatars/1/emoji/green_tick.png" title="green tick"> realm emoji.</p>',
|
|
|
|
'expected_output': 'Testing :green_tick: realm emoji.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'mentions',
|
2018-01-21 06:33:42 +01:00
|
|
|
'rendered_content': '<p>Mentioning <span class="user-mention" data-user-id="3">@Cordelia Lear</span>.</p>',
|
2017-10-06 23:16:29 +02:00
|
|
|
'expected_output': 'Mentioning @Cordelia Lear.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'stream_names',
|
|
|
|
'rendered_content': '<p>Testing stream names <a class="stream" data-stream-id="5" href="/#narrow/stream/Verona">#Verona</a>.</p>',
|
|
|
|
'expected_output': 'Testing stream names #Verona.',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
for test in fixtures:
|
|
|
|
actual_output = get_mobile_push_content(test["rendered_content"])
|
|
|
|
self.assertEqual(actual_output, test["expected_output"])
|
2018-05-04 01:40:46 +02:00
|
|
|
|
|
|
|
class PushBouncerSignupTest(ZulipTestCase):
|
|
|
|
def test_push_signup_invalid_host(self) -> None:
|
|
|
|
zulip_org_id = str(uuid.uuid4())
|
|
|
|
zulip_org_key = get_random_string(64)
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=zulip_org_key,
|
|
|
|
hostname="invalid-host",
|
|
|
|
contact_email="server-admin@example.com",
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
|
|
|
self.assert_json_error(result, "invalid-host is not a valid hostname")
|
|
|
|
|
|
|
|
def test_push_signup_invalid_email(self) -> None:
|
|
|
|
zulip_org_id = str(uuid.uuid4())
|
|
|
|
zulip_org_key = get_random_string(64)
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=zulip_org_key,
|
|
|
|
hostname="example.com",
|
|
|
|
contact_email="server-admin",
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
|
|
|
self.assert_json_error(result, "Enter a valid email address.")
|
|
|
|
|
|
|
|
def test_push_signup_success(self) -> None:
|
|
|
|
zulip_org_id = str(uuid.uuid4())
|
|
|
|
zulip_org_key = get_random_string(64)
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=zulip_org_key,
|
|
|
|
hostname="example.com",
|
|
|
|
contact_email="server-admin@example.com",
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
server = RemoteZulipServer.objects.get(uuid=zulip_org_id)
|
|
|
|
self.assertEqual(server.hostname, "example.com")
|
|
|
|
self.assertEqual(server.contact_email, "server-admin@example.com")
|
|
|
|
|
|
|
|
# Update our hostname
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=zulip_org_key,
|
|
|
|
hostname="zulip.example.com",
|
|
|
|
contact_email="server-admin@example.com",
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
server = RemoteZulipServer.objects.get(uuid=zulip_org_id)
|
|
|
|
self.assertEqual(server.hostname, "zulip.example.com")
|
|
|
|
self.assertEqual(server.contact_email, "server-admin@example.com")
|
|
|
|
|
|
|
|
# Now test rotating our key
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=zulip_org_key,
|
|
|
|
hostname="example.com",
|
|
|
|
contact_email="server-admin@example.com",
|
|
|
|
new_org_key=get_random_string(64),
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
server = RemoteZulipServer.objects.get(uuid=zulip_org_id)
|
|
|
|
self.assertEqual(server.hostname, "example.com")
|
|
|
|
self.assertEqual(server.contact_email, "server-admin@example.com")
|
|
|
|
zulip_org_key = request["new_org_key"]
|
|
|
|
self.assertEqual(server.api_key, zulip_org_key)
|
|
|
|
|
|
|
|
# Update our hostname
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=zulip_org_key,
|
|
|
|
hostname="zulip.example.com",
|
|
|
|
contact_email="new-server-admin@example.com",
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
server = RemoteZulipServer.objects.get(uuid=zulip_org_id)
|
|
|
|
self.assertEqual(server.hostname, "zulip.example.com")
|
|
|
|
self.assertEqual(server.contact_email, "new-server-admin@example.com")
|
|
|
|
|
|
|
|
# Now test trying to double-create with a new random key fails
|
|
|
|
request = dict(
|
|
|
|
zulip_org_id=zulip_org_id,
|
|
|
|
zulip_org_key=get_random_string(64),
|
|
|
|
hostname="example.com",
|
|
|
|
contact_email="server-admin@example.com",
|
|
|
|
)
|
|
|
|
result = self.client_post("/api/v1/remotes/server/register", request)
|
2018-05-04 18:04:01 +02:00
|
|
|
self.assert_json_error(result, "Zulip server auth failure: key does not match role %s" %
|
|
|
|
(zulip_org_id,))
|