2016-10-12 20:57:59 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import ujson
|
2017-08-04 02:25:38 +02:00
|
|
|
from typing import Any, Mapping, List
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2018-08-21 02:41:48 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
|
|
from zerver.lib.actions import recipient_for_user_ids
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_helpers import tornado_redirected_to_list, get_display_recipient
|
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2018-08-21 02:41:48 +02:00
|
|
|
from zerver.models import get_realm, get_user, get_display_recipient
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
class TypingNotificationOperatorTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_missing_parameter(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification without op parameter fails
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
sender = self.example_email("hamlet")
|
2017-05-25 02:08:35 +02:00
|
|
|
recipient = self.example_email("othello")
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender, '/api/v1/typing', {'to': recipient})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_error(result, 'Missing \'op\' argument')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_invalid_parameter(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification with invalid value for op parameter fails
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
sender = self.example_email("hamlet")
|
2017-05-25 02:08:35 +02:00
|
|
|
recipient = self.example_email("othello")
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender, '/api/v1/typing', {'to': recipient, 'op': 'foo'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_error(result, 'Invalid \'op\' value (should be start or stop)')
|
|
|
|
|
|
|
|
class TypingNotificationRecipientsTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_missing_recipient(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification without recipient fails
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
sender = self.example_email("hamlet")
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender, '/api/v1/typing', {'op': 'start'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_error(result, 'Missing parameter: \'to\' (recipient)')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_invalid_recipient(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification to invalid recipient fails
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
sender = self.example_email("hamlet")
|
2016-10-12 20:57:59 +02:00
|
|
|
invalid = 'invalid email'
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender, '/api/v1/typing', {'op': 'start', 'to': invalid})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_error(result, 'Invalid email \'' + invalid + '\'')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_single_recipient(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification to a single recipient is successful
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
sender = self.example_user('hamlet')
|
|
|
|
recipient = self.example_user('othello')
|
|
|
|
expected_recipients = set([sender, recipient])
|
|
|
|
expected_recipient_emails = set([user.email for user in expected_recipients])
|
|
|
|
expected_recipient_ids = set([user.id for user in expected_recipients])
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2016-10-12 20:57:59 +02:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender.email, '/api/v1/typing', {'to': recipient.email,
|
|
|
|
'op': 'start'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_success(result)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(len(events), 1)
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
event = events[0]['event']
|
|
|
|
event_recipient_emails = set(user['email'] for user in event['recipients'])
|
2016-10-28 18:50:21 +02:00
|
|
|
event_user_ids = set(events[0]['users'])
|
|
|
|
event_recipient_user_ids = set(user['user_id'] for user in event['recipients'])
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(expected_recipient_ids, event_recipient_user_ids)
|
|
|
|
self.assertEqual(expected_recipient_ids, event_user_ids)
|
2017-05-24 02:42:31 +02:00
|
|
|
self.assertEqual(event['sender']['email'], sender.email)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(event_recipient_emails, expected_recipient_emails)
|
|
|
|
self.assertEqual(event['type'], 'typing')
|
|
|
|
self.assertEqual(event['op'], 'start')
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_multiple_recipients(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification to a single recipient is successful
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
sender = self.example_user('hamlet')
|
|
|
|
recipient = [self.example_user('othello'), self.example_user('cordelia')]
|
|
|
|
expected_recipients = set(recipient) | set([sender])
|
|
|
|
expected_recipient_emails = set([user.email for user in expected_recipients])
|
|
|
|
expected_recipient_ids = set([user.id for user in expected_recipients])
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2016-10-12 20:57:59 +02:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender.email, '/api/v1/typing',
|
|
|
|
{'to': ujson.dumps([user.email for user in recipient]),
|
|
|
|
'op': 'start'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_success(result)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(len(events), 1)
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
event = events[0]['event']
|
|
|
|
event_recipient_emails = set(user['email'] for user in event['recipients'])
|
2016-10-28 18:50:21 +02:00
|
|
|
event_user_ids = set(events[0]['users'])
|
|
|
|
event_recipient_user_ids = set(user['user_id'] for user in event['recipients'])
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(expected_recipient_ids, event_recipient_user_ids)
|
|
|
|
self.assertEqual(expected_recipient_ids, event_user_ids)
|
2017-05-24 02:42:31 +02:00
|
|
|
self.assertEqual(event['sender']['email'], sender.email)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(event_recipient_emails, expected_recipient_emails)
|
|
|
|
self.assertEqual(event['type'], 'typing')
|
|
|
|
self.assertEqual(event['op'], 'start')
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
class TypingStartedNotificationTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_notification_to_self_event(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification to yourself
|
|
|
|
is successful.
|
|
|
|
"""
|
2017-05-08 16:23:43 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
2016-10-28 18:50:21 +02:00
|
|
|
expected_recipient_emails = set([email])
|
2017-05-08 16:23:43 +02:00
|
|
|
expected_recipient_ids = set([user.id])
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2016-10-12 20:57:59 +02:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(email, '/api/v1/typing', {'to': email,
|
|
|
|
'op': 'start'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_success(result)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(len(events), 1)
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
event = events[0]['event']
|
|
|
|
event_recipient_emails = set(user['email'] for user in event['recipients'])
|
2016-10-28 18:50:21 +02:00
|
|
|
event_user_ids = set(events[0]['users'])
|
|
|
|
event_recipient_user_ids = set(user['user_id'] for user in event['recipients'])
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(expected_recipient_ids, event_recipient_user_ids)
|
|
|
|
self.assertEqual(expected_recipient_ids, event_user_ids)
|
|
|
|
self.assertEqual(event_recipient_emails, expected_recipient_emails)
|
|
|
|
self.assertEqual(event['sender']['email'], email)
|
|
|
|
self.assertEqual(event['type'], 'typing')
|
|
|
|
self.assertEqual(event['op'], 'start')
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_notification_to_another_user_event(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending typing notification to another user
|
|
|
|
is successful.
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
sender = self.example_user('hamlet')
|
|
|
|
recipient = self.example_user('othello')
|
|
|
|
expected_recipients = set([sender, recipient])
|
|
|
|
expected_recipient_emails = set([user.email for user in expected_recipients])
|
|
|
|
expected_recipient_ids = set([user.id for user in expected_recipients])
|
2016-10-28 18:50:21 +02:00
|
|
|
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2016-10-12 20:57:59 +02:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender.email, '/api/v1/typing', {'to': recipient.email,
|
|
|
|
'op': 'start'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_success(result)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(len(events), 1)
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
event = events[0]['event']
|
|
|
|
event_recipient_emails = set(user['email'] for user in event['recipients'])
|
2016-10-28 18:50:21 +02:00
|
|
|
event_user_ids = set(events[0]['users'])
|
|
|
|
event_recipient_user_ids = set(user['user_id'] for user in event['recipients'])
|
|
|
|
|
|
|
|
self.assertEqual(expected_recipient_ids, event_recipient_user_ids)
|
|
|
|
self.assertEqual(expected_recipient_ids, event_user_ids)
|
|
|
|
self.assertEqual(event_recipient_emails, expected_recipient_emails)
|
2017-05-24 02:42:31 +02:00
|
|
|
self.assertEqual(event['sender']['email'], sender.email)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(event['type'], 'typing')
|
|
|
|
self.assertEqual(event['op'], 'start')
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
class StoppedTypingNotificationTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_notification_to_self_event(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending stopped typing notification to yourself
|
|
|
|
is successful.
|
|
|
|
"""
|
2017-05-08 16:23:43 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
2016-10-28 18:50:21 +02:00
|
|
|
expected_recipient_emails = set([email])
|
2017-05-08 16:23:43 +02:00
|
|
|
expected_recipient_ids = set([user.id])
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2016-10-12 20:57:59 +02:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(email, '/api/v1/typing', {'to': email,
|
|
|
|
'op': 'stop'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_success(result)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(len(events), 1)
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
event = events[0]['event']
|
|
|
|
event_recipient_emails = set(user['email'] for user in event['recipients'])
|
2016-10-28 18:50:21 +02:00
|
|
|
event_user_ids = set(events[0]['users'])
|
|
|
|
event_recipient_user_ids = set(user['user_id'] for user in event['recipients'])
|
|
|
|
|
|
|
|
self.assertEqual(expected_recipient_ids, event_recipient_user_ids)
|
|
|
|
self.assertEqual(expected_recipient_ids, event_user_ids)
|
|
|
|
self.assertEqual(event_recipient_emails, expected_recipient_emails)
|
|
|
|
self.assertEqual(event['sender']['email'], email)
|
|
|
|
self.assertEqual(event['type'], 'typing')
|
|
|
|
self.assertEqual(event['op'], 'stop')
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_notification_to_another_user_event(self) -> None:
|
2016-10-12 20:57:59 +02:00
|
|
|
"""
|
|
|
|
Sending stopped typing notification to another user
|
|
|
|
is successful.
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
sender = self.example_user('hamlet')
|
|
|
|
recipient = self.example_user('othello')
|
|
|
|
expected_recipients = set([sender, recipient])
|
|
|
|
expected_recipient_emails = set([user.email for user in expected_recipients])
|
|
|
|
expected_recipient_ids = set([user.id for user in expected_recipients])
|
2016-10-28 18:50:21 +02:00
|
|
|
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2016-10-12 20:57:59 +02:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(sender.email, '/api/v1/typing', {'to': recipient.email,
|
|
|
|
'op': 'stop'})
|
2016-10-12 20:57:59 +02:00
|
|
|
self.assert_json_success(result)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(len(events), 1)
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
event = events[0]['event']
|
|
|
|
event_recipient_emails = set(user['email'] for user in event['recipients'])
|
2016-10-28 18:50:21 +02:00
|
|
|
event_user_ids = set(events[0]['users'])
|
|
|
|
event_recipient_user_ids = set(user['user_id'] for user in event['recipients'])
|
|
|
|
|
|
|
|
self.assertEqual(expected_recipient_ids, event_recipient_user_ids)
|
|
|
|
self.assertEqual(expected_recipient_ids, event_user_ids)
|
|
|
|
self.assertEqual(event_recipient_emails, expected_recipient_emails)
|
2017-05-24 02:42:31 +02:00
|
|
|
self.assertEqual(event['sender']['email'], sender.email)
|
2016-10-28 18:50:21 +02:00
|
|
|
self.assertEqual(event['type'], 'typing')
|
|
|
|
self.assertEqual(event['op'], 'stop')
|
2018-08-21 02:41:48 +02:00
|
|
|
|
|
|
|
class TypingValidationHelpersTest(ZulipTestCase):
|
|
|
|
def test_recipient_for_user_ids(self) -> None:
|
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
othello = self.example_user('othello')
|
|
|
|
cross_realm_bot = self.example_user('welcome_bot')
|
|
|
|
sender = self.example_user('iago')
|
|
|
|
recipient_user_ids = [hamlet.id, othello.id, cross_realm_bot.id]
|
|
|
|
|
|
|
|
result = recipient_for_user_ids(recipient_user_ids, sender)
|
|
|
|
recipient = get_display_recipient(result)
|
|
|
|
recipient_ids = [recipient[0]['id'], recipient[1]['id'], # type: ignore
|
|
|
|
recipient[2]['id'], recipient[3]['id']] # type: ignore
|
|
|
|
|
|
|
|
expected_recipient_ids = [hamlet.id, othello.id,
|
|
|
|
sender.id, cross_realm_bot.id]
|
|
|
|
self.assertEqual(set(recipient_ids), set(expected_recipient_ids))
|
|
|
|
|
|
|
|
def test_recipient_for_user_ids_non_existent_id(self) -> None:
|
|
|
|
sender = self.example_user('iago')
|
|
|
|
recipient_user_ids = [999]
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, 'Invalid user ID '):
|
|
|
|
recipient_for_user_ids(recipient_user_ids, sender)
|