2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, Set
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2018-12-17 22:04:07 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.test_helpers import EventInfo, capture_event
|
|
|
|
from zerver.lib.user_status import get_user_info_dict, update_user_status
|
|
|
|
from zerver.models import UserProfile, UserStatus, get_client
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
def get_away_user_ids(realm_id: int) -> Set[int]:
|
|
|
|
user_dict = get_user_info_dict(realm_id)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return {int(user_id) for user_id in user_dict if user_dict[user_id].get("away")}
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
def user_info(user: UserProfile) -> Dict[str, Any]:
|
|
|
|
user_dict = get_user_info_dict(user.realm_id)
|
2020-09-02 08:14:51 +02:00
|
|
|
return user_dict.get(str(user.id), {})
|
2018-12-17 22:04:07 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-12-17 16:19:18 +01:00
|
|
|
class UserStatusTest(ZulipTestCase):
|
|
|
|
def test_basics(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
king_lear = self.lear_user("king")
|
2018-12-17 16:19:18 +01:00
|
|
|
|
|
|
|
realm_id = hamlet.realm_id
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, set())
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
client1 = get_client("web")
|
|
|
|
client2 = get_client("ZT")
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=hamlet.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.AWAY,
|
2019-01-21 19:06:03 +01:00
|
|
|
status_text=None,
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client1.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, {hamlet.id})
|
|
|
|
|
|
|
|
# Test that second client just updates
|
|
|
|
# the record. We only store one record
|
|
|
|
# per user. The user's status transcends
|
|
|
|
# clients; we only store the client for
|
|
|
|
# reference and to maybe reconcile timeout
|
|
|
|
# situations.
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=hamlet.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.AWAY,
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="out to lunch",
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client2.id,
|
|
|
|
)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(away=True, status_text="out to lunch"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2018-12-17 16:19:18 +01:00
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, {hamlet.id})
|
|
|
|
|
|
|
|
rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count()
|
|
|
|
self.assertEqual(rec_count, 1)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
# Setting status_text to None causes it be ignored.
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=hamlet.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.NORMAL,
|
2019-01-21 19:06:03 +01:00
|
|
|
status_text=None,
|
|
|
|
client_id=client2.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status_text="out to lunch"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Clear the status_text now.
|
|
|
|
update_user_status(
|
|
|
|
user_profile_id=hamlet.id,
|
|
|
|
status=None,
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="",
|
2019-01-21 18:19:59 +01:00
|
|
|
client_id=client2.id,
|
2018-12-17 16:19:18 +01:00
|
|
|
)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2020-09-02 08:14:51 +02:00
|
|
|
{},
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2018-12-17 16:19:18 +01:00
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, set())
|
|
|
|
|
|
|
|
# Now set away status for three different users across
|
|
|
|
# two realms.
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=hamlet.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.AWAY,
|
2019-01-21 19:06:03 +01:00
|
|
|
status_text=None,
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client1.id,
|
|
|
|
)
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=cordelia.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.AWAY,
|
2019-01-21 19:06:03 +01:00
|
|
|
status_text=None,
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client2.id,
|
|
|
|
)
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=king_lear.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.AWAY,
|
2019-01-21 19:06:03 +01:00
|
|
|
status_text=None,
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client2.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, {cordelia.id, hamlet.id})
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=king_lear.realm.id)
|
|
|
|
self.assertEqual(away_user_ids, {king_lear.id})
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
# Set Hamlet to NORMAL but in a meeting.
|
2019-01-21 18:19:59 +01:00
|
|
|
update_user_status(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=hamlet.id,
|
2019-01-21 18:19:59 +01:00
|
|
|
status=UserStatus.NORMAL,
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="in a meeting",
|
2019-01-21 18:19:59 +01:00
|
|
|
client_id=client2.id,
|
2018-12-17 16:19:18 +01:00
|
|
|
)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status_text="in a meeting"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2018-12-17 16:19:18 +01:00
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, {cordelia.id})
|
2018-12-17 22:04:07 +01:00
|
|
|
|
|
|
|
def test_endpoints(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2018-12-17 22:04:07 +01:00
|
|
|
realm_id = hamlet.realm_id
|
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(hamlet)
|
2018-12-17 22:04:07 +01:00
|
|
|
|
|
|
|
# Try to omit parameter--this should be an error.
|
2020-09-02 08:14:51 +02:00
|
|
|
payload: Dict[str, Any] = {}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assert_json_error(result, "Client did not pass any new values.")
|
2018-12-17 22:04:07 +01:00
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
# Try a long message.
|
2021-02-12 08:20:45 +01:00
|
|
|
long_text = "x" * 61
|
2019-01-21 19:06:03 +01:00
|
|
|
payload = dict(status_text=long_text)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assert_json_error(result, "status_text is too long (limit: 60 characters)")
|
|
|
|
|
|
|
|
payload = dict(
|
2020-08-07 01:09:47 +02:00
|
|
|
away=orjson.dumps(True).decode(),
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="on vacation",
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
2018-12-17 22:04:07 +01:00
|
|
|
|
|
|
|
event_info = EventInfo()
|
|
|
|
with capture_event(event_info):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2018-12-17 22:04:07 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event_info.payload,
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="user_status", user_id=hamlet.id, away=True, status_text="on vacation"),
|
2018-12-17 22:04:07 +01:00
|
|
|
)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(away=True, status_text="on vacation"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
2018-12-17 22:04:07 +01:00
|
|
|
|
|
|
|
# Now revoke "away" status.
|
2020-08-07 01:09:47 +02:00
|
|
|
payload = dict(away=orjson.dumps(False).decode())
|
2018-12-17 22:04:07 +01:00
|
|
|
|
|
|
|
event_info = EventInfo()
|
|
|
|
with capture_event(event_info):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2018-12-17 22:04:07 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event_info.payload,
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="user_status", user_id=hamlet.id, away=False),
|
2018-12-17 22:04:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, set())
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
# And now just update your info.
|
|
|
|
# The server will trim the whitespace here.
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = dict(status_text=" in office ")
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
event_info = EventInfo()
|
|
|
|
with capture_event(event_info):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event_info.payload,
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="user_status", user_id=hamlet.id, status_text="in office"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status_text="in office"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# And finally clear your info.
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = dict(status_text="")
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
event_info = EventInfo()
|
|
|
|
with capture_event(event_info):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event_info.payload,
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="user_status", user_id=hamlet.id, status_text=""),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
get_user_info_dict(realm_id=realm_id),
|
|
|
|
{},
|
|
|
|
)
|
2021-01-20 19:14:53 +01:00
|
|
|
|
|
|
|
# Turn on "away" status again.
|
|
|
|
payload = dict(away=orjson.dumps(True).decode())
|
|
|
|
|
|
|
|
event_info = EventInfo()
|
|
|
|
with capture_event(event_info):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2021-01-20 19:14:53 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event_info.payload,
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="user_status", user_id=hamlet.id, away=True),
|
2021-01-20 19:14:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, {hamlet.id})
|
|
|
|
|
|
|
|
# And set status text while away.
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = dict(status_text=" at the beach ")
|
2021-01-20 19:14:53 +01:00
|
|
|
|
|
|
|
event_info = EventInfo()
|
|
|
|
with capture_event(event_info):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
2021-01-20 19:14:53 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event_info.payload,
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="user_status", user_id=hamlet.id, status_text="at the beach"),
|
2021-01-20 19:14:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
user_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status_text="at the beach", away=True),
|
2021-01-20 19:14:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
away_user_ids = get_away_user_ids(realm_id=realm_id)
|
|
|
|
self.assertEqual(away_user_ids, {hamlet.id})
|