def test_is_interval_expired__hourly(self):
        self.config.set_interval('hourly')

        self.assertEqual(utils.get_last_notification_date(), None)
        self.assertEqual(utils.is_interval_expired(), True)

        self.now = datetime.datetime(2010, 12, 27, 10, 00)
        utils.set_last_notification_date_to_now()
        self.assertEqual(utils.is_interval_expired(), False)

        self.now = datetime.datetime(2010, 12, 27, 10, 31)
        self.assertEqual(utils.is_interval_expired(), False)

        self.now = datetime.datetime(2010, 12, 27, 11, 01)
        self.assertEqual(utils.is_interval_expired(), True)
    def test_adapter_not_called_when_inverval_not_expired(self):
        self.expect(self.notifier()).count(0)
        self.replay()

        set_last_notification_date_to_now()
        self.assertFalse(is_interval_expired())
        self.portal.unrestrictedTraverse('@@publisher.executeQueue')()
def invoke_notification(obj, event):
    """Event handler for `IQueueExecutedEvent` which invokes the
    `IReportNotifier` adapter.
    """

    config = INotifierConfigurationSchema(obj)
    if config.enabled and is_interval_expired():
        return getAdapter(obj, IReportNotifier)()
    def test_report_sent_after_executing_queue(self):
        self.set_time(1)
        utils.set_last_notification_date_to_now()

        self.set_time(2)
        self.suppose_job_was_executed(successful=True)
        self.suppose_job_was_executed(successful=True)
        self.suppose_job_was_executed(error=True)
        self.suppose_job_was_executed(warning=True)
        self.suppose_job_was_executed(warning=True)
        self.suppose_job_was_executed(warning=True)

        self.set_time(3)
        self.assertTrue(utils.is_interval_expired())

        self.portal.restrictedTraverse('@@publisher.executeQueue')()

        self.assertEqual(len(self.mails), 1)
        args, kwargs = self.mails.pop()

        self.assertEqual(kwargs.get('mfrom'), '*****@*****.**')
        self.assertEqual(kwargs.get('mto'), '*****@*****.**')
        self.assertEqual(kwargs.get('subject'),
                         u'Publisher report: Plone site')

        statistics_table = self.get_normalize_statistics_table_from_message(
            args[0])

        self.assertIn('<tr><th>Successfull jobs:</th><td>2</td></tr>',
                      statistics_table)

        self.assertIn('<tr><th>Jobs with warning:</th><td>3</td></tr>',
                      statistics_table)

        self.assertIn('<tr><th>Jobs with errors:</th><td>1</td></tr>',
                      statistics_table)

        self.assertIn('<tr><th>Total executed jobs:</th><td>6</td></tr>',
                      statistics_table)
    def test_report_does_only_contain_new_jobs(self):
        self.set_time(1)
        self.suppose_job_was_executed(successful=True)
        self.suppose_job_was_executed(successful=True)

        self.set_time(2)
        utils.set_last_notification_date_to_now()

        self.set_time(3)
        self.suppose_job_was_executed(successful=True)

        self.set_time(4)
        self.assertTrue(utils.is_interval_expired())
        self.portal.restrictedTraverse('@@publisher.executeQueue')()

        self.assertEqual(len(self.mails), 1)
        args, kwargs = self.mails.pop()

        statistics_table = self.get_normalize_statistics_table_from_message(
            args[0])

        self.assertIn('<tr><th>Total executed jobs:</th><td>1</td></tr>',
                      statistics_table)
    def test_report_is_sent_to_each_receivers(self):
        self.notifier_config.set_receivers_plain('\n'.join((
                    '*****@*****.**',
                    '*****@*****.**')))

        self.set_time(1)
        self.suppose_job_was_executed(successful=True)

        self.set_time(2)
        utils.set_last_notification_date_to_now()

        self.set_time(3)
        self.assertTrue(utils.is_interval_expired())
        self.portal.restrictedTraverse('@@publisher.executeQueue')()

        self.assertEqual(len(self.mails), 2)

        # we pop it reversed, therfore we test in opposite order than
        # it is configured.
        args, kwargs = self.mails.pop()
        self.assertEqual(kwargs.get('mto'), '*****@*****.**')

        args, kwargs = self.mails.pop()
        self.assertEqual(kwargs.get('mto'), '*****@*****.**')
 def test_default_values(self):
     import datetime
     self.assertEqual(utils.get_last_notification_date(), None)
     self.assertEqual(utils.is_interval_expired(), True)
     self.assertEqual(utils.get_interval_delta(), datetime.timedelta(1))