Exemplo n.º 1
0
 def test_inactive_users_queued_for_digest(
         self, mock_django_timezone: mock.MagicMock,
         mock_queue_digest_recipient: mock.MagicMock) -> None:
     # Turn on realm digest emails for all realms
     Realm.objects.update(digest_emails_enabled=True)
     cutoff = timezone_now()
     # Test Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016,
                                                           month=1,
                                                           day=5)
     all_user_profiles = UserProfile.objects.filter(
         is_active=True, is_bot=False, enable_digest_emails=True)
     # Check that all users without an a UserActivity entry are considered
     # inactive users and get enqueued.
     enqueue_emails(cutoff)
     self.assertEqual(mock_queue_digest_recipient.call_count,
                      all_user_profiles.count())
     mock_queue_digest_recipient.reset_mock()
     for realm in Realm.objects.filter(deactivated=False,
                                       digest_emails_enabled=True):
         user_profiles = all_user_profiles.filter(realm=realm)
         for user_profile in user_profiles:
             UserActivity.objects.create(last_visit=cutoff -
                                         datetime.timedelta(days=1),
                                         user_profile=user_profile,
                                         count=0,
                                         client=get_client('test_client'))
     # Check that inactive users are enqueued
     enqueue_emails(cutoff)
     self.assertEqual(mock_queue_digest_recipient.call_count,
                      all_user_profiles.count())
Exemplo n.º 2
0
 def test_active_users_not_enqueued(self, mock_django_timezone: mock.MagicMock,
                                    mock_enough_traffic: mock.MagicMock) -> None:
     cutoff = timezone_now()
     # A Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)
     realms = Realm.objects.filter(deactivated=False, show_digest_email=True)
     for realm in realms:
         user_profiles = UserProfile.objects.filter(realm=realm)
         for counter, user_profile in enumerate(user_profiles, 1):
             UserActivity.objects.create(
                 last_visit=cutoff + datetime.timedelta(days=1),
                 user_profile=user_profile,
                 count=0,
                 client=get_client('test_client'))
             # When there are only two users left who have not been set to active,
             # check the full enqueue-digest flow for them.
             if realm == realms.last() and user_profiles.count() - counter == 2:
                 with mock.patch('zerver.models.ScheduledEmail.objects.create') as mock_email_create:
                     enqueue_emails(cutoff)
                     self.assert_length(mock_email_create.call_args_list, 2)
                     for call_args in mock_email_create.call_args_list:
                         email_data = ujson.loads(call_args[1]['data'])
                         self.assertEqual(email_data['template_prefix'], 'zerver/emails/digest')
     # Check that an active user is not enqueued
     with mock.patch('zerver.lib.digest.queue_digest_recipient') as mock_queue_digest_recipient:
         enqueue_emails(cutoff)
         self.assertEqual(mock_queue_digest_recipient.call_count, 0)
Exemplo n.º 3
0
 def test_disabled(self, mock_django_timezone: mock.MagicMock,
                   mock_queue_digest_recipient: mock.MagicMock) -> None:
     cutoff = timezone_now()
     # A Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)
     enqueue_emails(cutoff)
     mock_queue_digest_recipient.assert_not_called()
Exemplo n.º 4
0
 def test_active_users_not_enqueued(
         self, mock_django_timezone: mock.MagicMock,
         mock_enough_traffic: mock.MagicMock) -> None:
     # Turn on realm digest emails for all realms
     Realm.objects.update(digest_emails_enabled=True)
     cutoff = timezone_now()
     # A Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016,
                                                           month=1,
                                                           day=5)
     realms = Realm.objects.filter(deactivated=False,
                                   digest_emails_enabled=True)
     for realm in realms:
         user_profiles = UserProfile.objects.filter(realm=realm)
         for counter, user_profile in enumerate(user_profiles, 1):
             UserActivity.objects.create(last_visit=cutoff +
                                         datetime.timedelta(days=1),
                                         user_profile=user_profile,
                                         count=0,
                                         client=get_client('test_client'))
     # Check that an active user is not enqueued
     with mock.patch('zerver.lib.digest.queue_digest_recipient'
                     ) as mock_queue_digest_recipient:
         enqueue_emails(cutoff)
         self.assertEqual(mock_queue_digest_recipient.call_count, 0)
