Esempio n. 1
0
    def test_invalid_addresses(self):
        from med_djutils.email import send_mail2

        _reset_test_outbox()
        outbox = django_mail.outbox

        subject = 'hello!'
        body = 'my email body'
        invalid_addresses = ['', ' ', 'x', 'x@x']
        not_str_addresses = [None, -15]

        # empty 'to' list: sends nothing
        send_mail2(subject=subject, body=body, to=[])
        self.assertEqual(len(outbox), 0)

        # Even though the addresses are invalid, the messages to them ARE SENT
        for address in invalid_addresses:
            temp = len(outbox)
            send_mail2(subject=subject, body=body, to=[address])
            self.assertEqual(len(outbox), temp + 1)

        # if address contains a newline, BadHeaderError(ValueError) is raised
        with self.assertRaises(ValueError):
            send_mail2(subject=subject, body=body, to=['\n'])

        # if address is not a string, ValueError is raised
        for address in not_str_addresses:
            with self.assertRaises(ValueError):
                send_mail2(subject=subject, body=body, to=[address])
Esempio n. 2
0
    def test_real_call(self):
        from med_djutils.email import send_mail2

        _reset_test_outbox()
        outbox = django_mail.outbox

        subject = 'hello!'
        body = 'my email body'
        to = ['*****@*****.**', '*****@*****.**']

        self.assertEqual(len(outbox), 0)
        send_mail2(subject=subject, body=body, to=to)
        self.assertEqual(len(outbox), 1)
        self.assertEqual(outbox[0].to, to)
        self.assertIn(body, outbox[0].body)
Esempio n. 3
0
    def test_full_call(self):
        """Test the function using all the parameters with mock objects."""
        from med_djutils.email import send_mail2

        (subject, body, from_email, to, cc, bcc, reply_to, fail_silently,
            auth_user, auth_password, connection, attachments, headers) = \
        (self.subject, self.body, self.from_email, self.to, self.cc, self.bcc,
            self.reply_to, self.fail_silently, self.auth_user,
            self.auth_password, self.connection, self.attachments,
            self.headers)
        send_return_value = NonCallableMock()

        with patch.object(django_mail.EmailMessage,
                          '__init__',
                          return_value=None) as mock_init:
            with patch.object(django_mail.EmailMessage, 'send') as mock_send:
                mock_send.return_value = send_return_value

                rvalue = send_mail2(subject, body, from_email, to, cc, bcc,
                                    reply_to, fail_silently, auth_user,
                                    auth_password, connection, attachments,
                                    headers)

                # check EmailMessage object construction
                mock_init.assert_called_once_with(subject, body, from_email,
                                                  to, bcc, connection,
                                                  attachments, headers, cc)
                # check the returned value is the result of EmailMessage's send method
                self.assertEqual(rvalue, send_return_value)
Esempio n. 4
0
    def test_connection(self):
        """Check which ``connection`` object is used to create the
        :class:`EmailMessage` object.

        If ``connection`` is passed, that one is used. If not, the result of
        :func:`django.core.mail.get_connection` is used.

        """
        from med_djutils.email import send_mail2

        # Case A: if 'connection' is passed, that is used
        with patch.object(django_mail.EmailMessage,
                          '__init__',
                          return_value=None) as mock_init:
            with patch.object(django_mail.EmailMessage, 'send'):
                send_mail2(connection=self.connection)

                mock_init.assert_called_once_with('', '', None, None, None,
                                                  self.connection, None, None,
                                                  None)

        # Case B: else, the result of 'get_connection' is used
        # (also, the arguments of that call are checked)
        with patch('django.core.mail.get_connection') as mock_get_connection:
            mock_connection = Mock(name='connection')
            mock_get_connection.return_value = mock_connection

            with patch.object(django_mail.EmailMessage,
                              '__init__',
                              return_value=None) as mock_init:
                with patch.object(django_mail.EmailMessage, 'send'):
                    send_mail2()

                    mock_init.assert_called_once_with('', '', None, None, None,
                                                      mock_connection, None,
                                                      None, None)
                    mock_get_connection.assert_called_once_with(
                        username=None, password=None, fail_silently=False)
Esempio n. 5
0
    def test_reply_to(self):
        from med_djutils.email import send_mail2

        mock_conn = Mock(name='connection')

        with patch('django.core.mail.get_connection') as mock_get_connection:
            mock_get_connection.return_value = mock_conn

            with patch.object(django_mail.EmailMessage,
                              '__init__',
                              return_value=None) as mock_init:
                with patch.object(django_mail.EmailMessage, 'send'):

                    send_mail2(headers=self.headers)
                    mock_init.assert_called_once_with('', '', None, None, None,
                                                      mock_conn, None,
                                                      self.headers, None)
                    self.assertFalse(self.headers.__setitem__.called)

                    mock_init.reset_mock()
                    self.headers.reset_mock()

                    send_mail2(reply_to=self.reply_to, headers=self.headers)
                    mock_init.assert_called_once_with('', '', None, None, None,
                                                      mock_conn, None,
                                                      self.headers, None)
                    self.headers.__setitem__.assert_called_once_with(
                        'Reply-To', self.reply_to)

                    mock_init.reset_mock()
                    self.headers.reset_mock()

                    send_mail2(reply_to=self.reply_to)
                    mock_init.assert_called_once_with(
                        '', '', None, None, None, mock_conn, None,
                        {'Reply-To': self.reply_to}, None)
                    self.assertFalse(self.headers.__setitem__.called)
Esempio n. 6
0
    def test_simplest_call(self):
        from med_djutils.email import send_mail2

        self.assertEqual(send_mail2(), 0)