Пример #1
0
    def test_expired_messages_in_each_realm(self) -> None:
        """General test for archiving expired messages properly with
        multiple realms involved"""
        # Make some expired messages in MIT:
        expired_mit_msg_ids = self._make_mit_messages(
            5,
            timezone_now() - timedelta(days=MIT_REALM_DAYS + 1))
        # Make some non-expired messages in MIT:
        self._make_mit_messages(
            4,
            timezone_now() - timedelta(days=MIT_REALM_DAYS - 1))

        # Change some Zulip messages to be expired:
        expired_zulip_msg_ids = list(
            Message.objects.order_by('id').filter(
                sender__realm=self.zulip_realm).values_list('id',
                                                            flat=True)[3:10])
        self._change_messages_pub_date(
            expired_zulip_msg_ids,
            timezone_now() - timedelta(ZULIP_REALM_DAYS + 1))

        expired_msg_ids = expired_mit_msg_ids + expired_zulip_msg_ids
        move_expired_to_archive()

        self.assertEqual(ArchivedMessage.objects.count(), len(expired_msg_ids))
        self.assertEqual(
            ArchivedUserMessage.objects.count(),
            UserMessage.objects.filter(message_id__in=expired_msg_ids).count())

        self._verify_archive_data(expired_msg_ids)
Пример #2
0
    def test_cross_realm_messages_archiving_two_realm_expired(self) -> None:
        """Check that archiving a message that's expired in both
        realms is archived both in Message and UserMessage."""
        msg_id = self._send_cross_realm_message()
        # Make the message expired on both realms:
        self._change_messages_pub_date([msg_id],
                                       timezone_now() -
                                       timedelta(MIT_REALM_DAYS + 1))

        move_expired_to_archive()

        self.assertEqual(ArchivedMessage.objects.count(), 1)
        self.assertEqual(ArchivedUserMessage.objects.count(), 2)

        clean_expired()

        self.assertEqual(
            UserMessage.objects.filter(message_id=msg_id).count(), 0)
        self.assertFalse(Message.objects.filter(id=msg_id).exists())
Пример #3
0
    def test_cross_realm_messages_archiving_one_realm_expired(self) -> None:
        """Test that a cross-realm message that is expired in only
        one of the realms only has the UserMessage for that realm archived"""
        msg_id = self._send_cross_realm_message()
        # Make the message expired on Zulip only:
        self._change_messages_pub_date([msg_id],
                                       timezone_now() -
                                       timedelta(ZULIP_REALM_DAYS + 1))

        move_expired_to_archive()

        self.assertEqual(ArchivedMessage.objects.count(), 1)
        self.assertEqual(ArchivedUserMessage.objects.count(), 1)

        clean_expired()

        self.assertEqual(
            UserMessage.objects.filter(message_id=msg_id).count(), 1)
        self.assertTrue(Message.objects.filter(id=msg_id).exists())
Пример #4
0
    def test_expired_messages_in_one_realm(self) -> None:
        """Test with a retention policy set for only the MIT realm"""
        self._set_realm_message_retention_value(self.zulip_realm, None)

        # Make some expired messages in MIT:
        expired_mit_msg_ids = self._make_mit_messages(
            5,
            timezone_now() - timedelta(days=MIT_REALM_DAYS + 1))
        # Make some non-expired messages in MIT:
        self._make_mit_messages(
            4,
            timezone_now() - timedelta(days=MIT_REALM_DAYS - 1))

        # Change some Zulip messages pub_date, but the realm has no retention policy,
        # so they shouldn't get archived
        zulip_msg_ids = list(
            Message.objects.order_by('id').filter(
                sender__realm=self.zulip_realm).values_list('id',
                                                            flat=True)[3:10])
        self._change_messages_pub_date(
            zulip_msg_ids,
            timezone_now() - timedelta(ZULIP_REALM_DAYS + 1))

        # Only MIT has a retention policy:
        expired_msg_ids = expired_mit_msg_ids
        move_expired_to_archive()

        self.assertEqual(ArchivedMessage.objects.count(), len(expired_msg_ids))
        self.assertEqual(
            ArchivedUserMessage.objects.count(),
            UserMessage.objects.filter(message_id__in=expired_msg_ids).count())

        self._verify_archive_data(expired_msg_ids)

        self._set_realm_message_retention_value(self.zulip_realm,
                                                ZULIP_REALM_DAYS)
Пример #5
0
    def test_cross_realm_messages_archiving_two_realm_expired(self) -> None:
        """Check that archiving a message that's expired in both
        realms is archived both in Message and UserMessage."""
        msg_id = self._send_cross_realm_message()
        # Make the message expired on both realms:
        self._change_messages_pub_date([msg_id],
                                       timezone_now() -
                                       timedelta(MIT_REALM_DAYS + 1))

        archived_data_info = move_expired_to_archive()

        self.assertEqual(ArchivedMessage.objects.count(), 1)
        self.assertEqual(ArchivedUserMessage.objects.count(), 2)

        clean_expired(archived_data_info)

        # Recipient's usermessage will not be deleted in the current implementation:
        leftover_usermessage = UserMessage.objects.get(message_id=msg_id)
        self.assertEqual(leftover_usermessage.user_profile.realm_id,
                         self.mit_realm.id)
        self.assertTrue(Message.objects.filter(id=msg_id).exists())
Пример #6
0
    def test_cross_realm_messages_archiving_one_realm_expired(self) -> None:
        """Test that a cross-realm message that is expired in only
        one of the realms only has the UserMessage for that realm archived"""
        msg_id = self._send_cross_realm_message()
        # Make the message expired on Zulip only:
        self._change_messages_pub_date([msg_id],
                                       timezone_now() -
                                       timedelta(ZULIP_REALM_DAYS + 1))

        archived_data_info = move_expired_to_archive()

        self.assertEqual(ArchivedMessage.objects.count(), 1)
        # Normally one should be archived, but we redundantly archive both usermessages,
        # until we implement proper handling of cross-realm messages.
        self.assertEqual(ArchivedUserMessage.objects.count(), 2)

        clean_expired(archived_data_info)

        self.assertEqual(
            UserMessage.objects.filter(message_id=msg_id).count(), 1)
        self.assertTrue(Message.objects.filter(id=msg_id).exists())
Пример #7
0
    def test_no_expired_messages(self) -> None:
        move_expired_to_archive()

        self.assertEqual(ArchivedUserMessage.objects.count(), 0)
        self.assertEqual(ArchivedMessage.objects.count(), 0)