2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Callable, Dict, Iterable, List, Tuple
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
2016-09-16 15:25:04 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-01-24 07:54:18 +01:00
|
|
|
from django.test import override_settings
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-08-14 10:03:36 +02:00
|
|
|
from zerver.lib.test_helpers import mock_queue_publish
|
2016-09-16 15:25:04 +02:00
|
|
|
from zerver.lib.utils import statsd
|
|
|
|
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def fix_params(raw_params: Dict[str, Any]) -> Dict[str, str]:
|
2016-09-16 15:25:04 +02:00
|
|
|
# A few of our few legacy endpoints need their
|
|
|
|
# individual parameters serialized as JSON.
|
2020-08-07 01:09:47 +02:00
|
|
|
return {k: orjson.dumps(v).decode() for k, v in raw_params.items()}
|
2016-09-16 15:25:04 +02:00
|
|
|
|
2017-11-05 11:49:43 +01:00
|
|
|
class StatsMock:
|
2017-11-05 10:51:25 +01:00
|
|
|
def __init__(self, settings: Callable[..., Any]) -> None:
|
2016-09-16 15:25:04 +02:00
|
|
|
self.settings = settings
|
|
|
|
self.real_impl = statsd
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.func_calls: List[Tuple[str, Iterable[Any]]] = []
|
2016-09-16 15:25:04 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def __getattr__(self, name: str) -> Callable[..., Any]:
|
|
|
|
def f(*args: Any) -> None:
|
2016-09-16 15:25:04 +02:00
|
|
|
with self.settings(STATSD_HOST=''):
|
|
|
|
getattr(self.real_impl, name)(*args)
|
|
|
|
self.func_calls.append((name, args))
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
class TestReport(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_time(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2016-09-16 15:25:04 +02:00
|
|
|
|
|
|
|
params = dict(
|
|
|
|
time=5,
|
|
|
|
received=6,
|
|
|
|
displayed=7,
|
|
|
|
locally_echoed='true',
|
|
|
|
rendered_content_disparity='true',
|
|
|
|
)
|
|
|
|
|
|
|
|
stats_mock = StatsMock(self.settings)
|
|
|
|
with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/send_times", params)
|
2016-09-16 15:25:04 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
2017-03-13 17:50:28 +01:00
|
|
|
('timing', ('endtoend.send_time.zulip', 5)),
|
|
|
|
('timing', ('endtoend.receive_time.zulip', 6)),
|
|
|
|
('timing', ('endtoend.displayed_time.zulip', 7)),
|
2016-09-16 15:25:04 +02:00
|
|
|
('incr', ('locally_echoed',)),
|
|
|
|
('incr', ('render_disparity',)),
|
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_narrow_time(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2016-09-16 15:25:04 +02:00
|
|
|
|
|
|
|
params = dict(
|
|
|
|
initial_core=5,
|
|
|
|
initial_free=6,
|
|
|
|
network=7,
|
|
|
|
)
|
|
|
|
|
|
|
|
stats_mock = StatsMock(self.settings)
|
|
|
|
with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/narrow_times", params)
|
2016-09-16 15:25:04 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
2017-03-13 17:50:28 +01:00
|
|
|
('timing', ('narrow.initial_core.zulip', 5)),
|
|
|
|
('timing', ('narrow.initial_free.zulip', 6)),
|
|
|
|
('timing', ('narrow.network.zulip', 7)),
|
2016-09-16 15:25:04 +02:00
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
2020-08-31 09:22:40 +02:00
|
|
|
def test_anonymous_user_narrow_time(self) -> None:
|
|
|
|
params = dict(
|
|
|
|
initial_core=5,
|
|
|
|
initial_free=6,
|
|
|
|
network=7,
|
|
|
|
)
|
|
|
|
|
|
|
|
stats_mock = StatsMock(self.settings)
|
|
|
|
with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
|
|
|
|
result = self.client_post("/json/report/narrow_times", params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
|
|
|
('timing', ('narrow.initial_core.zulip', 5)),
|
|
|
|
('timing', ('narrow.initial_free.zulip', 6)),
|
|
|
|
('timing', ('narrow.network.zulip', 7)),
|
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unnarrow_time(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2016-09-16 15:25:04 +02:00
|
|
|
|
|
|
|
params = dict(
|
2020-08-31 09:22:40 +02:00
|
|
|
initial_core=5,
|
|
|
|
initial_free=6,
|
|
|
|
)
|
|
|
|
|
|
|
|
stats_mock = StatsMock(self.settings)
|
|
|
|
with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
|
|
|
|
result = self.client_post("/json/report/unnarrow_times", params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
|
|
|
('timing', ('unnarrow.initial_core.zulip', 5)),
|
|
|
|
('timing', ('unnarrow.initial_free.zulip', 6)),
|
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
|
|
|
def test_anonymous_user_unnarrow_time(self) -> None:
|
|
|
|
params = dict(
|
2016-09-16 15:25:04 +02:00
|
|
|
initial_core=5,
|
|
|
|
initial_free=6,
|
|
|
|
)
|
|
|
|
|
|
|
|
stats_mock = StatsMock(self.settings)
|
|
|
|
with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/unnarrow_times", params)
|
2016-09-16 15:25:04 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
2017-03-13 17:50:28 +01:00
|
|
|
('timing', ('unnarrow.initial_core.zulip', 5)),
|
|
|
|
('timing', ('unnarrow.initial_free.zulip', 6)),
|
2016-09-16 15:25:04 +02:00
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
2017-01-24 07:54:18 +01:00
|
|
|
@override_settings(BROWSER_ERROR_REPORTING=True)
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_report_error(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.login_user(user)
|
2020-07-20 19:08:58 +02:00
|
|
|
self.make_stream('errors', user.realm)
|
2016-09-16 15:25:04 +02:00
|
|
|
|
|
|
|
params = fix_params(dict(
|
|
|
|
message='hello',
|
|
|
|
stacktrace='trace',
|
|
|
|
ui_message=True,
|
|
|
|
user_agent='agent',
|
|
|
|
href='href',
|
|
|
|
log='log',
|
2017-10-12 03:43:34 +02:00
|
|
|
more_info=dict(foo='bar', draft_content="**draft**"),
|
2016-09-16 15:25:04 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
subprocess_mock = mock.patch(
|
|
|
|
'zerver.views.report.subprocess.check_output',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
side_effect=KeyError('foo'),
|
2016-09-16 15:25:04 +02:00
|
|
|
)
|
2020-08-14 10:03:36 +02:00
|
|
|
with mock_queue_publish('zerver.views.report.queue_json_publish') as m, subprocess_mock:
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/error", params)
|
2016-09-16 15:25:04 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
report = m.call_args[0][1]['report']
|
2020-04-09 21:51:58 +02:00
|
|
|
for k in set(params) - {'ui_message', 'more_info'}:
|
2016-09-16 15:25:04 +02:00
|
|
|
self.assertEqual(report[k], params[k])
|
|
|
|
|
2017-10-12 03:43:34 +02:00
|
|
|
self.assertEqual(report['more_info'], dict(foo='bar', draft_content="'**xxxxx**'"))
|
2020-03-12 14:17:25 +01:00
|
|
|
self.assertEqual(report['user_email'], user.delivery_email)
|
2016-09-16 15:25:04 +02:00
|
|
|
|
2017-10-13 02:22:47 +02:00
|
|
|
# Teset with no more_info
|
|
|
|
del params['more_info']
|
2020-08-14 10:03:36 +02:00
|
|
|
with mock_queue_publish('zerver.views.report.queue_json_publish') as m, subprocess_mock:
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/error", params)
|
2017-10-13 02:22:47 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-01-24 07:54:18 +01:00
|
|
|
with self.settings(BROWSER_ERROR_REPORTING=False):
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/error", params)
|
2016-09-16 15:25:04 +02:00
|
|
|
self.assert_json_success(result)
|
2017-03-01 04:19:56 +01:00
|
|
|
|
|
|
|
# If js_source_map is present, then the stack trace should be annotated.
|
2017-03-01 06:42:31 +01:00
|
|
|
# DEVELOPMENT=False and TEST_SUITE=False are necessary to ensure that
|
2017-03-01 04:19:56 +01:00
|
|
|
# js_source_map actually gets instantiated.
|
|
|
|
with \
|
2017-03-01 06:42:31 +01:00
|
|
|
self.settings(DEVELOPMENT=False, TEST_SUITE=False), \
|
2020-07-27 01:00:52 +02:00
|
|
|
mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate, \
|
|
|
|
self.assertLogs(level='INFO') as info_logs:
|
2017-10-16 22:07:19 +02:00
|
|
|
result = self.client_post("/json/report/error", params)
|
2017-03-01 04:19:56 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
# fix_params (see above) adds quotes when JSON encoding.
|
|
|
|
annotate.assert_called_once_with('"trace"')
|
2020-07-27 01:00:52 +02:00
|
|
|
self.assertEqual(info_logs.output, [
|
|
|
|
'INFO:root:Processing traceback with type browser for None'
|
|
|
|
])
|
2018-04-11 05:50:08 +02:00
|
|
|
|
2018-12-17 00:10:20 +01:00
|
|
|
# Now test without authentication.
|
|
|
|
self.logout()
|
|
|
|
with \
|
|
|
|
self.settings(DEVELOPMENT=False, TEST_SUITE=False), \
|
2020-07-27 01:00:52 +02:00
|
|
|
mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate, \
|
|
|
|
self.assertLogs(level='INFO') as info_logs:
|
2018-12-17 00:10:20 +01:00
|
|
|
result = self.client_post("/json/report/error", params)
|
|
|
|
self.assert_json_success(result)
|
2020-07-27 01:00:52 +02:00
|
|
|
self.assertEqual(info_logs.output, [
|
|
|
|
'INFO:root:Processing traceback with type browser for None'
|
|
|
|
])
|
2018-12-17 00:10:20 +01:00
|
|
|
|
2018-04-11 05:50:08 +02:00
|
|
|
def test_report_csp_violations(self) -> None:
|
2018-04-20 03:57:21 +02:00
|
|
|
fixture_data = self.fixture_data('csp_report.json')
|
2020-07-27 01:00:52 +02:00
|
|
|
with self.assertLogs(level='WARNING') as warn_logs:
|
|
|
|
result = self.client_post("/report/csp_violations", fixture_data, content_type="application/json")
|
2018-04-11 05:50:08 +02:00
|
|
|
self.assert_json_success(result)
|
2020-07-27 01:00:52 +02:00
|
|
|
self.assertEqual(warn_logs.output, [
|
|
|
|
"WARNING:root:CSP Violation in Document(''). Blocked URI(''), Original Policy(''), Violated Directive(''), Effective Directive(''), Disposition(''), Referrer(''), Status Code(''), Script Sample('')"
|
|
|
|
])
|