2023-04-10 20:45:14 +02:00
|
|
|
from typing import Callable, ContextManager, List, Tuple
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
2016-09-16 15:25:04 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2016-09-16 15:25:04 +02:00
|
|
|
from zerver.lib.utils import statsd
|
|
|
|
|
|
|
|
|
2017-11-05 11:49:43 +01:00
|
|
|
class StatsMock:
|
2021-02-15 23:56:29 +01:00
|
|
|
def __init__(self, settings: Callable[..., ContextManager[None]]) -> None:
|
2016-09-16 15:25:04 +02:00
|
|
|
self.settings = settings
|
|
|
|
self.real_impl = statsd
|
2021-02-15 23:56:29 +01:00
|
|
|
self.func_calls: List[Tuple[str, Tuple[object, ...]]] = []
|
2016-09-16 15:25:04 +02:00
|
|
|
|
2021-02-15 23:56:29 +01:00
|
|
|
def __getattr__(self, name: str) -> Callable[..., None]:
|
|
|
|
def f(*args: object) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.settings(STATSD_HOST=""):
|
2016-09-16 15:25:04 +02:00
|
|
|
getattr(self.real_impl, name)(*args)
|
|
|
|
self.func_calls.append((name, args))
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-09-16 15:25:04 +02:00
|
|
|
class TestReport(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_time(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2016-09-16 15:25:04 +02:00
|
|
|
|
|
|
|
params = dict(
|
|
|
|
time=5,
|
|
|
|
received=6,
|
|
|
|
displayed=7,
|
2021-02-12 08:20:45 +01:00
|
|
|
locally_echoed="true",
|
|
|
|
rendered_content_disparity="true",
|
2016-09-16 15:25:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
stats_mock = StatsMock(self.settings)
|
2021-02-12 08:20:45 +01:00
|
|
|
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 = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("timing", ("endtoend.send_time.zulip", 5)),
|
|
|
|
("timing", ("endtoend.receive_time.zulip", 6)),
|
|
|
|
("timing", ("endtoend.displayed_time.zulip", 7)),
|
|
|
|
("incr", ("locally_echoed",)),
|
|
|
|
("incr", ("render_disparity",)),
|
2016-09-16 15:25:04 +02:00
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_narrow_time(self) -> None:
|
2021-02-12 08:20:45 +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)
|
2021-02-12 08:20:45 +01:00
|
|
|
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 = [
|
2021-02-12 08:20:45 +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)
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.views.report.statsd", wraps=stats_mock):
|
2020-08-31 09:22:40 +02:00
|
|
|
result = self.client_post("/json/report/narrow_times", params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("timing", ("narrow.initial_core.zulip", 5)),
|
|
|
|
("timing", ("narrow.initial_free.zulip", 6)),
|
|
|
|
("timing", ("narrow.network.zulip", 7)),
|
2020-08-31 09:22:40 +02:00
|
|
|
]
|
|
|
|
self.assertEqual(stats_mock.func_calls, expected_calls)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unnarrow_time(self) -> None:
|
2021-02-12 08:20:45 +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)
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.views.report.statsd", wraps=stats_mock):
|
2020-08-31 09:22:40 +02:00
|
|
|
result = self.client_post("/json/report/unnarrow_times", params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_calls = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("timing", ("unnarrow.initial_core.zulip", 5)),
|
|
|
|
("timing", ("unnarrow.initial_free.zulip", 6)),
|
2020-08-31 09:22:40 +02:00
|
|
|
]
|
|
|
|
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)
|
2021-02-12 08:20:45 +01:00
|
|
|
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 = [
|
2021-02-12 08:20:45 +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)
|
|
|
|
|
2018-04-11 05:50:08 +02:00
|
|
|
def test_report_csp_violations(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_data = self.fixture_data("csp_report.json")
|
|
|
|
with self.assertLogs(level="WARNING") as warn_logs:
|
2021-02-12 08:19:30 +01:00
|
|
|
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)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
warn_logs.output,
|
|
|
|
[
|
2021-05-10 07:02:14 +02:00
|
|
|
"WARNING:root:CSP violation in document(''). blocked URI(''), original policy(''), violated directive(''), effective directive(''), disposition(''), referrer(''), status code(''), script sample('')"
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|