Exemplo n.º 1
0
    def test_process_task_multiple_times(self):

        with self.settings(TRANSMISSIONS_TRIGGERS=self.trigger_settings):
            user = factories.User()

            # Create trigger
            trigger_name = TRIGGER_NAME

            # Trigger notification
            notification = TaskTestMessage.trigger(user)
            self.assertEqual(notification.trigger_name, trigger_name)
            self.assertEqual(notification.target_user, user)
            self.assertEqual(notification.status, Notification.Status.CREATED)

            # Check channel and template are correct
            channel = Channel(notification)
            self.assertIsInstance(channel.message, TaskTestMessage)

            # Trigger single task
            tasks.process_notification(notification.id)
            tasks.process_notification(notification.id)
            tasks.process_notification(notification.id)
            tasks.process_notification(notification.id)
            tasks.process_notification(notification.id)
            notification = Notification.objects.get(pk=notification.id)

            # Check status
            self.assertEqual(notification.status,
                             Notification.Status.SUCCESSFULLY_SENT)
            self.assertGreaterEqual(notification.datetime_processed,
                                    notification.datetime_scheduled)

            # Check email was sent
            self.assertEqual(len(mail.outbox), 1)
            self.assertEqual(mail.outbox[0].subject, TRIGGER_SUBJECT)
Exemplo n.º 2
0
    def test_process_all_notifications_task_except_future_one(self):

        with self.settings(TRANSMISSIONS_TRIGGERS=self.trigger_settings):
            users = [factories.User() for i in xrange(3)]
            notifications = []

            # Create trigger
            trigger_name = TRIGGER_NAME

            # Trigger notification
            for user in users:
                notification = TaskTestMessage.trigger(user)
                notifications.append(notification)
                self.assertEqual(notification.trigger_name, trigger_name)
                self.assertEqual(notification.target_user, user)
                self.assertEqual(notification.status,
                                 Notification.Status.CREATED)

                # Check channel and template are correct
                channel = Channel(notification)
                self.assertIsInstance(channel.message, TaskTestMessage)

            self.assertEqual(len(users), len(notifications))

            # Schedule notification for later
            user = factories.User()
            later_notification = TaskTestMessage.trigger(
                user,
                datetime_scheduled=timezone.now() + timezone.timedelta(days=2))
            self.assertEqual(later_notification.trigger_name, trigger_name)
            self.assertEqual(later_notification.target_user, user)
            self.assertEqual(later_notification.status,
                             Notification.Status.CREATED)

            # Trigger task to process all remaining notifications
            processed_count = tasks.process_all_notifications()
            self.assertEqual(processed_count, len(notifications))

            # Check status
            for notification in notifications:
                notification = Notification.objects.get(pk=notification.id)
                self.assertEqual(notification.status,
                                 Notification.Status.SUCCESSFULLY_SENT)
                self.assertGreaterEqual(notification.datetime_processed,
                                        notification.datetime_scheduled)

            # Check email was sent
            self.assertEqual(len(mail.outbox), len(notifications))
            self.assertEqual(mail.outbox[0].subject, TRIGGER_SUBJECT)

            # Check status for later notification
            self.assertEqual(later_notification.status,
                             Notification.Status.CREATED)
            self.assertIsNone(later_notification.datetime_processed)

            # Trigger task to process all remaining notifications
            processed_count = tasks.process_all_notifications()
            self.assertEqual(processed_count, 0)
Exemplo n.º 3
0
    def send(self):
        """ Process notification and send via designated channel
        """

        try:
            channel = Channel(self)
            # Notification is not needed anymore
            if not channel.check_validity():
                self.status = self.Status.CANCELLED
            else:
                channel.send()
                self.status = self.Status.SUCCESSFULLY_SENT
            if channel.message.behavior == TriggerBehavior.DELETE_AFTER_PROCESSING:
                self.delete()
        except ChannelSendException:
            self.status = self.Status.FAILED
        except:
            self.status = self.Status.BROKEN
            raise
        finally:
            if self.pk:
                self.datetime_processed = timezone.now()
                self.save()
Exemplo n.º 4
0
    def send(self):
        """ Process notification and send via designated channel
        """

        try:
            channel = Channel(self)
            # Notification is not needed anymore
            if not channel.check_validity():
                self.status = self.Status.CANCELLED
            else:
                channel.send()
                self.status = self.Status.SUCCESSFULLY_SENT
            if channel.message.behavior == TriggerBehavior.DELETE_AFTER_PROCESSING:
                self.delete()
        except ChannelSendException:
            self.status = self.Status.FAILED
        except:
            self.status = self.Status.BROKEN
            raise
        finally:
            if self.pk:
                self.datetime_processed = timezone.now()
                self.save()
Exemplo n.º 5
0
    def test_sms_channel(self):

        welcome_path = "{}.{}".format(SimpleMessage.__module__, SimpleMessage.__name__)
        trigger_settings = {TRIGGER_SIMPLE: welcome_path}
        with self.settings(TRANSMISSIONS_TRIGGERS=trigger_settings):
            user = factories.User()

            # Trigger notification
            notification = SimpleMessage.trigger(user)
            self.assertEqual(notification.trigger_name, TRIGGER_SIMPLE)
            self.assertEqual(notification.target_user, user)

            # Check channel and template are correct
            channel = Channel(notification)
            self.assertIsInstance(channel.message, DefaultSMSMessage)
Exemplo n.º 6
0
    def test_simple(self):

        trigger_name = 'welcome'
        user = factories.User()

        welcome_path = "{}.{}".format(HelloWorld.__module__,
                                      HelloWorld.__name__)
        trigger_settings = {trigger_name: welcome_path}

        with self.settings(TRANSMISSIONS_TRIGGERS=trigger_settings):

            # Trigger notification
            notification = HelloWorld.trigger(user, datetime_scheduled=None)
            self.assertEqual(notification.trigger_name, trigger_name)
            self.assertEqual(notification.target_user, user)

            # Check channel and template are correct
            channel = Channel(notification)
            self.assertIsInstance(channel.message, HelloWorld)
Exemplo n.º 7
0
    def test_template_class(self):

        welcome_path = "{}.{}".format(SimpleMessage.__module__, SimpleMessage.__name__)
        trigger_settings = {TRIGGER_SIMPLE: welcome_path}
        with self.settings(TRANSMISSIONS_TRIGGERS=trigger_settings):

            user = factories.User()

            # Trigger notification
            notification = SimpleMessage.trigger(user)
            self.assertEqual(notification.trigger_name, TRIGGER_SIMPLE)
            self.assertEqual(notification.target_user, user)
            self.assertIsNone(notification.content)
            diff = notification.datetime_created - notification.datetime_scheduled
            self.assertLessEqual(abs(diff.seconds), 1)

            # Check channel and template are correct
            channel = Channel(notification)
            self.assertIsInstance(channel.message, DefaultSMSMessage)