2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime, timedelta
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2023-11-28 19:16:58 +01:00
|
|
|
import time_machine
|
2020-06-11 16:03:47 +02:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import do_deactivate_user
|
2020-06-11 16:03:47 +02:00
|
|
|
from zerver.lib.presence import format_legacy_presence_dict, get_presence_dict_by_realm
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2023-03-01 07:34:25 +01:00
|
|
|
from zerver.lib.test_helpers import make_client, reset_email_visibility_to_everyone_in_zulip_realm
|
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 (
|
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,
|
2017-02-19 16:43:32 +01:00
|
|
|
UserPresence,
|
2020-06-11 00:54:34 +02:00
|
|
|
UserProfile,
|
2020-06-11 16:03:47 +02:00
|
|
|
get_realm,
|
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:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-10-27 09:06:40 +02:00
|
|
|
This test is designed to cover __str__ method for Client.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
client = make_client("some_client")
|
2023-03-08 22:18:59 +01:00
|
|
|
self.assertEqual(repr(client), "<Client: some_client>")
|
2017-03-08 12:53:26 +01:00
|
|
|
|
2021-02-12 08:19:30 +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()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2017-05-07 19:39:30 +02:00
|
|
|
email = user_profile.email
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(presence_dct, 0)
|
2017-02-19 16:43:32 +01:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {"status": "active"})
|
2017-02-19 16:43:32 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2020-02-02 18:22:19 +01:00
|
|
|
slim_presence = False
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm, slim_presence)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(presence_dct, 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(presence_dct[email]["website"]["status"], "active")
|
2017-02-19 16:43:32 +01:00
|
|
|
|
2020-02-02 18:22:19 +01:00
|
|
|
slim_presence = True
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm, slim_presence)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(presence_dct, 1)
|
2020-02-07 14:50:30 +01:00
|
|
|
info = presence_dct[str(user_profile.id)]
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(set(info.keys()), {"active_timestamp", "idle_timestamp"})
|
2020-02-02 18:22:19 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def back_date(num_weeks: int) -> None:
|
2020-06-11 16:03:47 +02:00
|
|
|
user_presence = UserPresence.objects.get(user_profile=user_profile)
|
2023-11-19 19:45:19 +01:00
|
|
|
backdated_timestamp = timezone_now() - timedelta(weeks=num_weeks)
|
2020-06-11 16:03:47 +02:00
|
|
|
user_presence.last_active_time = backdated_timestamp
|
|
|
|
user_presence.last_connected_time = backdated_timestamp
|
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)
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(presence_dct, 1)
|
2017-02-19 16:43:32 +01:00
|
|
|
|
|
|
|
# If the UserPresence row is three weeks old, we ignore it.
|
|
|
|
back_date(num_weeks=3)
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(presence_dct, 0)
|
2017-02-19 16:43:32 +01:00
|
|
|
|
2023-04-08 15:52:48 +02:00
|
|
|
# If the values are set to "never", ignore it just like for sufficiently old presence rows.
|
|
|
|
UserPresence.objects.filter(id=user_profile.id).update(
|
|
|
|
last_active_time=None, last_connected_time=None
|
|
|
|
)
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm)
|
2023-04-08 15:52:48 +02:00
|
|
|
self.assert_length(presence_dct, 0)
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
def test_pushable_always_false(self) -> None:
|
|
|
|
# This field was never used by clients of the legacy API, so we
|
|
|
|
# just want to have it always set to False for API format compatibility.
|
2017-09-08 17:55:46 +02:00
|
|
|
UserPresence.objects.all().delete()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2017-09-08 17:55:46 +02:00
|
|
|
email = user_profile.email
|
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {"status": "active"})
|
2017-09-08 17:55:46 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def pushable() -> bool:
|
2023-10-17 12:56:39 +02:00
|
|
|
presence_dct = get_presence_dict_by_realm(user_profile.realm)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(presence_dct, 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
return presence_dct[email]["website"]["pushable"]
|
2017-09-08 17:55:46 +02:00
|
|
|
|
|
|
|
self.assertFalse(pushable())
|
|
|
|
|
|
|
|
user_profile.enable_offline_push_notifications = True
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
self.assertFalse(pushable())
|
|
|
|
|
|
|
|
PushDeviceToken.objects.create(
|
|
|
|
user=user_profile,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
kind=PushDeviceToken.APNS,
|
2017-09-08 17:55:46 +02:00
|
|
|
)
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertFalse(pushable())
|
2017-09-08 17:55:46 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-09-13 23:45:39 +02:00
|
|
|
class UserPresenceTests(ZulipTestCase):
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2020-06-11 16:03:47 +02:00
|
|
|
def setUp(self) -> None:
|
|
|
|
"""
|
|
|
|
Create some initial, old presence data to make the intended set up
|
|
|
|
simpler for the tests.
|
|
|
|
"""
|
2023-10-12 18:49:16 +02:00
|
|
|
super().setUp()
|
2020-06-11 16:03:47 +02:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
now = timezone_now()
|
|
|
|
initial_presence = now - timedelta(days=365)
|
|
|
|
UserPresence.objects.bulk_create(
|
|
|
|
[
|
|
|
|
UserPresence(
|
|
|
|
user_profile=user_profile,
|
|
|
|
realm=user_profile.realm,
|
|
|
|
last_active_time=initial_presence,
|
|
|
|
last_connected_time=initial_presence,
|
|
|
|
)
|
|
|
|
for user_profile in UserProfile.objects.filter(realm=realm)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_invalid_presence(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login_user(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {"status": "foo"})
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
client = "website"
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
othello = self.example_user("othello")
|
2020-03-12 14:17:25 +01:00
|
|
|
|
|
|
|
self.login_user(hamlet)
|
2020-03-06 18:40:46 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
params = dict(status="idle")
|
2020-02-07 14:50:30 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", params)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json["presences"][hamlet.email][client]["status"], "idle")
|
|
|
|
self.assertIn("timestamp", json["presences"][hamlet.email][client])
|
|
|
|
self.assertIsInstance(json["presences"][hamlet.email][client]["timestamp"], int)
|
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email})
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2020-03-12 14:17:25 +01:00
|
|
|
self.login_user(othello)
|
2021-02-12 08:20:45 +01:00
|
|
|
params = dict(status="idle", slim_presence="true")
|
2020-02-07 14:50:30 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", params)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
presences = json["presences"]
|
2020-02-07 14:50:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
set(presences.keys()),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
{str(hamlet.id), str(othello.id)},
|
2020-02-07 14:50:30 +01:00
|
|
|
)
|
|
|
|
hamlet_info = presences[str(hamlet.id)]
|
|
|
|
othello_info = presences[str(othello.id)]
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(set(hamlet_info.keys()), {"idle_timestamp", "active_timestamp"})
|
|
|
|
self.assertEqual(set(othello_info.keys()), {"idle_timestamp", "active_timestamp"})
|
2020-02-07 14:50:30 +01:00
|
|
|
|
|
|
|
self.assertGreaterEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
othello_info["idle_timestamp"],
|
|
|
|
hamlet_info["idle_timestamp"],
|
2020-02-07 14:50:30 +01:00
|
|
|
)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_set_active(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
othello = self.example_user("othello")
|
2020-03-12 14:17:25 +01:00
|
|
|
|
|
|
|
self.login_user(hamlet)
|
2021-02-12 08:20:45 +01:00
|
|
|
client = "website"
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
params = dict(status="idle")
|
2020-02-07 14:50:30 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", params)
|
2022-06-07 01:37:01 +02:00
|
|
|
response_dict = self.assert_json_success(result)
|
2020-02-07 14:50:30 +01:00
|
|
|
|
2022-06-07 01:37:01 +02:00
|
|
|
self.assertEqual(response_dict["presences"][hamlet.email][client]["status"], "idle")
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("othello")
|
|
|
|
params = dict(status="idle")
|
2020-02-07 14:50:30 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", params)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2020-02-07 14:50:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json["presences"][othello.email][client]["status"], "idle")
|
|
|
|
self.assertEqual(json["presences"][hamlet.email][client]["status"], "idle")
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
params = dict(status="active")
|
2020-02-07 14:50:30 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", params)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2020-02-07 14:50:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json["presences"][othello.email][client]["status"], "active")
|
|
|
|
self.assertEqual(json["presences"][hamlet.email][client]["status"], "idle")
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
self.login_user(hamlet)
|
2021-02-12 08:20:45 +01:00
|
|
|
params = dict(status="active", slim_presence="true")
|
2020-02-07 14:50:30 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", params)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2020-02-07 14:50:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
presences = json["presences"]
|
2020-02-07 14:50:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
set(presences.keys()),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
{str(hamlet.id), str(othello.id)},
|
2020-02-07 14:50:30 +01:00
|
|
|
)
|
|
|
|
othello_info = presences[str(othello.id)]
|
|
|
|
hamlet_info = presences[str(hamlet.id)]
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(othello_info.keys()),
|
2020-06-11 16:03:47 +02:00
|
|
|
{"active_timestamp", "idle_timestamp"},
|
2020-02-07 14:50:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(hamlet_info.keys()),
|
2020-06-11 16:03:47 +02:00
|
|
|
{"active_timestamp", "idle_timestamp"},
|
2020-02-07 14:50:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertGreaterEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet_info["active_timestamp"],
|
|
|
|
othello_info["active_timestamp"],
|
2020-02-07 14:50:30 +01: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")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2018-05-10 01:54:45 +02:00
|
|
|
self.assertEqual(UserActivityInterval.objects.filter(user_profile=user_profile).count(), 0)
|
|
|
|
time_zero = timezone_now().replace(microsecond=0)
|
2023-11-28 19:16:58 +01:00
|
|
|
with time_machine.travel(time_zero, tick=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/users/me/presence", {"status": "active", "new_user_input": "true"}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-05-10 01:54:45 +02:00
|
|
|
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
|
2023-11-28 19:16:58 +01:00
|
|
|
with time_machine.travel(second_time, tick=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/users/me/presence", {"status": "active", "new_user_input": "true"}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-05-10 01:54:45 +02:00
|
|
|
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)
|
2023-11-28 19:16:58 +01:00
|
|
|
with time_machine.travel(third_time, tick=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/users/me/presence", {"status": "active", "new_user_input": "true"}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-05-10 01:54:45 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(UserActivityInterval.objects.filter(user_profile=user_profile).count(), 2)
|
2021-02-12 08:20:45 +01:00
|
|
|
interval = UserActivityInterval.objects.filter(user_profile=user_profile).order_by("start")[
|
2021-02-12 08:19:30 +01:00
|
|
|
0
|
|
|
|
]
|
2018-05-10 01:54:45 +02:00
|
|
|
self.assertEqual(interval.start, time_zero)
|
|
|
|
self.assertEqual(interval.end, second_time + UserActivityInterval.MIN_INTERVAL_LENGTH)
|
2021-02-12 08:20:45 +01:00
|
|
|
interval = UserActivityInterval.objects.filter(user_profile=user_profile).order_by("start")[
|
2021-02-12 08:19:30 +01:00
|
|
|
1
|
|
|
|
]
|
2018-05-10 01:54:45 +02:00
|
|
|
self.assertEqual(interval.start, third_time)
|
|
|
|
self.assertEqual(interval.end, third_time + UserActivityInterval.MIN_INTERVAL_LENGTH)
|
|
|
|
|
2018-05-16 03:52:18 +02:00
|
|
|
# Now test /activity with actual data
|
|
|
|
user_profile.is_staff = True
|
|
|
|
user_profile.save()
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/activity")
|
2018-05-16 03:52:18 +02:00
|
|
|
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")
|
2022-04-14 23:50:10 +02:00
|
|
|
from zerver.actions.message_send import filter_presence_idle_user_ids
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2018-03-17 00:23:38 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
# Ensure we're starting with a clean slate.
|
|
|
|
UserPresence.objects.all().delete()
|
2018-03-17 00:23:38 +01:00
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [user_profile.id])
|
2020-06-11 16:03:47 +02:00
|
|
|
|
2023-04-08 15:52:48 +02:00
|
|
|
# Create a first presence for the user. It's the first one and has status idle,
|
|
|
|
# so it'll initialize just last_connected time with the current time and last_active_time with None.
|
|
|
|
# Thus the user will be considered idle.
|
2020-06-11 16:03:47 +02:00
|
|
|
self.client_post("/json/users/me/presence", {"status": "idle"})
|
2023-04-08 15:52:48 +02:00
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [user_profile.id])
|
|
|
|
|
|
|
|
# Now create a first presence with active status and check that the user is not filtered.
|
|
|
|
# This initializes the presence with both last_connected_time and last_active_time set to
|
|
|
|
# current time.
|
|
|
|
self.client_post("/json/users/me/presence", {"status": "active"})
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [])
|
|
|
|
|
|
|
|
# Make last_active_time be older than OFFLINE_THRESHOLD_SECS. That should
|
|
|
|
# get the user filtered.
|
|
|
|
UserPresence.objects.filter(user_profile=user_profile).update(
|
|
|
|
last_active_time=timezone_now() - timedelta(seconds=settings.OFFLINE_THRESHOLD_SECS + 1)
|
|
|
|
)
|
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [user_profile.id])
|
|
|
|
|
|
|
|
# Sending an idle presence doesn't change anything for filtering.
|
2021-02-12 08:20:45 +01:00
|
|
|
self.client_post("/json/users/me/presence", {"status": "idle"})
|
2018-03-17 00:23:38 +01:00
|
|
|
self.assertEqual(filter_presence_idle_user_ids({user_profile.id}), [user_profile.id])
|
2019-12-11 01:58:44 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
# Active presence from the mobile app should count (in the old API it didn't)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/users/me/presence", {"status": "active"}, HTTP_USER_AGENT="ZulipMobile/1.0"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-03-17 00:23:38 +01:00
|
|
|
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"""
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.mit_user("espuser")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {"status": "idle"}, subdomain="zephyr")
|
2022-06-07 01:37:01 +02:00
|
|
|
response_dict = self.assert_json_success(result)
|
|
|
|
self.assertEqual(response_dict["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"""
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.mit_user("espuser")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def post_presence() -> Dict[str, Any]:
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/users/me/presence", {"status": "idle"}, subdomain="zephyr"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2016-09-13 23:45:39 +02:00
|
|
|
return json
|
|
|
|
|
|
|
|
json = post_presence()
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json["zephyr_mirror_active"], False)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
|
|
|
self._simulate_mirror_activity_for_user(user_profile)
|
|
|
|
json = post_presence()
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json["zephyr_mirror_active"], True)
|
2016-09-13 23:45:39 +02:00
|
|
|
|
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()
|
2021-02-12 08:20:45 +01:00
|
|
|
client = make_client("zephyr_mirror")
|
2016-09-13 23:45:39 +02:00
|
|
|
|
|
|
|
UserActivity.objects.get_or_create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
client=client,
|
2021-02-12 08:20:45 +01:00
|
|
|
query="get_events",
|
2016-09-13 23:45:39 +02:00
|
|
|
count=2,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
last_visit=last_visit,
|
2016-09-13 23:45:39 +02:00
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_same_realm(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
espuser = self.mit_user("espuser")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(espuser)
|
2021-02-12 08:20:45 +01: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
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2020-03-12 14:17:25 +01:00
|
|
|
self.login_user(hamlet)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {"status": "idle"})
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json["presences"][hamlet.email]["website"]["status"], "idle")
|
2020-03-12 14:17:25 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
json["presences"].keys(),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
{hamlet.email},
|
2020-03-12 14:17:25 +01:00
|
|
|
)
|
2017-02-11 08:38:16 +01:00
|
|
|
|
2023-04-08 15:52:48 +02:00
|
|
|
def test_null_timestamps_handling(self) -> None:
|
|
|
|
"""
|
|
|
|
Checks that the API handles presences with null presence timestamps correctly.
|
|
|
|
The data model now supports them being null, but the API should still return
|
|
|
|
valid timestamps for backwards compatibility - using date_joined as the default
|
|
|
|
to fall back on. Also it should correctly filter out users with null presence
|
|
|
|
just like it would have filtered them out if they had very old presence.
|
|
|
|
"""
|
|
|
|
self.login("hamlet")
|
|
|
|
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
# Set a predictable value for date_joined
|
|
|
|
othello.date_joined = timezone_now() - timedelta(days=1)
|
|
|
|
othello.save()
|
|
|
|
UserPresence.objects.filter(user_profile=othello).update(
|
|
|
|
last_active_time=None, last_connected_time=None
|
|
|
|
)
|
|
|
|
|
|
|
|
result = self.client_get(f"/json/users/{othello.id}/presence")
|
|
|
|
result_dict = self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Ensure date_joined was used as the fallback.
|
|
|
|
self.assertEqual(
|
|
|
|
result_dict["presence"]["website"]["timestamp"],
|
|
|
|
datetime_to_timestamp(othello.date_joined),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Othello has null presence values, so should not appear in the /realm/presence response
|
|
|
|
# just like a user with over two weeks old presence.
|
|
|
|
result = self.client_get("/json/realm/presence")
|
|
|
|
result_dict = self.assert_json_success(result)
|
|
|
|
self.assertEqual(result_dict["presences"], {})
|
|
|
|
|
|
|
|
# If othello's presence is fresh however, it should appear in the response.
|
|
|
|
now = timezone_now()
|
|
|
|
UserPresence.objects.filter(user_profile=othello).update(
|
|
|
|
last_active_time=now, last_connected_time=now
|
|
|
|
)
|
|
|
|
result = self.client_get("/json/realm/presence")
|
|
|
|
result_dict = self.assert_json_success(result)
|
|
|
|
self.assertEqual(set(result_dict["presences"].keys()), {othello.email})
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-02-11 08:38:16 +01:00
|
|
|
class SingleUserPresenceTests(ZulipTestCase):
|
2020-03-18 15:19:03 +01:00
|
|
|
def test_email_access(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-03-18 15:19:03 +01:00
|
|
|
self.login_user(user)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
other_user = self.example_user("othello")
|
|
|
|
other_user.email = "email@zulip.com"
|
|
|
|
other_user.delivery_email = "delivery_email@zulip.com"
|
2020-03-18 15:19:03 +01:00
|
|
|
other_user.save()
|
|
|
|
|
|
|
|
# Note that we don't leak any info on delivery emails.
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/json/users/delivery_email@zulip.com/presence")
|
|
|
|
self.assert_json_error(result, "No such user")
|
2020-03-18 15:19:03 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/json/users/not_even_in_realm@zulip.com/presence")
|
|
|
|
self.assert_json_error(result, "No such user")
|
2020-03-18 15:19:03 +01:00
|
|
|
|
|
|
|
# For a known email, we may simply complain about lack of presence info.
|
|
|
|
result = self.client_get("/json/users/email@zulip.com/presence")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "No presence data for email@zulip.com")
|
2020-03-18 15:19:03 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_single_user_get(self) -> None:
|
2023-03-01 07:34:25 +01:00
|
|
|
reset_email_visibility_to_everyone_in_zulip_realm()
|
2017-02-11 08:38:16 +01:00
|
|
|
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
# First, we set up the test with some data
|
2020-03-10 11:48:26 +01:00
|
|
|
user = self.example_user("othello")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/presence", {"status": "active"})
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/users/me/presence", {"status": "active"}, HTTP_USER_AGENT="ZulipDesktop/1.0"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
result = self.api_post(
|
|
|
|
user,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
{"status": "idle"},
|
2021-02-12 08:19:30 +01:00
|
|
|
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")
|
|
|
|
|
2021-01-04 19:36:00 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
result = self.client_get(f"/json/users/{cordelia.id}/presence")
|
|
|
|
self.assert_json_error(result, f"No presence data for {cordelia.id}")
|
|
|
|
|
2021-03-27 06:02:12 +01:00
|
|
|
do_deactivate_user(self.example_user("cordelia"), acting_user=None)
|
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")
|
|
|
|
|
2021-01-04 19:36:00 +01:00
|
|
|
result = self.client_get(f"/json/users/{cordelia.id}/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
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sipbtest = self.mit_user("sipbtest")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(sipbtest)
|
2021-02-12 08:19:30 +01: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")
|
|
|
|
|
2021-01-04 19:36:00 +01:00
|
|
|
othello = self.example_user("othello")
|
|
|
|
result = self.client_get(f"/json/users/{othello.id}/presence", subdomain="zephyr")
|
|
|
|
self.assert_json_error(result, "No such user")
|
|
|
|
|
2023-10-17 12:56:39 +02:00
|
|
|
self.set_up_db_for_testing_user_access()
|
|
|
|
self.login("polonius")
|
|
|
|
with self.settings(CAN_ACCESS_ALL_USERS_GROUP_LIMITS_PRESENCE=True):
|
|
|
|
result = self.client_get(f"/json/users/{othello.id}/presence")
|
|
|
|
self.assert_json_error(result, "Insufficient permission")
|
|
|
|
|
|
|
|
result = self.client_get(f"/json/users/{othello.id}/presence")
|
|
|
|
result_dict = self.assert_json_success(result)
|
|
|
|
self.assertEqual(set(result_dict["presence"].keys()), {"website", "aggregated"})
|
|
|
|
self.assertEqual(set(result_dict["presence"]["website"].keys()), {"status", "timestamp"})
|
|
|
|
|
2017-02-11 08:38:16 +01:00
|
|
|
# Then, we check everything works
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2017-02-11 08:38:16 +01:00
|
|
|
result = self.client_get("/json/users/othello@zulip.com/presence")
|
2022-06-07 01:37:01 +02:00
|
|
|
result_dict = self.assert_json_success(result)
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(set(result_dict["presence"].keys()), {"website", "aggregated"})
|
2021-01-04 19:36:00 +01:00
|
|
|
self.assertEqual(set(result_dict["presence"]["website"].keys()), {"status", "timestamp"})
|
|
|
|
|
|
|
|
result = self.client_get(f"/json/users/{othello.id}/presence")
|
2022-06-07 01:37:01 +02:00
|
|
|
result_dict = self.assert_json_success(result)
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(set(result_dict["presence"].keys()), {"website", "aggregated"})
|
2021-02-12 08:20:45 +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:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("othello")
|
2017-03-31 01:46:45 +02:00
|
|
|
req = dict(
|
2021-02-12 08:20:45 +01:00
|
|
|
status="active",
|
|
|
|
ping_only="true",
|
2017-03-31 01:46:45 +02:00
|
|
|
)
|
|
|
|
result = self.client_post("/json/users/me/presence", req)
|
2022-06-07 01:37:01 +02:00
|
|
|
self.assertEqual(self.assert_json_success(result)["msg"], "")
|
2017-03-02 09:52:17 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-03-02 09:52:17 +01:00
|
|
|
class UserPresenceAggregationTests(ZulipTestCase):
|
2021-02-12 08:19:30 +01:00
|
|
|
def _send_presence_for_aggregated_tests(
|
2023-11-19 19:45:19 +01:00
|
|
|
self, user: UserProfile, status: str, validate_time: datetime
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> Dict[str, Dict[str, Any]]:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2020-06-11 16:03:47 +02:00
|
|
|
# First create some initial, old presence to avoid the details of the edge case of initial
|
|
|
|
# presence creation messing with the intended setup.
|
2023-11-19 19:45:19 +01:00
|
|
|
with time_machine.travel((validate_time - timedelta(days=365)), tick=False):
|
2020-06-11 16:03:47 +02:00
|
|
|
self.client_post("/json/users/me/presence", {"status": status})
|
|
|
|
|
2023-11-19 19:45:19 +01:00
|
|
|
with time_machine.travel((validate_time - timedelta(seconds=5)), tick=False):
|
2021-02-12 08:20:45 +01:00
|
|
|
self.client_post("/json/users/me/presence", {"status": status})
|
2023-11-19 19:45:19 +01:00
|
|
|
with time_machine.travel((validate_time - timedelta(seconds=2)), tick=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
self.api_post(
|
|
|
|
user,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
{"status": status},
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipAndroid/1.0",
|
|
|
|
)
|
2023-11-19 19:45:19 +01:00
|
|
|
with time_machine.travel((validate_time - timedelta(seconds=7)), tick=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
latest_result = self.api_post(
|
|
|
|
user,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
{"status": status},
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipIOS/1.0",
|
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
latest_result_dict = self.assert_json_success(latest_result)
|
2017-03-18 10:41:57 +01:00
|
|
|
self.assertDictEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
latest_result_dict["presences"][user.email]["aggregated"],
|
2017-03-18 10:41:57 +01:00
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"status": status,
|
2023-11-19 19:45:19 +01:00
|
|
|
"timestamp": datetime_to_timestamp(validate_time - timedelta(seconds=5)),
|
2020-06-11 16:03:47 +02:00
|
|
|
# We no longer store the client information, but keep the field in these dicts,
|
|
|
|
# with the value 'website' for backwards compatibility.
|
|
|
|
"client": "website",
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2017-03-18 10:41:57 +01:00
|
|
|
)
|
2020-03-12 14:17:25 +01:00
|
|
|
|
2020-06-09 00:25:09 +02:00
|
|
|
result = self.client_get(f"/json/users/{user.email}/presence")
|
2022-06-07 01:37:01 +02:00
|
|
|
return self.assert_json_success(result)
|
2017-03-02 09:52:17 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_aggregated_info(self) -> None:
|
2020-03-10 11:48:26 +01:00
|
|
|
user = self.example_user("othello")
|
2023-11-19 19:45:19 +01:00
|
|
|
offset = timedelta(seconds=settings.PRESENCE_UPDATE_MIN_FREQ_SECONDS + 1)
|
2020-06-11 16:03:47 +02:00
|
|
|
validate_time = timezone_now() - offset
|
2021-02-12 08:20:45 +01:00
|
|
|
self._send_presence_for_aggregated_tests(user, "active", validate_time)
|
2023-11-28 19:16:58 +01:00
|
|
|
with time_machine.travel((validate_time + offset), tick=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
user,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
{"status": "active"},
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipTestDev/1.0",
|
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
result_dict = self.assert_json_success(result)
|
2017-03-18 10:41:57 +01:00
|
|
|
self.assertDictEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
result_dict["presences"][user.email]["aggregated"],
|
2017-03-18 10:41:57 +01:00
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"status": "active",
|
2020-06-11 16:03:47 +02:00
|
|
|
"timestamp": datetime_to_timestamp(validate_time + offset),
|
|
|
|
"client": "website", # This fields is no longer used and is permamenently set to 'website'.
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2017-03-18 10:41:57 +01:00
|
|
|
)
|
|
|
|
|
2023-01-02 20:50:23 +01:00
|
|
|
def test_aggregated_presence_active(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("othello")
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(user, "active", validate_time)
|
2017-03-02 09:52:17 +01:00
|
|
|
self.assertDictEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
result_dict["presence"]["aggregated"],
|
2017-03-02 09:52:17 +01:00
|
|
|
{
|
|
|
|
"status": "active",
|
2023-11-19 19:45:19 +01:00
|
|
|
"timestamp": datetime_to_timestamp(validate_time - timedelta(seconds=5)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2017-03-02 09:52:17 +01:00
|
|
|
)
|
|
|
|
|
2023-01-02 20:50:23 +01:00
|
|
|
def test_aggregated_presence_idle(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("othello")
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(user, "idle", validate_time)
|
2017-03-02 09:52:17 +01:00
|
|
|
self.assertDictEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
result_dict["presence"]["aggregated"],
|
2017-03-02 09:52:17 +01:00
|
|
|
{
|
|
|
|
"status": "idle",
|
2023-11-19 19:45:19 +01:00
|
|
|
"timestamp": datetime_to_timestamp(validate_time - timedelta(seconds=5)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2017-03-02 09:52:17 +01:00
|
|
|
)
|
|
|
|
|
2023-01-02 20:50:23 +01:00
|
|
|
def test_aggregated_presence_mixed(self) -> None:
|
2020-03-10 11:48:26 +01:00
|
|
|
user = self.example_user("othello")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2020-06-11 16:03:47 +02:00
|
|
|
self._send_presence_for_aggregated_tests(user, "idle", validate_time)
|
2023-11-19 19:45:19 +01:00
|
|
|
with time_machine.travel((validate_time - timedelta(seconds=3)), tick=False):
|
2020-06-11 16:03:47 +02:00
|
|
|
result_dict = self.api_post(
|
2021-02-12 08:19:30 +01:00
|
|
|
user,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
{"status": "active"},
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipTestDev/1.0",
|
2020-06-11 16:03:47 +02:00
|
|
|
).json()
|
|
|
|
|
2017-03-02 09:52:17 +01:00
|
|
|
self.assertDictEqual(
|
2020-06-11 16:03:47 +02:00
|
|
|
result_dict["presences"][user.email]["aggregated"],
|
2017-03-02 09:52:17 +01:00
|
|
|
{
|
2020-06-11 16:03:47 +02:00
|
|
|
"client": "website",
|
|
|
|
"status": "active",
|
2023-11-19 19:45:19 +01:00
|
|
|
"timestamp": datetime_to_timestamp(validate_time - timedelta(seconds=3)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2017-03-02 09:52:17 +01:00
|
|
|
)
|
2017-03-06 12:25:37 +01:00
|
|
|
|
2023-01-02 20:50:23 +01:00
|
|
|
def test_aggregated_presence_offline(self) -> None:
|
2020-03-10 11:48:26 +01:00
|
|
|
user = self.example_user("othello")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-04-15 04:03:56 +02:00
|
|
|
validate_time = timezone_now()
|
2020-06-11 16:03:47 +02:00
|
|
|
result_dict = self._send_presence_for_aggregated_tests(user, "idle", validate_time)
|
|
|
|
|
2023-11-28 19:16:58 +01:00
|
|
|
with time_machine.travel(
|
2023-11-19 19:45:19 +01:00
|
|
|
(validate_time + timedelta(seconds=settings.OFFLINE_THRESHOLD_SECS + 1)),
|
2023-11-28 19:16:58 +01:00
|
|
|
tick=False,
|
2020-06-11 16:03:47 +02:00
|
|
|
):
|
|
|
|
# After settings.OFFLINE_THRESHOLD_SECS + 1 this generated, recent presence data
|
|
|
|
# will count as offline.
|
|
|
|
result = self.client_get(f"/json/users/{user.email}/presence")
|
|
|
|
result_dict = self.assert_json_success(result)
|
|
|
|
|
2017-03-06 12:25:37 +01:00
|
|
|
self.assertDictEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
result_dict["presence"]["aggregated"],
|
2017-03-06 12:25:37 +01:00
|
|
|
{
|
|
|
|
"status": "offline",
|
2023-11-19 19:45:19 +01:00
|
|
|
"timestamp": datetime_to_timestamp(validate_time - timedelta(seconds=5)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2017-03-06 12:25:37 +01:00
|
|
|
)
|
2018-10-14 19:22:04 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-10-14 19:22:04 +02:00
|
|
|
class GetRealmStatusesTest(ZulipTestCase):
|
|
|
|
def test_get_statuses(self) -> None:
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
# Set up the test by simulating users reporting their presence data.
|
2020-02-02 17:29:05 +01:00
|
|
|
othello = self.example_user("othello")
|
|
|
|
hamlet = self.example_user("hamlet")
|
2023-10-17 12:56:39 +02:00
|
|
|
self.example_user("cordelia")
|
2020-02-02 17:29:05 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
othello,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status="active"),
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipAndroid/1.0",
|
|
|
|
)
|
2018-10-14 19:22:04 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
hamlet,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status="idle"),
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0",
|
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email, othello.email})
|
2020-02-02 17:29:05 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
hamlet,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status="active", slim_presence="true"),
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0",
|
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(set(json["presences"].keys()), {str(hamlet.id), str(othello.id)})
|
2018-10-14 19:22:04 +02:00
|
|
|
|
|
|
|
# Check that a bot can fetch the presence data for the realm.
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_get(self.example_user("default_bot"), "/api/v1/realm/presence")
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email, othello.email})
|
2020-05-01 20:39:26 +02:00
|
|
|
|
2023-10-17 12:56:39 +02:00
|
|
|
# Check that polonius cannot fetch presence data for inaccessible user Othello
|
|
|
|
# if CAN_ACCESS_ALL_USERS_GROUP_LIMITS_PRESENCE is set to True.
|
|
|
|
self.set_up_db_for_testing_user_access()
|
|
|
|
polonius = self.example_user("polonius")
|
|
|
|
with self.settings(CAN_ACCESS_ALL_USERS_GROUP_LIMITS_PRESENCE=True):
|
|
|
|
result = self.api_get(polonius, "/api/v1/realm/presence")
|
|
|
|
json = self.assert_json_success(result)
|
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email})
|
|
|
|
|
|
|
|
result = self.api_get(polonius, "/api/v1/realm/presence")
|
|
|
|
json = self.assert_json_success(result)
|
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email, othello.email})
|
|
|
|
|
|
|
|
with self.settings(CAN_ACCESS_ALL_USERS_GROUP_LIMITS_PRESENCE=True):
|
|
|
|
result = self.api_post(
|
|
|
|
polonius,
|
|
|
|
"/api/v1/users/me/presence",
|
|
|
|
dict(status="idle"),
|
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0",
|
|
|
|
)
|
|
|
|
json = self.assert_json_success(result)
|
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email, polonius.email})
|
|
|
|
|
|
|
|
result = self.api_post(
|
|
|
|
polonius,
|
|
|
|
"/api/v1/users/me/presence",
|
|
|
|
dict(status="idle"),
|
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0",
|
|
|
|
)
|
|
|
|
json = self.assert_json_success(result)
|
|
|
|
self.assertEqual(
|
|
|
|
set(json["presences"].keys()), {hamlet.email, polonius.email, othello.email}
|
|
|
|
)
|
|
|
|
|
2020-05-01 20:39:26 +02:00
|
|
|
def test_presence_disabled(self) -> None:
|
|
|
|
# Disable presence status and test whether the presence
|
|
|
|
# is reported or not.
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
othello.presence_enabled = False
|
|
|
|
hamlet.presence_enabled = True
|
2021-02-12 08:20:45 +01:00
|
|
|
othello.save(update_fields=["presence_enabled"])
|
|
|
|
hamlet.save(update_fields=["presence_enabled"])
|
2020-05-01 20:39:26 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
othello,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status="active"),
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipAndroid/1.0",
|
|
|
|
)
|
2020-05-01 20:39:26 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
hamlet,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status="idle"),
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0",
|
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2020-05-01 20:39:26 +02:00
|
|
|
|
|
|
|
# Othello's presence status is disabled so it won't be reported.
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(set(json["presences"].keys()), {hamlet.email})
|
2020-05-01 20:39:26 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.api_post(
|
|
|
|
hamlet,
|
|
|
|
"/api/v1/users/me/presence",
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status="active", slim_presence="true"),
|
2021-02-12 08:19:30 +01:00
|
|
|
HTTP_USER_AGENT="ZulipDesktop/1.0",
|
|
|
|
)
|
2022-06-07 01:37:01 +02:00
|
|
|
json = self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(set(json["presences"].keys()), {str(hamlet.id)})
|
2020-06-11 16:03:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FormatLegacyPresenceDictTest(ZulipTestCase):
|
|
|
|
def test_format_legacy_presence_dict(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
now = timezone_now()
|
|
|
|
recently = now - timedelta(seconds=50)
|
|
|
|
a_while_ago = now - timedelta(minutes=3)
|
|
|
|
presence = UserPresence(
|
|
|
|
user_profile=hamlet, realm=hamlet.realm, last_active_time=now, last_connected_time=now
|
|
|
|
)
|
2023-04-08 15:52:48 +02:00
|
|
|
assert presence.last_active_time is not None and presence.last_connected_time is not None
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(
|
2023-04-04 19:35:06 +02:00
|
|
|
format_legacy_presence_dict(presence.last_active_time, presence.last_connected_time),
|
2020-06-11 16:03:47 +02:00
|
|
|
dict(
|
|
|
|
client="website",
|
|
|
|
status=UserPresence.LEGACY_STATUS_ACTIVE,
|
|
|
|
timestamp=datetime_to_timestamp(now),
|
|
|
|
pushable=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
presence = UserPresence(
|
|
|
|
user_profile=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
last_active_time=recently,
|
|
|
|
last_connected_time=now,
|
|
|
|
)
|
2023-04-08 15:52:48 +02:00
|
|
|
assert presence.last_active_time is not None and presence.last_connected_time is not None
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(
|
2023-04-04 19:35:06 +02:00
|
|
|
format_legacy_presence_dict(presence.last_active_time, presence.last_connected_time),
|
2020-06-11 16:03:47 +02:00
|
|
|
dict(
|
|
|
|
client="website",
|
|
|
|
status=UserPresence.LEGACY_STATUS_ACTIVE,
|
|
|
|
timestamp=datetime_to_timestamp(recently),
|
|
|
|
pushable=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
presence = UserPresence(
|
|
|
|
user_profile=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
last_active_time=a_while_ago,
|
|
|
|
last_connected_time=now,
|
|
|
|
)
|
2023-04-08 15:52:48 +02:00
|
|
|
assert presence.last_active_time is not None and presence.last_connected_time is not None
|
2020-06-11 16:03:47 +02:00
|
|
|
self.assertEqual(
|
2023-04-04 19:35:06 +02:00
|
|
|
format_legacy_presence_dict(presence.last_active_time, presence.last_connected_time),
|
2020-06-11 16:03:47 +02:00
|
|
|
dict(
|
|
|
|
client="website",
|
|
|
|
status=UserPresence.LEGACY_STATUS_IDLE,
|
|
|
|
timestamp=datetime_to_timestamp(now),
|
|
|
|
pushable=False,
|
|
|
|
),
|
|
|
|
)
|