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
|
|
|
|
|
2017-07-30 19:48:49 +02:00
|
|
|
from zerver.lib.soft_deactivation import (
|
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,
|
2017-07-30 19:48:49 +02:00
|
|
|
)
|
2017-07-31 05:25:36 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Client, UserActivity, UserMessage, UserProfile, get_realm
|
|
|
|
|
2017-07-30 19:48:49 +02:00
|
|
|
|
|
|
|
class UserSoftDeactivationTests(ZulipTestCase):
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_soft_deactivate_user(self) -> None:
|
2017-07-30 19:48:49 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.assertFalse(user.long_term_idle)
|
|
|
|
|
2019-03-14 04:55:41 +01:00
|
|
|
with mock.patch('logging.info'):
|
|
|
|
do_soft_deactivate_user(user)
|
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 = [
|
|
|
|
self.example_user('hamlet'),
|
|
|
|
self.example_user('iago'),
|
|
|
|
self.example_user('cordelia'),
|
|
|
|
]
|
|
|
|
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
|
|
|
|
|
|
|
with mock.patch('logging.info'):
|
|
|
|
do_soft_deactivate_users(users)
|
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 = [
|
|
|
|
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'),
|
2018-04-20 22:12:02 +02:00
|
|
|
self.example_user('polonius'),
|
2020-05-17 18:46:14 +02:00
|
|
|
self.example_user('desdemona'),
|
2017-07-31 05:25:36 +02:00
|
|
|
]
|
|
|
|
client, _ = Client.objects.get_or_create(name='website')
|
|
|
|
query = '/json/users/me/pointer'
|
|
|
|
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
|
|
|
)
|
2017-08-22 02:43:03 +02:00
|
|
|
filter_kwargs = dict(user_profile__realm=get_realm('zulip'))
|
|
|
|
users_to_deactivate = get_users_for_soft_deactivation(-1, filter_kwargs)
|
2017-07-31 05:25:36 +02:00
|
|
|
|
2020-05-17 18:46:14 +02:00
|
|
|
self.assert_length(users_to_deactivate, 9)
|
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 = [
|
|
|
|
self.example_user('hamlet'),
|
|
|
|
self.example_user('iago'),
|
|
|
|
self.example_user('cordelia'),
|
|
|
|
]
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_huddle_message(users[0], users)
|
|
|
|
|
2019-03-14 04:55:41 +01:00
|
|
|
with mock.patch('logging.info'):
|
|
|
|
do_soft_deactivate_users(users)
|
2017-07-31 05:25:36 +02:00
|
|
|
for user in users:
|
|
|
|
self.assertTrue(user.long_term_idle)
|
|
|
|
|
2019-03-14 04:55:41 +01:00
|
|
|
with mock.patch('logging.info'):
|
|
|
|
do_soft_activate_users(users)
|
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 = [
|
|
|
|
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'),
|
2020-05-17 18:46:14 +02:00
|
|
|
self.example_user('desdemona'),
|
2019-03-12 03:59:02 +01:00
|
|
|
]
|
|
|
|
for user_profile in UserProfile.objects.all():
|
|
|
|
user_profile.long_term_idle = True
|
|
|
|
user_profile.save(update_fields=['long_term_idle'])
|
|
|
|
|
|
|
|
filter_kwargs = dict(realm=get_realm('zulip'))
|
|
|
|
users_to_catch_up = get_soft_deactivated_users_for_catch_up(filter_kwargs)
|
|
|
|
|
2020-05-17 18:46:14 +02:00
|
|
|
self.assert_length(users_to_catch_up, 9)
|
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:
|
|
|
|
stream = 'Verona'
|
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
users = [
|
|
|
|
self.example_user('iago'),
|
|
|
|
self.example_user('cordelia'),
|
|
|
|
]
|
|
|
|
all_users = users + [hamlet]
|
|
|
|
for user in all_users:
|
|
|
|
self.subscribe(user, stream)
|
|
|
|
|
2019-03-14 04:55:41 +01:00
|
|
|
with mock.patch('logging.info'):
|
|
|
|
do_soft_deactivate_users(users)
|
2019-03-12 03:59:02 +01:00
|
|
|
for user in users:
|
|
|
|
self.assertTrue(user.long_term_idle)
|
|
|
|
|
2020-03-07 11:43:05 +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()
|
2019-03-14 04:55:41 +01:00
|
|
|
with mock.patch('logging.info'):
|
|
|
|
do_catch_up_soft_deactivated_users(users)
|
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 = [
|
|
|
|
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'),
|
2020-05-17 18:46:14 +02:00
|
|
|
self.example_user('desdemona'),
|
2019-03-12 04:28:40 +01:00
|
|
|
]
|
|
|
|
sender = self.example_user('hamlet')
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
stream_name = 'announce'
|
|
|
|
for user in users + [sender]:
|
|
|
|
self.subscribe(user, stream_name)
|
|
|
|
|
|
|
|
client, _ = Client.objects.get_or_create(name='website')
|
|
|
|
query = '/json/users/me/pointer'
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
with mock.patch('logging.info'):
|
|
|
|
users_deactivated = do_auto_soft_deactivate_users(-1, realm)
|
|
|
|
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)
|
2019-03-12 04:28:40 +01:00
|
|
|
received_count = UserMessage.objects.filter(user_profile__in=users,
|
|
|
|
message_id=message_id).count()
|
|
|
|
self.assertEqual(0, received_count)
|
|
|
|
|
|
|
|
with mock.patch('logging.info'):
|
|
|
|
users_deactivated = do_auto_soft_deactivate_users(-1, realm)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.assertEqual(len(users), received_count)
|
|
|
|
|
|
|
|
# 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)
|
2019-03-12 04:28:40 +01:00
|
|
|
received_count = UserMessage.objects.filter(user_profile__in=users,
|
|
|
|
message_id=message_id).count()
|
|
|
|
self.assertEqual(0, received_count)
|
|
|
|
|
|
|
|
with self.settings(AUTO_CATCH_UP_SOFT_DEACTIVATED_USERS=False):
|
|
|
|
with mock.patch('logging.info'):
|
|
|
|
users_deactivated = do_auto_soft_deactivate_users(-1, realm)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.assertEqual(0, received_count)
|