2023-10-17 12:56:39 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2020-06-11 00:54:34 +02:00
|
|
|
|
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
|
2022-09-16 18:15:15 +02:00
|
|
|
from zerver.lib.user_status import UserInfoDict, get_user_status_dict, update_user_status
|
2023-12-15 04:33:19 +01:00
|
|
|
from zerver.models import UserProfile, UserStatus
|
|
|
|
from zerver.models.clients import get_client
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
|
2023-10-17 12:56:39 +02:00
|
|
|
def user_status_info(user: UserProfile, acting_user: Optional[UserProfile] = None) -> UserInfoDict:
|
|
|
|
if acting_user is None:
|
|
|
|
acting_user = user
|
|
|
|
user_dict = get_user_status_dict(user.realm, acting_user)
|
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
|
|
|
hamlet = self.example_user("hamlet")
|
2018-12-17 16:19:18 +01:00
|
|
|
|
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,
|
2022-09-22 12:21:00 +02:00
|
|
|
status_text="working",
|
2021-06-22 18:42:31 +02:00
|
|
|
emoji_name=None,
|
|
|
|
emoji_code=None,
|
|
|
|
reaction_type=None,
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client1.id,
|
|
|
|
)
|
|
|
|
|
2022-09-22 12:21:00 +02:00
|
|
|
self.assertEqual(
|
|
|
|
user_status_info(hamlet),
|
|
|
|
dict(
|
|
|
|
status_text="working",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count()
|
|
|
|
self.assertEqual(rec_count, 1)
|
2018-12-17 16:19:18 +01:00
|
|
|
|
|
|
|
# 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,
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="out to lunch",
|
2021-06-22 18:42:31 +02:00
|
|
|
emoji_name="car",
|
|
|
|
emoji_code="1f697",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
2018-12-17 16:19:18 +01:00
|
|
|
client_id=client2.id,
|
|
|
|
)
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_info(hamlet),
|
2021-06-22 18:42:31 +02:00
|
|
|
dict(
|
|
|
|
status_text="out to lunch",
|
|
|
|
emoji_name="car",
|
|
|
|
emoji_code="1f697",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
|
|
|
),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2018-12-17 16:19:18 +01:00
|
|
|
rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count()
|
|
|
|
self.assertEqual(rec_count, 1)
|
|
|
|
|
2021-06-22 18:42:31 +02:00
|
|
|
# Setting status_text and emoji_info 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 19:06:03 +01:00
|
|
|
status_text=None,
|
2021-06-22 18:42:31 +02:00
|
|
|
emoji_name=None,
|
|
|
|
emoji_code=None,
|
|
|
|
reaction_type=None,
|
2019-01-21 19:06:03 +01:00
|
|
|
client_id=client2.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_info(hamlet),
|
2021-06-22 18:42:31 +02:00
|
|
|
dict(
|
|
|
|
status_text="out to lunch",
|
|
|
|
emoji_name="car",
|
|
|
|
emoji_code="1f697",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
|
|
|
),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2021-06-22 18:42:31 +02:00
|
|
|
# Clear the status_text and emoji_info now.
|
2019-01-21 19:06:03 +01:00
|
|
|
update_user_status(
|
|
|
|
user_profile_id=hamlet.id,
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="",
|
2021-06-22 18:42:31 +02:00
|
|
|
emoji_name="",
|
|
|
|
emoji_code="",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
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(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_info(hamlet),
|
2020-09-02 08:14:51 +02:00
|
|
|
{},
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2022-09-22 12:21:00 +02:00
|
|
|
# Set Hamlet to 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,
|
2021-02-12 08:20:45 +01:00
|
|
|
status_text="in a meeting",
|
2021-06-22 18:42:31 +02:00
|
|
|
emoji_name=None,
|
|
|
|
emoji_code=None,
|
|
|
|
reaction_type=None,
|
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(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_info(hamlet),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(status_text="in a meeting"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2023-10-17 12:56:39 +02:00
|
|
|
# Test user status for inaccessible users.
|
|
|
|
self.set_up_db_for_testing_user_access()
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
update_user_status(
|
|
|
|
user_profile_id=cordelia.id,
|
|
|
|
status_text="on vacation",
|
|
|
|
emoji_name=None,
|
|
|
|
emoji_code=None,
|
|
|
|
reaction_type=None,
|
|
|
|
client_id=client2.id,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
user_status_info(hamlet, self.example_user("polonius")),
|
|
|
|
dict(status_text="in a meeting"),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
user_status_info(cordelia, self.example_user("polonius")),
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
|
2021-05-27 16:11:50 +02:00
|
|
|
def update_status_and_assert_event(
|
2022-09-22 11:56:58 +02:00
|
|
|
self, payload: Dict[str, Any], expected_event: Dict[str, Any], num_events: int = 1
|
2021-05-27 16:11:50 +02:00
|
|
|
) -> None:
|
2023-04-05 13:36:01 +02:00
|
|
|
with self.capture_send_event_calls(expected_num_events=num_events) as events:
|
2021-05-27 16:11:50 +02:00
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
|
|
|
self.assert_json_success(result)
|
2021-05-27 16:15:34 +02:00
|
|
|
self.assertEqual(events[0]["event"], expected_event)
|
2021-05-27 16:11:50 +02:00
|
|
|
|
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")
|
2023-10-17 12:56:39 +02:00
|
|
|
realm = hamlet.realm
|
2018-12-17 22:04:07 +01:00
|
|
|
|
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
|
|
|
|
2021-06-22 18:42:31 +02:00
|
|
|
# Try to omit emoji_name parameter but passing emoji_code --this should be an error.
|
|
|
|
payload = {"status_text": "In a meeting", "emoji_code": "1f4bb"}
|
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
|
|
|
self.assert_json_error(
|
|
|
|
result, "Client must pass emoji_name if they pass either emoji_code or reaction_type."
|
|
|
|
)
|
|
|
|
|
2022-09-22 12:21:00 +02:00
|
|
|
# Invalid emoji requests fail.
|
2021-06-22 18:42:31 +02:00
|
|
|
payload = {"status_text": "In a meeting", "emoji_code": "1f4bb", "emoji_name": "invalid"}
|
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
|
|
|
self.assert_json_error(result, "Emoji 'invalid' does not exist")
|
|
|
|
|
|
|
|
payload = {"status_text": "In a meeting", "emoji_code": "1f4bb", "emoji_name": "car"}
|
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
|
|
|
self.assert_json_error(result, "Invalid emoji name.")
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"status_text": "In a meeting",
|
|
|
|
"emoji_code": "1f4bb",
|
|
|
|
"emoji_name": "car",
|
|
|
|
"reaction_type": "realm_emoji",
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/users/me/status", payload)
|
|
|
|
self.assert_json_error(result, "Invalid custom emoji.")
|
|
|
|
|
2022-09-22 12:21:00 +02:00
|
|
|
# Try a long message--this should be an error.
|
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)")
|
|
|
|
|
2022-09-22 12:21:00 +02:00
|
|
|
# Set "away" with a normal length message.
|
2021-05-27 16:11:50 +02:00
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(
|
|
|
|
away=orjson.dumps(True).decode(),
|
|
|
|
status_text="on vacation",
|
|
|
|
),
|
|
|
|
expected_event=dict(
|
|
|
|
type="user_status", user_id=hamlet.id, away=True, status_text="on vacation"
|
|
|
|
),
|
2022-09-22 11:56:58 +02:00
|
|
|
num_events=4,
|
2018-12-17 22:04:07 +01:00
|
|
|
)
|
2019-01-21 19:06:03 +01:00
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_info(hamlet),
|
2021-06-22 18:42:31 +02:00
|
|
|
dict(away=True, status_text="on vacation"),
|
|
|
|
)
|
|
|
|
|
2022-09-22 12:21:00 +02:00
|
|
|
# Setting away is a deprecated way of accessing a user's presence_enabled
|
|
|
|
# setting. Can be removed when clients migrate "away" (also referred to as
|
|
|
|
# "unavailable") feature to directly use the presence_enabled setting.
|
|
|
|
user = UserProfile.objects.get(id=hamlet.id)
|
|
|
|
self.assertEqual(user.presence_enabled, False)
|
|
|
|
|
2021-06-22 18:42:31 +02:00
|
|
|
# Server should fill emoji_code and reaction_type by emoji_name.
|
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(
|
|
|
|
emoji_name="car",
|
|
|
|
),
|
|
|
|
expected_event=dict(
|
|
|
|
type="user_status",
|
|
|
|
user_id=hamlet.id,
|
|
|
|
emoji_name="car",
|
|
|
|
emoji_code="1f697",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_info(hamlet),
|
2021-06-22 18:42:31 +02:00
|
|
|
dict(
|
|
|
|
away=True,
|
|
|
|
status_text="on vacation",
|
|
|
|
emoji_name="car",
|
|
|
|
emoji_code="1f697",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Server should remove emoji_code and reaction_type if emoji_name is empty.
|
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(
|
|
|
|
emoji_name="",
|
|
|
|
),
|
|
|
|
expected_event=dict(
|
|
|
|
type="user_status",
|
|
|
|
user_id=hamlet.id,
|
|
|
|
emoji_name="",
|
|
|
|
emoji_code="",
|
|
|
|
reaction_type=UserStatus.UNICODE_EMOJI,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_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.
|
2021-05-27 16:11:50 +02:00
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(away=orjson.dumps(False).decode()),
|
|
|
|
expected_event=dict(type="user_status", user_id=hamlet.id, away=False),
|
2022-09-22 11:56:58 +02:00
|
|
|
num_events=4,
|
2018-12-17 22:04:07 +01:00
|
|
|
)
|
2022-09-22 12:21:00 +02:00
|
|
|
self.assertEqual(
|
|
|
|
user_status_info(hamlet),
|
|
|
|
dict(status_text="on vacation"),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Setting away is a deprecated way of accessing a user's presence_enabled
|
|
|
|
# setting. Can be removed when clients migrate "away" (also referred to as
|
|
|
|
# "unavailable") feature to directly use the presence_enabled setting.
|
|
|
|
user = UserProfile.objects.get(id=hamlet.id)
|
|
|
|
self.assertEqual(user.presence_enabled, True)
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
# And now just update your info.
|
|
|
|
# The server will trim the whitespace here.
|
2021-05-27 16:11:50 +02:00
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(status_text=" in office "),
|
|
|
|
expected_event=dict(type="user_status", user_id=hamlet.id, status_text="in office"),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_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-05-27 16:11:50 +02:00
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(status_text=""),
|
|
|
|
expected_event=dict(type="user_status", user_id=hamlet.id, status_text=""),
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2023-10-17 12:56:39 +02:00
|
|
|
get_user_status_dict(realm=realm, user_profile=hamlet),
|
2019-01-21 19:06:03 +01:00
|
|
|
{},
|
|
|
|
)
|
2021-01-20 19:14:53 +01:00
|
|
|
|
|
|
|
# Turn on "away" status again.
|
2021-05-27 16:11:50 +02:00
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(away=orjson.dumps(True).decode()),
|
|
|
|
expected_event=dict(type="user_status", user_id=hamlet.id, away=True),
|
2022-09-22 11:56:58 +02:00
|
|
|
num_events=4,
|
2021-01-20 19:14:53 +01:00
|
|
|
)
|
2022-09-22 12:21:00 +02:00
|
|
|
|
|
|
|
# Setting away is a deprecated way of accessing a user's presence_enabled
|
|
|
|
# setting. Can be removed when clients migrate "away" (also referred to as
|
|
|
|
# "unavailable") feature to directly use the presence_enabled setting.
|
|
|
|
user = UserProfile.objects.get(id=hamlet.id)
|
|
|
|
self.assertEqual(user.presence_enabled, False)
|
2021-01-20 19:14:53 +01:00
|
|
|
|
|
|
|
# And set status text while away.
|
2021-05-27 16:11:50 +02:00
|
|
|
self.update_status_and_assert_event(
|
|
|
|
payload=dict(status_text=" at the beach "),
|
|
|
|
expected_event=dict(type="user_status", user_id=hamlet.id, status_text="at the beach"),
|
2021-01-20 19:14:53 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-09-16 18:15:15 +02:00
|
|
|
user_status_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
|
|
|
)
|