2020-06-11 00:54:34 +02:00
|
|
|
from datetime import timedelta
|
2020-07-12 23:41:53 +02:00
|
|
|
from typing import Any, Dict, Union
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.contrib.auth.password_validation import validate_password
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-02-15 04:35:10 +01:00
|
|
|
|
2018-08-16 18:23:52 +02:00
|
|
|
from analytics.models import StreamCount
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
bulk_add_subscriptions,
|
|
|
|
bulk_remove_subscriptions,
|
2021-07-09 02:27:06 +02:00
|
|
|
do_activate_mirror_dummy_user,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_change_avatar_fields,
|
|
|
|
do_change_bot_owner,
|
2020-07-12 23:45:50 +02:00
|
|
|
do_change_default_all_public_streams,
|
|
|
|
do_change_default_events_register_stream,
|
|
|
|
do_change_default_sending_stream,
|
2020-07-11 20:59:52 +02:00
|
|
|
do_change_icon_source,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_change_password,
|
2020-07-12 23:48:08 +02:00
|
|
|
do_change_subscription_property,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_change_tos_version,
|
|
|
|
do_change_user_delivery_email,
|
|
|
|
do_change_user_role,
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_create_user,
|
|
|
|
do_deactivate_realm,
|
2020-06-29 15:02:07 +02:00
|
|
|
do_deactivate_stream,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_deactivate_user,
|
|
|
|
do_reactivate_realm,
|
|
|
|
do_reactivate_user,
|
|
|
|
do_regenerate_api_key,
|
2020-07-12 23:39:54 +02:00
|
|
|
do_rename_stream,
|
2020-07-02 14:03:43 +02:00
|
|
|
do_set_realm_authentication_methods,
|
2020-07-02 15:17:16 +02:00
|
|
|
do_set_realm_message_editing,
|
2020-07-12 23:31:47 +02:00
|
|
|
do_set_realm_notifications_stream,
|
|
|
|
do_set_realm_signup_notifications_stream,
|
2020-06-11 00:54:34 +02:00
|
|
|
get_streams_traffic,
|
|
|
|
)
|
2020-10-13 15:49:40 +02:00
|
|
|
from zerver.lib.message import get_last_message_id
|
2020-06-29 23:31:25 +02:00
|
|
|
from zerver.lib.streams import create_stream_if_needed
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-07-12 23:48:08 +02:00
|
|
|
from zerver.models import (
|
|
|
|
Message,
|
2021-05-26 12:21:37 +02:00
|
|
|
Realm,
|
2020-07-12 23:48:08 +02:00
|
|
|
RealmAuditLog,
|
|
|
|
Recipient,
|
|
|
|
Subscription,
|
|
|
|
UserProfile,
|
|
|
|
get_realm,
|
|
|
|
get_stream,
|
|
|
|
)
|
2017-02-15 04:35:10 +01:00
|
|
|
|
2017-04-07 07:28:28 +02:00
|
|
|
|
2017-04-05 21:53:14 +02:00
|
|
|
class TestRealmAuditLog(ZulipTestCase):
|
2019-10-05 02:36:16 +02:00
|
|
|
def check_role_count_schema(self, role_counts: Dict[str, Any]) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
for key in [
|
|
|
|
UserProfile.ROLE_REALM_ADMINISTRATOR,
|
|
|
|
UserProfile.ROLE_MEMBER,
|
|
|
|
UserProfile.ROLE_GUEST,
|
|
|
|
UserProfile.ROLE_REALM_OWNER,
|
|
|
|
]:
|
2019-10-05 02:36:16 +02:00
|
|
|
# str(key) since json keys are always strings, and ujson.dumps will have converted
|
|
|
|
# the UserProfile.role values into strings
|
|
|
|
self.assertTrue(isinstance(role_counts[RealmAuditLog.ROLE_COUNT_HUMANS][str(key)], int))
|
|
|
|
self.assertTrue(isinstance(role_counts[RealmAuditLog.ROLE_COUNT_BOTS], int))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_activation(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = do_create_user("email", "password", realm, "full_name", acting_user=None)
|
2020-06-29 13:12:17 +02:00
|
|
|
do_deactivate_user(user, acting_user=user)
|
2021-07-09 02:27:06 +02:00
|
|
|
do_activate_mirror_dummy_user(user, acting_user=user)
|
2020-06-29 13:12:17 +02:00
|
|
|
do_deactivate_user(user, acting_user=user)
|
|
|
|
do_reactivate_user(user, acting_user=user)
|
2021-04-29 17:22:48 +02:00
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_time__gte=now).count(), 6)
|
2021-02-12 08:19:30 +01:00
|
|
|
event_types = list(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=user,
|
|
|
|
modified_user=user,
|
|
|
|
modified_stream=None,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_time__lte=now + timedelta(minutes=60),
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
.order_by("event_time")
|
|
|
|
.values_list("event_type", flat=True)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
event_types,
|
|
|
|
[
|
|
|
|
RealmAuditLog.USER_CREATED,
|
|
|
|
RealmAuditLog.USER_DEACTIVATED,
|
|
|
|
RealmAuditLog.USER_ACTIVATED,
|
|
|
|
RealmAuditLog.USER_DEACTIVATED,
|
|
|
|
RealmAuditLog.USER_REACTIVATED,
|
|
|
|
],
|
|
|
|
)
|
2019-10-05 02:36:16 +02:00
|
|
|
for event in RealmAuditLog.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
realm=realm,
|
|
|
|
acting_user=user,
|
|
|
|
modified_user=user,
|
|
|
|
modified_stream=None,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_time__lte=now + timedelta(minutes=60),
|
|
|
|
):
|
2020-08-07 01:09:47 +02:00
|
|
|
extra_data = orjson.loads(event.extra_data)
|
2019-10-05 02:36:16 +02:00
|
|
|
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
|
|
|
|
self.assertNotIn(RealmAuditLog.OLD_VALUE, extra_data)
|
|
|
|
|
|
|
|
def test_change_role(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2019-10-05 02:36:16 +02:00
|
|
|
now = timezone_now()
|
|
|
|
user_profile = self.example_user("hamlet")
|
2021-02-12 08:20:45 +01:00
|
|
|
acting_user = self.example_user("iago")
|
2021-02-12 08:19:30 +01:00
|
|
|
do_change_user_role(
|
|
|
|
user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=acting_user
|
|
|
|
)
|
2020-06-29 15:07:10 +02:00
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=acting_user)
|
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_GUEST, acting_user=acting_user)
|
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=acting_user)
|
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_REALM_OWNER, acting_user=acting_user)
|
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=acting_user)
|
2020-12-22 18:49:48 +01:00
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_MODERATOR, acting_user=acting_user)
|
|
|
|
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=acting_user)
|
2020-05-26 15:50:24 +02:00
|
|
|
old_values_seen = set()
|
|
|
|
new_values_seen = set()
|
2019-10-05 02:36:16 +02:00
|
|
|
for event in RealmAuditLog.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
event_type=RealmAuditLog.USER_ROLE_CHANGED,
|
|
|
|
realm=realm,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_time__lte=now + timedelta(minutes=60),
|
|
|
|
):
|
2020-08-07 01:09:47 +02:00
|
|
|
extra_data = orjson.loads(event.extra_data)
|
2019-10-05 02:36:16 +02:00
|
|
|
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
|
|
|
|
self.assertIn(RealmAuditLog.OLD_VALUE, extra_data)
|
|
|
|
self.assertIn(RealmAuditLog.NEW_VALUE, extra_data)
|
2020-05-26 15:50:24 +02:00
|
|
|
old_values_seen.add(extra_data[RealmAuditLog.OLD_VALUE])
|
|
|
|
new_values_seen.add(extra_data[RealmAuditLog.NEW_VALUE])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
old_values_seen,
|
|
|
|
{
|
|
|
|
UserProfile.ROLE_GUEST,
|
|
|
|
UserProfile.ROLE_MEMBER,
|
|
|
|
UserProfile.ROLE_REALM_ADMINISTRATOR,
|
|
|
|
UserProfile.ROLE_REALM_OWNER,
|
2020-12-22 18:49:48 +01:00
|
|
|
UserProfile.ROLE_MODERATOR,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2020-05-26 15:50:24 +02:00
|
|
|
self.assertEqual(old_values_seen, new_values_seen)
|
2017-03-14 06:07:14 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_password(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
password = "test1"
|
2017-03-14 06:07:14 +01:00
|
|
|
do_change_password(user, password)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_PASSWORD_CHANGED, event_time__gte=now
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2017-03-14 06:07:14 +01:00
|
|
|
self.assertIsNone(validate_password(password, user))
|
2017-03-14 06:06:56 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_email(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
new_email = "test@example.com"
|
2020-03-12 14:17:25 +01:00
|
|
|
do_change_user_delivery_email(user, new_email)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_EMAIL_CHANGED, event_time__gte=now
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-03-12 14:17:25 +01:00
|
|
|
self.assertEqual(new_email, user.delivery_email)
|
2017-03-23 22:02:35 +01:00
|
|
|
|
2017-09-22 16:09:37 +02:00
|
|
|
# Test the RealmAuditLog stringification
|
2021-02-12 08:19:30 +01:00
|
|
|
audit_entry = RealmAuditLog.objects.get(
|
|
|
|
event_type=RealmAuditLog.USER_EMAIL_CHANGED, event_time__gte=now
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
str(audit_entry).startswith(
|
|
|
|
f"<RealmAuditLog: <UserProfile: {user.email} {user.realm}> {RealmAuditLog.USER_EMAIL_CHANGED} "
|
|
|
|
)
|
|
|
|
)
|
2017-09-22 16:09:37 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_avatar_source(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
avatar_source = "G"
|
2020-06-29 12:47:44 +02:00
|
|
|
do_change_avatar_fields(user, avatar_source, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_AVATAR_SOURCE_CHANGED,
|
|
|
|
modified_user=user,
|
|
|
|
acting_user=user,
|
|
|
|
event_time__gte=now,
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2017-03-23 22:02:35 +01:00
|
|
|
self.assertEqual(avatar_source, user.avatar_source)
|
2017-03-31 17:27:08 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_full_name(self) -> None:
|
2017-04-07 07:28:28 +02:00
|
|
|
start = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
new_name = "George Hamletovich"
|
|
|
|
self.login("iago")
|
2020-08-07 01:09:47 +02:00
|
|
|
req = dict(full_name=orjson.dumps(new_name).decode())
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req)
|
2017-04-07 07:28:28 +02:00
|
|
|
self.assertTrue(result.status_code == 200)
|
2021-02-12 08:19:30 +01:00
|
|
|
query = RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_FULL_NAME_CHANGED, event_time__gte=start
|
|
|
|
)
|
2017-04-07 07:28:28 +02:00
|
|
|
self.assertEqual(query.count(), 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_tos_version(self) -> None:
|
2017-04-07 07:29:29 +02:00
|
|
|
now = timezone_now()
|
|
|
|
user = self.example_user("hamlet")
|
2021-02-12 08:20:45 +01:00
|
|
|
tos_version = "android"
|
2017-04-07 07:29:29 +02:00
|
|
|
do_change_tos_version(user, tos_version)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_TOS_VERSION_CHANGED, event_time__gte=now
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2017-04-07 07:29:29 +02:00
|
|
|
self.assertEqual(tos_version, user.tos_version)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_bot_owner(self) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
admin = self.example_user("iago")
|
2021-03-08 11:39:48 +01:00
|
|
|
bot = self.notification_bot(admin.realm)
|
2021-02-12 08:20:45 +01:00
|
|
|
bot_owner = self.example_user("hamlet")
|
2017-03-31 17:27:08 +02:00
|
|
|
do_change_bot_owner(bot, bot_owner, admin)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_BOT_OWNER_CHANGED, event_time__gte=now
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2017-03-31 17:27:08 +02:00
|
|
|
self.assertEqual(bot_owner, bot.bot_owner)
|
2017-04-06 12:27:58 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_regenerate_api_key(self) -> None:
|
2017-04-06 12:27:58 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2017-04-06 12:27:58 +02:00
|
|
|
do_regenerate_api_key(user, user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.USER_API_KEY_CHANGED, event_time__gte=now
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2017-04-06 12:27:58 +02:00
|
|
|
self.assertTrue(user.api_key)
|
2017-07-12 17:28:27 +02:00
|
|
|
|
2018-08-16 18:23:52 +02:00
|
|
|
def test_get_streams_traffic(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
stream_name = "whatever"
|
2018-08-16 18:23:52 +02:00
|
|
|
stream = self.make_stream(stream_name, realm)
|
2018-08-16 18:37:28 +02:00
|
|
|
stream_ids = {stream.id}
|
|
|
|
|
|
|
|
result = get_streams_traffic(stream_ids)
|
2018-08-16 18:23:52 +02:00
|
|
|
self.assertEqual(result, {})
|
|
|
|
|
|
|
|
StreamCount.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
stream=stream,
|
2021-02-12 08:20:45 +01:00
|
|
|
property="messages_in_stream:is_bot:day",
|
2018-08-16 18:23:52 +02:00
|
|
|
end_time=timezone_now(),
|
|
|
|
value=999,
|
|
|
|
)
|
|
|
|
|
2018-08-16 18:37:28 +02:00
|
|
|
result = get_streams_traffic(stream_ids)
|
2018-08-16 18:23:52 +02:00
|
|
|
self.assertEqual(result, {stream.id: 999})
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_subscriptions(self) -> None:
|
2017-07-12 17:28:27 +02:00
|
|
|
now = timezone_now()
|
2020-10-13 15:16:27 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
stream = self.make_stream("test_stream")
|
|
|
|
acting_user = self.example_user("iago")
|
2020-10-13 15:44:31 +02:00
|
|
|
bulk_add_subscriptions(user.realm, [stream], [user], acting_user=acting_user)
|
2021-02-12 08:19:30 +01:00
|
|
|
subscription_creation_logs = RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.SUBSCRIPTION_CREATED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=acting_user,
|
|
|
|
modified_user=user,
|
|
|
|
modified_stream=stream,
|
|
|
|
)
|
2021-07-24 16:56:39 +02:00
|
|
|
modified_stream = subscription_creation_logs[0].modified_stream
|
|
|
|
assert modified_stream is not None
|
2017-07-12 17:28:27 +02:00
|
|
|
self.assertEqual(subscription_creation_logs.count(), 1)
|
2021-07-24 16:56:39 +02:00
|
|
|
self.assertEqual(modified_stream.id, stream.id)
|
2020-10-13 15:44:31 +02:00
|
|
|
self.assertEqual(subscription_creation_logs[0].modified_user, user)
|
2017-07-12 17:28:27 +02:00
|
|
|
|
2021-08-14 02:28:52 +02:00
|
|
|
bulk_remove_subscriptions([user], [stream], acting_user=acting_user)
|
2021-02-12 08:19:30 +01:00
|
|
|
subscription_deactivation_logs = RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.SUBSCRIPTION_DEACTIVATED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=acting_user,
|
|
|
|
modified_user=user,
|
|
|
|
modified_stream=stream,
|
|
|
|
)
|
2021-07-24 16:56:39 +02:00
|
|
|
modified_stream = subscription_deactivation_logs[0].modified_stream
|
|
|
|
assert modified_stream is not None
|
2017-07-12 17:28:27 +02:00
|
|
|
self.assertEqual(subscription_deactivation_logs.count(), 1)
|
2021-07-24 16:56:39 +02:00
|
|
|
self.assertEqual(modified_stream.id, stream.id)
|
2020-10-13 15:44:31 +02:00
|
|
|
self.assertEqual(subscription_deactivation_logs[0].modified_user, user)
|
2019-10-08 06:05:22 +02:00
|
|
|
|
|
|
|
def test_realm_activation(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2021-04-02 17:11:25 +02:00
|
|
|
user = self.example_user("desdemona")
|
|
|
|
do_deactivate_realm(realm, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
log_entry = RealmAuditLog.objects.get(
|
2021-04-02 17:11:25 +02:00
|
|
|
realm=realm, event_type=RealmAuditLog.REALM_DEACTIVATED, acting_user=user
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-08-07 01:09:47 +02:00
|
|
|
extra_data = orjson.loads(log_entry.extra_data)
|
2019-10-08 06:05:22 +02:00
|
|
|
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
|
|
|
|
|
|
|
|
do_reactivate_realm(realm)
|
2021-02-12 08:19:30 +01:00
|
|
|
log_entry = RealmAuditLog.objects.get(
|
|
|
|
realm=realm, event_type=RealmAuditLog.REALM_REACTIVATED
|
|
|
|
)
|
2020-08-07 01:09:47 +02:00
|
|
|
extra_data = orjson.loads(log_entry.extra_data)
|
2019-10-08 06:05:22 +02:00
|
|
|
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
|
2020-06-29 23:31:25 +02:00
|
|
|
|
|
|
|
def test_create_stream_if_needed(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
stream = create_stream_if_needed(
|
|
|
|
realm,
|
|
|
|
"test",
|
|
|
|
invite_only=False,
|
2021-05-10 07:02:14 +02:00
|
|
|
stream_description="Test description",
|
2021-02-12 08:19:30 +01:00
|
|
|
acting_user=user,
|
|
|
|
)[0]
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.STREAM_CREATED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_stream=stream,
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-06-29 15:02:07 +02:00
|
|
|
|
|
|
|
def test_deactivate_stream(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
stream_name = "test"
|
2020-06-29 15:02:07 +02:00
|
|
|
stream = self.make_stream(stream_name, realm)
|
|
|
|
do_deactivate_stream(stream, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.STREAM_DEACTIVATED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_stream=stream,
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-06-29 15:02:07 +02:00
|
|
|
self.assertEqual(stream.deactivated, True)
|
2020-07-02 14:03:43 +02:00
|
|
|
|
|
|
|
def test_set_realm_authentication_methods(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2020-08-07 21:39:24 +02:00
|
|
|
expected_old_value = realm.authentication_methods_dict()
|
2021-02-12 08:19:30 +01:00
|
|
|
auth_method_dict = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"Google": False,
|
|
|
|
"Email": False,
|
|
|
|
"GitHub": False,
|
|
|
|
"Apple": False,
|
|
|
|
"Dev": True,
|
|
|
|
"SAML": True,
|
|
|
|
"GitLab": False,
|
2021-05-21 16:45:43 +02:00
|
|
|
"OpenID Connect": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
2020-07-02 14:03:43 +02:00
|
|
|
|
|
|
|
do_set_realm_authentication_methods(realm, auth_method_dict, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
realm_audit_logs = RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
)
|
2020-07-02 14:03:43 +02:00
|
|
|
self.assertEqual(realm_audit_logs.count(), 1)
|
2020-08-07 01:09:47 +02:00
|
|
|
extra_data = orjson.loads(realm_audit_logs[0].extra_data)
|
2020-08-07 21:39:24 +02:00
|
|
|
expected_new_value = auth_method_dict
|
2020-07-02 14:03:43 +02:00
|
|
|
self.assertEqual(extra_data[RealmAuditLog.OLD_VALUE], expected_old_value)
|
|
|
|
self.assertEqual(extra_data[RealmAuditLog.NEW_VALUE], expected_new_value)
|
2020-07-08 02:12:43 +02:00
|
|
|
|
|
|
|
def test_get_last_message_id(self) -> None:
|
|
|
|
# get_last_message_id is a helper mainly used for RealmAuditLog
|
|
|
|
self.assertEqual(
|
|
|
|
get_last_message_id(),
|
2021-02-12 08:20:45 +01:00
|
|
|
Message.objects.latest("id").id,
|
2020-07-08 02:12:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Message.objects.all().delete()
|
|
|
|
|
|
|
|
self.assertEqual(get_last_message_id(), -1)
|
2020-07-02 15:17:16 +02:00
|
|
|
|
|
|
|
def test_set_realm_message_editing(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2020-08-07 21:39:24 +02:00
|
|
|
values_expected = [
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"property": "message_content_edit_limit_seconds",
|
2020-08-07 21:39:24 +02:00
|
|
|
RealmAuditLog.OLD_VALUE: realm.message_content_edit_limit_seconds,
|
|
|
|
RealmAuditLog.NEW_VALUE: 1000,
|
|
|
|
},
|
|
|
|
{
|
2021-05-26 12:21:37 +02:00
|
|
|
"property": "edit_topic_policy",
|
|
|
|
RealmAuditLog.OLD_VALUE: Realm.POLICY_EVERYONE,
|
|
|
|
RealmAuditLog.NEW_VALUE: Realm.POLICY_ADMINS_ONLY,
|
2020-08-07 21:39:24 +02:00
|
|
|
},
|
|
|
|
]
|
2020-07-02 15:17:16 +02:00
|
|
|
|
2021-05-26 12:21:37 +02:00
|
|
|
do_set_realm_message_editing(realm, True, 1000, Realm.POLICY_ADMINS_ONLY, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
realm_audit_logs = RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
).order_by("id")
|
2020-07-02 15:17:16 +02:00
|
|
|
self.assertEqual(realm_audit_logs.count(), 2)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
[orjson.loads(entry.extra_data) for entry in realm_audit_logs], values_expected
|
|
|
|
)
|
2020-07-12 23:31:47 +02:00
|
|
|
|
|
|
|
def test_set_realm_notifications_stream(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2020-08-07 03:08:04 +02:00
|
|
|
old_value = realm.notifications_stream_id
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_name = "test"
|
2020-07-12 23:31:47 +02:00
|
|
|
stream = self.make_stream(stream_name, realm)
|
|
|
|
|
|
|
|
do_set_realm_notifications_stream(realm, stream, stream.id, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
2021-02-12 08:20:45 +01:00
|
|
|
"property": "notifications_stream",
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-12 23:31:47 +02:00
|
|
|
|
|
|
|
def test_set_realm_signup_notifications_stream(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2020-08-07 03:08:04 +02:00
|
|
|
old_value = realm.signup_notifications_stream_id
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_name = "test"
|
2020-07-12 23:31:47 +02:00
|
|
|
stream = self.make_stream(stream_name, realm)
|
|
|
|
|
|
|
|
do_set_realm_signup_notifications_stream(realm, stream, stream.id, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
2021-02-12 08:20:45 +01:00
|
|
|
"property": "signup_notifications_stream",
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-11 20:59:52 +02:00
|
|
|
|
|
|
|
def test_change_icon_source(self) -> None:
|
|
|
|
test_start = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
icon_source = "G"
|
2020-07-11 20:59:52 +02:00
|
|
|
do_change_icon_source(realm, icon_source, acting_user=user)
|
|
|
|
audit_entries = RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_ICON_SOURCE_CHANGED,
|
|
|
|
acting_user=user,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=test_start,
|
|
|
|
)
|
2021-07-24 16:56:39 +02:00
|
|
|
audit_log = audit_entries.first()
|
|
|
|
assert audit_log is not None
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(audit_entries, 1)
|
2020-07-11 20:59:52 +02:00
|
|
|
self.assertEqual(icon_source, realm.icon_source)
|
2021-07-24 16:56:39 +02:00
|
|
|
self.assertEqual(audit_log.extra_data, "{'icon_source': 'G', 'icon_version': 2}")
|
2020-07-12 23:48:08 +02:00
|
|
|
|
|
|
|
def test_change_subscription_property(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-07-12 23:48:08 +02:00
|
|
|
# Fetch the Denmark stream for testing
|
|
|
|
stream = get_stream("Denmark", user.realm)
|
2021-02-12 08:19:30 +01:00
|
|
|
sub = Subscription.objects.get(
|
|
|
|
user_profile=user, recipient__type=Recipient.STREAM, recipient__type_id=stream.id
|
|
|
|
)
|
|
|
|
properties = {
|
|
|
|
"color": True,
|
|
|
|
"is_muted": True,
|
|
|
|
"desktop_notifications": False,
|
|
|
|
"audible_notifications": False,
|
|
|
|
"push_notifications": True,
|
|
|
|
"email_notifications": True,
|
|
|
|
"pin_to_top": True,
|
|
|
|
"wildcard_mentions_notify": False,
|
|
|
|
}
|
2020-07-12 23:48:08 +02:00
|
|
|
|
|
|
|
for property, value in properties.items():
|
|
|
|
now = timezone_now()
|
|
|
|
|
|
|
|
old_value = getattr(sub, property)
|
|
|
|
self.assertNotEqual(old_value, value)
|
|
|
|
do_change_subscription_property(user, sub, stream, property, value, acting_user=user)
|
2020-08-07 21:39:24 +02:00
|
|
|
expected_extra_data = {
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: value,
|
2021-02-12 08:20:45 +01:00
|
|
|
"property": property,
|
2020-08-07 21:39:24 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
|
|
|
event_type=RealmAuditLog.SUBSCRIPTION_PROPERTY_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_user=user,
|
|
|
|
extra_data=orjson.dumps(expected_extra_data).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-12 23:48:08 +02:00
|
|
|
self.assertEqual(getattr(sub, property), value)
|
2020-07-12 23:45:50 +02:00
|
|
|
|
|
|
|
def test_change_default_streams(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-07-12 23:45:50 +02:00
|
|
|
stream = get_stream("Denmark", user.realm)
|
|
|
|
|
2020-08-07 03:08:04 +02:00
|
|
|
old_value = user.default_sending_stream_id
|
2020-07-12 23:45:50 +02:00
|
|
|
do_change_default_sending_stream(user, stream, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
|
|
|
event_type=RealmAuditLog.USER_DEFAULT_SENDING_STREAM_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-08-07 03:08:04 +02:00
|
|
|
self.assertEqual(user.default_sending_stream, stream)
|
2020-07-12 23:45:50 +02:00
|
|
|
|
2020-08-07 03:08:04 +02:00
|
|
|
old_value = user.default_events_register_stream_id
|
2020-07-12 23:45:50 +02:00
|
|
|
do_change_default_events_register_stream(user, stream, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
|
|
|
event_type=RealmAuditLog.USER_DEFAULT_REGISTER_STREAM_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-08-07 03:08:04 +02:00
|
|
|
self.assertEqual(user.default_events_register_stream, stream)
|
2020-07-12 23:45:50 +02:00
|
|
|
|
2020-08-07 03:08:04 +02:00
|
|
|
old_value = user.default_all_public_streams
|
2020-07-12 23:45:50 +02:00
|
|
|
do_change_default_all_public_streams(user, False, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
|
|
|
event_type=RealmAuditLog.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{RealmAuditLog.OLD_VALUE: old_value, RealmAuditLog.NEW_VALUE: False}
|
|
|
|
).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-08-07 03:08:04 +02:00
|
|
|
self.assertEqual(user.default_all_public_streams, False)
|
2020-07-12 23:39:54 +02:00
|
|
|
|
|
|
|
def test_rename_stream(self) -> None:
|
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
stream = self.make_stream("test", user.realm)
|
2020-07-12 23:39:54 +02:00
|
|
|
old_name = stream.name
|
2021-02-12 08:20:45 +01:00
|
|
|
do_rename_stream(stream, "updated name", user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
|
|
|
event_type=RealmAuditLog.STREAM_NAME_CHANGED,
|
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_stream=stream,
|
|
|
|
extra_data=orjson.dumps(
|
2021-02-12 08:20:45 +01:00
|
|
|
{RealmAuditLog.OLD_VALUE: old_name, RealmAuditLog.NEW_VALUE: "updated name"}
|
2021-02-12 08:19:30 +01:00
|
|
|
).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(stream.name, "updated name")
|
2020-07-12 23:41:53 +02:00
|
|
|
|
|
|
|
def test_change_notification_settings(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-07-12 23:41:53 +02:00
|
|
|
value: Union[bool, int, str]
|
|
|
|
for setting, v in user.notification_setting_types.items():
|
|
|
|
if setting == "notification_sound":
|
2021-02-12 08:20:45 +01:00
|
|
|
value = "ding"
|
2020-07-12 23:41:53 +02:00
|
|
|
elif setting == "desktop_icon_count_display":
|
|
|
|
value = 3
|
|
|
|
else:
|
|
|
|
value = False
|
|
|
|
now = timezone_now()
|
|
|
|
|
|
|
|
old_value = getattr(user, setting)
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(user, setting, value, acting_user=user)
|
2020-08-07 21:39:24 +02:00
|
|
|
expected_extra_data = {
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: value,
|
2021-02-12 08:20:45 +01:00
|
|
|
"property": setting,
|
2020-08-07 21:39:24 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2021-09-07 19:33:26 +02:00
|
|
|
event_type=RealmAuditLog.USER_SETTING_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_user=user,
|
|
|
|
extra_data=orjson.dumps(expected_extra_data).decode(),
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-12 23:41:53 +02:00
|
|
|
self.assertEqual(getattr(user, setting), value)
|