diff --git a/HISTORY.rst b/HISTORY.rst index 031c06a095..27d885646f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -262,6 +262,9 @@ * Add task path validation. * Increase dropzone upload file size limit to 2GB. * Add cabinet created and edited events. +* Show a null mailer backend if there is backend with an + invalid path. Due to the app full path change, existing + mailer setups need to be recreated. 3.1.11 (2019-04-XX) =================== diff --git a/docs/releases/3.2.rst b/docs/releases/3.2.rst index cc748e59ac..eeeaa75b91 100644 --- a/docs/releases/3.2.rst +++ b/docs/releases/3.2.rst @@ -572,6 +572,9 @@ Other changes * Add task path validation. * Increase dropzone upload file size limit to 2GB * Add cabinet created and edited events. +* Show a null mailer backend if there is backend with an + invalid path. Due to the app full path change, existing + mailer setups need to be recreated. Removals diff --git a/mayan/apps/mailer/classes.py b/mayan/apps/mailer/classes.py index df6b20ddc2..03f82a234d 100644 --- a/mayan/apps/mailer/classes.py +++ b/mayan/apps/mailer/classes.py @@ -6,6 +6,7 @@ import logging from django.apps import apps from django.utils import six from django.utils.encoding import force_text +from django.utils.translation import ugettext_lazy as _ logger = logging.getLogger(__name__) @@ -74,3 +75,7 @@ class MailerBackend(six.with_metaclass(MailerBackendMetaclass, MailerBackendBase 'Error importing %s mailers.py file; %s', app.name, exception ) + + +class NullBackend(MailerBackend): + label = _('Null backend') diff --git a/mayan/apps/mailer/models.py b/mayan/apps/mailer/models.py index cf37aeb94d..fb9e638826 100644 --- a/mayan/apps/mailer/models.py +++ b/mayan/apps/mailer/models.py @@ -11,6 +11,7 @@ from django.utils.html import strip_tags from django.utils.module_loading import import_string from django.utils.translation import ugettext_lazy as _ +from .classes import NullBackend from .events import event_email_sent from .managers import UserMailerManager from .utils import split_recipient_list @@ -99,7 +100,10 @@ class UserMailer(models.Model): """ Retrieves the backend by importing the module and the class """ - return import_string(self.backend_path) + try: + return import_string(self.backend_path) + except ImportError: + return NullBackend def get_connection(self): """ diff --git a/mayan/apps/mailer/tests/test_views.py b/mayan/apps/mailer/tests/test_views.py index 95b8443b9b..4058c566aa 100644 --- a/mayan/apps/mailer/tests/test_views.py +++ b/mayan/apps/mailer/tests/test_views.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from django.core import mail +from mayan.apps.common.tests import GenericViewTestCase from mayan.apps.documents.tests import GenericDocumentViewTestCase from ..models import UserMailer @@ -21,53 +22,8 @@ from .mailers import TestBackend from .mixins import MailerTestMixin, MailerViewTestMixin -class MailerViewsTestCase(MailerTestMixin, MailerViewTestMixin, GenericDocumentViewTestCase): - def test_mail_link_view_no_permissions(self): - self._create_test_user_mailer() - - response = self._request_test_document_link_send_view() - self.assertEqual(response.status_code, 404) - - def test_mail_link_view_with_access(self): - self._create_test_user_mailer() - - self.grant_access( - obj=self.test_document, permission=permission_mailing_link - ) - self.grant_access( - obj=self.test_user_mailer, permission=permission_user_mailer_use - ) - - response = self._request_test_document_link_send_view() - self.assertEqual(response.status_code, 302) - - self.assertEqual(len(mail.outbox), 1) - self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) - self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) - - def test_mail_document_view_no_permissions(self): - self._create_test_user_mailer() - - response = self._request_test_document_send_view() - self.assertEqual(response.status_code, 404) - - def test_mail_document_view_with_access(self): - self._create_test_user_mailer() - - self.grant_access( - obj=self.test_document, permission=permission_mailing_send_document - ) - self.grant_access( - obj=self.test_user_mailer, permission=permission_user_mailer_use - ) - - response = self._request_test_document_send_view() - self.assertEqual(response.status_code, 302) - - self.assertEqual(len(mail.outbox), 1) - self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) - self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) +class MailerViewsTestCase(MailerTestMixin, MailerViewTestMixin, GenericViewTestCase): def test_user_mailer_create_view_no_permissions(self): self.grant_permission(permission=permission_user_mailer_view) @@ -121,8 +77,20 @@ class MailerViewsTestCase(MailerTestMixin, MailerViewTestMixin, GenericDocumentV self._create_test_user_mailer() self.grant_access( - obj=self.test_document, permission=permission_mailing_send_document + obj=self.test_user_mailer, permission=permission_user_mailer_view ) + + response = self._request_test_user_mailer_list_view() + self.assertContains( + response=response, text=self.test_user_mailer.label, status_code=200 + ) + + def test_user_mailer_list_bad_data_view_with_access(self): + self._create_test_user_mailer() + self.test_user_mailer.backend_path = 'bad.backend.path' + self.test_user_mailer.backend_data = '{"bad_field": "bad_data"}' + self.test_user_mailer.save() + self.grant_access( obj=self.test_user_mailer, permission=permission_user_mailer_view ) @@ -206,6 +174,54 @@ class MailerViewsTestCase(MailerTestMixin, MailerViewTestMixin, GenericDocumentV mail.outbox[0].to, TEST_RECIPIENTS_MULTIPLE_SEMICOLON_RESULT ) + +class MailDocumentViewsTestCase(MailerTestMixin, MailerViewTestMixin, GenericDocumentViewTestCase): + def test_mail_link_view_no_permissions(self): + self._create_test_user_mailer() + + response = self._request_test_document_link_send_view() + self.assertEqual(response.status_code, 404) + + def test_mail_link_view_with_access(self): + self._create_test_user_mailer() + + self.grant_access( + obj=self.test_document, permission=permission_mailing_link + ) + self.grant_access( + obj=self.test_user_mailer, permission=permission_user_mailer_use + ) + + response = self._request_test_document_link_send_view() + self.assertEqual(response.status_code, 302) + + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) + self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) + + def test_mail_document_view_no_permissions(self): + self._create_test_user_mailer() + + response = self._request_test_document_send_view() + self.assertEqual(response.status_code, 404) + + def test_mail_document_view_with_access(self): + self._create_test_user_mailer() + + self.grant_access( + obj=self.test_document, permission=permission_mailing_send_document + ) + self.grant_access( + obj=self.test_user_mailer, permission=permission_user_mailer_use + ) + + response = self._request_test_document_send_view() + self.assertEqual(response.status_code, 302) + + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].from_email, TEST_EMAIL_FROM_ADDRESS) + self.assertEqual(mail.outbox[0].to, [TEST_EMAIL_ADDRESS]) + def test_mail_link_view_recipients_comma(self): self._create_test_user_mailer()