2020-06-11 00:54:34 +02:00
|
|
|
from datetime import timedelta
|
2024-07-12 02:30:23 +02:00
|
|
|
from typing import Any
|
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
|
2022-04-14 23:55:07 +02:00
|
|
|
from zerver.actions.bots import (
|
|
|
|
do_change_bot_owner,
|
|
|
|
do_change_default_all_public_streams,
|
|
|
|
do_change_default_events_register_stream,
|
|
|
|
do_change_default_sending_stream,
|
|
|
|
)
|
2023-07-06 14:27:14 +02:00
|
|
|
from zerver.actions.create_realm import do_create_realm
|
2022-04-14 23:53:15 +02:00
|
|
|
from zerver.actions.create_user import (
|
|
|
|
do_activate_mirror_dummy_user,
|
|
|
|
do_create_user,
|
|
|
|
do_reactivate_user,
|
|
|
|
)
|
2022-04-14 23:57:26 +02:00
|
|
|
from zerver.actions.realm_domains import (
|
|
|
|
do_add_realm_domain,
|
|
|
|
do_change_realm_domain,
|
|
|
|
do_remove_realm_domain,
|
|
|
|
)
|
2022-04-07 12:24:30 +02:00
|
|
|
from zerver.actions.realm_emoji import check_add_realm_emoji, do_remove_realm_emoji
|
2022-04-14 23:39:22 +02:00
|
|
|
from zerver.actions.realm_icon import do_change_icon_source
|
2022-03-14 14:50:55 +01:00
|
|
|
from zerver.actions.realm_linkifiers import (
|
|
|
|
do_add_linkifier,
|
|
|
|
do_remove_linkifier,
|
|
|
|
do_update_linkifier,
|
|
|
|
)
|
2023-07-24 21:14:42 +02:00
|
|
|
from zerver.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import (
|
|
|
|
do_deactivate_realm,
|
|
|
|
do_reactivate_realm,
|
|
|
|
do_set_realm_authentication_methods,
|
2024-02-07 12:13:02 +01:00
|
|
|
do_set_realm_new_stream_announcements_stream,
|
2022-09-22 10:53:37 +02:00
|
|
|
do_set_realm_property,
|
2024-02-07 17:11:43 +01:00
|
|
|
do_set_realm_signup_announcements_stream,
|
2024-01-26 14:45:37 +01:00
|
|
|
do_set_realm_zulip_update_announcements_stream,
|
2022-04-14 23:57:15 +02:00
|
|
|
)
|
2022-04-14 23:51:16 +02:00
|
|
|
from zerver.actions.streams import (
|
|
|
|
bulk_add_subscriptions,
|
|
|
|
bulk_remove_subscriptions,
|
|
|
|
do_change_subscription_property,
|
|
|
|
do_deactivate_stream,
|
|
|
|
do_rename_stream,
|
|
|
|
)
|
2022-11-21 07:26:34 +01:00
|
|
|
from zerver.actions.user_groups import (
|
2022-12-12 03:29:10 +01:00
|
|
|
add_subgroups_to_user_group,
|
2023-09-29 01:45:53 +02:00
|
|
|
bulk_add_members_to_user_groups,
|
|
|
|
bulk_remove_members_from_user_groups,
|
2022-11-21 07:26:34 +01:00
|
|
|
check_add_user_group,
|
2023-06-05 22:34:52 +02:00
|
|
|
do_change_user_group_permission_setting,
|
2024-05-15 15:44:18 +02:00
|
|
|
do_deactivate_user_group,
|
2023-06-05 22:34:52 +02:00
|
|
|
do_update_user_group_description,
|
|
|
|
do_update_user_group_name,
|
2022-12-12 03:29:10 +01:00
|
|
|
remove_subgroups_from_user_group,
|
2022-11-21 07:26:34 +01:00
|
|
|
)
|
2022-04-14 23:49:26 +02:00
|
|
|
from zerver.actions.user_settings import (
|
|
|
|
do_change_avatar_fields,
|
|
|
|
do_change_password,
|
|
|
|
do_change_tos_version,
|
|
|
|
do_change_user_delivery_email,
|
|
|
|
do_change_user_setting,
|
|
|
|
do_regenerate_api_key,
|
|
|
|
)
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import do_change_user_role, do_deactivate_user
|
2022-04-06 18:34:07 +02:00
|
|
|
from zerver.lib.emoji import get_emoji_file_name, get_emoji_url
|
2020-10-13 15:49:40 +02:00
|
|
|
from zerver.lib.message import get_last_message_id
|
2022-03-12 13:33:15 +01:00
|
|
|
from zerver.lib.stream_traffic import get_streams_traffic
|
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
|
2022-04-06 18:34:07 +02:00
|
|
|
from zerver.lib.test_helpers import get_test_image_file
|
2022-03-14 11:50:24 +01:00
|
|
|
from zerver.lib.types import LinkifierDict, RealmPlaygroundDict
|
2024-05-28 11:22:42 +02:00
|
|
|
from zerver.lib.user_groups import get_group_setting_value_for_api
|
2022-05-31 01:32:29 +02:00
|
|
|
from zerver.lib.utils import assert_is_not_none
|
2020-07-12 23:48:08 +02:00
|
|
|
from zerver.models import (
|
|
|
|
Message,
|
2024-04-18 10:50:51 +02:00
|
|
|
NamedUserGroup,
|
2020-07-12 23:48:08 +02:00
|
|
|
RealmAuditLog,
|
2022-03-11 15:40:42 +01:00
|
|
|
RealmPlayground,
|
2020-07-12 23:48:08 +02:00
|
|
|
Recipient,
|
|
|
|
Subscription,
|
|
|
|
UserProfile,
|
|
|
|
)
|
2023-12-15 01:55:59 +01:00
|
|
|
from zerver.models.groups import SystemGroups
|
2023-12-15 02:51:31 +01:00
|
|
|
from zerver.models.linkifiers import linkifiers_for_realm
|
2024-08-30 18:15:41 +02:00
|
|
|
from zerver.models.realm_audit_logs import AuditLogEventType
|
2023-12-15 02:43:18 +01:00
|
|
|
from zerver.models.realm_emoji import EmojiInfo, get_all_custom_emoji_for_realm
|
2023-12-15 02:58:46 +01:00
|
|
|
from zerver.models.realm_playgrounds import get_realm_playgrounds
|
2024-05-22 11:43:10 +02:00
|
|
|
from zerver.models.realms import EditTopicPolicyEnum, RealmDomainDict, get_realm, get_realm_domains
|
2023-12-15 03:57:04 +01:00
|
|
|
from zerver.models.streams import 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):
|
2024-07-12 02:30:17 +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)
|
2024-03-26 13:58:30 +01:00
|
|
|
self.assertEqual(RealmAuditLog.objects.filter(event_time__gte=now).count(), 10)
|
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),
|
|
|
|
)
|
2023-07-13 19:46:06 +02:00
|
|
|
.order_by("event_time", "event_type")
|
2021-02-12 08:20:45 +01:00
|
|
|
.values_list("event_type", flat=True)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
event_types,
|
|
|
|
[
|
2024-08-30 18:15:41 +02:00
|
|
|
AuditLogEventType.USER_CREATED,
|
2024-09-06 17:06:03 +02:00
|
|
|
AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
|
|
|
AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
2024-08-30 18:15:41 +02:00
|
|
|
AuditLogEventType.USER_DEACTIVATED,
|
|
|
|
AuditLogEventType.USER_ACTIVATED,
|
|
|
|
AuditLogEventType.USER_DEACTIVATED,
|
|
|
|
AuditLogEventType.USER_REACTIVATED,
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2022-11-21 07:26:34 +01:00
|
|
|
modified_user_group_names = []
|
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),
|
|
|
|
):
|
2024-09-06 17:06:03 +02:00
|
|
|
if event.event_type == AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED:
|
2023-07-13 19:46:06 +02:00
|
|
|
self.assertDictEqual(event.extra_data, {})
|
2022-11-21 07:26:34 +01:00
|
|
|
modified_user_group_names.append(assert_is_not_none(event.modified_user_group).name)
|
|
|
|
continue
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data = 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)
|
|
|
|
|
2022-11-21 07:26:34 +01:00
|
|
|
self.assertListEqual(
|
|
|
|
modified_user_group_names,
|
|
|
|
[
|
2023-09-21 13:06:39 +02:00
|
|
|
SystemGroups.MEMBERS,
|
|
|
|
SystemGroups.FULL_MEMBERS,
|
2022-11-21 07:26:34 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2019-10-05 02:36:16 +02:00
|
|
|
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(
|
2024-08-30 18:15:41 +02:00
|
|
|
event_type=AuditLogEventType.USER_ROLE_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
realm=realm,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_time__lte=now + timedelta(minutes=60),
|
|
|
|
):
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data = 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
|
|
|
|
2022-11-21 07:26:34 +01:00
|
|
|
expected_system_user_group_names = [
|
2023-09-21 13:06:39 +02:00
|
|
|
SystemGroups.ADMINISTRATORS,
|
|
|
|
SystemGroups.MEMBERS,
|
|
|
|
SystemGroups.FULL_MEMBERS,
|
|
|
|
SystemGroups.EVERYONE,
|
|
|
|
SystemGroups.MEMBERS,
|
|
|
|
SystemGroups.FULL_MEMBERS,
|
|
|
|
SystemGroups.OWNERS,
|
|
|
|
SystemGroups.MEMBERS,
|
|
|
|
SystemGroups.FULL_MEMBERS,
|
|
|
|
SystemGroups.MODERATORS,
|
2022-11-21 07:26:34 +01:00
|
|
|
]
|
|
|
|
user_group_modified_names = (
|
|
|
|
RealmAuditLog.objects.filter(
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
2022-11-21 07:26:34 +01:00
|
|
|
realm=realm,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_time__lte=now + timedelta(minutes=60),
|
|
|
|
)
|
|
|
|
.order_by("event_time")
|
|
|
|
.values_list("modified_user_group__name", flat=True)
|
|
|
|
)
|
|
|
|
self.assertListEqual(
|
|
|
|
list(user_group_modified_names),
|
|
|
|
[
|
|
|
|
*expected_system_user_group_names,
|
2023-09-21 13:06:39 +02:00
|
|
|
SystemGroups.MEMBERS,
|
|
|
|
SystemGroups.FULL_MEMBERS,
|
2022-11-21 07:26:34 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
user_group_modified_names = (
|
|
|
|
RealmAuditLog.objects.filter(
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_REMOVED,
|
2022-11-21 07:26:34 +01:00
|
|
|
realm=realm,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_time__lte=now + timedelta(minutes=60),
|
|
|
|
)
|
|
|
|
.order_by("event_time")
|
|
|
|
.values_list("modified_user_group__name", flat=True)
|
|
|
|
)
|
|
|
|
self.assertListEqual(
|
|
|
|
list(user_group_modified_names),
|
|
|
|
[
|
2023-09-21 13:06:39 +02:00
|
|
|
SystemGroups.MEMBERS,
|
|
|
|
SystemGroups.FULL_MEMBERS,
|
2022-11-21 07:26:34 +01:00
|
|
|
*expected_system_user_group_names,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_PASSWORD_CHANGED, event_time__gte=now
|
2021-02-12 08:19:30 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-07-19 22:41:49 +02:00
|
|
|
# No error should be raised here
|
|
|
|
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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now
|
2021-02-12 08:19:30 +01:00
|
|
|
).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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
self.assertTrue(
|
2023-03-08 22:18:59 +01:00
|
|
|
repr(audit_entry).startswith(
|
2024-09-06 20:28:32 +02:00
|
|
|
f"<RealmAuditLog: {AuditLogEventType.USER_EMAIL_CHANGED.name} "
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_AVATAR_SOURCE_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
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")
|
2021-10-16 19:52:57 +02:00
|
|
|
req = dict(full_name=new_name)
|
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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_FULL_NAME_CHANGED, event_time__gte=start
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_TERMS_OF_SERVICE_VERSION_CHANGED,
|
|
|
|
event_time__gte=now,
|
2021-02-12 08:19:30 +01:00
|
|
|
).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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_BOT_OWNER_CHANGED, event_time__gte=now
|
2021-02-12 08:19:30 +01:00
|
|
|
).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(
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_API_KEY_CHANGED, event_time__gte=now
|
2021-02-12 08:19:30 +01:00
|
|
|
).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}
|
|
|
|
|
2023-11-30 21:05:34 +01:00
|
|
|
result = get_streams_traffic(stream_ids, realm)
|
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,
|
|
|
|
)
|
|
|
|
|
2023-11-30 21:05:34 +01:00
|
|
|
result = get_streams_traffic(stream_ids, realm)
|
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")
|
2021-12-24 14:29:40 +01:00
|
|
|
realm = user.realm
|
2021-02-12 08:20:45 +01:00
|
|
|
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(
|
2024-09-03 17:25:32 +02:00
|
|
|
event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
|
2021-02-12 08:19:30 +01:00
|
|
|
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-12-24 14:29:40 +01:00
|
|
|
bulk_remove_subscriptions(realm, [user], [stream], acting_user=acting_user)
|
2021-02-12 08:19:30 +01:00
|
|
|
subscription_deactivation_logs = RealmAuditLog.objects.filter(
|
2024-09-03 17:25:32 +02:00
|
|
|
event_type=AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
|
2021-02-12 08:19:30 +01:00
|
|
|
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")
|
2023-09-25 22:59:44 +02:00
|
|
|
do_deactivate_realm(
|
|
|
|
realm, acting_user=user, deactivation_reason="owner_request", email_owners=False
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
log_entry = RealmAuditLog.objects.get(
|
2024-09-03 15:58:19 +02:00
|
|
|
realm=realm, event_type=AuditLogEventType.REALM_DEACTIVATED, acting_user=user
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data = log_entry.extra_data
|
2024-05-19 01:30:36 +02:00
|
|
|
|
|
|
|
deactivation_reason = extra_data["deactivation_reason"]
|
|
|
|
self.assertEqual(deactivation_reason, "owner_request")
|
|
|
|
|
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(
|
2024-09-03 15:58:19 +02:00
|
|
|
realm=realm, event_type=AuditLogEventType.REALM_REACTIVATED
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data = 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,
|
2024-09-06 13:56:53 +02:00
|
|
|
event_type=AuditLogEventType.CHANNEL_CREATED,
|
2021-02-12 08:19:30 +01:00
|
|
|
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,
|
2024-09-06 13:56:53 +02:00
|
|
|
event_type=AuditLogEventType.CHANNEL_DEACTIVATED,
|
2021-02-12 08:19:30 +01:00
|
|
|
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,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
)
|
2020-07-02 14:03:43 +02:00
|
|
|
self.assertEqual(realm_audit_logs.count(), 1)
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data = 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")
|
2022-09-22 10:53:37 +02:00
|
|
|
value_expected = {
|
|
|
|
RealmAuditLog.OLD_VALUE: realm.message_content_edit_limit_seconds,
|
|
|
|
RealmAuditLog.NEW_VALUE: 1000,
|
|
|
|
"property": "message_content_edit_limit_seconds",
|
|
|
|
}
|
2020-07-02 15:17:16 +02:00
|
|
|
|
2022-09-22 10:53:37 +02:00
|
|
|
do_set_realm_property(realm, "message_content_edit_limit_seconds", 1000, acting_user=user)
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
|
2022-09-22 10:53:37 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=value_expected,
|
2022-09-22 10:53:37 +02:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
|
|
|
|
value_expected = {
|
2024-05-22 11:43:10 +02:00
|
|
|
RealmAuditLog.OLD_VALUE: EditTopicPolicyEnum.EVERYONE,
|
|
|
|
RealmAuditLog.NEW_VALUE: EditTopicPolicyEnum.ADMINS_ONLY,
|
2022-09-22 10:53:37 +02:00
|
|
|
"property": "edit_topic_policy",
|
|
|
|
}
|
|
|
|
|
|
|
|
do_set_realm_property(
|
2024-05-22 11:43:10 +02:00
|
|
|
realm, "edit_topic_policy", EditTopicPolicyEnum.ADMINS_ONLY, acting_user=user
|
2022-09-22 10:53:37 +02:00
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2022-09-22 10:53:37 +02:00
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
|
2022-09-22 10:53:37 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=value_expected,
|
2022-09-22 10:53:37 +02:00
|
|
|
).count(),
|
|
|
|
1,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-07-12 23:31:47 +02:00
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
def test_set_realm_new_stream_announcements_stream(self) -> None:
|
2020-07-12 23:31:47 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2024-02-07 12:13:02 +01:00
|
|
|
old_value = realm.new_stream_announcements_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)
|
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
do_set_realm_new_stream_announcements_stream(realm, stream, stream.id, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
2024-02-07 12:13:02 +01:00
|
|
|
"property": "new_stream_announcements_stream",
|
2023-07-13 19:46:06 +02:00
|
|
|
},
|
2021-02-12 08:19:30 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-12 23:31:47 +02:00
|
|
|
|
2024-02-07 17:11:43 +01:00
|
|
|
def test_set_realm_signup_announcements_stream(self) -> None:
|
2020-07-12 23:31:47 +02:00
|
|
|
now = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
2024-02-07 17:11:43 +01:00
|
|
|
old_value = realm.signup_announcements_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)
|
|
|
|
|
2024-02-07 17:11:43 +01:00
|
|
|
do_set_realm_signup_announcements_stream(realm, stream, stream.id, acting_user=user)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
2024-02-07 17:11:43 +01:00
|
|
|
"property": "signup_announcements_stream",
|
2023-07-13 19:46:06 +02:00
|
|
|
},
|
2021-02-12 08:19:30 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-11 20:59:52 +02:00
|
|
|
|
2024-01-26 14:45:37 +01:00
|
|
|
def test_set_realm_zulip_update_announcements_stream(self) -> None:
|
|
|
|
now = timezone_now()
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
old_value = realm.zulip_update_announcements_stream_id
|
|
|
|
stream_name = "test"
|
|
|
|
stream = self.make_stream(stream_name, realm)
|
|
|
|
|
|
|
|
do_set_realm_zulip_update_announcements_stream(realm, stream, stream.id, acting_user=user)
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
|
2024-01-26 14:45:37 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
|
|
|
"property": "zulip_update_announcements_stream",
|
|
|
|
},
|
|
|
|
).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,
|
2024-09-03 15:58:19 +02:00
|
|
|
event_type=AuditLogEventType.REALM_ICON_SOURCE_CHANGED,
|
2020-07-11 20:59:52 +02:00
|
|
|
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)
|
2023-07-13 19:46:06 +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,
|
2024-09-03 17:25:32 +02:00
|
|
|
event_type=AuditLogEventType.SUBSCRIPTION_PROPERTY_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2021-02-12 08:19:30 +01:00
|
|
|
).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,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_DEFAULT_SENDING_STREAM_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
|
|
|
},
|
2021-02-12 08:19:30 +01:00
|
|
|
).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,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_DEFAULT_REGISTER_STREAM_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: stream.id,
|
|
|
|
},
|
2021-02-12 08:19:30 +01:00
|
|
|
).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,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={RealmAuditLog.OLD_VALUE: old_value, RealmAuditLog.NEW_VALUE: False},
|
2021-02-12 08:19:30 +01:00
|
|
|
).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,
|
2024-09-06 13:56:53 +02:00
|
|
|
event_type=AuditLogEventType.CHANNEL_NAME_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_stream=stream,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_name,
|
|
|
|
RealmAuditLog.NEW_VALUE: "updated name",
|
|
|
|
},
|
2021-02-12 08:19:30 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(stream.name, "updated name")
|
2020-07-12 23:41:53 +02:00
|
|
|
|
2023-03-23 13:36:02 +01:00
|
|
|
def test_change_user_settings(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2024-07-12 02:30:23 +02:00
|
|
|
value: bool | int | str
|
2023-03-23 13:36:02 +01:00
|
|
|
test_values = dict(
|
|
|
|
default_language="de",
|
2024-07-21 15:23:56 +02:00
|
|
|
web_animate_image_previews="on_hover",
|
2023-10-23 09:02:57 +02:00
|
|
|
web_home_view="all_messages",
|
2023-03-23 13:36:02 +01:00
|
|
|
emojiset="twitter",
|
|
|
|
notification_sound="ding",
|
|
|
|
)
|
|
|
|
|
|
|
|
for setting, setting_type in user.property_types.items():
|
|
|
|
if setting in test_values:
|
|
|
|
value = test_values[setting]
|
|
|
|
elif setting_type is int:
|
2020-07-12 23:41:53 +02:00
|
|
|
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,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_SETTING_CHANGED,
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
|
|
|
modified_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2021-02-12 08:19:30 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2020-07-12 23:41:53 +02:00
|
|
|
self.assertEqual(getattr(user, setting), value)
|
2022-03-07 14:49:16 +01:00
|
|
|
|
|
|
|
def test_realm_domain_entries(self) -> None:
|
|
|
|
user = self.example_user("iago")
|
|
|
|
initial_domains = get_realm_domains(user.realm)
|
|
|
|
|
|
|
|
now = timezone_now()
|
2022-03-07 15:19:13 +01:00
|
|
|
realm_domain = do_add_realm_domain(user.realm, "zulip.org", False, acting_user=user)
|
2022-06-23 20:04:16 +02:00
|
|
|
added_domain = RealmDomainDict(
|
|
|
|
domain="zulip.org",
|
|
|
|
allow_subdomains=False,
|
|
|
|
)
|
2022-03-07 14:49:16 +01:00
|
|
|
expected_extra_data = {
|
2023-01-26 00:12:09 +01:00
|
|
|
"realm_domains": [*initial_domains, added_domain],
|
2022-03-07 14:49:16 +01:00
|
|
|
"added_domain": added_domain,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_DOMAIN_ADDED,
|
2022-03-07 14:49:16 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-07 14:49:16 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-07 15:19:13 +01:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_change_realm_domain(realm_domain, True, acting_user=user)
|
2022-06-23 20:04:16 +02:00
|
|
|
changed_domain = RealmDomainDict(
|
|
|
|
domain="zulip.org",
|
|
|
|
allow_subdomains=True,
|
|
|
|
)
|
2022-03-07 15:19:13 +01:00
|
|
|
expected_extra_data = {
|
2023-01-26 00:12:09 +01:00
|
|
|
"realm_domains": [*initial_domains, changed_domain],
|
2022-03-07 15:19:13 +01:00
|
|
|
"changed_domain": changed_domain,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_DOMAIN_CHANGED,
|
2022-03-07 15:19:13 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-07 15:19:13 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-07 15:35:17 +01:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_remove_realm_domain(realm_domain, acting_user=user)
|
2022-06-23 20:04:16 +02:00
|
|
|
removed_domain = RealmDomainDict(
|
|
|
|
domain="zulip.org",
|
|
|
|
allow_subdomains=True,
|
|
|
|
)
|
2022-03-07 15:35:17 +01:00
|
|
|
expected_extra_data = {
|
|
|
|
"realm_domains": initial_domains,
|
|
|
|
"removed_domain": removed_domain,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_DOMAIN_REMOVED,
|
2022-03-07 15:35:17 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-07 15:35:17 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-11 15:16:04 +01:00
|
|
|
|
|
|
|
def test_realm_playground_entries(self) -> None:
|
|
|
|
user = self.example_user("iago")
|
2023-01-02 20:50:23 +01:00
|
|
|
initial_playgrounds = get_realm_playgrounds(user.realm)
|
2022-03-11 15:16:04 +01:00
|
|
|
now = timezone_now()
|
2023-07-24 21:14:42 +02:00
|
|
|
playground_id = check_add_realm_playground(
|
2022-03-11 15:16:04 +01:00
|
|
|
user.realm,
|
|
|
|
acting_user=user,
|
|
|
|
name="Python playground",
|
|
|
|
pygments_language="Python",
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template="https://python.example.com{code}",
|
2022-03-11 15:16:04 +01:00
|
|
|
)
|
2022-03-15 11:10:29 +01:00
|
|
|
added_playground = RealmPlaygroundDict(
|
|
|
|
id=playground_id,
|
|
|
|
name="Python playground",
|
|
|
|
pygments_language="Python",
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template="https://python.example.com{code}",
|
2022-03-15 11:10:29 +01:00
|
|
|
)
|
2022-03-11 15:16:04 +01:00
|
|
|
expected_extra_data = {
|
2023-01-26 00:12:09 +01:00
|
|
|
"realm_playgrounds": [*initial_playgrounds, added_playground],
|
2022-03-11 15:16:04 +01:00
|
|
|
"added_playground": added_playground,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PLAYGROUND_ADDED,
|
2022-03-11 15:16:04 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-11 15:16:04 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-11 15:40:42 +01:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
realm_playground = RealmPlayground.objects.get(id=playground_id)
|
|
|
|
do_remove_realm_playground(
|
|
|
|
user.realm,
|
|
|
|
realm_playground,
|
|
|
|
acting_user=user,
|
|
|
|
)
|
|
|
|
removed_playground = {
|
|
|
|
"name": "Python playground",
|
|
|
|
"pygments_language": "Python",
|
2023-05-27 03:29:40 +02:00
|
|
|
"url_template": "https://python.example.com{code}",
|
2022-03-11 15:40:42 +01:00
|
|
|
}
|
|
|
|
expected_extra_data = {
|
2023-01-02 20:50:23 +01:00
|
|
|
"realm_playgrounds": initial_playgrounds,
|
2022-03-11 15:40:42 +01:00
|
|
|
"removed_playground": removed_playground,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_PLAYGROUND_REMOVED,
|
2022-03-11 15:40:42 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-11 15:40:42 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-14 11:50:24 +01:00
|
|
|
|
|
|
|
def test_realm_linkifier_entries(self) -> None:
|
|
|
|
user = self.example_user("iago")
|
2023-01-02 20:50:23 +01:00
|
|
|
initial_linkifiers = linkifiers_for_realm(user.realm.id)
|
2022-03-14 11:50:24 +01:00
|
|
|
now = timezone_now()
|
|
|
|
linkifier_id = do_add_linkifier(
|
|
|
|
user.realm,
|
|
|
|
pattern="#(?P<id>[123])",
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template="https://realm.com/my_realm_filter/{id}",
|
2022-03-14 11:50:24 +01:00
|
|
|
acting_user=user,
|
|
|
|
)
|
|
|
|
|
|
|
|
added_linkfier = LinkifierDict(
|
|
|
|
pattern="#(?P<id>[123])",
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template="https://realm.com/my_realm_filter/{id}",
|
2022-03-14 11:50:24 +01:00
|
|
|
id=linkifier_id,
|
|
|
|
)
|
|
|
|
expected_extra_data = {
|
2023-01-26 00:12:09 +01:00
|
|
|
"realm_linkifiers": [*initial_linkifiers, added_linkfier],
|
2022-03-14 11:50:24 +01:00
|
|
|
"added_linkifier": added_linkfier,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_LINKIFIER_ADDED,
|
2022-03-14 11:50:24 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-14 11:50:24 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-14 12:10:25 +01:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_update_linkifier(
|
|
|
|
user.realm,
|
|
|
|
id=linkifier_id,
|
|
|
|
pattern="#(?P<id>[0-9]+)",
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template="https://realm.com/my_realm_filter/issues/{id}",
|
2022-03-14 12:10:25 +01:00
|
|
|
acting_user=user,
|
|
|
|
)
|
|
|
|
changed_linkifier = LinkifierDict(
|
|
|
|
pattern="#(?P<id>[0-9]+)",
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template="https://realm.com/my_realm_filter/issues/{id}",
|
2022-03-14 12:10:25 +01:00
|
|
|
id=linkifier_id,
|
|
|
|
)
|
|
|
|
expected_extra_data = {
|
2023-01-26 00:12:09 +01:00
|
|
|
"realm_linkifiers": [*initial_linkifiers, changed_linkifier],
|
2022-03-14 12:10:25 +01:00
|
|
|
"changed_linkifier": changed_linkifier,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_LINKIFIER_CHANGED,
|
2022-03-14 12:10:25 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-14 12:10:25 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-03-14 14:50:55 +01:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_remove_linkifier(
|
|
|
|
user.realm,
|
|
|
|
id=linkifier_id,
|
|
|
|
acting_user=user,
|
|
|
|
)
|
|
|
|
removed_linkifier = {
|
|
|
|
"pattern": "#(?P<id>[0-9]+)",
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
"url_template": "https://realm.com/my_realm_filter/issues/{id}",
|
2022-03-14 14:50:55 +01:00
|
|
|
}
|
|
|
|
expected_extra_data = {
|
2023-01-02 20:50:23 +01:00
|
|
|
"realm_linkifiers": initial_linkifiers,
|
2022-03-14 14:50:55 +01:00
|
|
|
"removed_linkifier": removed_linkifier,
|
|
|
|
}
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_LINKIFIER_REMOVED,
|
2022-03-14 14:50:55 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-03-14 14:50:55 +01:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-04-06 18:34:07 +02:00
|
|
|
|
|
|
|
def test_realm_emoji_entries(self) -> None:
|
|
|
|
user = self.example_user("iago")
|
2023-07-14 12:37:29 +02:00
|
|
|
realm_emoji_dict = get_all_custom_emoji_for_realm(user.realm_id)
|
2022-04-06 18:34:07 +02:00
|
|
|
now = timezone_now()
|
|
|
|
with get_test_image_file("img.png") as img_file:
|
|
|
|
# Because we want to verify the IntegrityError handling
|
|
|
|
# logic in check_add_realm_emoji rather than the primary
|
|
|
|
# check in upload_emoji, we need to make this request via
|
|
|
|
# that helper rather than via the API.
|
|
|
|
realm_emoji = check_add_realm_emoji(
|
2024-07-11 20:46:33 +02:00
|
|
|
realm=user.realm,
|
|
|
|
name="test_emoji",
|
|
|
|
author=user,
|
|
|
|
image_file=img_file,
|
|
|
|
content_type="image/png",
|
2022-04-06 18:34:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
added_emoji = EmojiInfo(
|
|
|
|
id=str(realm_emoji.id),
|
|
|
|
name="test_emoji",
|
2024-07-11 21:10:17 +02:00
|
|
|
source_url=get_emoji_url(
|
|
|
|
get_emoji_file_name("image/png", realm_emoji.id), user.realm_id
|
|
|
|
),
|
2022-04-06 18:34:07 +02:00
|
|
|
deactivated=False,
|
|
|
|
author_id=user.id,
|
|
|
|
still_url=None,
|
|
|
|
)
|
|
|
|
realm_emoji_dict[str(realm_emoji.id)] = added_emoji
|
|
|
|
expected_extra_data = {
|
|
|
|
"realm_emoji": dict(sorted(realm_emoji_dict.items())),
|
|
|
|
"added_emoji": added_emoji,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_EMOJI_ADDED,
|
2022-04-06 18:34:07 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-04-06 18:34:07 +02:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-04-07 12:24:30 +02:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_remove_realm_emoji(user.realm, "test_emoji", acting_user=user)
|
|
|
|
|
|
|
|
deactivated_emoji = EmojiInfo(
|
|
|
|
id=str(realm_emoji.id),
|
|
|
|
name="test_emoji",
|
2024-07-11 21:10:17 +02:00
|
|
|
source_url=get_emoji_url(
|
|
|
|
get_emoji_file_name("image/png", realm_emoji.id), user.realm_id
|
|
|
|
),
|
2022-04-07 12:24:30 +02:00
|
|
|
deactivated=True,
|
|
|
|
author_id=user.id,
|
|
|
|
still_url=None,
|
|
|
|
)
|
|
|
|
realm_emoji_dict[str(realm_emoji.id)] = deactivated_emoji
|
|
|
|
|
|
|
|
expected_extra_data = {
|
|
|
|
"realm_emoji": dict(sorted(realm_emoji_dict.items())),
|
|
|
|
"deactivated_emoji": deactivated_emoji,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=user.realm,
|
2024-09-03 16:46:18 +02:00
|
|
|
event_type=AuditLogEventType.REALM_EMOJI_REMOVED,
|
2022-04-07 12:24:30 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data=expected_extra_data,
|
2022-04-07 12:24:30 +02:00
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2022-11-21 05:43:03 +01:00
|
|
|
|
|
|
|
def test_system_user_groups_creation(self) -> None:
|
|
|
|
now = timezone_now()
|
2023-07-06 14:27:14 +02:00
|
|
|
realm = do_create_realm(string_id="test", name="foo")
|
2022-11-21 05:43:03 +01:00
|
|
|
|
|
|
|
# The expected number of system user group is the total number of roles
|
2024-04-18 10:50:51 +02:00
|
|
|
# from NamedUserGroup.SYSTEM_USER_GROUP_ROLE_MAP in addition to
|
2022-11-21 05:43:03 +01:00
|
|
|
# full_members_system_group, everyone_on_internet_system_group and
|
|
|
|
# nobody_system_group.
|
2024-04-18 10:50:51 +02:00
|
|
|
expected_system_user_group_count = len(NamedUserGroup.SYSTEM_USER_GROUP_ROLE_MAP) + 3
|
2022-11-21 05:43:03 +01:00
|
|
|
|
|
|
|
system_user_group_ids = sorted(
|
2024-04-18 12:23:46 +02:00
|
|
|
NamedUserGroup.objects.filter(
|
2022-11-21 05:43:03 +01:00
|
|
|
realm=realm,
|
|
|
|
is_system_group=True,
|
|
|
|
).values_list("id", flat=True)
|
|
|
|
)
|
|
|
|
self.assert_length(system_user_group_ids, expected_system_user_group_count)
|
|
|
|
|
|
|
|
logged_system_group_ids = sorted(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_CREATED,
|
2022-11-21 05:43:03 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=None,
|
|
|
|
).values_list("modified_user_group_id", flat=True)
|
|
|
|
)
|
|
|
|
self.assertListEqual(logged_system_group_ids, system_user_group_ids)
|
|
|
|
|
2022-12-12 03:29:10 +01:00
|
|
|
logged_subgroup_entries = sorted(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUBGROUP_MEMBERSHIP_ADDED,
|
2022-12-12 03:29:10 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=None,
|
|
|
|
).values_list("modified_user_group_id", "extra_data")
|
|
|
|
)
|
2022-12-12 03:29:10 +01:00
|
|
|
logged_supergroup_entries = sorted(
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUPERGROUP_MEMBERSHIP_ADDED,
|
2022-12-12 03:29:10 +01:00
|
|
|
event_time__gte=now,
|
|
|
|
acting_user=None,
|
|
|
|
).values_list("modified_user_group_id", "extra_data")
|
|
|
|
)
|
2022-12-12 03:29:10 +01:00
|
|
|
# Excluding nobody_system_group, the rest of the user groups should have
|
|
|
|
# a chain of subgroup memberships in between.
|
|
|
|
self.assert_length(logged_subgroup_entries, expected_system_user_group_count - 2)
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assert_length(logged_supergroup_entries, expected_system_user_group_count - 2)
|
2022-12-12 03:29:10 +01:00
|
|
|
for i in range(len(logged_subgroup_entries)):
|
|
|
|
# The offset of 1 is due to nobody_system_group being skipped as
|
|
|
|
# the first user group in the list.
|
|
|
|
# For supergroup, we add an additional 1 because of the order we
|
|
|
|
# put the chain together.
|
|
|
|
expected_subgroup_id = system_user_group_ids[i + 1]
|
|
|
|
expected_supergroup_id = system_user_group_ids[i + 2]
|
|
|
|
|
|
|
|
supergroup_id, subgroup_extra_data = logged_subgroup_entries[i]
|
2022-12-12 03:29:10 +01:00
|
|
|
subgroup_id, supergroup_extra_data = logged_supergroup_entries[i]
|
2023-07-13 19:46:06 +02:00
|
|
|
self.assertEqual(subgroup_extra_data["subgroup_ids"][0], expected_subgroup_id)
|
|
|
|
self.assertEqual(supergroup_extra_data["supergroup_ids"][0], expected_supergroup_id)
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assertEqual(supergroup_id, expected_supergroup_id)
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assertEqual(subgroup_id, expected_subgroup_id)
|
2022-12-12 03:29:10 +01:00
|
|
|
|
2022-11-21 05:43:03 +01:00
|
|
|
def test_user_group_creation(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
now = timezone_now()
|
2024-04-18 12:23:46 +02:00
|
|
|
public_group = NamedUserGroup.objects.get(
|
2023-09-21 13:06:39 +02:00
|
|
|
name=SystemGroups.EVERYONE_ON_INTERNET, realm=hamlet.realm
|
2023-06-05 22:34:52 +02:00
|
|
|
)
|
2022-11-21 05:43:03 +01:00
|
|
|
user_group = check_add_user_group(
|
2023-06-05 22:34:52 +02:00
|
|
|
hamlet.realm,
|
|
|
|
"empty",
|
|
|
|
[hamlet, cordelia],
|
|
|
|
acting_user=hamlet,
|
|
|
|
description="lorem",
|
|
|
|
group_settings_map={"can_mention_group": public_group},
|
2022-11-21 05:43:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
acting_user=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_CREATED,
|
2022-11-21 05:43:03 +01:00
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertIsNone(audit_log_entries[0].modified_user)
|
2024-04-17 16:34:39 +02:00
|
|
|
self.assertEqual(audit_log_entries[0].modified_user_group, user_group)
|
2022-11-21 05:43:03 +01:00
|
|
|
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
acting_user=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
2022-11-21 05:43:03 +01:00
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 2)
|
|
|
|
self.assertEqual(audit_log_entries[0].modified_user, hamlet)
|
|
|
|
self.assertEqual(audit_log_entries[1].modified_user, cordelia)
|
2022-11-21 07:26:34 +01:00
|
|
|
|
|
|
|
def test_change_user_group_memberships(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
now = timezone_now()
|
|
|
|
user_group = check_add_user_group(hamlet.realm, "foo", [], acting_user=None)
|
|
|
|
|
2023-09-29 01:45:53 +02:00
|
|
|
bulk_add_members_to_user_groups([user_group], [hamlet.id, cordelia.id], acting_user=hamlet)
|
2022-11-21 07:26:34 +01:00
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
acting_user=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
modified_user_group=user_group,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
2022-11-21 07:26:34 +01:00
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 2)
|
|
|
|
self.assertEqual(audit_log_entries[0].modified_user, hamlet)
|
|
|
|
self.assertEqual(audit_log_entries[1].modified_user, cordelia)
|
|
|
|
|
2023-09-29 01:45:53 +02:00
|
|
|
bulk_remove_members_from_user_groups([user_group], [hamlet.id], acting_user=hamlet)
|
2022-11-21 07:26:34 +01:00
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
acting_user=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
modified_user_group=user_group,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_REMOVED,
|
2022-11-21 07:26:34 +01:00
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertEqual(audit_log_entries[0].modified_user, hamlet)
|
2022-12-12 03:29:10 +01:00
|
|
|
|
|
|
|
def test_change_user_group_subgroups_memberships(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
user_group = check_add_user_group(hamlet.realm, "main", [], acting_user=None)
|
|
|
|
subgroups = [
|
2024-04-17 05:45:32 +02:00
|
|
|
check_add_user_group(hamlet.realm, f"subgroup{num}", [], acting_user=hamlet)
|
2022-12-12 03:29:10 +01:00
|
|
|
for num in range(3)
|
|
|
|
]
|
|
|
|
|
|
|
|
now = timezone_now()
|
2024-04-17 05:45:32 +02:00
|
|
|
add_subgroups_to_user_group(user_group, subgroups, acting_user=hamlet)
|
2022-12-12 03:29:10 +01:00
|
|
|
# Only one audit log entry for the subgroup membership is expected.
|
|
|
|
audit_log_entry = RealmAuditLog.objects.get(
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUBGROUP_MEMBERSHIP_ADDED,
|
2022-12-12 03:29:10 +01:00
|
|
|
)
|
2024-04-17 16:34:39 +02:00
|
|
|
self.assertEqual(audit_log_entry.modified_user_group, user_group)
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assertEqual(audit_log_entry.acting_user, hamlet)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entry.extra_data,
|
2022-12-12 03:29:10 +01:00
|
|
|
{"subgroup_ids": [subgroup.id for subgroup in subgroups]},
|
|
|
|
)
|
2022-12-12 03:29:10 +01:00
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUPERGROUP_MEMBERSHIP_ADDED,
|
2022-12-12 03:29:10 +01:00
|
|
|
).order_by("id")
|
|
|
|
self.assert_length(audit_log_entries, 3)
|
|
|
|
for i in range(3):
|
2024-04-17 16:34:39 +02:00
|
|
|
self.assertEqual(audit_log_entries[i].modified_user_group, subgroups[i])
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assertEqual(audit_log_entries[i].acting_user, hamlet)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entries[i].extra_data,
|
2022-12-12 03:29:10 +01:00
|
|
|
{"supergroup_ids": [user_group.id]},
|
|
|
|
)
|
2022-12-12 03:29:10 +01:00
|
|
|
|
2024-04-17 05:45:32 +02:00
|
|
|
remove_subgroups_from_user_group(user_group, subgroups[:2], acting_user=hamlet)
|
2022-12-12 03:29:10 +01:00
|
|
|
audit_log_entry = RealmAuditLog.objects.get(
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUBGROUP_MEMBERSHIP_REMOVED,
|
2022-12-12 03:29:10 +01:00
|
|
|
)
|
2024-04-17 16:34:39 +02:00
|
|
|
self.assertEqual(audit_log_entry.modified_user_group, user_group)
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assertEqual(audit_log_entry.acting_user, hamlet)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entry.extra_data,
|
2022-12-12 03:29:10 +01:00
|
|
|
{"subgroup_ids": [subgroup.id for subgroup in subgroups[:2]]},
|
|
|
|
)
|
2022-12-12 03:29:10 +01:00
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUPERGROUP_MEMBERSHIP_REMOVED,
|
2022-12-12 03:29:10 +01:00
|
|
|
).order_by("id")
|
|
|
|
self.assert_length(audit_log_entries, 2)
|
|
|
|
for i in range(2):
|
2024-04-17 16:34:39 +02:00
|
|
|
self.assertEqual(audit_log_entries[i].modified_user_group, subgroups[i])
|
2022-12-12 03:29:10 +01:00
|
|
|
self.assertEqual(audit_log_entries[i].acting_user, hamlet)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entries[i].extra_data,
|
2022-12-12 03:29:10 +01:00
|
|
|
{"supergroup_ids": [user_group.id]},
|
|
|
|
)
|
2023-06-05 22:34:52 +02:00
|
|
|
|
|
|
|
def test_user_group_property_change(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
user_group = check_add_user_group(
|
|
|
|
hamlet.realm,
|
|
|
|
"demo",
|
|
|
|
[],
|
|
|
|
description="No description",
|
|
|
|
acting_user=hamlet,
|
|
|
|
)
|
|
|
|
now = timezone_now()
|
|
|
|
|
|
|
|
do_update_user_group_name(user_group, "bar", acting_user=hamlet)
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
realm=hamlet.realm,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_NAME_CHANGED,
|
2023-06-05 22:34:52 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entries[0].extra_data,
|
2023-06-05 22:34:52 +02:00
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: "demo",
|
|
|
|
RealmAuditLog.NEW_VALUE: "bar",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
do_update_user_group_description(user_group, "Foo", acting_user=hamlet)
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
realm=hamlet.realm,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DESCRIPTION_CHANGED,
|
2023-06-05 22:34:52 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entries[0].extra_data,
|
2023-06-05 22:34:52 +02:00
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: "No description",
|
|
|
|
RealmAuditLog.NEW_VALUE: "Foo",
|
|
|
|
},
|
|
|
|
)
|
2023-06-05 22:34:52 +02:00
|
|
|
|
|
|
|
old_group = user_group.can_mention_group
|
2024-04-18 12:23:46 +02:00
|
|
|
new_group = NamedUserGroup.objects.get(
|
2023-09-21 13:06:39 +02:00
|
|
|
name=SystemGroups.EVERYONE_ON_INTERNET, realm=user_group.realm
|
2024-05-28 11:22:42 +02:00
|
|
|
).usergroup_ptr
|
2023-06-05 22:34:52 +02:00
|
|
|
self.assertNotEqual(old_group.id, new_group.id)
|
|
|
|
do_change_user_group_permission_setting(
|
2024-05-28 11:22:42 +02:00
|
|
|
user_group,
|
|
|
|
"can_mention_group",
|
|
|
|
new_group,
|
|
|
|
old_setting_api_value=old_group.id,
|
|
|
|
acting_user=None,
|
2023-06-05 22:34:52 +02:00
|
|
|
)
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_GROUP_BASED_SETTING_CHANGED,
|
2023-06-05 22:34:52 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertIsNone(audit_log_entries[0].acting_user)
|
|
|
|
self.assertDictEqual(
|
2023-07-13 19:46:06 +02:00
|
|
|
audit_log_entries[0].extra_data,
|
2023-06-05 22:34:52 +02:00
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: old_group.id,
|
|
|
|
RealmAuditLog.NEW_VALUE: new_group.id,
|
|
|
|
"property": "can_mention_group",
|
|
|
|
},
|
|
|
|
)
|
2024-04-30 15:16:58 +02:00
|
|
|
|
|
|
|
moderators_group = NamedUserGroup.objects.get(
|
|
|
|
name=SystemGroups.MODERATORS, realm=user_group.realm, is_system_group=True
|
|
|
|
)
|
|
|
|
old_group = user_group.can_mention_group
|
2024-05-28 09:25:40 +02:00
|
|
|
new_group = self.create_or_update_anonymous_group_for_setting([hamlet], [moderators_group])
|
2024-04-30 15:16:58 +02:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_change_user_group_permission_setting(
|
2024-05-28 11:22:42 +02:00
|
|
|
user_group,
|
|
|
|
"can_mention_group",
|
|
|
|
new_group,
|
|
|
|
old_setting_api_value=old_group.id,
|
|
|
|
acting_user=None,
|
2024-04-30 15:16:58 +02:00
|
|
|
)
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_GROUP_BASED_SETTING_CHANGED,
|
2024-04-30 15:16:58 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertIsNone(audit_log_entries[0].acting_user)
|
|
|
|
self.assertDictEqual(
|
|
|
|
audit_log_entries[0].extra_data,
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: old_group.id,
|
|
|
|
RealmAuditLog.NEW_VALUE: {
|
|
|
|
"direct_members": [hamlet.id],
|
|
|
|
"direct_subgroups": [moderators_group.id],
|
|
|
|
},
|
|
|
|
"property": "can_mention_group",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
othello = self.example_user("othello")
|
2024-05-28 11:22:42 +02:00
|
|
|
old_setting_api_value = get_group_setting_value_for_api(user_group.can_mention_group)
|
|
|
|
# Since the old setting value was a anonymous group, we just update the
|
|
|
|
# members and subgroups of the already existing UserGroup instead of creating
|
|
|
|
# a new UserGroup object to keep this consistent with the actual code.
|
2024-05-28 09:25:40 +02:00
|
|
|
new_group = self.create_or_update_anonymous_group_for_setting(
|
|
|
|
[othello], [moderators_group], existing_setting_group=user_group.can_mention_group
|
|
|
|
)
|
2024-04-30 15:16:58 +02:00
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
do_change_user_group_permission_setting(
|
2024-05-28 11:22:42 +02:00
|
|
|
user_group,
|
|
|
|
"can_mention_group",
|
|
|
|
new_group,
|
|
|
|
old_setting_api_value=old_setting_api_value,
|
|
|
|
acting_user=None,
|
2024-04-30 15:16:58 +02:00
|
|
|
)
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_GROUP_BASED_SETTING_CHANGED,
|
2024-04-30 15:16:58 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertIsNone(audit_log_entries[0].acting_user)
|
|
|
|
self.assertDictEqual(
|
|
|
|
audit_log_entries[0].extra_data,
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: {
|
|
|
|
"direct_members": [hamlet.id],
|
|
|
|
"direct_subgroups": [moderators_group.id],
|
|
|
|
},
|
|
|
|
RealmAuditLog.NEW_VALUE: {
|
|
|
|
"direct_members": [othello.id],
|
|
|
|
"direct_subgroups": [moderators_group.id],
|
|
|
|
},
|
|
|
|
"property": "can_mention_group",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-05-28 11:22:42 +02:00
|
|
|
old_setting_api_value = get_group_setting_value_for_api(user_group.can_mention_group)
|
2024-04-30 15:16:58 +02:00
|
|
|
new_group = NamedUserGroup.objects.get(
|
|
|
|
name=SystemGroups.EVERYONE, realm=user_group.realm, is_system_group=True
|
|
|
|
)
|
|
|
|
now = timezone_now()
|
|
|
|
do_change_user_group_permission_setting(
|
2024-05-28 11:22:42 +02:00
|
|
|
user_group,
|
|
|
|
"can_mention_group",
|
|
|
|
new_group,
|
|
|
|
old_setting_api_value=old_setting_api_value,
|
|
|
|
acting_user=None,
|
2024-04-30 15:16:58 +02:00
|
|
|
)
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_GROUP_BASED_SETTING_CHANGED,
|
2024-04-30 15:16:58 +02:00
|
|
|
event_time__gte=now,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertIsNone(audit_log_entries[0].acting_user)
|
|
|
|
self.assertDictEqual(
|
|
|
|
audit_log_entries[0].extra_data,
|
|
|
|
{
|
|
|
|
RealmAuditLog.OLD_VALUE: {
|
|
|
|
"direct_members": [othello.id],
|
|
|
|
"direct_subgroups": [moderators_group.id],
|
|
|
|
},
|
|
|
|
RealmAuditLog.NEW_VALUE: new_group.id,
|
|
|
|
"property": "can_mention_group",
|
|
|
|
},
|
|
|
|
)
|
2024-05-15 15:44:18 +02:00
|
|
|
|
|
|
|
def test_user_group_deactivation(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
user_group = check_add_user_group(
|
|
|
|
hamlet.realm,
|
|
|
|
"test",
|
|
|
|
[hamlet, cordelia],
|
|
|
|
acting_user=hamlet,
|
|
|
|
)
|
|
|
|
now = timezone_now()
|
|
|
|
do_deactivate_user_group(user_group, acting_user=hamlet)
|
|
|
|
|
|
|
|
audit_log_entries = RealmAuditLog.objects.filter(
|
|
|
|
acting_user=hamlet,
|
|
|
|
realm=hamlet.realm,
|
|
|
|
event_time__gte=now,
|
|
|
|
event_type=AuditLogEventType.USER_GROUP_DEACTIVATED,
|
|
|
|
)
|
|
|
|
self.assert_length(audit_log_entries, 1)
|
|
|
|
self.assertIsNone(audit_log_entries[0].modified_user)
|
|
|
|
self.assertEqual(audit_log_entries[0].modified_user_group, user_group)
|