Exemplo n.º 5
0
 def test_disabled(self, mock_django_timezone: mock.MagicMock,
                   mock_queue_digest_recipient: mock.MagicMock) -> None:
     cutoff = timezone_now()
     # A Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)
     enqueue_emails(cutoff)
     mock_queue_digest_recipient.assert_not_called()
Exemplo n.º 6
0
    def test_no_email_digest_for_bots(
            self, mock_django_timezone: mock.MagicMock,
            mock_queue_digest_recipient: mock.MagicMock) -> None:
        # Turn on realm digest emails for all realms
        Realm.objects.update(digest_emails_enabled=True)
        cutoff = timezone_now()
        # A Tuesday
        mock_django_timezone.return_value = datetime.datetime(year=2016,
                                                              month=1,
                                                              day=5)
        bot = do_create_user(
            '*****@*****.**',
            'password',
            get_realm('zulip'),
            'some_bot',
            bot_type=UserProfile.DEFAULT_BOT,
        )
        UserActivity.objects.create(last_visit=cutoff -
                                    datetime.timedelta(days=1),
                                    user_profile=bot,
                                    count=0,
                                    client=get_client('test_client'))

        # Check that bots are not sent emails
        enqueue_emails(cutoff)
        for arg in mock_queue_digest_recipient.call_args_list:
            user = arg[0][0]
            self.assertNotEqual(user.id, bot.id)
Exemplo n.º 7
0
    def test_only_enqueue_on_valid_day(self, mock_django_timezone: mock.MagicMock,
                                       mock_queue_digest_recipient: mock.MagicMock) -> None:
        # Not a Tuesday
        mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=6)

        # Check that digests are not sent on days other than Tuesday.
        cutoff = timezone_now()
        enqueue_emails(cutoff)
        self.assertEqual(mock_queue_digest_recipient.call_count, 0)
Exemplo n.º 8
0
    def test_only_enqueue_on_valid_day(self, mock_django_timezone: mock.MagicMock,
                                       mock_queue_digest_recipient: mock.MagicMock) -> None:
        # Not a Tuesday
        mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=6)

        # Check that digests are not sent on days other than Tuesday.
        cutoff = timezone_now()
        enqueue_emails(cutoff)
        self.assertEqual(mock_queue_digest_recipient.call_count, 0)
Exemplo n.º 9
0
    def test_only_enqueue_on_valid_day(self) -> None:
        RealmAuditLog.objects.all().delete()

        not_tuesday = datetime.datetime(year=2016, month=1, day=6, tzinfo=datetime.timezone.utc)
        cutoff = not_tuesday - datetime.timedelta(days=5)

        with mock.patch("zerver.lib.digest.timezone_now", return_value=not_tuesday):
            with mock.patch("zerver.lib.digest.queue_digest_user_ids") as queue_mock:
                enqueue_emails(cutoff)
        queue_mock.assert_not_called()
Exemplo n.º 10
0
    def test_disabled(self) -> None:
        RealmAuditLog.objects.all().delete()

        tuesday = self.tuesday()
        cutoff = tuesday - datetime.timedelta(days=5)

        with mock.patch("zerver.lib.digest.timezone_now", return_value=tuesday):
            with mock.patch("zerver.lib.digest.queue_digest_user_ids") as queue_mock:
                enqueue_emails(cutoff)
        queue_mock.assert_not_called()
Exemplo n.º 11
0
 def call_enqueue_emails(realm: Realm) -> int:
     do_set_realm_property(realm,
                           "digest_emails_enabled",
                           True,
                           acting_user=None)
     do_set_realm_property(realm,
                           "digest_weekday",
                           timezone_now().weekday(),
                           acting_user=None)
     cutoff = timezone_now() - datetime.timedelta(days=0)
     with mock.patch(
             "zerver.worker.queue_processors.bulk_handle_digest_email"
     ) as queue_mock:
         enqueue_emails(cutoff)
     return 0 if queue_mock.call_args is None else len(
         queue_mock.call_args[0][0])
