Пример #1
0
 def test_no_expired_messages(self) -> None:
     for realm_instance in Realm.objects.filter(
             message_retention_days__isnull=False):
         move_expired_messages_to_archive(realm_instance)
         move_expired_user_messages_to_archive(realm_instance)
     self.assertEqual(ArchivedUserMessage.objects.count(), 0)
     self.assertEqual(ArchivedMessage.objects.count(), 0)
Пример #2
0
    def test_expired_messages_in_each_realm(self) -> None:
        """General test for archiving expired messages properly with
        multiple realms involved"""
        expected_message_ids = []
        expected_mit_msgs = self._make_mit_messages(3, timezone_now() - timedelta(days=MIT_REALM_DAYS + 1))
        expected_message_ids.extend([msg.id for msg in expected_mit_msgs.order_by('id')])
        self._make_mit_messages(4, timezone_now() - timedelta(days=MIT_REALM_DAYS - 1))
        zulip_msgs_ids = list(Message.objects.order_by('id').filter(
            sender__realm=self.zulip_realm).values_list('id', flat=True)[3:10])
        expected_message_ids.extend(zulip_msgs_ids)
        expected_zulip_msgs = self._change_messages_pub_date(
            zulip_msgs_ids,
            timezone_now() - timedelta(days=ZULIP_REALM_DAYS + 1))

        for realm_instance in Realm.objects.filter(message_retention_days__isnull=False):
            move_expired_messages_to_archive(realm_instance)
            move_expired_user_messages_to_archive(realm_instance)
        self.assertEqual(ArchivedMessage.objects.count(), len(expected_message_ids))
        self.assertEqual(
            ArchivedUserMessage.objects.count(),
            UserMessage.objects.filter(message_id__in=expected_message_ids).count()
        )

        # Compare expected messages ids with archived messages for both realms
        self._check_archive_data_by_realm(expected_mit_msgs, self.mit_realm)
        self._check_archive_data_by_realm(expected_zulip_msgs, self.zulip_realm)
Пример #3
0
    def _check_cross_realm_messages_archiving(
            self,
            arc_user_msg_qty: int,
            period: int,
            realm: Optional[Realm] = None) -> int:
        sent_message_id = self._send_cross_realm_message()
        all_user_messages_qty = UserMessage.objects.count()
        self._change_messages_pub_date([sent_message_id],
                                       timezone_now() - timedelta(days=period))
        realms = Realm.objects.filter(message_retention_days__isnull=False)
        for realm_instance in realms:
            move_expired_messages_to_archive(realm_instance)
            move_expired_user_messages_to_archive(realm_instance)
        user_messages_sent = UserMessage.objects.order_by('id').filter(
            message_id=sent_message_id)
        archived_messages = ArchivedMessage.objects.all()
        archived_user_messages = ArchivedUserMessage.objects.order_by('id')
        self.assertEqual(user_messages_sent.count(), 2)

        # Compare archived messages and user messages
        # with expired sent messages.
        self.assertEqual(archived_messages.count(), 1)
        self.assertEqual(archived_user_messages.count(), arc_user_msg_qty)
        if realm:
            user_messages_sent = user_messages_sent.filter(
                user_profile__realm=self.zulip_realm)
        self.assertEqual(
            [arc_user_msg.id for arc_user_msg in archived_user_messages],
            [user_msg.id for user_msg in user_messages_sent])
        for realm_instance in realms:
            delete_expired_user_messages(realm_instance)
            delete_expired_messages(realm_instance)
        clean_unused_messages()

        # Check messages and user messages after deleting expired messages
        # from the main tables.
        self.assertEqual(
            UserMessage.objects.filter(message_id=sent_message_id).count(),
            2 - arc_user_msg_qty)
        self.assertEqual(UserMessage.objects.count(),
                         all_user_messages_qty - arc_user_msg_qty)
        return sent_message_id
Пример #4
0
    def test_expired_messages_in_one_realm(self) -> None:
        """Test with a retention policy set for only the MIT realm"""
        expected_mit_msgs = self._make_mit_messages(
            5, timezone_now() - timedelta(days=MIT_REALM_DAYS + 1))

        for realm_instance in Realm.objects.filter(message_retention_days__isnull=False):
            move_expired_messages_to_archive(realm_instance)
            move_expired_user_messages_to_archive(realm_instance)

        self.assertEqual(ArchivedMessage.objects.count(), 5)
        self.assertEqual(ArchivedUserMessage.objects.count(), 10)

        # Compare expected messages ids with archived messages in mit realm
        self._check_archive_data_by_realm(expected_mit_msgs, self.mit_realm)
        # Check no archive messages for zulip realm.
        self.assertEqual(
            ArchivedMessage.objects.filter(
                archivedusermessage__user_profile__realm=self.zulip_realm).count(),
            0
        )
        self.assertEqual(
            ArchivedUserMessage.objects.filter(user_profile__realm=self.zulip_realm).count(),
            0
        )