Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
report: Allow anonymous queries to report (un)narrow times.
Aman Agrawaled8796f23f61
diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 0d6cda0438f..e46ac7b7e6b 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -1429,8 +1429,6 @@ class TestHumanUsersOnlyDecorator(ZulipTestCase):"/api/v1/users/me/presence","/api/v1/users/me/tutorial_status","/api/v1/report/send_times",- "/api/v1/report/narrow_times",- "/api/v1/report/unnarrow_times",]for endpoint in post_endpoints:result = self.api_post(default_bot, endpoint)diff --git a/zerver/tests/test_report.py b/zerver/tests/test_report.pyindex ffada6bb6bf..e8706f314b5 100644--- a/zerver/tests/test_report.py+++ b/zerver/tests/test_report.py@@ -75,6 +75,25 @@ class TestReport(ZulipTestCase):]self.assertEqual(stats_mock.func_calls, expected_calls)+ 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)+def test_unnarrow_time(self) -> None:self.login('hamlet')@@ -94,6 +113,23 @@ class TestReport(ZulipTestCase):]self.assertEqual(stats_mock.func_calls, expected_calls)+ def test_anonymous_user_unnarrow_time(self) -> None:+ params = dict(+ 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)+@override_settings(BROWSER_ERROR_REPORTING=True)def test_report_error(self) -> None:user = self.example_user('hamlet')diff --git a/zerver/views/report.py b/zerver/views/report.pyindex c408cf9c456..ec371d38c8d 100644--- a/zerver/views/report.py+++ b/zerver/views/report.py@@ -1,14 +1,16 @@# System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.htmlimport loggingimport subprocess-from typing import Any, Dict, Mapping, Optional+from typing import Any, Dict, Mapping, Optional, Unionfrom urllib.parse import SplitResultfrom django.conf import settings+from django.contrib.auth.models import AnonymousUserfrom django.http import HttpRequest, HttpResponsefrom django.views.decorators.csrf import csrf_exemptfrom django.views.decorators.http import require_POST+from zerver.context_processors import get_valid_realm_from_requestfrom zerver.decorator import human_users_onlyfrom zerver.lib.markdown import privacy_clean_markdownfrom zerver.lib.queue import queue_json_publish@@ -61,26 +63,26 @@ def report_send_times(request: HttpRequest, user_profile: UserProfile,statsd.incr('render_disparity')return json_success()-@human_users_only@has_request_variables-def report_narrow_times(request: HttpRequest, user_profile: UserProfile,+def report_narrow_times(request: HttpRequest, user_profile: Union[UserProfile, AnonymousUser],initial_core: int=REQ(converter=to_non_negative_int),initial_free: int=REQ(converter=to_non_negative_int),network: int=REQ(converter=to_non_negative_int)) -> HttpResponse:request._log_data["extra"] = f"[{initial_core}ms/{initial_free}ms/{network}ms]"- base_key = statsd_key(user_profile.realm.string_id, clean_periods=True)+ realm = get_valid_realm_from_request(request)+ base_key = statsd_key(realm.string_id, clean_periods=True)statsd.timing(f"narrow.initial_core.{base_key}", initial_core)statsd.timing(f"narrow.initial_free.{base_key}", initial_free)statsd.timing(f"narrow.network.{base_key}", network)return json_success()-@human_users_only@has_request_variables-def report_unnarrow_times(request: HttpRequest, user_profile: UserProfile,+def report_unnarrow_times(request: HttpRequest, user_profile: Union[UserProfile, AnonymousUser],initial_core: int=REQ(converter=to_non_negative_int),initial_free: int=REQ(converter=to_non_negative_int)) -> HttpResponse:request._log_data["extra"] = f"[{initial_core}ms/{initial_free}ms]"- base_key = statsd_key(user_profile.realm.string_id, clean_periods=True)+ realm = get_valid_realm_from_request(request)+ base_key = statsd_key(realm.string_id, clean_periods=True)statsd.timing(f"unnarrow.initial_core.{base_key}", initial_core)statsd.timing(f"unnarrow.initial_free.{base_key}", initial_free)return json_success()diff --git a/zproject/urls.py b/zproject/urls.pyindex ac02c8936aa..45555636439 100644--- a/zproject/urls.py+++ b/zproject/urls.py@@ -404,9 +404,11 @@ v1_api_and_json_patterns = [path('report/send_times', rest_dispatch,{'POST': ('zerver.views.report.report_send_times', {'intentionally_undocumented'})}),path('report/narrow_times', rest_dispatch,- {'POST': ('zerver.views.report.report_narrow_times', {'intentionally_undocumented'})}),+ {'POST': ('zerver.views.report.report_narrow_times', {'allow_anonymous_user_web',+ 'intentionally_undocumented'})}),path('report/unnarrow_times', rest_dispatch,- {'POST': ('zerver.views.report.report_unnarrow_times', {'intentionally_undocumented'})}),+ {'POST': ('zerver.views.report.report_unnarrow_times', {'allow_anonymous_user_web',+ 'intentionally_undocumented'})}),# Used to generate a Zoom video call URLpath('calls/zoom/create', rest_dispatch,