diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8f27ce5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,39 @@ +sudo: false + + +language: python + + +branches: + only: + - master + + +matrix: + fast_finish: true + include: + - python: 3.5 + env: TOXENV=py35-django111 + - python: 3.5 + env: TOXENV=py35-django20 + - python: 3.6 + env: TOXENV=py36-django111 + - python: 3.6 + env: TOXENV=py36-django20 + + +before_cache: + - rm -rf $HOME/.cache/pip/log + + +cache: + directories: + - $HOME/.cache/pip + + +install: + - pip install tox + + +script: + - tox diff --git a/django_postmark_utils/backends.py b/django_postmark_utils/backends.py index 7b4856c..72a723a 100644 --- a/django_postmark_utils/backends.py +++ b/django_postmark_utils/backends.py @@ -3,8 +3,8 @@ class EmailBackend(EmailBackend): """ - A wrapper that by default quashes exceptions raised while sending messages. + A wrapper that quashes exceptions raised while sending messages. """ - def __init__(self, token=None, fail_silently=True, **kwargs): - super().__init__(token=token, fail_silently=fail_silently, **kwargs) + def __init__(self, token=None, fail_silently=False, **kwargs): + super().__init__(token=token, fail_silently=True, **kwargs) diff --git a/setup.py b/setup.py index 4609dfc..61542d1 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,12 @@ # allow setup.py to be run from any path os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) +test_requires = [ + 'pytest==3.6.3', + 'pytest-django==3.3.2', + 'pytest-xdist==1.22.2', +] + setup( name='django-postmark-utils', version='0.1', @@ -21,6 +27,9 @@ 'postmarker>=0.11.3', 'python-dateutil>=2.0', ], + extras_require={ + 'test': test_requires, + }, classifiers=[ 'Environment :: Web Environment', 'Framework :: Django', diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..088a8d9 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,9 @@ +SECRET_KEY = 'foo' + + +# Postmarker + +EMAIL_BACKEND = 'django_postmark_utils.backends.EmailBackend' +POSTMARK = { + 'TOKEN': '', +} diff --git a/tests/test_backends.py b/tests/test_backends.py new file mode 100644 index 0000000..5e491ea --- /dev/null +++ b/tests/test_backends.py @@ -0,0 +1,42 @@ +import pytest + +from django.core import mail + +from postmarker.exceptions import PostmarkerException + + +@pytest.fixture +def email_backend(settings): + """ + Overrides the EMAIL_BACKEND setting, because "pytest-django" uses + "django.test.utils.setup_test_environment", where it is set to + "django.core.mail.backends.locmem.EmailBackend". + """ + settings.EMAIL_BACKEND = 'django_postmark_utils.backends.EmailBackend' + + +@pytest.fixture +def postmark_response(postmark_request): + """ + Mocks an error response from a Postmark API call via Postmarker. + """ + postmark_request.return_value.json.side_effect = PostmarkerException + + +def send_with_connection(connection): + mail.EmailMessage( + 'Subject', 'Body', 'sender@example.com', ['receiver@example.com'], + connection=connection, + ).send() + + +@pytest.mark.usefixtures('email_backend', 'postmark_response') +class TestExceptions: + + def test_silent_exception_by_default(self): + with mail.get_connection() as connection: + send_with_connection(connection) + + def test_silent_exception_forced(self): + with mail.get_connection(fail_silently=False) as connection: + send_with_connection(connection) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..6c0e9e7 --- /dev/null +++ b/tox.ini @@ -0,0 +1,17 @@ +[pytest] +DJANGO_SETTINGS_MODULE = tests.settings + + +[tox] +envlist = + py{35,36}-django{111,20} + + +[testenv] +commands = pytest {posargs} +extras = test +deps = + django111: Django>=1.11,<2.0 + django20: Django>=2.0,<2.1 +setenv = + PYTHONPATH = {toxinidir}