Opened 10 years ago

Closed 10 years ago

#23003 closed New feature (wontfix)

Implement an EmailBackend raising an smtplib.SMTPException on send_messages()

Reported by: brgl Owned by: brgl
Component: Core (Mail) Version: dev
Severity: Normal Keywords: mail emailbackend smtpexception
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There is no standard django mail backend that allows to test the behaviour of an application when handling SMTPExceptions. Implement a simple subclass of BaseEmailBackend that raises SMTPException on send_messages().

Change History (4)

comment:1 by brgl, 10 years ago

Owner: set to brgl
Status: newassigned

comment:2 by Tim Graham, 10 years ago

This is 3 lines of code or so? I don't think it is such a common need that it needs to be included in Django.

comment:3 by brgl, 10 years ago

Yes, it's fairly easy, but still - I've always needed to implement it in every project to cover error handling with tests. Since there are things like 'dummy' mail backend in core/mail/backends, which is a three-liner too I thought it would be nice to have it as standard.

Tell me if you don't want to include it anyway, I won't bother sending a patch.

Last edited 10 years ago by brgl (previous) (diff)

comment:4 by Simon Charette, 10 years ago

Resolution: wontfix
Status: assignedclosed

@brgl, I agree with Tim that this doesn't need to be included in Django.

I suggest you take a look at the mock library for such use cases. It's part of the standard library since Python3.3 (in the unittest package) and available as standalone package on pypi for Python3.2-.

from smtplib import SMTPException
try:
    from unittest import mock  # Python 3.3+
except ImportError:
    try:
        import mock  # Python 3.2-
    except ImportError:
        raise ImportError(
            'Make sure to install the `mock` package on Python 3.2-.'
        )

from django.conf import settings
from django.test import TestCase


class SMTPExceptionHandlingTests(TestCase):
    def test_handling(self):
        send_messages = "%s.%s" % (settings.EMAIL_BACKEND, 'send_messages')
        with mock.patch(send_messages, side_effect=SMTPException):
            # Assert exception is correctly handled in this context below...

Note: See TracTickets for help on using tickets.
Back to Top