Exemplo n.º 12
0
    def test_active_users_not_enqueued(self, mock_django_timezone, mock_queue_digest_recipient):
        # type: (mock.MagicMock, mock.MagicMock) -> None

        cutoff = timezone_now()
        # A Tuesday
        mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)

        for realm in Realm.objects.filter(deactivated=False, show_digest_email=True):
            for user_profile in UserProfile.objects.filter(realm=realm):
                UserActivity.objects.create(
                    last_visit=cutoff + datetime.timedelta(days=1),
                    user_profile=user_profile,
                    count=0,
                    client=get_client('test_client'))

        # Check that an active user is not enqueued
        enqueue_emails(cutoff)
        self.assertEqual(mock_queue_digest_recipient.call_count, 0)
Exemplo n.º 13
0
 def test_active_users_not_enqueued(self, mock_django_timezone: mock.MagicMock,
                                    mock_enough_traffic: mock.MagicMock) -> None:
     cutoff = timezone_now()
     # A Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)
     realms = Realm.objects.filter(deactivated=False, show_digest_email=True)
     for realm in realms:
         user_profiles = UserProfile.objects.filter(realm=realm)
         for counter, user_profile in enumerate(user_profiles, 1):
             UserActivity.objects.create(
                 last_visit=cutoff + datetime.timedelta(days=1),
                 user_profile=user_profile,
                 count=0,
                 client=get_client('test_client'))
     # Check that an active user is not enqueued
     with mock.patch('zerver.lib.digest.queue_digest_recipient') as mock_queue_digest_recipient:
         enqueue_emails(cutoff)
         self.assertEqual(mock_queue_digest_recipient.call_count, 0)
Exemplo n.º 14
0
    def test_no_email_digest_for_bots(self, mock_django_timezone: mock.MagicMock,
                                      mock_queue_digest_recipient: mock.MagicMock) -> None:
        cutoff = timezone_now()
        # A Tuesday
        mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)
        bot = do_create_user('*****@*****.**', 'password', get_realm('zulip'), 'some_bot', '',
                             bot_type=UserProfile.DEFAULT_BOT)
        UserActivity.objects.create(
            last_visit=cutoff - datetime.timedelta(days=1),
            user_profile=bot,
            count=0,
            client=get_client('test_client'))

        # Check that bots are not sent emails
        enqueue_emails(cutoff)
        for arg in mock_queue_digest_recipient.call_args_list:
            user = arg[0][0]
            self.assertNotEqual(user.id, bot.id)
Exemplo n.º 15
0
 def test_inactive_users_queued_for_digest(self, mock_django_timezone: mock.MagicMock,
                                           mock_queue_digest_recipient: mock.MagicMock) -> None:
     cutoff = timezone_now()
     # Test Tuesday
     mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5)
     all_user_profiles = UserProfile.objects.filter(
         is_active=True, is_bot=False, enable_digest_emails=True)
     # Check that all users without an a UserActivity entry are considered
     # inactive users and get enqueued.
     enqueue_emails(cutoff)
     self.assertEqual(mock_queue_digest_recipient.call_count, all_user_profiles.count())
     mock_queue_digest_recipient.reset_mock()
     for realm in Realm.objects.filter(deactivated=False, show_digest_email=True):
         user_profiles = all_user_profiles.filter(realm=realm)
         for user_profile in user_profiles:
             UserActivity.objects.create(
                 last_visit=cutoff - datetime.timedelta(days=1),
                 user_profile=user_profile,
                 count=0,
                 client=get_client('test_client'))
     # Check that inactive users are enqueued
     enqueue_emails(cutoff)
     self.assertEqual(mock_queue_digest_recipient.call_count, all_user_profiles.count())
Exemplo n.º 16
0
 def handle(self, *args: Any, **options: Any) -> None:
     cutoff = timezone_now() - datetime.timedelta(days=DIGEST_CUTOFF)
     enqueue_emails(cutoff)
Exemplo n.º 17
0
 def handle(self, *args: Any, **options: Any) -> None:
     cutoff = timezone_now() - datetime.timedelta(days=DIGEST_CUTOFF)
     enqueue_emails(cutoff)