mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 15:03:34 +00:00
Add test coverage for /notify_tornado.
We now have test coverage for all of our endpoints.
This commit is contained in:
@@ -4,6 +4,7 @@ from contextlib import contextmanager
|
||||
from typing import (cast, Any, Callable, Dict, Generator, Iterable, List, Mapping, Optional,
|
||||
Sized, Tuple, Union)
|
||||
|
||||
from django.core.urlresolvers import resolve
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.test.client import (
|
||||
@@ -134,6 +135,21 @@ class ZulipTestCase(TestCase):
|
||||
django_client = self.client # see WRAPPER_COMMENT
|
||||
return django_client.post(url, info, **kwargs)
|
||||
|
||||
@instrument_url
|
||||
def client_post_request(self, url, req):
|
||||
# type: (text_type, Any) -> HttpResponse
|
||||
"""
|
||||
We simulate hitting an endpoint here, although we
|
||||
actually resolve the URL manually and hit the view
|
||||
directly. We have this helper method to allow our
|
||||
instrumentation to work for /notify_tornado and
|
||||
future similar methods that require doing funny
|
||||
things to a request object.
|
||||
"""
|
||||
|
||||
match = resolve(url)
|
||||
return match.func(req)
|
||||
|
||||
@instrument_url
|
||||
def client_get(self, url, info={}, **kwargs):
|
||||
# type: (text_type, Dict[str, Any], **Any) -> HttpResponse
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
from __future__ import absolute_import
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.test import TestCase
|
||||
|
||||
@@ -66,10 +67,38 @@ from zerver.views.events_register import _default_all_public_streams, _default_n
|
||||
from zerver.tornadoviews import get_events_backend
|
||||
|
||||
from collections import OrderedDict
|
||||
import mock
|
||||
import time
|
||||
import ujson
|
||||
from six.moves import range
|
||||
|
||||
class TornadoTest(ZulipTestCase):
|
||||
def test_tornado_endpoint(self):
|
||||
# type: () -> None
|
||||
|
||||
# This test is mostly intended to get minimal coverage on
|
||||
# the /notify_tornado endpoint, so we can have 100% URL coverage,
|
||||
# but it does exercise a little bit of the codepath.
|
||||
post_data = dict(
|
||||
data=ujson.dumps(
|
||||
dict(
|
||||
event=dict(
|
||||
type='other'
|
||||
),
|
||||
users=[get_user_profile_by_email('hamlet@zulip.com').id],
|
||||
),
|
||||
),
|
||||
)
|
||||
req = POSTRequestMock(post_data, user_profile=None)
|
||||
req.META['REMOTE_ADDR'] = '127.0.0.1'
|
||||
result = self.client_post_request('/notify_tornado', req)
|
||||
self.assert_json_error(result, 'Access denied', status_code=403)
|
||||
|
||||
post_data['secret'] = settings.SHARED_SECRET
|
||||
req = POSTRequestMock(post_data, user_profile=None)
|
||||
req.META['REMOTE_ADDR'] = '127.0.0.1'
|
||||
result = self.client_post_request('/notify_tornado', req)
|
||||
self.assert_json_success(result)
|
||||
|
||||
class GetEventsTest(ZulipTestCase):
|
||||
def tornado_call(self, view_func, user_profile, post_data):
|
||||
|
||||
Reference in New Issue
Block a user