diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index 909d8df310..674ed1ba07 100644 --- a/analytics/tests/test_views.py +++ b/analytics/tests/test_views.py @@ -18,15 +18,17 @@ from zerver.lib.actions import ( do_set_realm_property, ) from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.test_helpers import reset_emails_in_zulip_realm +from zerver.lib.test_helpers import queries_captured, reset_emails_in_zulip_realm from zerver.lib.timestamp import ceiling_to_day, ceiling_to_hour, datetime_to_timestamp from zerver.models import ( Client, MultiuseInvite, PreregistrationUser, Realm, + UserActivity, UserMessage, UserProfile, + flush_per_request_caches, get_realm, ) @@ -1146,6 +1148,53 @@ class TestGetChartDataHelpers(ZulipTestCase): self.assertEqual(sort_client_labels(data), ["a", "b", "c", "d", "e", "f", "g", "h"]) +class ActivityTest(ZulipTestCase): + @mock.patch("stripe.Customer.list", return_value=[]) + def test_activity(self, unused_mock: mock.Mock) -> None: + self.login("hamlet") + client, _ = Client.objects.get_or_create(name="website") + query = "/json/messages/flags" + last_visit = timezone_now() + count = 150 + for activity_user_profile in UserProfile.objects.all(): + UserActivity.objects.get_or_create( + user_profile=activity_user_profile, + client=client, + query=query, + count=count, + last_visit=last_visit, + ) + + # Fails when not staff + result = self.client_get("/activity") + self.assertEqual(result.status_code, 302) + + user_profile = self.example_user("hamlet") + user_profile.is_staff = True + user_profile.save(update_fields=["is_staff"]) + + flush_per_request_caches() + with queries_captured() as queries: + result = self.client_get("/activity") + self.assertEqual(result.status_code, 200) + + self.assert_length(queries, 19) + + flush_per_request_caches() + with queries_captured() as queries: + result = self.client_get("/realm_activity/zulip/") + self.assertEqual(result.status_code, 200) + + self.assert_length(queries, 8) + + flush_per_request_caches() + with queries_captured() as queries: + result = self.client_get("/user_activity/iago@zulip.com/") + self.assertEqual(result.status_code, 200) + + self.assert_length(queries, 4) + + class TestTimeRange(ZulipTestCase): def test_time_range(self) -> None: HOUR = timedelta(hours=1) diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py index 82b6545d8a..2da589ca8a 100644 --- a/zerver/tests/test_presence.py +++ b/zerver/tests/test_presence.py @@ -9,66 +9,17 @@ from zerver.lib.actions import do_deactivate_user from zerver.lib.presence import get_status_dict_by_realm from zerver.lib.statistics import seconds_usage_between from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.test_helpers import make_client, queries_captured, reset_emails_in_zulip_realm +from zerver.lib.test_helpers import make_client, reset_emails_in_zulip_realm from zerver.lib.timestamp import datetime_to_timestamp from zerver.models import ( - Client, PushDeviceToken, UserActivity, UserActivityInterval, UserPresence, UserProfile, - flush_per_request_caches, ) -class ActivityTest(ZulipTestCase): - @mock.patch("stripe.Customer.list", return_value=[]) - def test_activity(self, unused_mock: mock.Mock) -> None: - self.login("hamlet") - client, _ = Client.objects.get_or_create(name="website") - query = "/json/messages/flags" - last_visit = timezone_now() - count = 150 - for activity_user_profile in UserProfile.objects.all(): - UserActivity.objects.get_or_create( - user_profile=activity_user_profile, - client=client, - query=query, - count=count, - last_visit=last_visit, - ) - - # Fails when not staff - result = self.client_get("/activity") - self.assertEqual(result.status_code, 302) - - user_profile = self.example_user("hamlet") - user_profile.is_staff = True - user_profile.save(update_fields=["is_staff"]) - - flush_per_request_caches() - with queries_captured() as queries: - result = self.client_get("/activity") - self.assertEqual(result.status_code, 200) - - self.assert_length(queries, 19) - - flush_per_request_caches() - with queries_captured() as queries: - result = self.client_get("/realm_activity/zulip/") - self.assertEqual(result.status_code, 200) - - self.assert_length(queries, 8) - - flush_per_request_caches() - with queries_captured() as queries: - result = self.client_get("/user_activity/iago@zulip.com/") - self.assertEqual(result.status_code, 200) - - self.assert_length(queries, 4) - - class TestClientModel(ZulipTestCase): def test_client_stringification(self) -> None: """