2021-05-11 13:55:49 +02:00
|
|
|
from typing import AbstractSet
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2019-03-14 04:55:41 +01:00
|
|
|
|
2017-07-31 05:25:36 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2022-04-14 23:35:09 +02:00
|
|
|
from zerver.actions.alert_words import do_add_alert_words
|
2017-07-30 19:48:49 +02:00
|
|
|
from zerver.lib.soft_deactivation import (
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_auto_soft_deactivate_users,
|
|
|
|
do_catch_up_soft_deactivated_users,
|
|
|
|
do_soft_activate_users,
|
2017-07-30 19:48:49 +02:00
|
|
|
do_soft_deactivate_user,
|
2017-07-31 05:25:36 +02:00
|
|
|
do_soft_deactivate_users,
|
2019-03-12 03:59:02 +01:00
|
|
|
get_soft_deactivated_users_for_catch_up,
|
2020-06-11 00:54:34 +02:00
|
|
|
get_users_for_soft_deactivation,
|
2020-07-07 01:19:40 +02:00
|
|
|
reactivate_user_if_soft_deactivated,
|
2017-07-30 19:48:49 +02:00
|
|
|
)
|
2021-05-11 13:55:49 +02:00
|
|
|
from zerver.lib.stream_subscription import get_subscriptions_for_send_message
|
2017-07-31 05:25:36 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2022-10-15 22:47:40 +02:00
|
|
|
from zerver.lib.test_helpers import get_subscription, get_user_messages, make_client
|
2020-07-07 01:19:40 +02:00
|
|
|
from zerver.models import (
|
2021-05-11 13:55:49 +02:00
|
|
|
AlertWord,
|
2020-07-07 01:19:40 +02:00
|
|
|
Client,
|
|
|
|
Message,
|
|
|
|
RealmAuditLog,
|
|
|
|
Stream,
|
|
|
|
UserActivity,
|
|
|
|
UserMessage,
|
|
|
|
UserProfile,
|
|
|
|
get_realm,
|
|
|
|
get_stream,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
logger_string = "zulip.soft_deactivation"
|
2017-07-30 19:48:49 +02:00
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
class UserSoftDeactivationTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_soft_deactivate_user(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2017-07-30 19:48:49 +02:00
|
|
|
self.assertFalse(user.long_term_idle)
|
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-14 04:55:41 +01:00
|
|
|
do_soft_deactivate_user(user)
|
2020-10-23 02:43:28 +02:00
|
|
|
self.assertEqual(m.output, [f"INFO:{logger_string}:Soft deactivated user {user.id}"])
|
2017-07-30 19:48:49 +02:00
|
|
|
|
|
|
|
user.refresh_from_db()
|
|
|
|
self.assertTrue(user.long_term_idle)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_soft_deactivate_users(self) -> None:
|
2017-07-30 19:48:49 +02:00
|
|
|
users = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.example_user("hamlet"),
|
|
|
|
self.example_user("iago"),
|
|
|
|
self.example_user("cordelia"),
|
2017-07-30 19:48:49 +02:00
|
|
|
]
|
|
|
|
for user in users:
|
|
|
|
self.assertFalse(user.long_term_idle)
|
|
|
|
|
2017-08-18 10:09:54 +02:00
|
|
|
# We are sending this message to ensure that users have at least
|
|
|
|
# one UserMessage row.
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_huddle_message(users[0], users)
|
2019-03-14 04:55:41 +01:00
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-14 04:55:41 +01:00
|
|
|
do_soft_deactivate_users(users)
|
2017-07-30 19:48:49 +02:00
|
|
|
|
2023-07-31 22:52:35 +02:00
|
|
|
log_output = [
|
|
|
|
*(f"INFO:{logger_string}:Soft deactivated user {user.id}" for user in users),
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of {len(users[:100])} users; {len(users[100:])} remain to process",
|
|
|
|
]
|
2020-08-09 14:15:58 +02:00
|
|
|
|
|
|
|
self.assertEqual(m.output, log_output)
|
|
|
|
|
2017-07-30 19:48:49 +02:00
|
|
|
for user in users:
|
|
|
|
user.refresh_from_db()
|
|
|
|
self.assertTrue(user.long_term_idle)
|
2017-07-31 05:25:36 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_users_for_soft_deactivation(self) -> None:
|
2017-07-31 05:25:36 +02:00
|
|
|
users = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.example_user("hamlet"),
|
|
|
|
self.example_user("iago"),
|
|
|
|
self.example_user("cordelia"),
|
|
|
|
self.example_user("ZOE"),
|
|
|
|
self.example_user("othello"),
|
|
|
|
self.example_user("prospero"),
|
|
|
|
self.example_user("aaron"),
|
|
|
|
self.example_user("polonius"),
|
|
|
|
self.example_user("desdemona"),
|
2020-12-22 15:46:00 +01:00
|
|
|
self.example_user("shiva"),
|
2017-07-31 05:25:36 +02:00
|
|
|
]
|
2021-02-12 08:20:45 +01:00
|
|
|
client, _ = Client.objects.get_or_create(name="website")
|
|
|
|
query = "/some/random/endpoint"
|
2017-07-31 05:25:36 +02:00
|
|
|
last_visit = timezone_now()
|
|
|
|
count = 150
|
|
|
|
for user_profile in UserProfile.objects.all():
|
|
|
|
UserActivity.objects.get_or_create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
client=client,
|
|
|
|
query=query,
|
|
|
|
count=count,
|
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,
|
2017-07-31 05:25:36 +02:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
filter_kwargs = dict(user_profile__realm=get_realm("zulip"))
|
2017-08-22 02:43:03 +02:00
|
|
|
users_to_deactivate = get_users_for_soft_deactivation(-1, filter_kwargs)
|
2017-07-31 05:25:36 +02:00
|
|
|
|
2020-12-22 15:46:00 +01:00
|
|
|
self.assert_length(users_to_deactivate, 10)
|
2017-07-31 05:25:36 +02:00
|
|
|
for user in users_to_deactivate:
|
|
|
|
self.assertTrue(user in users)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_soft_activate_users(self) -> None:
|
2017-07-31 05:25:36 +02:00
|
|
|
users = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.example_user("hamlet"),
|
|
|
|
self.example_user("iago"),
|
|
|
|
self.example_user("cordelia"),
|
2017-07-31 05:25:36 +02:00
|
|
|
]
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_huddle_message(users[0], users)
|
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-14 04:55:41 +01:00
|
|
|
do_soft_deactivate_users(users)
|
2020-08-09 14:15:58 +02:00
|
|
|
|
2023-07-31 22:52:35 +02:00
|
|
|
log_output = [
|
|
|
|
*(f"INFO:{logger_string}:Soft deactivated user {user.id}" for user in users),
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of {len(users[:100])} users; {len(users[100:])} remain to process",
|
|
|
|
]
|
2020-08-09 14:15:58 +02:00
|
|
|
self.assertEqual(m.output, log_output)
|
|
|
|
|
2017-07-31 05:25:36 +02:00
|
|
|
for user in users:
|
|
|
|
self.assertTrue(user.long_term_idle)
|
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-14 04:55:41 +01:00
|
|
|
do_soft_activate_users(users)
|
2017-07-31 05:25:36 +02:00
|
|
|
|
2023-07-31 22:52:35 +02:00
|
|
|
log_output = [f"INFO:{logger_string}:Soft reactivated user {user.id}" for user in users]
|
2020-08-09 14:15:58 +02:00
|
|
|
self.assertEqual(m.output, log_output)
|
|
|
|
|
2017-07-31 05:25:36 +02:00
|
|
|
for user in users:
|
|
|
|
user.refresh_from_db()
|
|
|
|
self.assertFalse(user.long_term_idle)
|
2019-03-12 03:59:02 +01:00
|
|
|
|
|
|
|
def test_get_users_for_catch_up(self) -> None:
|
|
|
|
users = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.example_user("hamlet"),
|
|
|
|
self.example_user("iago"),
|
|
|
|
self.example_user("cordelia"),
|
|
|
|
self.example_user("ZOE"),
|
|
|
|
self.example_user("othello"),
|
|
|
|
self.example_user("prospero"),
|
|
|
|
self.example_user("aaron"),
|
|
|
|
self.example_user("polonius"),
|
|
|
|
self.example_user("desdemona"),
|
2020-12-22 15:46:00 +01:00
|
|
|
self.example_user("shiva"),
|
2019-03-12 03:59:02 +01:00
|
|
|
]
|
|
|
|
for user_profile in UserProfile.objects.all():
|
|
|
|
user_profile.long_term_idle = True
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile.save(update_fields=["long_term_idle"])
|
2019-03-12 03:59:02 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
filter_kwargs = dict(realm=get_realm("zulip"))
|
2019-03-12 03:59:02 +01:00
|
|
|
users_to_catch_up = get_soft_deactivated_users_for_catch_up(filter_kwargs)
|
|
|
|
|
2020-12-22 15:46:00 +01:00
|
|
|
self.assert_length(users_to_catch_up, 10)
|
2019-03-12 03:59:02 +01:00
|
|
|
for user in users_to_catch_up:
|
|
|
|
self.assertTrue(user in users)
|
|
|
|
|
|
|
|
def test_do_catch_up_users(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = "Verona"
|
|
|
|
hamlet = self.example_user("hamlet")
|
2019-03-12 03:59:02 +01:00
|
|
|
users = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.example_user("iago"),
|
|
|
|
self.example_user("cordelia"),
|
2019-03-12 03:59:02 +01:00
|
|
|
]
|
2020-09-02 06:59:07 +02:00
|
|
|
all_users = [*users, hamlet]
|
2019-03-12 03:59:02 +01:00
|
|
|
for user in all_users:
|
|
|
|
self.subscribe(user, stream)
|
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-14 04:55:41 +01:00
|
|
|
do_soft_deactivate_users(users)
|
2020-08-09 14:15:58 +02:00
|
|
|
|
2023-07-31 22:52:35 +02:00
|
|
|
log_output = [
|
|
|
|
*(f"INFO:{logger_string}:Soft deactivated user {user.id}" for user in users),
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of {len(users[:100])} users; {len(users[100:])} remain to process",
|
|
|
|
]
|
2020-08-09 14:15:58 +02:00
|
|
|
self.assertEqual(m.output, log_output)
|
|
|
|
|
2019-03-12 03:59:02 +01:00
|
|
|
for user in users:
|
|
|
|
self.assertTrue(user.long_term_idle)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message_id = self.send_stream_message(hamlet, stream, "Hello world!")
|
2019-03-12 03:59:02 +01:00
|
|
|
already_received = UserMessage.objects.filter(message_id=message_id).count()
|
2020-08-09 14:15:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-14 04:55:41 +01:00
|
|
|
do_catch_up_soft_deactivated_users(users)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
m.output, [f"INFO:{logger_string}:Caught up {len(users)} soft-deactivated users"]
|
|
|
|
)
|
2020-08-09 14:15:58 +02:00
|
|
|
|
2019-03-14 04:42:28 +01:00
|
|
|
catch_up_received = UserMessage.objects.filter(message_id=message_id).count()
|
|
|
|
self.assertEqual(already_received + len(users), catch_up_received)
|
2019-03-12 03:59:02 +01:00
|
|
|
|
|
|
|
for user in users:
|
|
|
|
user.refresh_from_db()
|
|
|
|
self.assertTrue(user.long_term_idle)
|
2019-03-14 04:42:28 +01:00
|
|
|
self.assertEqual(user.last_active_message_id, message_id)
|
2019-03-12 04:28:40 +01:00
|
|
|
|
|
|
|
def test_do_auto_soft_deactivate_users(self) -> None:
|
|
|
|
users = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.example_user("iago"),
|
|
|
|
self.example_user("cordelia"),
|
|
|
|
self.example_user("ZOE"),
|
|
|
|
self.example_user("othello"),
|
|
|
|
self.example_user("prospero"),
|
|
|
|
self.example_user("aaron"),
|
|
|
|
self.example_user("polonius"),
|
|
|
|
self.example_user("desdemona"),
|
2019-03-12 04:28:40 +01:00
|
|
|
]
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("hamlet")
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
stream_name = "announce"
|
2020-09-02 06:59:07 +02:00
|
|
|
for user in [*users, sender]:
|
2019-03-12 04:28:40 +01:00
|
|
|
self.subscribe(user, stream_name)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
client, _ = Client.objects.get_or_create(name="website")
|
|
|
|
query = "/some/random/endpoint"
|
2019-03-12 04:28:40 +01:00
|
|
|
last_visit = timezone_now()
|
|
|
|
count = 150
|
|
|
|
for user_profile in users:
|
|
|
|
UserActivity.objects.get_or_create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
client=client,
|
|
|
|
query=query,
|
|
|
|
count=count,
|
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,
|
2019-03-12 04:28:40 +01:00
|
|
|
)
|
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-12 04:28:40 +01:00
|
|
|
users_deactivated = do_auto_soft_deactivate_users(-1, realm)
|
2020-08-09 14:15:58 +02:00
|
|
|
|
2023-07-31 22:52:35 +02:00
|
|
|
log_output = [
|
|
|
|
*(f"INFO:{logger_string}:Soft deactivated user {user.id}" for user in users),
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of {len(users[:100])} users; {len(users[100:])} remain to process",
|
|
|
|
f"INFO:{logger_string}:Caught up {len(users)} soft-deactivated users",
|
|
|
|
]
|
2020-08-09 14:15:58 +02:00
|
|
|
self.assertEqual(set(m.output), set(log_output))
|
|
|
|
|
2019-03-12 04:28:40 +01:00
|
|
|
self.assert_length(users_deactivated, len(users))
|
|
|
|
for user in users:
|
|
|
|
self.assertTrue(user in users_deactivated)
|
|
|
|
|
|
|
|
# Verify that deactivated users are caught up automatically
|
|
|
|
|
2020-03-07 11:43:05 +01:00
|
|
|
message_id = self.send_stream_message(sender, stream_name)
|
2021-02-12 08:19:30 +01:00
|
|
|
received_count = UserMessage.objects.filter(
|
|
|
|
user_profile__in=users, message_id=message_id
|
|
|
|
).count()
|
2019-03-12 04:28:40 +01:00
|
|
|
self.assertEqual(0, received_count)
|
|
|
|
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-12 04:28:40 +01:00
|
|
|
users_deactivated = do_auto_soft_deactivate_users(-1, realm)
|
|
|
|
|
2023-07-31 22:52:35 +02:00
|
|
|
log_output = [f"INFO:{logger_string}:Caught up {len(users)} soft-deactivated users"]
|
2020-08-09 14:15:58 +02:00
|
|
|
self.assertEqual(set(m.output), set(log_output))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_length(users_deactivated, 0) # all users are already deactivated
|
|
|
|
received_count = UserMessage.objects.filter(
|
|
|
|
user_profile__in=users, message_id=message_id
|
|
|
|
).count()
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(users, received_count)
|
2019-03-12 04:28:40 +01:00
|
|
|
|
|
|
|
# Verify that deactivated users are NOT caught up if
|
|
|
|
# AUTO_CATCH_UP_SOFT_DEACTIVATED_USERS is off
|
|
|
|
|
2020-03-07 11:43:05 +01:00
|
|
|
message_id = self.send_stream_message(sender, stream_name)
|
2021-02-12 08:19:30 +01:00
|
|
|
received_count = UserMessage.objects.filter(
|
|
|
|
user_profile__in=users, message_id=message_id
|
|
|
|
).count()
|
2019-03-12 04:28:40 +01:00
|
|
|
self.assertEqual(0, received_count)
|
|
|
|
|
|
|
|
with self.settings(AUTO_CATCH_UP_SOFT_DEACTIVATED_USERS=False):
|
2020-08-09 14:15:58 +02:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as m:
|
2019-03-12 04:28:40 +01:00
|
|
|
users_deactivated = do_auto_soft_deactivate_users(-1, realm)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
m.output,
|
|
|
|
[
|
|
|
|
f"INFO:{logger_string}:Not catching up users since AUTO_CATCH_UP_SOFT_DEACTIVATED_USERS is off"
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assert_length(users_deactivated, 0) # all users are already deactivated
|
|
|
|
received_count = UserMessage.objects.filter(
|
|
|
|
user_profile__in=users, message_id=message_id
|
|
|
|
).count()
|
2019-03-12 04:28:40 +01:00
|
|
|
self.assertEqual(0, received_count)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SoftDeactivationMessageTest(ZulipTestCase):
|
|
|
|
def test_reactivate_user_if_soft_deactivated(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient_list = [self.example_user("hamlet"), self.example_user("iago")]
|
2020-07-07 01:19:40 +02:00
|
|
|
for user_profile in recipient_list:
|
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("iago")
|
|
|
|
stream_name = "Denmark"
|
|
|
|
topic_name = "foo"
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
def last_realm_audit_log_entry(event_type: int) -> RealmAuditLog:
|
2021-02-12 08:20:45 +01:00
|
|
|
return RealmAuditLog.objects.filter(event_type=event_type).order_by("-event_time")[0]
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
long_term_idle_user = self.example_user("hamlet")
|
2020-07-07 01:19:40 +02:00
|
|
|
# We are sending this message to ensure that long_term_idle_user has
|
|
|
|
# at least one UserMessage row.
|
|
|
|
self.send_stream_message(long_term_idle_user, stream_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as info_logs:
|
2020-07-26 14:05:18 +02:00
|
|
|
do_soft_deactivate_users([long_term_idle_user])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
info_logs.output,
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"INFO:{logger_string}:Soft deactivated user {long_term_idle_user.id}",
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of 1 users; 0 remain to process",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
message = "Test message 1"
|
2021-02-12 08:19:30 +01:00
|
|
|
message_id = self.send_stream_message(sender, stream_name, message, topic_name)
|
2020-07-07 01:19:40 +02:00
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
self.assertNotEqual(idle_user_msg_list[-1].content, message)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(7):
|
2020-07-07 01:19:40 +02:00
|
|
|
reactivate_user_if_soft_deactivated(long_term_idle_user)
|
|
|
|
self.assertFalse(long_term_idle_user.long_term_idle)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
last_realm_audit_log_entry(RealmAuditLog.USER_SOFT_ACTIVATED).modified_user,
|
|
|
|
long_term_idle_user,
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + 1)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.assertEqual(idle_user_msg_list[-1].content, message)
|
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, message_id)
|
|
|
|
|
|
|
|
def test_add_missing_messages(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient_list = [self.example_user("hamlet"), self.example_user("iago")]
|
2020-07-07 01:19:40 +02:00
|
|
|
for user_profile in recipient_list:
|
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("iago")
|
2020-07-07 01:19:40 +02:00
|
|
|
realm = sender.realm
|
|
|
|
sending_client = make_client(name="test suite")
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_name = "Denmark"
|
2020-07-07 01:19:40 +02:00
|
|
|
stream = get_stream(stream_name, realm)
|
2021-02-12 08:20:45 +01:00
|
|
|
topic_name = "foo"
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
def send_fake_message(message_content: str, stream: Stream) -> Message:
|
2022-09-29 21:31:10 +02:00
|
|
|
"""
|
|
|
|
The purpose of this helper is to create a Message object without corresponding
|
|
|
|
UserMessage rows being created.
|
|
|
|
"""
|
2020-07-07 01:19:40 +02:00
|
|
|
recipient = stream.recipient
|
2021-02-12 08:19:30 +01:00
|
|
|
message = Message(
|
|
|
|
sender=sender,
|
2022-09-27 21:42:31 +02:00
|
|
|
realm=realm,
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient=recipient,
|
|
|
|
content=message_content,
|
|
|
|
date_sent=timezone_now(),
|
|
|
|
sending_client=sending_client,
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
message.set_topic_name(topic_name)
|
|
|
|
message.save()
|
|
|
|
return message
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
long_term_idle_user = self.example_user("hamlet")
|
2020-07-07 01:19:40 +02:00
|
|
|
self.send_stream_message(long_term_idle_user, stream_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as info_logs:
|
2020-07-26 14:05:18 +02:00
|
|
|
do_soft_deactivate_users([long_term_idle_user])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
info_logs.output,
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"INFO:{logger_string}:Soft deactivated user {long_term_idle_user.id}",
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of 1 users; 0 remain to process",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
# Test that add_missing_messages() in simplest case of adding a
|
|
|
|
# message for which UserMessage row doesn't exist for this user.
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message = send_fake_message("Test message 1", stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
self.assertNotEqual(idle_user_msg_list[-1], sent_message)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(5):
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + 1)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.assertEqual(idle_user_msg_list[-1], sent_message)
|
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, sent_message.id)
|
|
|
|
|
|
|
|
# Test that add_missing_messages() only adds messages that aren't
|
|
|
|
# already present in the UserMessage table. This test works on the
|
|
|
|
# fact that previous test just above this added a message but didn't
|
|
|
|
# updated the last_active_message_id field for the user.
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message = send_fake_message("Test message 2", stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
self.assertNotEqual(idle_user_msg_list[-1], sent_message)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(6):
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + 1)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.assertEqual(idle_user_msg_list[-1], sent_message)
|
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, sent_message.id)
|
|
|
|
|
|
|
|
# Test UserMessage rows are created correctly in case of stream
|
|
|
|
# Subscription was altered by admin while user was away.
|
|
|
|
|
|
|
|
# Test for a public stream.
|
|
|
|
sent_message_list = []
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_list.append(send_fake_message("Test message 3", stream))
|
2020-07-07 01:19:40 +02:00
|
|
|
# Alter subscription to stream.
|
|
|
|
self.unsubscribe(long_term_idle_user, stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
send_fake_message("Test message 4", stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.subscribe(long_term_idle_user, stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_list.append(send_fake_message("Test message 5", stream))
|
2020-07-07 01:19:40 +02:00
|
|
|
sent_message_list.reverse()
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
for sent_message in sent_message_list:
|
|
|
|
self.assertNotEqual(idle_user_msg_list.pop(), sent_message)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(5):
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + 2)
|
2020-07-07 01:19:40 +02:00
|
|
|
for sent_message in sent_message_list:
|
|
|
|
self.assertEqual(idle_user_msg_list.pop(), sent_message)
|
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, sent_message_list[0].id)
|
|
|
|
|
|
|
|
# Test consecutive subscribe/unsubscribe in a public stream
|
|
|
|
sent_message_list = []
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_list.append(send_fake_message("Test message 6", stream))
|
2020-07-07 01:19:40 +02:00
|
|
|
# Unsubscribe from stream and then immediately subscribe back again.
|
|
|
|
self.unsubscribe(long_term_idle_user, stream_name)
|
|
|
|
self.subscribe(long_term_idle_user, stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_list.append(send_fake_message("Test message 7", stream))
|
2020-07-07 01:19:40 +02:00
|
|
|
# Again unsubscribe from stream and send a message.
|
|
|
|
# This will make sure that if initially in a unsubscribed state
|
|
|
|
# a consecutive subscribe/unsubscribe doesn't misbehave.
|
|
|
|
self.unsubscribe(long_term_idle_user, stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
send_fake_message("Test message 8", stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
# Do a subscribe and unsubscribe immediately.
|
|
|
|
self.subscribe(long_term_idle_user, stream_name)
|
|
|
|
self.unsubscribe(long_term_idle_user, stream_name)
|
|
|
|
|
|
|
|
sent_message_list.reverse()
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
for sent_message in sent_message_list:
|
|
|
|
self.assertNotEqual(idle_user_msg_list.pop(), sent_message)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(5):
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + 2)
|
2020-07-07 01:19:40 +02:00
|
|
|
for sent_message in sent_message_list:
|
|
|
|
self.assertEqual(idle_user_msg_list.pop(), sent_message)
|
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, sent_message_list[0].id)
|
|
|
|
|
|
|
|
# Test for when user unsubscribes before soft deactivation
|
|
|
|
# (must reactivate them in order to do this).
|
|
|
|
|
|
|
|
do_soft_activate_users([long_term_idle_user])
|
|
|
|
self.subscribe(long_term_idle_user, stream_name)
|
|
|
|
# Send a real message to update last_active_message_id
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_id = self.send_stream_message(sender, stream_name, "Test message 9")
|
2020-07-07 01:19:40 +02:00
|
|
|
self.unsubscribe(long_term_idle_user, stream_name)
|
|
|
|
# Soft deactivate and send another message to the unsubscribed stream.
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as info_logs:
|
2020-07-26 14:05:18 +02:00
|
|
|
do_soft_deactivate_users([long_term_idle_user])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
info_logs.output,
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"INFO:{logger_string}:Soft deactivated user {long_term_idle_user.id}",
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of 1 users; 0 remain to process",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2021-05-10 07:02:14 +02:00
|
|
|
send_fake_message("Test message 10", stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
self.assertEqual(idle_user_msg_list[-1].id, sent_message_id)
|
|
|
|
# There are no streams to fetch missing messages from, so
|
|
|
|
# the Message.objects query will be avoided.
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(3):
|
2022-10-15 22:47:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
2020-07-07 01:19:40 +02:00
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
# No new UserMessage rows should have been created.
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
# Note: At this point in this test we have long_term_idle_user
|
|
|
|
# unsubscribed from the 'Denmark' stream.
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
# Test for a private stream.
|
2020-07-07 01:19:40 +02:00
|
|
|
stream_name = "Core"
|
2021-02-12 08:20:45 +01:00
|
|
|
private_stream = self.make_stream("Core", invite_only=True)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.subscribe(self.example_user("iago"), stream_name)
|
|
|
|
sent_message_list = []
|
2021-05-10 07:02:14 +02:00
|
|
|
send_fake_message("Test message 11", private_stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.subscribe(self.example_user("hamlet"), stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_list.append(send_fake_message("Test message 12", private_stream))
|
2020-07-07 01:19:40 +02:00
|
|
|
self.unsubscribe(long_term_idle_user, stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
send_fake_message("Test message 13", private_stream)
|
2020-07-07 01:19:40 +02:00
|
|
|
self.subscribe(long_term_idle_user, stream_name)
|
2021-05-10 07:02:14 +02:00
|
|
|
sent_message_list.append(send_fake_message("Test message 14", private_stream))
|
2020-07-07 01:19:40 +02:00
|
|
|
sent_message_list.reverse()
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
|
|
|
for sent_message in sent_message_list:
|
|
|
|
self.assertNotEqual(idle_user_msg_list.pop(), sent_message)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(5):
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + 2)
|
2020-07-07 01:19:40 +02:00
|
|
|
for sent_message in sent_message_list:
|
|
|
|
self.assertEqual(idle_user_msg_list.pop(), sent_message)
|
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, sent_message_list[0].id)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@mock.patch("zerver.lib.soft_deactivation.BULK_CREATE_BATCH_SIZE", 2)
|
2020-07-07 01:19:40 +02:00
|
|
|
def test_add_missing_messages_pagination(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient_list = [self.example_user("hamlet"), self.example_user("iago")]
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_name = "Denmark"
|
2020-07-07 01:19:40 +02:00
|
|
|
for user_profile in recipient_list:
|
|
|
|
self.subscribe(user_profile, stream_name)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("iago")
|
|
|
|
long_term_idle_user = self.example_user("hamlet")
|
2020-07-07 01:19:40 +02:00
|
|
|
self.send_stream_message(long_term_idle_user, stream_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as info_logs:
|
2020-07-26 14:05:18 +02:00
|
|
|
do_soft_deactivate_users([long_term_idle_user])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
info_logs.output,
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"INFO:{logger_string}:Soft deactivated user {long_term_idle_user.id}",
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of 1 users; 0 remain to process",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
num_new_messages = 5
|
|
|
|
message_ids = []
|
|
|
|
for _ in range(num_new_messages):
|
|
|
|
message_id = self.send_stream_message(sender, stream_name)
|
|
|
|
message_ids.append(message_id)
|
|
|
|
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_count = len(idle_user_msg_list)
|
2023-09-29 17:45:33 +02:00
|
|
|
with self.assert_database_query_count(9):
|
2020-07-07 01:19:40 +02:00
|
|
|
add_missing_messages(long_term_idle_user)
|
|
|
|
idle_user_msg_list = get_user_messages(long_term_idle_user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(idle_user_msg_list, idle_user_msg_count + num_new_messages)
|
2020-07-07 01:19:40 +02:00
|
|
|
long_term_idle_user.refresh_from_db()
|
|
|
|
self.assertEqual(long_term_idle_user.last_active_message_id, message_ids[-1])
|
|
|
|
|
|
|
|
def test_user_message_filter(self) -> None:
|
|
|
|
# In this test we are basically testing out the logic used out in
|
|
|
|
# do_send_messages() in action.py for filtering the messages for which
|
|
|
|
# UserMessage rows should be created for a soft-deactivated user.
|
2021-05-11 13:55:49 +02:00
|
|
|
AlertWord.objects.all().delete()
|
|
|
|
|
2021-05-11 15:14:27 +02:00
|
|
|
long_term_idle_user = self.example_user("hamlet")
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
sender = self.example_user("iago")
|
2021-05-11 13:55:49 +02:00
|
|
|
stream_name = "Brand New Stream"
|
2021-02-12 08:20:45 +01:00
|
|
|
topic_name = "foo"
|
2021-05-11 13:55:49 +02:00
|
|
|
realm_id = cordelia.realm_id
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2021-05-11 15:14:27 +02:00
|
|
|
self.subscribe(long_term_idle_user, stream_name)
|
|
|
|
self.subscribe(cordelia, stream_name)
|
|
|
|
self.subscribe(sender, stream_name)
|
|
|
|
|
2021-05-11 13:55:49 +02:00
|
|
|
stream_id = get_stream(stream_name, cordelia.realm).id
|
|
|
|
|
2020-07-07 01:19:40 +02:00
|
|
|
def send_stream_message(content: str) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
self.send_stream_message(sender, stream_name, content, topic_name)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
def send_personal_message(content: str) -> None:
|
|
|
|
self.send_personal_message(sender, self.example_user("hamlet"), content)
|
|
|
|
|
|
|
|
self.send_stream_message(long_term_idle_user, stream_name)
|
2021-05-11 15:14:27 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.assertLogs(logger_string, level="INFO") as info_logs:
|
2020-07-26 14:05:18 +02:00
|
|
|
do_soft_deactivate_users([long_term_idle_user])
|
2021-05-11 15:14:27 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
info_logs.output,
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"INFO:{logger_string}:Soft deactivated user {long_term_idle_user.id}",
|
|
|
|
f"INFO:{logger_string}:Soft-deactivated batch of 1 users; 0 remain to process",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
def assert_um_count(user: UserProfile, count: int) -> None:
|
|
|
|
user_messages = get_user_messages(user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(user_messages, count)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def assert_last_um_content(user: UserProfile, content: str, negate: bool = False) -> None:
|
2020-07-07 01:19:40 +02:00
|
|
|
user_messages = get_user_messages(user)
|
|
|
|
if negate:
|
|
|
|
self.assertNotEqual(user_messages[-1].content, content)
|
|
|
|
else:
|
|
|
|
self.assertEqual(user_messages[-1].content, content)
|
|
|
|
|
2021-05-11 13:55:49 +02:00
|
|
|
def assert_num_possible_users(
|
|
|
|
expected_count: int,
|
|
|
|
*,
|
2023-06-03 16:51:38 +02:00
|
|
|
possible_stream_wildcard_mention: bool = False,
|
2023-05-31 16:56:18 +02:00
|
|
|
topic_participant_user_ids: AbstractSet[int] = set(),
|
2021-05-11 13:55:49 +02:00
|
|
|
possibly_mentioned_user_ids: AbstractSet[int] = set(),
|
|
|
|
) -> None:
|
|
|
|
self.assertEqual(
|
|
|
|
len(
|
|
|
|
get_subscriptions_for_send_message(
|
|
|
|
realm_id=realm_id,
|
|
|
|
stream_id=stream_id,
|
2023-05-17 16:01:16 +02:00
|
|
|
topic_name=topic_name,
|
2023-06-03 16:51:38 +02:00
|
|
|
possible_stream_wildcard_mention=possible_stream_wildcard_mention,
|
2023-05-31 16:56:18 +02:00
|
|
|
topic_participant_user_ids=topic_participant_user_ids,
|
2021-05-11 13:55:49 +02:00
|
|
|
possibly_mentioned_user_ids=possibly_mentioned_user_ids,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
expected_count,
|
|
|
|
)
|
|
|
|
|
|
|
|
def assert_stream_message_sent_to_idle_user(
|
|
|
|
content: str,
|
|
|
|
*,
|
2023-06-03 16:51:38 +02:00
|
|
|
possible_stream_wildcard_mention: bool = False,
|
2023-06-07 19:19:33 +02:00
|
|
|
topic_participant_user_ids: AbstractSet[int] = set(),
|
2021-05-11 13:55:49 +02:00
|
|
|
possibly_mentioned_user_ids: AbstractSet[int] = set(),
|
|
|
|
) -> None:
|
|
|
|
assert_num_possible_users(
|
|
|
|
expected_count=3,
|
2023-06-03 16:51:38 +02:00
|
|
|
possible_stream_wildcard_mention=possible_stream_wildcard_mention,
|
2023-06-07 19:19:33 +02:00
|
|
|
topic_participant_user_ids=topic_participant_user_ids,
|
2021-05-11 13:55:49 +02:00
|
|
|
possibly_mentioned_user_ids=possibly_mentioned_user_ids,
|
|
|
|
)
|
2021-05-11 15:14:27 +02:00
|
|
|
general_user_msg_count = len(get_user_messages(cordelia))
|
|
|
|
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
|
|
|
|
send_stream_message(content)
|
|
|
|
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
|
|
|
|
assert_um_count(cordelia, general_user_msg_count + 1)
|
|
|
|
assert_last_um_content(long_term_idle_user, content)
|
|
|
|
assert_last_um_content(cordelia, content)
|
|
|
|
|
2021-05-11 13:55:49 +02:00
|
|
|
def assert_stream_message_not_sent_to_idle_user(
|
|
|
|
content: str,
|
|
|
|
*,
|
|
|
|
possibly_mentioned_user_ids: AbstractSet[int] = set(),
|
|
|
|
false_alarm_row: bool = False,
|
|
|
|
) -> None:
|
|
|
|
if false_alarm_row:
|
|
|
|
# We will query for our idle user if he has **ANY** alert
|
|
|
|
# words, but we won't actually write a UserMessage row until
|
|
|
|
# we truly parse the message. We also get false alarms for
|
|
|
|
# messages with quoted mentions.
|
|
|
|
assert_num_possible_users(
|
|
|
|
3, possibly_mentioned_user_ids=possibly_mentioned_user_ids
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert_num_possible_users(2)
|
2021-05-11 15:14:27 +02:00
|
|
|
general_user_msg_count = len(get_user_messages(cordelia))
|
|
|
|
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
|
|
|
|
send_stream_message(content)
|
|
|
|
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count)
|
|
|
|
assert_um_count(cordelia, general_user_msg_count + 1)
|
|
|
|
assert_last_um_content(long_term_idle_user, content, negate=True)
|
|
|
|
assert_last_um_content(cordelia, content)
|
|
|
|
|
2020-07-07 01:19:40 +02:00
|
|
|
# Test that sending a message to a stream with soft deactivated user
|
|
|
|
# doesn't end up creating UserMessage row for deactivated user.
|
2021-05-11 15:14:27 +02:00
|
|
|
assert_stream_message_not_sent_to_idle_user("Test message 1")
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
sub = get_subscription(stream_name, long_term_idle_user)
|
2021-05-11 16:22:32 +02:00
|
|
|
|
|
|
|
# Sub settings override user settings.
|
2020-07-07 01:19:40 +02:00
|
|
|
sub.push_notifications = True
|
|
|
|
sub.save()
|
2021-05-11 16:22:32 +02:00
|
|
|
assert_stream_message_sent_to_idle_user("Sub push")
|
|
|
|
|
2020-07-07 01:19:40 +02:00
|
|
|
sub.push_notifications = False
|
|
|
|
sub.save()
|
2021-05-11 16:22:32 +02:00
|
|
|
assert_stream_message_not_sent_to_idle_user("Sub no push")
|
|
|
|
|
|
|
|
# Let user defaults take over
|
|
|
|
sub.push_notifications = None
|
|
|
|
sub.save()
|
|
|
|
|
|
|
|
long_term_idle_user.enable_stream_push_notifications = True
|
|
|
|
long_term_idle_user.save()
|
|
|
|
assert_stream_message_sent_to_idle_user("User push")
|
|
|
|
|
|
|
|
long_term_idle_user.enable_stream_push_notifications = False
|
|
|
|
long_term_idle_user.save()
|
|
|
|
assert_stream_message_not_sent_to_idle_user("User no push")
|
|
|
|
|
|
|
|
# Sub settings override user settings.
|
|
|
|
sub.email_notifications = True
|
|
|
|
sub.save()
|
|
|
|
assert_stream_message_sent_to_idle_user("Sub email")
|
|
|
|
|
|
|
|
sub.email_notifications = False
|
|
|
|
sub.save()
|
|
|
|
assert_stream_message_not_sent_to_idle_user("Sub no email")
|
|
|
|
|
|
|
|
# Let user defaults take over
|
|
|
|
sub.email_notifications = None
|
|
|
|
sub.save()
|
|
|
|
|
|
|
|
long_term_idle_user.enable_stream_email_notifications = True
|
|
|
|
long_term_idle_user.save()
|
|
|
|
assert_stream_message_sent_to_idle_user("User email")
|
|
|
|
|
|
|
|
long_term_idle_user.enable_stream_email_notifications = False
|
|
|
|
long_term_idle_user.save()
|
|
|
|
assert_stream_message_not_sent_to_idle_user("User no email")
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2023-06-19 16:26:12 +02:00
|
|
|
# Test sending a direct message to soft deactivated user creates
|
2020-07-07 01:19:40 +02:00
|
|
|
# UserMessage row.
|
|
|
|
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
|
2023-06-19 16:26:12 +02:00
|
|
|
message = "Test direct message"
|
2020-07-07 01:19:40 +02:00
|
|
|
send_personal_message(message)
|
|
|
|
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
|
|
|
|
assert_last_um_content(long_term_idle_user, message)
|
|
|
|
|
|
|
|
# Test UserMessage row is created while user is deactivated if
|
|
|
|
# user itself is mentioned.
|
2021-05-11 13:55:49 +02:00
|
|
|
assert_stream_message_sent_to_idle_user(
|
|
|
|
"Test @**King Hamlet** mention",
|
|
|
|
possibly_mentioned_user_ids={long_term_idle_user.id},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_stream_message_not_sent_to_idle_user(
|
|
|
|
"Test `@**King Hamlet**` mention",
|
|
|
|
possibly_mentioned_user_ids={long_term_idle_user.id},
|
|
|
|
false_alarm_row=True,
|
|
|
|
)
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
# Test UserMessage row is not created while user is deactivated if
|
|
|
|
# anyone is mentioned but the user.
|
2021-05-11 15:14:27 +02:00
|
|
|
assert_stream_message_not_sent_to_idle_user("Test @**Cordelia, Lear's daughter** mention")
|
2020-07-07 01:19:40 +02:00
|
|
|
|
|
|
|
# Test UserMessage row is created while user is deactivated if
|
2023-06-03 16:51:38 +02:00
|
|
|
# there is a stream wildcard mention such as @all or @everyone
|
2021-05-11 13:55:49 +02:00
|
|
|
assert_stream_message_sent_to_idle_user(
|
2023-06-03 16:51:38 +02:00
|
|
|
"Test @**all** mention", possible_stream_wildcard_mention=True
|
2021-05-11 13:55:49 +02:00
|
|
|
)
|
|
|
|
assert_stream_message_sent_to_idle_user(
|
2023-06-03 16:51:38 +02:00
|
|
|
"Test @**everyone** mention", possible_stream_wildcard_mention=True
|
2021-05-11 13:55:49 +02:00
|
|
|
)
|
|
|
|
assert_stream_message_sent_to_idle_user(
|
2023-06-03 16:51:38 +02:00
|
|
|
"Test @**stream** mention", possible_stream_wildcard_mention=True
|
2021-05-11 13:55:49 +02:00
|
|
|
)
|
2021-05-11 15:14:27 +02:00
|
|
|
assert_stream_message_not_sent_to_idle_user("Test @**bogus** mention")
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2023-06-07 19:19:33 +02:00
|
|
|
# Test UserMessage row is created while user is deactivated if
|
|
|
|
# there is a topic wildcard mention i.e. @topic
|
|
|
|
do_soft_activate_users([long_term_idle_user])
|
|
|
|
self.send_stream_message(long_term_idle_user, stream_name, "Hi", topic_name)
|
|
|
|
topic_participant_user_ids = {long_term_idle_user.id}
|
|
|
|
|
|
|
|
do_soft_deactivate_users([long_term_idle_user])
|
|
|
|
assert_stream_message_sent_to_idle_user(
|
|
|
|
"Test @**topic** mention", topic_participant_user_ids=topic_participant_user_ids
|
|
|
|
)
|
|
|
|
|
2021-05-11 15:14:27 +02:00
|
|
|
# Test UserMessage row is created while user is deactivated if there
|
2020-07-07 01:19:40 +02:00
|
|
|
# is a alert word in message.
|
2021-02-12 08:20:45 +01:00
|
|
|
do_add_alert_words(long_term_idle_user, ["test_alert_word"])
|
2021-05-11 15:14:27 +02:00
|
|
|
assert_stream_message_sent_to_idle_user("Testing test_alert_word")
|
2020-07-07 01:19:40 +02:00
|
|
|
|
2021-05-11 13:55:49 +02:00
|
|
|
do_add_alert_words(cordelia, ["cordelia"])
|
|
|
|
assert_stream_message_not_sent_to_idle_user("cordelia", false_alarm_row=True)
|
|
|
|
|
2021-05-11 15:14:27 +02:00
|
|
|
# Test UserMessage row is not created while user is deactivated if
|
2020-07-07 01:19:40 +02:00
|
|
|
# message is a me message.
|
2021-05-11 13:55:49 +02:00
|
|
|
assert_stream_message_not_sent_to_idle_user("/me says test", false_alarm_row=True)
|
|
|
|
|
|
|
|
# Sanity check after removing the alert word for Hamlet.
|
|
|
|
AlertWord.objects.filter(user_profile=long_term_idle_user).delete()
|
|
|
|
assert_stream_message_not_sent_to_idle_user("no alert words")
|