mirror of https://github.com/zulip/zulip.git
tests: Move tests of /activity pages from zerver to analytics module.
This module deals with the testing of /activity, /realm_activity and /user_activity. All these pages reside in analytics module. Keeping these tests in zerver/tests is kind is not appropriate since person who makes changes to /activity pages would not think it is necessary to run tests in zerver. So better to keep them in the analytics module.
This commit is contained in:
parent
46c902686d
commit
4e26746b98
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue