Ejemplo n.º 1
0
def test_ignore_mail_sending_error(mock_mail):
    "If sending throws an error, we log and continue."
    mock_mail.send.side_effect = Exception()
    notification = NotificationFactory()
    mock_mail.send.return_value = Email()
    with patch.object(Notification, 'save'):
        with patch('unicef_notification.models.logger') as mock_logger:
            notification.send_mail()
    mock_logger.exception.assert_called_with('Failed to send mail.')
    # recipients weren't marked as successful
    assert notification.sent_recipients == []
Ejemplo n.º 2
0
def test_sender_is_not_a_user(mock_mail):
    "If sender is not a User, send DEFAULT_FROM_EMAIL"
    mock_mail.send.return_value = Email()
    notification = NotificationFactory()
    with patch.object(Notification, 'save'):
        notification.send_mail()
    # we called send ...
    mock_mail.send.assert_called()
    call_kwargs = mock_mail.send.call_args[1]
    # ... with the proper email
    assert settings.DEFAULT_FROM_EMAIL == call_kwargs['sender']
Ejemplo n.º 3
0
def test_sender_is_user(mock_mail):
    "If sender is a User, send from their email address"
    sender = UserFactory()
    notification = NotificationFactory(sender=sender)
    mock_mail.send.return_value = Email()
    with patch.object(Notification, 'save'):
        notification.send_mail()
    # we called send ...
    mock_mail.send.assert_called()
    call_kwargs = mock_mail.send.call_args[1]
    # ... with the proper email
    assert sender.email == call_kwargs['sender']
Ejemplo n.º 4
0
def test_template_data_is_str(mock_mail):
    "We accept string data for the template context."
    template_data = '{"foo": "bar"}'
    notification = NotificationFactory(template_data=template_data)
    mock_mail.send.return_value = Email()
    with patch.object(Notification, 'save'):
        notification.send_mail()
    # we called send ...
    mock_mail.send.assert_called()
    call_kwargs = mock_mail.send.call_args[1]
    # ... with the proper context
    assert {'foo': 'bar'} == call_kwargs['context']
Ejemplo n.º 5
0
def test_sender_is_from_address(mock_mail):
    "If sender is not a User, use from address if set"
    mock_mail.send.return_value = Email()
    from_address = "*****@*****.**"
    notification = NotificationFactory(from_address=from_address)
    with patch.object(Notification, 'save'):
        notification.send_mail()
    # we called send ...
    mock_mail.send.assert_called()
    call_kwargs = mock_mail.send.call_args[1]
    # ... with the proper email
    assert from_address == call_kwargs['sender']
Ejemplo n.º 6
0
def test_success(mock_mail):
    "On successful notification, sent_recipients should be populated."
    cc = ['*****@*****.**']
    notification = NotificationFactory(template_data={'foo': 'bar'}, cc=cc)
    mock_mail.send.return_value = Email()
    with patch.object(Notification, 'save'):
        notification.send_mail()
    # we called send with all the proper args
    mock_mail.send.assert_called_with(
        recipients=notification.recipients,
        cc=cc,
        sender=settings.DEFAULT_FROM_EMAIL,
        template=notification.template_name,
        context=notification.template_data,
        html_message='',
        message='',
        subject='',
    )
    # we marked the recipients as sent
    assert notification.recipients + cc == notification.sent_recipients
Ejemplo n.º 7
0
    def setUp(self):
        self.database = ':memory:'

        self.category_storage = CategoryStorage(self.database)
        self.task_storage = TaskStorage(self.database)
        self.task_plan_storage = TaskPlanStorage(self.database)
        self.notification_storage = NotificationStorage(self.database)
        self.category = CategoryFactory(user_id=10)
        self.task = TaskFactory()
        self.task_plan = TaskPlanFactory()
        self.notification = NotificationFactory()

        DatabaseConnector(self.database).create_tables()
Ejemplo n.º 8
0
    def setUp(self):
        self.database = ':memory:'

        self.user_id = 10
        self.categories_controller = CategoriesController(
            self.user_id, CategoryStorage(self.database))
        self.tasks_controller = TasksController(self.user_id,
                                                TaskStorage(self.database))
        self.task_plans_controller = TaskPlansController(
            self.user_id, TaskPlanStorage(self.database))
        self.notifications_controller = NotificationsController(
            self.user_id, NotificationStorage(self.database))
        self.category = CategoryFactory()
        self.task = TaskFactory()
        self.task_plan = TaskPlanFactory()
        self.notification = NotificationFactory()
        DatabaseConnector(self.database).create_tables()
Ejemplo n.º 9
0
def articles():
    LabelFactory.create_batch(5)
    AuthorFactory.create_batch(10)
    TopicFactory.create_batch(20)
    NotificationFactory.create_batch(20)
    return ArticleFactory.create_batch(100)