from datetime import timedelta
from enum import Enum
from typing import Any

from django.contrib.auth.password_validation import validate_password
from django.utils.timezone import now as timezone_now

from analytics.models import StreamCount
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,
)
from zerver.actions.channel_folders import (
    check_add_channel_folder,
    do_archive_channel_folder,
    do_change_channel_folder_description,
    do_change_channel_folder_name,
    do_unarchive_channel_folder,
)
from zerver.actions.create_realm import do_create_realm
from zerver.actions.create_user import (
    do_activate_mirror_dummy_user,
    do_create_user,
    do_reactivate_user,
)
from zerver.actions.navigation_views import (
    do_add_navigation_view,
    do_remove_navigation_view,
    do_update_navigation_view,
)
from zerver.actions.realm_domains import (
    do_add_realm_domain,
    do_change_realm_domain,
    do_remove_realm_domain,
)
from zerver.actions.realm_emoji import check_add_realm_emoji, do_remove_realm_emoji
from zerver.actions.realm_icon import do_change_icon_source
from zerver.actions.realm_linkifiers import (
    do_add_linkifier,
    do_remove_linkifier,
    do_update_linkifier,
)
from zerver.actions.realm_playgrounds import check_add_realm_playground, do_remove_realm_playground
from zerver.actions.realm_settings import (
    do_change_realm_permission_group_setting,
    do_deactivate_realm,
    do_reactivate_realm,
    do_set_realm_authentication_methods,
    do_set_realm_moderation_request_channel,
    do_set_realm_new_stream_announcements_stream,
    do_set_realm_property,
    do_set_realm_signup_announcements_stream,
    do_set_realm_zulip_update_announcements_stream,
)
from zerver.actions.streams import (
    bulk_add_subscriptions,
    bulk_remove_subscriptions,
    do_change_stream_folder,
    do_change_subscription_property,
    do_deactivate_stream,
    do_rename_stream,
)
from zerver.actions.user_groups import (
    add_subgroups_to_user_group,
    bulk_add_members_to_user_groups,
    bulk_remove_members_from_user_groups,
    check_add_user_group,
    do_change_user_group_permission_setting,
    do_deactivate_user_group,
    do_reactivate_user_group,
    do_update_user_group_description,
    do_update_user_group_name,
    remove_subgroups_from_user_group,
)
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,
)
from zerver.actions.users import do_change_is_imported_stub, do_change_user_role, do_deactivate_user
from zerver.lib.emoji import get_emoji_file_name, get_emoji_url
from zerver.lib.message import get_last_message_id
from zerver.lib.stream_traffic import get_streams_traffic
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import get_test_image_file
from zerver.lib.types import LinkifierDict, RealmPlaygroundDict
from zerver.lib.user_groups import get_group_setting_value_for_api
from zerver.lib.utils import assert_is_not_none
from zerver.models import (
    Message,
    NamedUserGroup,
    RealmAuditLog,
    RealmPlayground,
    Recipient,
    Subscription,
    UserProfile,
)
from zerver.models.groups import SystemGroups
from zerver.models.linkifiers import linkifiers_for_realm
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realm_emoji import EmojiInfo, get_all_custom_emoji_for_realm
from zerver.models.realm_playgrounds import get_realm_playgrounds
from zerver.models.realms import RealmDomainDict, get_realm, get_realm_domains
from zerver.models.streams import get_stream
from zerver.models.users import ResolvedTopicNoticeAutoReadPolicyEnum


