Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test send_mail in Django?

Using Django 1.7 and Python 2.7.

I want to test if the mail was sent and if the content of the mail is correct.

I've tried using outbox from django.core.mail, but to no avail. Also could I just get the stdout (since I can see the mail in the console when I run my tests)?

models.py

class User(AbstractBaseUser, PermissionsMixin):
    USERNAME_FIELD = 'email'

    email = models.EmailField(max_length=255, unique=True)
    is_staff =  models.BooleanField(default=False)
    org = models.ForeignKey('Org', null=True, blank=True,
        on_delete=models.SET_NULL)

    def __unicode__(self):
        return self.email

    @staticmethod
    def send_password_token(email):
        user = get_object_or_404(User, email=email)
        token = Token.objects.get(user=user)
        message_body = 'Your password reset token:\n\n\t%s' % token.key
        send_mail('Password reset:', message_body,
            settings.FROM_EMAIL, [email], fail_silently=False)

tests.py

class UserModelTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(email='[email protected]',
            password='0000')

    @override_settings(EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend')
    def test_send_password_token(self):
        """
        Sends a password reset mail with users authentication token.
        """
        token = Token.objects.get(user=self.user)
        User.send_password_token(self.user.email)
like image 220
Saša Kalaba Avatar asked Feb 12 '16 13:02

Saša Kalaba


People also ask

How do I get mail in Django?

With Django AdminGo to Django Admin, then to 'Mailboxes' page, check all the mailboxes you need to receive emails from. At the top of the list with mailboxes, choose the action selector 'Get new mail' and click 'Go'.

How do I send multiple emails in Django?

How to send multiple mass emails django. We need to create a Tuple of messages and send them using send mass mail. In this tutorial, we create a project which sends email using Django. We fill the data in the form and send it using Django Email.

What is test case in Django?

The best base class for most tests is django. test. TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a user interacting with the code at the view level.


1 Answers

Thanks for @Alasdair for the solution. Turns out it was quite simple. Just remove override_settings and import outbox.

tests.py

from django.core.mail import outbox

class UserModelTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(email='[email protected]',
            password='0000')

    def test_send_password_token(self):
        """
        Sends a password reset mail with users authentication token.
        """
        token = Token.objects.get(user=self.user)
        User.send_password_token(self.user.email)
        self.assertEqual(len(outbox), 1)
        self.assertEqual(outbox[0].subject, 'Password reset:')
        self.assertEqual(outbox[0].from_email, <insert_from_email>)
        self.assertEqual(outbox[0].to, [<insert_list_of_to_emails>])
        self.assertEqual(outbox[0].body,
            'Your password reset token:\n\n\t%s' % token.key)
like image 194
Saša Kalaba Avatar answered Oct 22 '22 09:10

Saša Kalaba