2016-11-16 12:37:26 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""WebSocketBaseTestCase is based on combination of Tornado
|
|
|
|
and Django test systems. It require to use decorator '@gen.coroutine'
|
|
|
|
for each test case method( see documentation: http://www.tornadoweb.org/en/stable/testing.html).
|
|
|
|
It requires implementation of 'get_app' method to initialize tornado application and launch it.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import ujson
|
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-05-25 11:50:13 +02:00
|
|
|
from django.db import close_old_connections
|
|
|
|
from django.core import signals
|
2017-10-28 01:17:38 +02:00
|
|
|
from django.test import override_settings
|
2016-11-16 12:37:26 +01:00
|
|
|
from tornado.gen import Return
|
2017-05-25 11:50:13 +02:00
|
|
|
from tornado.httpclient import HTTPRequest, HTTPResponse
|
2016-11-16 12:37:26 +01:00
|
|
|
|
|
|
|
from zerver.lib.test_helpers import POSTRequestMock
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
|
2017-05-25 11:50:13 +02:00
|
|
|
from zerver.models import UserProfile, get_client
|
2016-11-16 12:37:26 +01:00
|
|
|
|
|
|
|
from tornado import gen
|
|
|
|
from tornado.testing import AsyncHTTPTestCase, gen_test
|
|
|
|
from tornado.web import Application
|
|
|
|
from tornado.websocket import websocket_connect
|
|
|
|
|
|
|
|
from zerver.tornado.application import create_tornado_application
|
2017-05-25 11:50:13 +02:00
|
|
|
from zerver.tornado import event_queue
|
|
|
|
from zerver.tornado.event_queue import fetch_events, \
|
|
|
|
allocate_client_descriptor, process_event
|
2016-11-27 06:50:54 +01:00
|
|
|
from zerver.tornado.views import get_events_backend
|
2016-11-16 12:37:26 +01:00
|
|
|
|
2017-11-06 03:07:49 +01:00
|
|
|
from http.cookies import SimpleCookie
|
2017-11-06 03:14:57 +01:00
|
|
|
import urllib.parse
|
2016-11-16 12:37:26 +01:00
|
|
|
|
2017-11-10 21:35:47 +01:00
|
|
|
from unittest.mock import patch
|
2017-05-25 11:50:13 +02:00
|
|
|
from typing import Any, Callable, Dict, Generator, Optional, Text, List, cast
|
2016-11-16 12:37:26 +01:00
|
|
|
|
2017-05-25 11:50:13 +02:00
|
|
|
class TornadoWebTestCase(AsyncHTTPTestCase, ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().setUp()
|
2017-05-25 11:50:13 +02:00
|
|
|
signals.request_started.disconnect(close_old_connections)
|
|
|
|
signals.request_finished.disconnect(close_old_connections)
|
|
|
|
self.session_cookie = None # type: Optional[Dict[Text, Text]]
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def tearDown(self) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().tearDown()
|
2017-05-25 11:50:13 +02:00
|
|
|
self.session_cookie = None # type: Optional[Dict[Text, Text]]
|
|
|
|
|
2017-10-28 01:17:38 +02:00
|
|
|
@override_settings(DEBUG=False)
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_app(self) -> Application:
|
2017-05-25 11:50:13 +02:00
|
|
|
return create_tornado_application()
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def client_get(self, path: Text, **kwargs: Any) -> HTTPResponse:
|
2017-05-25 11:50:13 +02:00
|
|
|
self.add_session_cookie(kwargs)
|
2017-08-26 00:40:17 +02:00
|
|
|
self.set_http_host(kwargs)
|
|
|
|
if 'HTTP_HOST' in kwargs:
|
|
|
|
kwargs['headers']['Host'] = kwargs['HTTP_HOST']
|
|
|
|
del kwargs['HTTP_HOST']
|
2017-05-25 11:50:13 +02:00
|
|
|
return self.fetch(path, method='GET', **kwargs)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def fetch_async(self, method: Text, path: Text, **kwargs: Any) -> None:
|
2017-05-25 11:50:13 +02:00
|
|
|
self.add_session_cookie(kwargs)
|
2017-08-26 00:40:17 +02:00
|
|
|
self.set_http_host(kwargs)
|
|
|
|
if 'HTTP_HOST' in kwargs:
|
|
|
|
kwargs['headers']['Host'] = kwargs['HTTP_HOST']
|
|
|
|
del kwargs['HTTP_HOST']
|
2017-05-25 11:50:13 +02:00
|
|
|
self.http_client.fetch(
|
|
|
|
self.get_url(path),
|
|
|
|
self.stop,
|
|
|
|
method=method,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def client_get_async(self, path: Text, **kwargs: Any) -> None:
|
2017-08-26 00:40:17 +02:00
|
|
|
self.set_http_host(kwargs)
|
2017-05-25 11:50:13 +02:00
|
|
|
self.fetch_async('GET', path, **kwargs)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def login(self, *args: Any, **kwargs: Any) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().login(*args, **kwargs)
|
2017-05-25 11:50:13 +02:00
|
|
|
session_cookie = settings.SESSION_COOKIE_NAME
|
|
|
|
session_key = self.client.session.session_key
|
|
|
|
self.session_cookie = {
|
|
|
|
"Cookie": "{}={}".format(session_cookie, session_key)
|
|
|
|
}
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_session_cookie(self) -> Dict[Text, Text]:
|
2017-05-25 11:50:13 +02:00
|
|
|
return {} if self.session_cookie is None else self.session_cookie
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def add_session_cookie(self, kwargs: Dict[str, Any]) -> None:
|
2017-05-25 11:50:13 +02:00
|
|
|
# TODO: Currently only allows session cookie
|
|
|
|
headers = kwargs.get('headers', {})
|
|
|
|
headers.update(self.get_session_cookie())
|
|
|
|
kwargs['headers'] = headers
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def create_queue(self, **kwargs: Any) -> str:
|
2017-08-26 00:40:17 +02:00
|
|
|
response = self.client_get('/json/events?dont_block=true', subdomain="zulip")
|
2017-05-25 11:50:13 +02:00
|
|
|
self.assertEqual(response.code, 200)
|
|
|
|
body = ujson.loads(response.body)
|
|
|
|
self.assertEqual(body['events'], [])
|
|
|
|
self.assertIn('queue_id', body)
|
|
|
|
return body['queue_id']
|
|
|
|
|
|
|
|
class EventsTestCase(TornadoWebTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_create_queue(self) -> None:
|
2017-05-25 11:50:13 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
queue_id = self.create_queue()
|
|
|
|
self.assertIn(queue_id, event_queue.clients)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_events_async(self) -> None:
|
2017-05-25 11:50:13 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
self.login(user_profile.email)
|
|
|
|
event_queue_id = self.create_queue()
|
|
|
|
data = {
|
|
|
|
'queue_id': event_queue_id,
|
|
|
|
'last_event_id': 0,
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:14:57 +01:00
|
|
|
path = '/json/events?{}'.format(urllib.parse.urlencode(data))
|
2017-05-25 11:50:13 +02:00
|
|
|
self.client_get_async(path)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def process_events() -> None:
|
2017-05-25 11:50:13 +02:00
|
|
|
users = [user_profile.id]
|
|
|
|
event = dict(
|
|
|
|
type='test',
|
|
|
|
data='test data',
|
|
|
|
)
|
|
|
|
process_event(event, users)
|
|
|
|
|
|
|
|
self.io_loop.call_later(0.1, process_events)
|
|
|
|
response = self.wait()
|
|
|
|
data = ujson.loads(response.body)
|
|
|
|
events = data['events']
|
|
|
|
events = cast(List[Dict[str, Any]], events)
|
|
|
|
self.assertEqual(len(events), 1)
|
|
|
|
self.assertEqual(events[0]['data'], 'test data')
|
|
|
|
self.assertEqual(data['result'], 'success')
|
2016-11-16 12:37:26 +01:00
|
|
|
|
|
|
|
class WebSocketBaseTestCase(AsyncHTTPTestCase, ZulipTestCase):
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2016-11-16 12:37:26 +01:00
|
|
|
settings.RUNNING_INSIDE_TORNADO = True
|
2017-10-27 08:28:23 +02:00
|
|
|
super().setUp()
|
2016-11-16 12:37:26 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def tearDown(self) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().tearDown()
|
2016-11-16 12:37:26 +01:00
|
|
|
settings.RUNNING_INSIDE_TORNADO = False
|
|
|
|
|
|
|
|
@gen.coroutine
|
2017-11-05 10:51:25 +01:00
|
|
|
def ws_connect(self, path: str, cookie_header: str,
|
|
|
|
compression_options: Optional[Any]=None
|
|
|
|
) -> Generator[Any, Callable[[HTTPRequest, Optional[Any]], Any], None]:
|
2016-11-16 12:37:26 +01:00
|
|
|
request = HTTPRequest(url='ws://127.0.0.1:%d%s' % (self.get_http_port(), path))
|
|
|
|
request.headers.add('Cookie', cookie_header)
|
|
|
|
ws = yield websocket_connect(
|
|
|
|
request,
|
|
|
|
compression_options=compression_options)
|
|
|
|
raise gen.Return(ws)
|
|
|
|
|
|
|
|
@gen.coroutine
|
2017-11-05 10:51:25 +01:00
|
|
|
def close(self, ws: Any) -> None:
|
2016-11-16 12:37:26 +01:00
|
|
|
"""Close a websocket connection and wait for the server side.
|
|
|
|
"""
|
|
|
|
ws.close()
|
|
|
|
|
|
|
|
class TornadoTestCase(WebSocketBaseTestCase):
|
2017-10-28 01:17:38 +02:00
|
|
|
@override_settings(DEBUG=False)
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_app(self) -> Application:
|
2016-11-16 12:37:26 +01:00
|
|
|
""" Return tornado app to launch for test cases
|
|
|
|
"""
|
|
|
|
return create_tornado_application()
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 10:51:25 +01:00
|
|
|
def tornado_call(view_func: Callable[[HttpRequest, UserProfile], HttpResponse],
|
|
|
|
user_profile: UserProfile,
|
|
|
|
post_data: Dict[str, Any]) -> HttpResponse:
|
2016-11-16 12:37:26 +01:00
|
|
|
request = POSTRequestMock(post_data, user_profile)
|
|
|
|
return view_func(request, user_profile)
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_cookie_header(cookies: SimpleCookie) -> str:
|
2016-11-16 12:37:26 +01:00
|
|
|
return ';'.join(
|
|
|
|
["{}={}".format(name, value.value) for name, value in cookies.items()])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def _get_cookies(self, user_profile: UserProfile) -> SimpleCookie:
|
2016-11-16 12:37:26 +01:00
|
|
|
resp = self.login_with_return(user_profile.email)
|
|
|
|
return resp.cookies
|
|
|
|
|
|
|
|
@gen.coroutine
|
2017-11-05 10:51:25 +01:00
|
|
|
def _websocket_auth(self, ws: Any,
|
|
|
|
queue_events_data: Dict[str, Dict[str, str]],
|
|
|
|
cookies: SimpleCookie) -> Generator[str, str, None]:
|
2016-11-16 12:37:26 +01:00
|
|
|
auth_queue_id = ':'.join((queue_events_data['response']['queue_id'], '0'))
|
|
|
|
message = {
|
|
|
|
"req_id": auth_queue_id,
|
|
|
|
"type": "auth",
|
|
|
|
"request": {
|
|
|
|
"csrf_token": cookies.get('csrftoken').coded_value,
|
|
|
|
"queue_id": queue_events_data['response']['queue_id'],
|
|
|
|
"status_inquiries": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auth_frame_str = ujson.dumps(message)
|
|
|
|
ws.write_message(ujson.dumps([auth_frame_str]))
|
|
|
|
response_ack = yield ws.read_message()
|
|
|
|
response_message = yield ws.read_message()
|
|
|
|
raise gen.Return([response_ack, response_message])
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 10:51:25 +01:00
|
|
|
def _get_queue_events_data(email: Text) -> Dict[str, Dict[str, str]]:
|
2016-11-16 12:37:26 +01:00
|
|
|
user_profile = UserProfile.objects.filter(email=email).first()
|
|
|
|
events_query = {
|
|
|
|
'queue_id': None,
|
|
|
|
'narrow': [],
|
|
|
|
'handler_id': 0,
|
|
|
|
'user_profile_email': user_profile.email,
|
|
|
|
'all_public_streams': False,
|
|
|
|
'client_type_name': 'website',
|
|
|
|
'new_queue_data': {
|
|
|
|
'apply_markdown': True,
|
2017-10-31 18:36:18 +01:00
|
|
|
'client_gravatar': False,
|
2016-11-16 12:37:26 +01:00
|
|
|
'narrow': [],
|
|
|
|
'user_profile_email': user_profile.email,
|
|
|
|
'all_public_streams': False,
|
2017-01-03 21:04:55 +01:00
|
|
|
'realm_id': user_profile.realm_id,
|
2016-11-16 12:37:26 +01:00
|
|
|
'client_type_name': 'website',
|
|
|
|
'event_types': None,
|
|
|
|
'user_profile_id': user_profile.id,
|
|
|
|
'queue_timeout': 0,
|
|
|
|
'last_connection_time': time.time()},
|
|
|
|
'last_event_id': -1,
|
|
|
|
'event_types': None,
|
|
|
|
'user_profile_id': user_profile.id,
|
|
|
|
'dont_block': True,
|
|
|
|
'lifespan_secs': 0
|
|
|
|
}
|
|
|
|
result = fetch_events(events_query)
|
|
|
|
return result
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def _check_message_sending(self, request_id: str,
|
|
|
|
ack_resp: str,
|
|
|
|
msg_resp: str,
|
|
|
|
profile: UserProfile,
|
|
|
|
queue_events_data: Dict[str, Dict[str, str]]) -> None:
|
2016-11-16 12:37:26 +01:00
|
|
|
self.assertEqual(ack_resp[0], 'a')
|
|
|
|
self.assertEqual(
|
|
|
|
ujson.loads(ack_resp[1:]),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "ack",
|
|
|
|
"req_id": request_id
|
|
|
|
}
|
|
|
|
])
|
|
|
|
self.assertEqual(msg_resp[0], 'a')
|
|
|
|
result = self.tornado_call(get_events_backend, profile,
|
|
|
|
{"queue_id": queue_events_data['response']['queue_id'],
|
|
|
|
"user_client": "website",
|
|
|
|
"last_event_id": -1,
|
|
|
|
"dont_block": ujson.dumps(True),
|
|
|
|
})
|
|
|
|
result_content = ujson.loads(result.content)
|
|
|
|
self.assertEqual(len(result_content['events']), 1)
|
|
|
|
message_id = result_content['events'][0]['message']['id']
|
|
|
|
self.assertEqual(
|
|
|
|
ujson.loads(msg_resp[1:]),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "response",
|
|
|
|
"response":
|
|
|
|
{
|
|
|
|
"result": "success",
|
|
|
|
"id": message_id,
|
|
|
|
"msg": ""
|
|
|
|
},
|
|
|
|
"req_id": request_id
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
@gen_test
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_tornado_connect(self) -> Generator[str, Any, None]:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2016-11-16 12:37:26 +01:00
|
|
|
cookies = self._get_cookies(user_profile)
|
|
|
|
cookie_header = self.get_cookie_header(cookies)
|
|
|
|
ws = yield self.ws_connect('/sockjs/366/v8nw22qe/websocket', cookie_header=cookie_header)
|
|
|
|
response = yield ws.read_message()
|
|
|
|
self.assertEqual(response, 'o')
|
2017-10-28 01:28:33 +02:00
|
|
|
yield self.close(ws)
|
2016-11-16 12:37:26 +01:00
|
|
|
|
|
|
|
@gen_test
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_tornado_auth(self) -> Generator[str, 'TornadoTestCase', None]:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2016-11-16 12:37:26 +01:00
|
|
|
cookies = self._get_cookies(user_profile)
|
|
|
|
cookie_header = self.get_cookie_header(cookies)
|
|
|
|
ws = yield self.ws_connect('/sockjs/366/v8nw22qe/websocket', cookie_header=cookie_header)
|
|
|
|
yield ws.read_message()
|
|
|
|
queue_events_data = self._get_queue_events_data(user_profile.email)
|
|
|
|
request_id = ':'.join((queue_events_data['response']['queue_id'], '0'))
|
|
|
|
response = yield self._websocket_auth(ws, queue_events_data, cookies)
|
|
|
|
self.assertEqual(response[0][0], 'a')
|
|
|
|
self.assertEqual(
|
|
|
|
ujson.loads(response[0][1:]),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "ack",
|
|
|
|
"req_id": request_id
|
|
|
|
}
|
|
|
|
])
|
|
|
|
self.assertEqual(response[1][0], 'a')
|
|
|
|
self.assertEqual(
|
|
|
|
ujson.loads(response[1][1:]),
|
|
|
|
[
|
|
|
|
{"req_id": request_id,
|
|
|
|
"response": {
|
|
|
|
"result": "success",
|
|
|
|
"status_inquiries": {},
|
|
|
|
"msg": ""
|
|
|
|
},
|
|
|
|
"type": "response"}
|
|
|
|
])
|
2017-10-28 01:28:33 +02:00
|
|
|
yield self.close(ws)
|
2016-11-16 12:37:26 +01:00
|
|
|
|
|
|
|
@gen_test
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_sending_private_message(self) -> Generator[str, Any, None]:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2016-11-16 12:37:26 +01:00
|
|
|
cookies = self._get_cookies(user_profile)
|
|
|
|
cookie_header = self.get_cookie_header(cookies)
|
|
|
|
queue_events_data = self._get_queue_events_data(user_profile.email)
|
|
|
|
ws = yield self.ws_connect('/sockjs/366/v8nw22qe/websocket', cookie_header=cookie_header)
|
|
|
|
yield ws.read_message()
|
|
|
|
yield self._websocket_auth(ws, queue_events_data, cookies)
|
|
|
|
request_id = ':'.join((queue_events_data['response']['queue_id'], '1'))
|
|
|
|
user_message = {
|
|
|
|
"req_id": request_id,
|
|
|
|
"type": "request",
|
|
|
|
"request": {
|
|
|
|
"client": "website",
|
|
|
|
"type": "private",
|
|
|
|
"subject": "(no topic)",
|
|
|
|
"stream": "",
|
2017-05-24 05:08:49 +02:00
|
|
|
"private_message_recipient": self.example_email('othello'),
|
2016-11-16 12:37:26 +01:00
|
|
|
"content": "hello",
|
|
|
|
"sender_id": user_profile.id,
|
|
|
|
"queue_id": queue_events_data['response']['queue_id'],
|
2017-05-24 05:08:49 +02:00
|
|
|
"to": ujson.dumps([self.example_email('othello')]),
|
|
|
|
"reply_to": self.example_email('hamlet'),
|
2017-07-14 19:30:23 +02:00
|
|
|
"local_id": -1
|
2016-11-16 12:37:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
user_message_str = ujson.dumps(user_message)
|
|
|
|
ws.write_message(ujson.dumps([user_message_str]))
|
|
|
|
ack_resp = yield ws.read_message()
|
|
|
|
msg_resp = yield ws.read_message()
|
|
|
|
self._check_message_sending(request_id, ack_resp, msg_resp, user_profile, queue_events_data)
|
2017-10-28 01:28:33 +02:00
|
|
|
yield self.close(ws)
|
2016-11-16 12:37:26 +01:00
|
|
|
|
|
|
|
@gen_test
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_sending_stream_message(self) -> Generator[str, Any, None]:
|
2017-05-24 05:08:49 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2016-11-16 12:37:26 +01:00
|
|
|
cookies = self._get_cookies(user_profile)
|
|
|
|
cookie_header = self.get_cookie_header(cookies)
|
|
|
|
queue_events_data = self._get_queue_events_data(user_profile.email)
|
|
|
|
ws = yield self.ws_connect('/sockjs/366/v8nw22qe/websocket', cookie_header=cookie_header)
|
|
|
|
yield ws.read_message()
|
|
|
|
yield self._websocket_auth(ws, queue_events_data, cookies)
|
|
|
|
request_id = ':'.join((queue_events_data['response']['queue_id'], '1'))
|
|
|
|
user_message = {
|
|
|
|
"req_id": request_id,
|
|
|
|
"type": "request",
|
|
|
|
"request": {
|
|
|
|
"client": "website",
|
|
|
|
"type": "stream",
|
|
|
|
"subject": "Stream message",
|
|
|
|
"stream": "Denmark",
|
|
|
|
"private_message_recipient": "",
|
|
|
|
"content": "hello",
|
|
|
|
"sender_id": user_profile.id,
|
|
|
|
"queue_id": queue_events_data['response']['queue_id'],
|
|
|
|
"to": ujson.dumps(["Denmark"]),
|
2017-05-24 05:08:49 +02:00
|
|
|
"reply_to": self.example_email('hamlet'),
|
2017-07-14 19:30:23 +02:00
|
|
|
"local_id": -1
|
2016-11-16 12:37:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
user_message_str = ujson.dumps(user_message)
|
|
|
|
ws.write_message(ujson.dumps([user_message_str]))
|
|
|
|
ack_resp = yield ws.read_message()
|
|
|
|
msg_resp = yield ws.read_message()
|
|
|
|
self._check_message_sending(request_id, ack_resp, msg_resp, user_profile, queue_events_data)
|
2017-10-28 01:28:33 +02:00
|
|
|
yield self.close(ws)
|
2017-11-10 21:35:47 +01:00
|
|
|
|
2018-02-25 22:53:36 +01:00
|
|
|
@gen_test
|
|
|
|
def test_sending_stream_message_from_electron(self) -> Generator[str, Any, None]:
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
cookies = self._get_cookies(user_profile)
|
|
|
|
cookie_header = self.get_cookie_header(cookies)
|
|
|
|
queue_events_data = self._get_queue_events_data(user_profile.email)
|
|
|
|
ws = yield self.ws_connect('/sockjs/366/v8nw22qe/websocket', cookie_header=cookie_header)
|
|
|
|
yield ws.read_message()
|
|
|
|
yield self._websocket_auth(ws, queue_events_data, cookies)
|
|
|
|
request_id = ':'.join((queue_events_data['response']['queue_id'], '1'))
|
|
|
|
user_message = {
|
|
|
|
"req_id": request_id,
|
|
|
|
"type": "request",
|
|
|
|
"request": {
|
|
|
|
"client": "website",
|
|
|
|
"type": "stream",
|
|
|
|
"subject": "Stream message",
|
|
|
|
"stream": "Denmark",
|
|
|
|
"private_message_recipient": "",
|
|
|
|
"content": "hello",
|
|
|
|
"sender_id": user_profile.id,
|
|
|
|
"queue_id": queue_events_data['response']['queue_id'],
|
|
|
|
"to": ujson.dumps(["Denmark"]),
|
|
|
|
"reply_to": self.example_email('hamlet'),
|
|
|
|
"local_id": -1,
|
|
|
|
"socket_user_agent": "ZulipElectron/1.5.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user_message_str = ujson.dumps(user_message)
|
|
|
|
ws.write_message(ujson.dumps([user_message_str]))
|
|
|
|
ack_resp = yield ws.read_message()
|
|
|
|
msg_resp = yield ws.read_message()
|
|
|
|
self._check_message_sending(request_id, ack_resp, msg_resp, user_profile, queue_events_data)
|
|
|
|
yield self.close(ws)
|
|
|
|
|
2017-11-10 21:35:47 +01:00
|
|
|
@gen_test
|
|
|
|
def test_sending_message_error(self) -> Any:
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
cookies = self._get_cookies(user_profile)
|
|
|
|
cookie_header = self.get_cookie_header(cookies)
|
|
|
|
queue_events_data = self._get_queue_events_data(user_profile.email)
|
|
|
|
ws = yield self.ws_connect('/sockjs/366/v8nw22qe/websocket', cookie_header=cookie_header)
|
|
|
|
yield ws.read_message()
|
|
|
|
yield self._websocket_auth(ws, queue_events_data, cookies)
|
|
|
|
request_id = ':'.join((queue_events_data['response']['queue_id'], '1'))
|
|
|
|
user_message = {
|
|
|
|
"req_id": request_id,
|
|
|
|
"type": "request",
|
|
|
|
"request": {
|
|
|
|
"client": "website",
|
|
|
|
"type": "stream",
|
|
|
|
"subject": "Stream message",
|
|
|
|
"stream": "Denmark",
|
|
|
|
"private_message_recipient": "",
|
|
|
|
"content": "hello",
|
|
|
|
"sender_id": user_profile.id,
|
|
|
|
"queue_id": queue_events_data['response']['queue_id'],
|
|
|
|
"to": ujson.dumps(["Denmark"]),
|
|
|
|
"reply_to": self.example_email('hamlet'),
|
|
|
|
"local_id": -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user_message_str = ujson.dumps(user_message)
|
|
|
|
ws.write_message(ujson.dumps([user_message_str]))
|
|
|
|
|
|
|
|
def wrap_get_response(request: HttpRequest) -> HttpResponse:
|
|
|
|
request._log_data = {'bugdown_requests_start': 0,
|
|
|
|
'time_started': 0,
|
|
|
|
'bugdown_time_start': 0,
|
|
|
|
'remote_cache_time_start': 0,
|
|
|
|
'remote_cache_requests_start': 0,
|
|
|
|
'startup_time_delta': 0}
|
|
|
|
|
|
|
|
class ResponseObject(object):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.content = '{"msg":"","id":0,"result":"error"}'.encode('utf8')
|
|
|
|
return ResponseObject()
|
|
|
|
# Simulate an error response to cover the respective code paths.
|
|
|
|
with patch('django.core.handlers.base.BaseHandler.get_response', wraps=wrap_get_response), \
|
|
|
|
patch('django.db.connection.is_usable', return_value=False), \
|
|
|
|
patch('os.kill', side_effect=OSError()):
|
|
|
|
yield ws.read_message()
|
|
|
|
yield self.close(ws)
|