class TestRealmAuditLog(ZulipTestCase):
    def check_role_count_schema(self, role_counts: dict[str, Any]) -> None:
        for key in [
            UserProfile.ROLE_REALM_ADMINISTRATOR,
            UserProfile.ROLE_MEMBER,
            UserProfile.ROLE_GUEST,
            UserProfile.ROLE_REALM_OWNER,
        ]:
            # 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))

    def test_user_activation(self) -> None:
        realm = get_realm("zulip")
        now = timezone_now()
        user = do_create_user("email", "password", realm, "full_name", acting_user=None)
        do_deactivate_user(user, acting_user=user)

        user.is_mirror_dummy = True
        user.save(update_fields=["is_mirror_dummy"])
        do_activate_mirror_dummy_user(user, acting_user=user)
        do_deactivate_user(user, acting_user=user)
        do_reactivate_user(user, acting_user=user)
        self.assertEqual(RealmAuditLog.objects.filter(event_time__gte=now).count(), 10)
        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),
            )
            .order_by("event_time", "event_type")
            .values_list("event_type", flat=True)
        )
        self.assertEqual(
            event_types,
            [
                AuditLogEventType.USER_CREATED,
                AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
                AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
                AuditLogEventType.USER_DEACTIVATED,
                AuditLogEventType.USER_ACTIVATED,
                AuditLogEventType.USER_DEACTIVATED,
                AuditLogEventType.USER_REACTIVATED,
            ],
        )
        modified_user_group_names = []
        for event in 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),
        ):
            if event.event_type == AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED:
                self.assertDictEqual(event.extra_data, {})
                modified_user_group_names.append(assert_is_not_none(event.modified_user_group).name)
                continue
            extra_data = event.extra_data
            self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
            self.assertNotIn(RealmAuditLog.OLD_VALUE, extra_data)

        self.assertListEqual(
            modified_user_group_names,
            [
                SystemGroups.MEMBERS,
                SystemGroups.FULL_MEMBERS,
            ],
        )

    def test_change_role(self) -> None:
        realm = get_realm("zulip")
        now = timezone_now()
        user_profile = self.example_user("hamlet")
        acting_user = self.example_user("iago")
        do_change_user_role(
            user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR, 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_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)
        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)
        old_values_seen = set()
        new_values_seen = set()
        for event in RealmAuditLog.objects.filter(
            event_type=AuditLogEventType.USER_ROLE_CHANGED,
            realm=realm,
            modified_user=user_profile,
            acting_user=acting_user,
            event_time__gte=now,
            event_time__lte=now + timedelta(minutes=60),
        ):
            extra_data = event.extra_data
            self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
            self.assertIn(RealmAuditLog.OLD_VALUE, extra_data)
            self.assertIn(RealmAuditLog.NEW_VALUE, extra_data)
            old_values_seen.add(extra_data[RealmAuditLog.OLD_VALUE])
            new_values_seen.add(extra_data[RealmAuditLog.NEW_VALUE])
        self.assertEqual(
            old_values_seen,
            {
                UserProfile.ROLE_GUEST,
                UserProfile.ROLE_MEMBER,
                UserProfile.ROLE_REALM_ADMINISTRATOR,
                UserProfile.ROLE_REALM_OWNER,
                UserProfile.ROLE_MODERATOR,
            },
        )
        self.assertEqual(old_values_seen, new_values_seen)

        expected_system_user_group_names = [
            SystemGroups.ADMINISTRATORS,
            SystemGroups.MEMBERS,
            SystemGroups.FULL_MEMBERS,
            SystemGroups.EVERYONE,
            SystemGroups.MEMBERS,
            SystemGroups.FULL_MEMBERS,
            SystemGroups.OWNERS,
            SystemGroups.MEMBERS,
            SystemGroups.FULL_MEMBERS,
            SystemGroups.MODERATORS,
        ]
        user_group_modified_names = (
            RealmAuditLog.objects.filter(
                event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
                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,
                SystemGroups.MEMBERS,
                SystemGroups.FULL_MEMBERS,
            ],
        )
        user_group_modified_names = (
            RealmAuditLog.objects.filter(
                event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_REMOVED,
                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),
            [
                SystemGroups.MEMBERS,
                SystemGroups.FULL_MEMBERS,
                *expected_system_user_group_names,
            ],
        )

    def test_change_password(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        password = "test1"
        do_change_password(user, password)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                event_type=AuditLogEventType.USER_PASSWORD_CHANGED, event_time__gte=now
            ).count(),
            1,
        )
        # No error should be raised here
        validate_password(password, user)

    def test_change_email(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        original_email = user.delivery_email
        new_email = "test@example.com"
        do_change_user_delivery_email(user, new_email, acting_user=user)
        self.assertEqual(new_email, user.delivery_email)

        audit_entry = RealmAuditLog.objects.get(
            event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now
        )
        self.assertEqual(audit_entry.modified_user, user)
        self.assertEqual(
            audit_entry.extra_data,
            {RealmAuditLog.OLD_VALUE: original_email, RealmAuditLog.NEW_VALUE: new_email},
        )
        # Test the RealmAuditLog stringification
        self.assertTrue(
            repr(audit_entry).startswith(
                f"<RealmAuditLog: {AuditLogEventType.USER_EMAIL_CHANGED.name} "
            )
        )

    def test_change_avatar_source(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        avatar_source = "G"
        do_change_avatar_fields(user, avatar_source, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                event_type=AuditLogEventType.USER_AVATAR_SOURCE_CHANGED,
                modified_user=user,
                acting_user=user,
                event_time__gte=now,
            ).count(),
            1,
        )
        self.assertEqual(avatar_source, user.avatar_source)

    def test_change_full_name(self) -> None:
        start = timezone_now()
        new_name = "George Hamletovich"
        self.login("iago")
        req = dict(full_name=new_name)
        result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req)
        self.assertTrue(result.status_code == 200)
        query = RealmAuditLog.objects.filter(
            event_type=AuditLogEventType.USER_FULL_NAME_CHANGED, event_time__gte=start
        )
        self.assertEqual(query.count(), 1)

    def test_change_tos_version(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        old_tos_version = user.tos_version
        tos_version = "android"
        do_change_tos_version(user, tos_version)
        audit_log_entries = RealmAuditLog.objects.filter(
            event_type=AuditLogEventType.USER_TERMS_OF_SERVICE_VERSION_CHANGED, event_time__gte=now
        )
        self.assertEqual(audit_log_entries.count(), 1)
        expected_extra_data = {
            RealmAuditLog.OLD_VALUE: old_tos_version,
            RealmAuditLog.NEW_VALUE: tos_version,
        }
        self.assertEqual(audit_log_entries[0].extra_data, expected_extra_data)
        self.assertEqual(tos_version, user.tos_version)

    def test_change_bot_owner(self) -> None:
        now = timezone_now()
        admin = self.example_user("iago")
        bot = self.notification_bot(admin.realm)
        # Check that original owner of the notification bot is the bot itself.
        self.assertEqual(bot.bot_owner, bot)
        bot_owner = self.example_user("hamlet")
        do_change_bot_owner(bot, bot_owner, admin)
        audit_log_entries = RealmAuditLog.objects.filter(
            event_type=AuditLogEventType.USER_BOT_OWNER_CHANGED, event_time__gte=now
        )
        self.assertEqual(audit_log_entries.count(), 1)
        expected_extra_data = {
            RealmAuditLog.OLD_VALUE: bot.id,
            RealmAuditLog.NEW_VALUE: bot_owner.id,
        }
        self.assertEqual(audit_log_entries[0].extra_data, expected_extra_data)
        self.assertEqual(bot_owner, bot.bot_owner)

    def test_regenerate_api_key(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        do_regenerate_api_key(user, user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                event_type=AuditLogEventType.USER_API_KEY_CHANGED, event_time__gte=now
            ).count(),
            1,
        )
        self.assertTrue(user.api_key)

    def test_get_streams_traffic(self) -> None:
        realm = get_realm("zulip")
        stream = self.make_stream("whatever", realm)
        stream_2 = self.make_stream("whatever_2", realm)

        result = get_streams_traffic(realm)
        self.assertEqual(result, {})

        StreamCount.objects.create(
            realm=realm,
            stream=stream,
            property="messages_in_stream:is_bot:day",
            end_time=timezone_now(),
            value=999,
        )
        StreamCount.objects.create(
            realm=realm,
            stream=stream_2,
            property="messages_in_stream:is_bot:day",
            end_time=timezone_now(),
            value=23,
        )

        result = get_streams_traffic(realm, {stream.id})
        self.assertEqual(result, {stream.id: 999})

        all_streams_traffic = get_streams_traffic(realm)
        self.assertEqual(all_streams_traffic, {stream.id: 999, stream_2.id: 23})

    def test_subscriptions(self) -> None:
        now = timezone_now()

        user = self.example_user("hamlet")
        realm = user.realm
        stream = self.make_stream("test_stream")
        acting_user = self.example_user("iago")
        bulk_add_subscriptions(user.realm, [stream], [user], acting_user=acting_user)
        subscription_creation_logs = RealmAuditLog.objects.filter(
            event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
            event_time__gte=now,
            acting_user=acting_user,
            modified_user=user,
            modified_stream=stream,
        )
        modified_stream = subscription_creation_logs[0].modified_stream
        assert modified_stream is not None
        self.assertEqual(subscription_creation_logs.count(), 1)
        self.assertEqual(modified_stream.id, stream.id)
        self.assertEqual(subscription_creation_logs[0].modified_user, user)

        bulk_remove_subscriptions(realm, [user], [stream], acting_user=acting_user)
        subscription_deactivation_logs = RealmAuditLog.objects.filter(
            event_type=AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
            event_time__gte=now,
            acting_user=acting_user,
            modified_user=user,
            modified_stream=stream,
        )
        modified_stream = subscription_deactivation_logs[0].modified_stream
        assert modified_stream is not None
        self.assertEqual(subscription_deactivation_logs.count(), 1)
        self.assertEqual(modified_stream.id, stream.id)
        self.assertEqual(subscription_deactivation_logs[0].modified_user, user)

    def test_realm_activation(self) -> None:
        realm = get_realm("zulip")
        user = self.example_user("desdemona")
        do_deactivate_realm(
            realm, acting_user=user, deactivation_reason="owner_request", email_owners=False
        )
        log_entry = RealmAuditLog.objects.get(
            realm=realm, event_type=AuditLogEventType.REALM_DEACTIVATED, acting_user=user
        )
        extra_data = log_entry.extra_data

        deactivation_reason = extra_data["deactivation_reason"]
        self.assertEqual(deactivation_reason, "owner_request")

        self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])

        do_reactivate_realm(realm)
        log_entry = RealmAuditLog.objects.get(
            realm=realm, event_type=AuditLogEventType.REALM_REACTIVATED
        )
        extra_data = log_entry.extra_data
        self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])

    def test_create_stream_if_needed(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        stream = create_stream_if_needed(
            realm,
            "test",
            invite_only=False,
            stream_description="Test description",
            acting_user=user,
        )[0]
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.CHANNEL_CREATED,
                event_time__gte=now,
                acting_user=user,
                modified_stream=stream,
            ).count(),
            1,
        )

    def test_deactivate_stream(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        stream_name = "test"
        stream = self.make_stream(stream_name, realm)
        do_deactivate_stream(stream, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.CHANNEL_DEACTIVATED,
                event_time__gte=now,
                acting_user=user,
                modified_stream=stream,
            ).count(),
            1,
        )
        self.assertEqual(stream.deactivated, True)

    def test_set_realm_authentication_methods(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        expected_old_value = realm.authentication_methods_dict()
        auth_method_dict = {
            "Google": False,
            "Email": False,
            "GitHub": False,
            "Apple": False,
            "Dev": True,
            "SAML": True,
            "GitLab": False,
            "OpenID Connect": False,
        }

        do_set_realm_authentication_methods(realm, auth_method_dict, acting_user=user)
        realm_audit_logs = RealmAuditLog.objects.filter(
            realm=realm,
            event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
            event_time__gte=now,
            acting_user=user,
        )
        self.assertEqual(realm_audit_logs.count(), 1)
        extra_data = realm_audit_logs[0].extra_data
        expected_new_value = auth_method_dict
        self.assertEqual(extra_data[RealmAuditLog.OLD_VALUE], expected_old_value)
        self.assertEqual(extra_data[RealmAuditLog.NEW_VALUE], expected_new_value)

    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(),
            Message.objects.latest("id").id,
        )

        Message.objects.all().delete()

        self.assertEqual(get_last_message_id(), -1)

    def test_set_realm_message_editing(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        value_expected = {
            RealmAuditLog.OLD_VALUE: realm.message_content_edit_limit_seconds,
            RealmAuditLog.NEW_VALUE: 1000,
            "property": "message_content_edit_limit_seconds",
        }

        do_set_realm_property(realm, "message_content_edit_limit_seconds", 1000, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data=value_expected,
            ).count(),
            1,
        )

        administrators_system_group = NamedUserGroup.objects.get(
            name=SystemGroups.ADMINISTRATORS, realm_for_sharding=realm, is_system_group=True
        )
        everyone_system_group = NamedUserGroup.objects.get(
            name=SystemGroups.EVERYONE, realm_for_sharding=realm, is_system_group=True
        )

        value_expected = {
            RealmAuditLog.OLD_VALUE: everyone_system_group.id,
            RealmAuditLog.NEW_VALUE: administrators_system_group.id,
            "property": "can_move_messages_between_topics_group",
        }

        do_change_realm_permission_group_setting(
            realm,
            "can_move_messages_between_topics_group",
            administrators_system_group,
            acting_user=user,
        )
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data=value_expected,
            ).count(),
            1,
        )

    def test_set_realm_new_stream_announcements_stream(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        old_value = realm.new_stream_announcements_stream_id
        stream_name = "test"
        stream = self.make_stream(stream_name, realm)

        do_set_realm_new_stream_announcements_stream(realm, stream, stream.id, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data={
                    RealmAuditLog.OLD_VALUE: old_value,
                    RealmAuditLog.NEW_VALUE: stream.id,
                    "property": "new_stream_announcements_stream",
                },
            ).count(),
            1,
        )

    def test_set_realm_signup_announcements_stream(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        old_value = realm.signup_announcements_stream_id
        stream_name = "test"
        stream = self.make_stream(stream_name, realm)

        do_set_realm_signup_announcements_stream(realm, stream, stream.id, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data={
                    RealmAuditLog.OLD_VALUE: old_value,
                    RealmAuditLog.NEW_VALUE: stream.id,
                    "property": "signup_announcements_stream",
                },
            ).count(),
            1,
        )

    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,
                event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
                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,
        )

    def test_set_realm_moderation_request_channel(self) -> None:
        now = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        old_value = realm.moderation_request_channel
        stream = self.make_stream("private_stream", invite_only=True)
        assert old_value is not None
        do_set_realm_moderation_request_channel(realm, stream, stream.id, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=realm,
                event_type=AuditLogEventType.REALM_PROPERTY_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data={
                    # `populate_db` configures `moderation_request_channel` for
                    # API testing purposes, so the `old_value` here is
                    # not `None`.
                    RealmAuditLog.OLD_VALUE: old_value.id,
                    RealmAuditLog.NEW_VALUE: stream.id,
                    "property": "moderation_request_channel",
                },
            ).count(),
            1,
        )

    def test_change_icon_source(self) -> None:
        test_start = timezone_now()
        realm = get_realm("zulip")
        user = self.example_user("hamlet")
        icon_source = "G"
        do_change_icon_source(realm, icon_source, acting_user=user)
        audit_entries = RealmAuditLog.objects.filter(
            realm=realm,
            event_type=AuditLogEventType.REALM_ICON_SOURCE_CHANGED,
            acting_user=user,
            event_time__gte=test_start,
        )
        audit_log = audit_entries.first()
        assert audit_log is not None
        self.assert_length(audit_entries, 1)
        self.assertEqual(icon_source, realm.icon_source)
        self.assertEqual(audit_log.extra_data, {"icon_source": "G", "icon_version": 2})

    def test_change_subscription_property(self) -> None:
        user = self.example_user("hamlet")
        # Fetch the Denmark stream for testing
        stream = get_stream("Denmark", user.realm)
        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,
        }

        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)
            expected_extra_data = {
                RealmAuditLog.OLD_VALUE: old_value,
                RealmAuditLog.NEW_VALUE: value,
                "property": property,
            }
            self.assertEqual(
                RealmAuditLog.objects.filter(
                    realm=user.realm,
                    event_type=AuditLogEventType.SUBSCRIPTION_PROPERTY_CHANGED,
                    event_time__gte=now,
                    acting_user=user,
                    modified_user=user,
                    extra_data=expected_extra_data,
                ).count(),
                1,
            )
            self.assertEqual(getattr(sub, property), value)

    def test_change_default_streams(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        stream = get_stream("Denmark", user.realm)

        old_value = user.default_sending_stream_id
        do_change_default_sending_stream(user, stream, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=AuditLogEventType.USER_DEFAULT_SENDING_STREAM_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data={
                    RealmAuditLog.OLD_VALUE: old_value,
                    RealmAuditLog.NEW_VALUE: stream.id,
                },
            ).count(),
            1,
        )
        self.assertEqual(user.default_sending_stream, stream)

        old_value = user.default_events_register_stream_id
        do_change_default_events_register_stream(user, stream, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=AuditLogEventType.USER_DEFAULT_REGISTER_STREAM_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data={
                    RealmAuditLog.OLD_VALUE: old_value,
                    RealmAuditLog.NEW_VALUE: stream.id,
                },
            ).count(),
            1,
        )
        self.assertEqual(user.default_events_register_stream, stream)

        old_value = user.default_all_public_streams
        do_change_default_all_public_streams(user, False, acting_user=user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=AuditLogEventType.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data={RealmAuditLog.OLD_VALUE: old_value, RealmAuditLog.NEW_VALUE: False},
            ).count(),
            1,
        )
        self.assertEqual(user.default_all_public_streams, False)

    def test_rename_stream(self) -> None:
        now = timezone_now()
        user = self.example_user("hamlet")
        stream = self.make_stream("test", user.realm)
        old_name = stream.name
        do_rename_stream(stream, "updated name", user)
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=AuditLogEventType.CHANNEL_NAME_CHANGED,
                event_time__gte=now,
                acting_user=user,
                modified_stream=stream,
