2016-09-13 23:45:39 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-05-16 03:35:48 +02:00
|
|
|
from datetime import timedelta
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-07-27 23:47:33 +02:00
|
|
|
import mock
|
2016-09-13 23:45:39 +02:00
|
|
|
|
|
|
|
from typing import Any, Dict
|
2017-02-11 08:38:16 +01:00
|
|
|
from zerver.lib.actions import do_deactivate_user
|
2018-05-16 03:35:48 +02:00
|
|
|
from zerver.lib.statistics import seconds_usage_between
|
2016-09-13 23:45:39 +02:00
|
|
|
from zerver.lib.test_helpers import (
|
|
|
|
make_client,
|
|
|
|
queries_captured,
|
2016-11-10 19:30:09 +01:00
|
|
|
)
|
|
|
|
from zerver.lib.test_classes import (
|
2016-09-13 23:45:39 +02:00
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2017-03-02 09:52:17 +01:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2016-09-13 23:45:39 +02:00
|
|
|
from zerver.models import (
|
2016-11-11 21:13:30 +01:00
|
|
|
email_to_domain,
|
2016-09-13 23:45:39 +02:00
|
|
|
Client,
|
2017-09-08 17:55:46 +02:00
|
|
|
PushDeviceToken,
|
2016-09-13 23:45:39 +02:00
|
|
|
UserActivity,
|
2018-05-10 01:54:45 +02:00
|
|
|
UserActivityInterval,
|
2016-09-13 23:45:39 +02:00
|
|
|
UserProfile,
|
2017-02-19 16:43:32 +01:00
|
|
|
UserPresence,
|
2017-03-31 08:31:18 +02:00
|
|
|
flush_per_request_caches,
|
2017-11-18 00:11:24 +01:00
|
|
|
get_realm,
|
2016-09-13 23:45:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
class ActivityTest(ZulipTestCase):
|
2018-11-16 17:08:09 +01:00
|
|
|
@mock.patch("stripe.Customer.list", return_value=[])
|
|
|
|
def test_activity(self, unused_mock: mock.Mock) -> None:
|
2018-05-21 07:01:55 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-13 23:45:39 +02:00
|
|
|
client, _ = Client.objects.get_or_create(name='website')
|
|
|
|
query = '/json/users/me/pointer'
|
2017-04-15 04:03:56 +02:00
|
|
|
last_visit = timezone_now()
|
2016-11-28 23:29:01 +01:00
|
|
|
count = 150
|
2018-05-21 07:01:55 +02:00
|
|
|
for activity_user_profile in UserProfile.objects.all():
|
2016-09-13 23:45:39 +02:00
|
|
|
UserActivity.objects.get_or_create(
|
2018-05-21 07:01:55 +02:00
|
|
|
user_profile=activity_user_profile,
|
2016-09-13 23:45:39 +02:00
|
|
|
client=client,
|
|
|
|
query=query,
|
|
|
|
count=count,
|
|
|
|
last_visit=last_visit
|
|
|
|
)
|
2018-05-16 03:52:18 +02:00
|
|
|
|
|
|
|
# Fails when not staff
|
|
|
|
result = self.client_get('/activity')
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
|
2018-05-21 07:01:55 +02:00
|
|
|
user_profile = self.example_user("hamlet")
|
2018-05-16 03:52:18 +02:00
|
|
|
user_profile.is_staff = True
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
flush_per_request_caches()
|
|
|
|
with queries_captured() as queries:
|
|
|
|
result = self.client_get('/activity')
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2019-03-20 13:13:44 +01:00
|
|
|
self.assert_length(queries, 14)
|
2018-05-16 03:52:18 +02:00
|
|
|
|
|
|
|
flush_per_request_caches()
|
|
|
|
with queries_captured() as queries:
|
|
|
|
result = self.client_get('/realm_activity/zulip/')
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2019-03-20 13:13:44 +01:00
|
|
|
self.assert_length(queries, 8)
|
2018-05-16 03:52:18 +02:00
|
|
|
|
2017-03-31 08:31:18 +02:00
|
|
|
flush_per_request_caches()
|
2016-09-13 23:45:39 +02:00
|
|
|
with queries_captured() as queries:
|
2018-05-16 03:52:18 +02:00
|
|
|
result = self.client_get('/user_activity/iago@zulip.com/')
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2019-03-20 13:13:44 +01:00
|
|
|
self.assert_length(queries, 4)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-03-08 12:53:26 +01:00
|
|
|
class TestClientModel(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_client_stringification(self) -> None:
|
2017-03-08 12:53:26 +01:00
|
|
|
'''
|
2017-10-27 09:06:40 +02:00
|
|
|
This test is designed to cover __str__ method for Client.
|
2017-03-08 12:53:26 +01:00
|
|
|
'''
|
|
|
|
client = make_client('some_client')
|
2017-10-27 09:06:40 +02:00
|
|
|
self.assertEqual(str(client), '<Client: some_client>')
|
2017-03-08 12:53:26 +01:00
|
|
|
|
2017-02-19 16:43:32 +01:00
|
|
|
class UserPresenceModelTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_date_logic(self) -> None:
|
2017-02-19 16:43:32 +01:00
|
|
|
UserPresence.objects.all().delete()
|
|
|
|
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
2017-02-19 16:43:32 +01:00
|
|
|
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
|
|
|
|
self.assertEqual(len(presence_dct), 0)
|
|
|
|
|
|
|
|
self.login(email)
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active'})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
|
|
|
|
self.assertEqual(len(presence_dct), 1)
|
|
|
|
self.assertEqual(presence_dct[email]['website']['status'], 'active')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def back_date(num_weeks: int) -> None:
|
2017-02-19 16:43:32 +01:00
|
|
|
user_presence = UserPresence.objects.filter(user_profile=user_profile)[0]
|
2017-04-15 04:03:56 +02:00
|
|
|
user_presence.timestamp = timezone_now() - datetime.timedelta(weeks=num_weeks)
|
2017-02-19 16:43:32 +01:00
|
|
|
user_presence.save()
|
|
|
|
|
|
|
|
# Simulate the presence being a week old first. Nothing should change.
|
|
|
|
back_date(num_weeks=1)
|
|
|
|
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
|
|
|
|
self.assertEqual(len(presence_dct), 1)
|
|
|
|
|
|
|
|
# If the UserPresence row is three weeks old, we ignore it.
|
|
|
|
back_date(num_weeks=3)
|
|
|
|
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
|
|
|
|
self.assertEqual(len(presence_dct), 0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_push_tokens(self) -> None:
|
2017-09-08 17:55:46 +02:00
|
|
|
UserPresence.objects.all().delete()
|
|
|
|
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
|
|
|
|
|
|
|
self.login(email)
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active'})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def pushable() -> bool:
|
2017-09-08 17:55:46 +02:00
|
|
|
presence_dct = UserPresence.get_status_dict_by_realm(user_profile.realm_id)
|
|
|
|
self.assertEqual(len(presence_dct), 1)
|
|
|
|
return presence_dct[email]['website']['pushable']
|
|
|
|
|
|
|
|
self.assertFalse(pushable())
|
|
|
|
|
|
|
|
user_profile.enable_offline_push_notifications = True
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
self.assertFalse(pushable())
|
|
|
|
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
user=user_profile,
|
|
|
|
kind=PushDeviceToken.APNS
|
|
|
|
)
|
|
|
|
self.assertTrue(pushable())
|
|
|
|
|
2016-09-13 23:45:39 +02:00
|
|
|
class UserPresenceTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_invalid_presence(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2016-09-13 23:45:39 +02:00
|
|
|
self.login(email)
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'foo'})
|
2017-01-29 00:08:08 +01:00
|
|
|
self.assert_json_error(result, 'Invalid status: foo')
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_set_idle(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2016-09-13 23:45:39 +02:00
|
|
|
self.login(email)
|
|
|
|
client = 'website'
|
|
|
|
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'})
|
2016-09-27 18:28:40 +02:00
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:41:56 +02:00
|
|
|
json = result.json()
|
2016-09-27 18:28:40 +02:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'idle')
|
|
|
|
self.assertIn('timestamp', json['presences'][email][client])
|
|
|
|
self.assertIsInstance(json['presences'][email][client]['timestamp'], int)
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(list(json['presences'].keys()), [self.example_email("hamlet")])
|
2016-09-27 18:28:40 +02:00
|
|
|
timestamp = json['presences'][email][client]['timestamp']
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-05-25 02:08:35 +02:00
|
|
|
email = self.example_email("othello")
|
2016-09-13 23:45:39 +02:00
|
|
|
self.login(email)
|
2016-09-27 18:28:40 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'})
|
2017-08-17 08:41:56 +02:00
|
|
|
json = result.json()
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'idle')
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(json['presences'][self.example_email("hamlet")][client]['status'], 'idle')
|
2017-05-25 02:08:35 +02:00
|
|
|
self.assertEqual(sorted(json['presences'].keys()), [self.example_email("hamlet"), self.example_email("othello")])
|
2016-09-13 23:45:39 +02:00
|
|
|
newer_timestamp = json['presences'][email][client]['timestamp']
|
|
|
|
self.assertGreaterEqual(newer_timestamp, timestamp)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_set_active(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-13 23:45:39 +02:00
|
|
|
client = 'website'
|
|
|
|
|
2016-09-27 18:28:40 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'})
|
2016-09-13 23:45:39 +02:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:50:59 +02:00
|
|
|
self.assertEqual(result.json()['presences'][self.example_email("hamlet")][client]['status'], 'idle')
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-05-25 02:08:35 +02:00
|
|
|
email = self.example_email("othello")
|
|
|
|
self.login(self.example_email("othello"))
|
2016-09-27 18:28:40 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'})
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:41:56 +02:00
|
|
|
json = result.json()
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'idle')
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(json['presences'][self.example_email("hamlet")][client]['status'], 'idle')
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2016-09-27 18:28:40 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active'})
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:41:56 +02:00
|
|
|
json = result.json()
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'active')
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(json['presences'][self.example_email("hamlet")][client]['status'], 'idle')
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2018-11-16 17:08:09 +01:00
|
|
|
@mock.patch("stripe.Customer.list", return_value=[])
|
|
|
|
def test_new_user_input(self, unused_mock: mock.Mock) -> None:
|
2018-05-10 01:54:45 +02:00
|
|
|
"""Mostly a test for UserActivityInterval"""
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
self.assertEqual(UserActivityInterval.objects.filter(user_profile=user_profile).count(), 0)
|
|
|
|
time_zero = timezone_now().replace(microsecond=0)
|
|
|
|
with mock.patch('zerver.views.presence.timezone_now', return_value=time_zero):
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active',
|
|
|
|
'new_user_input': 'true'})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(UserActivityInterval.objects.filter(user_profile=user_profile).count(), 1)
|
|
|
|
interval = UserActivityInterval.objects.get(user_profile=user_profile)
|
|
|
|
self.assertEqual(interval.start, time_zero)
|
|
|
|
self.assertEqual(interval.end, time_zero + UserActivityInterval.MIN_INTERVAL_LENGTH)
|
|
|
|
|
|
|
|
second_time = time_zero + timedelta(seconds=600)
|
|
|
|
# Extent the interval
|
|
|
|
with mock.patch('zerver.views.presence.timezone_now', return_value=second_time):
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active',
|
|
|
|
'new_user_input': 'true'})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(UserActivityInterval.objects.filter(user_profile=user_profile).count(), 1)
|
|
|
|
interval = UserActivityInterval.objects.get(user_profile=user_profile)
|
|
|
|
self.assertEqual(interval.start, time_zero)
|
|
|
|
self.assertEqual(interval.end, second_time + UserActivityInterval.MIN_INTERVAL_LENGTH)
|
|
|
|
|
|
|
|
third_time = time_zero + timedelta(seconds=6000)
|
|
|
|
with mock.patch('zerver.views.presence.timezone_now', return_value=third_time):
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active',
|
|
|
|
'new_user_input': 'true'})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(UserActivityInterval.objects.filter(user_profile=user_profile).count(), 2)
|
|
|
|
interval = UserActivityInterval.objects.filter(user_profile=user_profile).order_by('start')[0]
|
|
|
|
self.assertEqual(interval.start, time_zero)
|
|
|
|
self.assertEqual(interval.end, second_time + UserActivityInterval.MIN_INTERVAL_LENGTH)
|
|
|
|
interval = UserActivityInterval.objects.filter(user_profile=user_profile).order_by('start')[1]
|
|
|
|
self.assertEqual(interval.start, third_time)
|
|
|
|
self.assertEqual(interval.end, third_time + UserActivityInterval.MIN_INTERVAL_LENGTH)
|
|
|
|
|
2018-05-16 03:35:48 +02:00
|
|
|
self.assertEqual(
|
|
|
|
seconds_usage_between(
|
|
|
|
user_profile, time_zero, third_time).total_seconds(),
|
|
|
|
1500)
|
|
|
|
self.assertEqual(
|
|
|
|
seconds_usage_between(
|
|
|
|
user_profile, time_zero, third_time+timedelta(seconds=10)).total_seconds(),
|
|
|
|
1510)
|
|
|
|
self.assertEqual(
|
|
|
|
seconds_usage_between(
|
|
|
|
user_profile, time_zero, third_time+timedelta(seconds=1000)).total_seconds(),
|
|
|
|
2400)
|
|
|
|
self.assertEqual(
|
|
|
|
seconds_usage_between(
|
|
|
|
user_profile, time_zero, third_time - timedelta(seconds=100)).total_seconds(),
|
|
|
|
1500)
|
|
|
|
self.assertEqual(
|
|
|
|
seconds_usage_between(
|
|
|
|
user_profile, time_zero + timedelta(seconds=100),
|
|
|
|
third_time - timedelta(seconds=100)).total_seconds(),
|
|
|
|
1400)
|
|
|
|
self.assertEqual(
|
|
|
|
seconds_usage_between(
|
|
|
|
user_profile, time_zero + timedelta(seconds=1200),
|
|
|
|
third_time - timedelta(seconds=100)).total_seconds(),
|
|
|
|
300)
|
|
|
|
|
2018-05-16 03:52:18 +02:00
|
|
|
# Now test /activity with actual data
|
|
|
|
user_profile.is_staff = True
|
|
|
|
user_profile.save()
|
|
|
|
result = self.client_get('/activity')
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2018-03-17 00:23:38 +01:00
|
|
|
def test_filter_presence_idle_user_ids(self) -> None:
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
from zerver.lib.actions import filter_presence_idle_user_ids
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
|
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [user_profile.id])
|
|
|
|
self.client_post("/json/users/me/presence", {'status': 'idle'})
|
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [user_profile.id])
|
|
|
|
self.client_post("/json/users/me/presence", {'status': 'active'})
|
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_no_mit(self) -> None:
|
2016-09-13 23:45:39 +02:00
|
|
|
"""Zephyr mirror realms such as MIT never get a list of users"""
|
2017-11-18 00:11:24 +01:00
|
|
|
self.login(self.mit_email("espuser"), realm=get_realm("zephyr"))
|
2017-08-26 00:52:05 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'},
|
|
|
|
subdomain="zephyr")
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:50:59 +02:00
|
|
|
self.assertEqual(result.json()['presences'], {})
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_mirror_presence(self) -> None:
|
2016-09-13 23:45:39 +02:00
|
|
|
"""Zephyr mirror realms find out the status of their mirror bot"""
|
2017-05-23 03:28:23 +02:00
|
|
|
user_profile = self.mit_user('espuser')
|
|
|
|
email = user_profile.email
|
2017-11-18 00:11:24 +01:00
|
|
|
self.login(email, realm=user_profile.realm)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def post_presence() -> Dict[str, Any]:
|
2017-08-26 00:52:05 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'},
|
|
|
|
subdomain="zephyr")
|
2016-09-13 23:45:39 +02:00
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:41:56 +02:00
|
|
|
json = result.json()
|
2016-09-13 23:45:39 +02:00
|
|
|
return json
|
|
|
|
|
|
|
|
json = post_presence()
|
|
|
|
self.assertEqual(json['zephyr_mirror_active'], False)
|
|
|
|
|
|
|
|
self._simulate_mirror_activity_for_user(user_profile)
|
|
|
|
json = post_presence()
|
|
|
|
self.assertEqual(json['zephyr_mirror_active'], True)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def _simulate_mirror_activity_for_user(self, user_profile: UserProfile) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
last_visit = timezone_now()
|
2016-09-13 23:45:39 +02:00
|
|
|
client = make_client('zephyr_mirror')
|
|
|
|
|
|
|
|
UserActivity.objects.get_or_create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
client=client,
|
2018-07-13 13:10:12 +02:00
|
|
|
query='get_events',
|
2016-09-13 23:45:39 +02:00
|
|
|
count=2,
|
|
|
|
last_visit=last_visit
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_same_realm(self) -> None:
|
2017-11-18 00:11:24 +01:00
|
|
|
self.login(self.mit_email("espuser"), realm=get_realm("zephyr"))
|
2017-08-26 00:52:05 +02:00
|
|
|
self.client_post("/json/users/me/presence", {'status': 'idle'},
|
|
|
|
subdomain="zephyr")
|
2017-04-18 03:23:32 +02:00
|
|
|
self.logout()
|
2016-09-13 23:45:39 +02:00
|
|
|
|
|
|
|
# Ensure we don't see hamlet@zulip.com information leakage
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-13 23:45:39 +02:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'idle'})
|
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:41:56 +02:00
|
|
|
json = result.json()
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(json['presences'][self.example_email("hamlet")]["website"]['status'], 'idle')
|
2016-09-13 23:45:39 +02:00
|
|
|
# We only want @zulip.com emails
|
|
|
|
for email in json['presences'].keys():
|
2016-11-11 21:13:30 +01:00
|
|
|
self.assertEqual(email_to_domain(email), 'zulip.com')
|
2017-02-11 08:38:16 +01:00
|
|
|
|
|
|
|
class SingleUserPresenceTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_single_user_get(self) -> None:
|
2017-02-11 08:38:16 +01:00
|
|
|
|
|
|
|
# First, we setup the test with some data
|
2017-05-25 02:08:35 +02:00
|
|
|
email = self.example_email("othello")
|
|
|
|
self.login(self.example_email("othello"))
|
2017-02-11 08:38:16 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active'})
|
|
|
|
result = self.client_post("/json/users/me/presence", {'status': 'active'},
|
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0")
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(email, "/api/v1/users/me/presence", {'status': 'idle'},
|
|
|
|
HTTP_USER_AGENT="ZulipAndroid/1.0")
|
2017-02-11 08:38:16 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Check some error conditions
|
|
|
|
result = self.client_get("/json/users/nonexistence@zulip.com/presence")
|
|
|
|
self.assert_json_error(result, "No such user")
|
|
|
|
|
|
|
|
result = self.client_get("/json/users/cordelia@zulip.com/presence")
|
|
|
|
self.assert_json_error(result, "No presence data for cordelia@zulip.com")
|
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
do_deactivate_user(self.example_user('cordelia'))
|
2017-02-11 08:38:16 +01:00
|
|
|
result = self.client_get("/json/users/cordelia@zulip.com/presence")
|
|
|
|
self.assert_json_error(result, "No such user")
|
|
|
|
|
2019-07-20 23:38:05 +02:00
|
|
|
result = self.client_get("/json/users/default-bot@zulip.com/presence")
|
2017-04-12 20:50:23 +02:00
|
|
|
self.assert_json_error(result, "Presence is not supported for bot users.")
|
2017-02-11 08:38:16 +01:00
|
|
|
|
2017-11-18 00:11:24 +01:00
|
|
|
self.login(self.mit_email("sipbtest"), realm=get_realm("zephyr"))
|
2017-08-26 00:52:05 +02:00
|
|
|
result = self.client_get("/json/users/othello@zulip.com/presence",
|
|
|
|
subdomain="zephyr")
|
2017-02-11 08:38:16 +01:00
|
|
|
self.assert_json_error(result, "No such user")
|
|
|
|
|
|
|
|
# Then, we check everything works
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-02-11 08:38:16 +01:00
|
|
|
result = self.client_get("/json/users/othello@zulip.com/presence")
|
2017-08-17 08:41:56 +02:00
|
|
|
result_dict = result.json()
|
2017-03-02 09:52:17 +01:00
|
|
|
self.assertEqual(
|
|
|
|
set(result_dict['presence'].keys()),
|
|
|
|
{"ZulipAndroid", "website", "aggregated"})
|
2017-02-11 08:38:16 +01:00
|
|
|
self.assertEqual(set(result_dict['presence']['website'].keys()), {"status", "timestamp"})
|
2017-03-02 09:52:17 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_ping_only(self) -> None:
|
2017-03-31 01:46:45 +02:00
|
|
|
|
2017-05-25 02:08:35 +02:00
|
|
|
self.login(self.example_email("othello"))
|
2017-03-31 01:46:45 +02:00
|
|
|
req = dict(
|
|
|
|
status='active',
|
|
|
|
ping_only='true',
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/users/me/presence", req)
|
|
|
|
self.assertEqual(result.json()['msg'], '')
|
2017-03-02 09:52:17 +01:00
|
|
|
|
|
|
|
class UserPresenceAggregationTests(ZulipTestCase):
|
2017-11-17 07:00:53 +01:00
|
|
|
def _send_presence_for_aggregated_tests(self, email: str, status: str,
|
|
|
|
validate_time: datetime.datetime) -> Dict[str, Dict[str, Any]]:
|
2017-03-02 09:52:17 +01:00
|
|
|
self.login(email)
|
2017-04-16 21:32:57 +02:00
|
|
|
timezone_util = 'zerver.views.presence.timezone_now'
|
2017-03-02 09:52:17 +01:00
|
|
|
with mock.patch(timezone_util, return_value=validate_time - datetime.timedelta(seconds=5)):
|
|
|
|
self.client_post("/json/users/me/presence", {'status': status})
|
|
|
|
with mock.patch(timezone_util, return_value=validate_time - datetime.timedelta(seconds=2)):
|
2017-12-14 19:02:31 +01:00
|
|
|
self.api_post(email, "/api/v1/users/me/presence", {'status': status},
|
|
|
|
HTTP_USER_AGENT="ZulipAndroid/1.0")
|
2017-03-02 09:52:17 +01:00
|
|
|
with mock.patch(timezone_util, return_value=validate_time - datetime.timedelta(seconds=7)):
|
2017-12-14 19:02:31 +01:00
|
|
|
latest_result = self.api_post(email, "/api/v1/users/me/presence", {'status': status},
|
|
|
|
HTTP_USER_AGENT="ZulipIOS/1.0")
|
2017-03-18 10:41:57 +01:00
|
|
|
latest_result_dict = latest_result.json()
|
|
|
|
self.assertDictEqual(
|
|
|
|
latest_result_dict['presences'][email]['aggregated'],
|
|
|
|
{
|
|
|
|
'status': status,
|
|
|
|
'timestamp': datetime_to_timestamp(validate_time - datetime.timedelta(seconds=2)),
|
|
|
|
'client': 'ZulipAndroid'
|
|
|
|
}
|
|
|
|
)
|
2017-03-02 09:52:17 +01:00
|
|
|
result = self.client_get("/json/users/%s/presence" % (email,))
|
|
|
|
return result.json()
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_aggregated_info(self) -> None:
|
2017-05-25 02:08:35 +02:00
|
|
|
email = self.example_email("othello")
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2017-05-25 02:08:35 +02:00
|
|
|
self._send_presence_for_aggregated_tests(str(self.example_email("othello")), 'active', validate_time)
|
2017-04-16 21:32:57 +02:00
|
|
|
with mock.patch('zerver.views.presence.timezone_now',
|
2017-03-18 10:41:57 +01:00
|
|
|
return_value=validate_time - datetime.timedelta(seconds=1)):
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_post(email, "/api/v1/users/me/presence", {'status': 'active'},
|
|
|
|
HTTP_USER_AGENT="ZulipTestDev/1.0")
|
2017-03-18 10:41:57 +01:00
|
|
|
result_dict = result.json()
|
|
|
|
self.assertDictEqual(
|
|
|
|
result_dict['presences'][email]['aggregated'],
|
|
|
|
{
|
|
|
|
'status': 'active',
|
|
|
|
'timestamp': datetime_to_timestamp(validate_time - datetime.timedelta(seconds=1)),
|
|
|
|
'client': 'ZulipTestDev'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_aggregated_presense_active(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2017-05-25 02:08:35 +02:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(str(self.example_email("othello")), 'active',
|
2017-03-02 09:52:17 +01:00
|
|
|
validate_time)
|
|
|
|
self.assertDictEqual(
|
|
|
|
result_dict['presence']['aggregated'],
|
|
|
|
{
|
|
|
|
"status": "active",
|
|
|
|
"timestamp": datetime_to_timestamp(validate_time - datetime.timedelta(seconds=2))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_aggregated_presense_idle(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2017-05-25 02:08:35 +02:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(str(self.example_email("othello")), 'idle',
|
2017-03-02 09:52:17 +01:00
|
|
|
validate_time)
|
|
|
|
self.assertDictEqual(
|
|
|
|
result_dict['presence']['aggregated'],
|
|
|
|
{
|
|
|
|
"status": "idle",
|
|
|
|
"timestamp": datetime_to_timestamp(validate_time - datetime.timedelta(seconds=2))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_aggregated_presense_mixed(self) -> None:
|
2017-05-25 02:08:35 +02:00
|
|
|
email = self.example_email("othello")
|
2017-03-02 09:52:17 +01:00
|
|
|
self.login(email)
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2017-04-16 21:32:57 +02:00
|
|
|
with mock.patch('zerver.views.presence.timezone_now',
|
2017-03-02 09:52:17 +01:00
|
|
|
return_value=validate_time - datetime.timedelta(seconds=3)):
|
2017-12-14 19:02:31 +01:00
|
|
|
self.api_post(email, "/api/v1/users/me/presence", {'status': 'active'},
|
|
|
|
HTTP_USER_AGENT="ZulipTestDev/1.0")
|
2017-05-25 02:08:35 +02:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(str(email), 'idle', validate_time)
|
2017-03-02 09:52:17 +01:00
|
|
|
self.assertDictEqual(
|
|
|
|
result_dict['presence']['aggregated'],
|
|
|
|
{
|
|
|
|
"status": "idle",
|
|
|
|
"timestamp": datetime_to_timestamp(validate_time - datetime.timedelta(seconds=2))
|
|
|
|
}
|
|
|
|
)
|
2017-03-06 12:25:37 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_aggregated_presense_offline(self) -> None:
|
2017-05-25 02:08:35 +02:00
|
|
|
email = self.example_email("othello")
|
2017-03-06 12:25:37 +01:00
|
|
|
self.login(email)
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2017-03-06 12:25:37 +01:00
|
|
|
with self.settings(OFFLINE_THRESHOLD_SECS=1):
|
2017-05-25 02:08:35 +02:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(str(email), 'idle', validate_time)
|
2017-03-06 12:25:37 +01:00
|
|
|
self.assertDictEqual(
|
|
|
|
result_dict['presence']['aggregated'],
|
|
|
|
{
|
|
|
|
"status": "offline",
|
|
|
|
"timestamp": datetime_to_timestamp(validate_time - datetime.timedelta(seconds=2))
|
|
|
|
}
|
|
|
|
)
|
2018-10-14 19:22:04 +02:00
|
|
|
|
|
|
|
class GetRealmStatusesTest(ZulipTestCase):
|
|
|
|
def test_get_statuses(self) -> None:
|
|
|
|
# Setup the test by simulating users reporting their presence data.
|
|
|
|
othello_email = self.example_email("othello")
|
|
|
|
result = self.api_post(othello_email, "/api/v1/users/me/presence", {'status': 'active'},
|
|
|
|
HTTP_USER_AGENT="ZulipAndroid/1.0")
|
|
|
|
|
|
|
|
hamlet_email = self.example_email("hamlet")
|
|
|
|
result = self.api_post(hamlet_email, "/api/v1/users/me/presence", {'status': 'idle'},
|
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Check that a bot can fetch the presence data for the realm.
|
|
|
|
result = self.api_get(self.example_email("welcome_bot"), "/api/v1/realm/presence")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = result.json()
|
|
|
|
self.assertEqual(sorted(json['presences'].keys()), [hamlet_email, othello_email])
|