Example #1
0
    def test_email_received_signal(self):
        """Test that a valid POST fires the email_received signal."""
        # define handler
        for klass, payload in self._get_payloads_and_parsers():

            def on_email_received(sender, **kwargs):
                self.on_email_received_fired = True
                self.assertEqual(sender.__name__, klass.split('.')[-1])
                request = kwargs.pop('request', None)
                email = kwargs.pop('email', None)
                self.assertIsNotNone(email)
                self.assertIsInstance(email, EmailMultiAlternatives)
                self.assertIsNotNone(request)
            email_received.connect(on_email_received)

            settings.INBOUND_EMAIL_PARSER = klass
            request = self.factory.post(self.url, data=payload)

            # connect handler
            self.on_email_received_fired = False

            # fire a request in to force the signal to fire
            receive_inbound_email(request)
            self.assertTrue(self.on_email_received_fired)

            email_received.disconnect(on_email_received)
Example #2
0
    def test_email_received_unacceptable_signal_fired_for_too_large_attachment(self):
        # set a zero allowed max attachment size
        settings.INBOUND_EMAIL_ATTACHMENT_SIZE_MAX = 0

        for klass, payload in self._get_payloads_and_parsers(with_attachments=True):
            settings.INBOUND_EMAIL_PARSER = klass
            _payload = payload.copy()

            if klass == SENDGRID_REQUEST_PARSER:
                _payload['attachment'] = open(self.test_upload_txt, 'r')
            if klass == MAILGUN_REQUEST_PARSER:
                _payload['attachment-1'] = open(self.test_upload_txt, 'r')

            # define handler
            def on_email_received(sender, **kwargs):
                self.on_email_received_fired = True
                request = kwargs.pop('request', None)
                email = kwargs.pop('email', None)
                exception = kwargs.pop('exception', None)
                self.assertEqual(sender.__name__, klass.split('.')[-1])
                self.assertIsNotNone(request)
                self.assertIsInstance(email, EmailMultiAlternatives)
                self.assertIsInstance(exception, AttachmentTooLargeError)
            email_received_unacceptable.connect(on_email_received)

            self.on_email_received_fired = False

            request = self.factory.post(self.url, data=_payload)
            receive_inbound_email(request)
            self.assertTrue(self.on_email_received_fired, klass)
            email_received_unacceptable.disconnect(on_email_received)
Example #3
0
    def test_email_received_unacceptable_signal_fired_for_too_large_attachment(
            self):
        # set a zero allowed max attachment size
        settings.INBOUND_EMAIL_ATTACHMENT_SIZE_MAX = 0

        for klass, payload in self._get_payloads_and_parsers(
                with_attachments=True):
            settings.INBOUND_EMAIL_PARSER = klass
            _payload = payload.copy()

            if klass == SENDGRID_REQUEST_PARSER:
                _payload['attachment'] = open(self.test_upload_txt, 'r')
            if klass == MAILGUN_REQUEST_PARSER:
                _payload['attachment-1'] = open(self.test_upload_txt, 'r')

            # define handler
            def on_email_received(sender, **kwargs):
                self.on_email_received_fired = True
                request = kwargs.pop('request', None)
                email = kwargs.pop('email', None)
                exception = kwargs.pop('exception', None)
                self.assertEqual(sender.__name__, klass.split('.')[-1])
                self.assertIsNotNone(request)
                self.assertIsInstance(email, EmailMultiAlternatives)
                self.assertIsInstance(exception, AttachmentTooLargeError)

            email_received_unacceptable.connect(on_email_received)

            self.on_email_received_fired = False

            request = self.factory.post(self.url, data=_payload)
            receive_inbound_email(request)
            self.assertTrue(self.on_email_received_fired, klass)
            email_received_unacceptable.disconnect(on_email_received)
Example #4
0
    def test_email_received_signal(self):
        """Test that a valid POST fires the email_received signal."""
        # define handler
        for klass, payload in self._get_payloads_and_parsers():

            def on_email_received(sender, **kwargs):
                self.on_email_received_fired = True
                self.assertEqual(sender.__name__, klass.split('.')[-1])
                request = kwargs.pop('request', None)
                email = kwargs.pop('email', None)
                self.assertIsNotNone(email)
                self.assertIsInstance(email, EmailMultiAlternatives)
                self.assertIsNotNone(request)

            email_received.connect(on_email_received)

            settings.INBOUND_EMAIL_PARSER = klass
            request = self.factory.post(self.url, data=payload)

            # connect handler
            self.on_email_received_fired = False

            # fire a request in to force the signal to fire
            receive_inbound_email(request)
            self.assertTrue(self.on_email_received_fired)

            email_received.disconnect(on_email_received)
Example #5
0
 def test_valid_request(self):
     """Test the RequestParseErrors are handled correctly, and return HTTP 200."""
     for klass, payload in self._get_payloads_and_parsers():
         settings.INBOUND_EMAIL_PARSER = klass
         request = self.factory.post(self.url, data=payload)
         response = receive_inbound_email(request)
         self.assertContains(response, u"Successfully parsed", status_code=200)
Example #6
0
 def test_parse_error_response_200(self):
     """Test the RequestParseErrors are handled correctly, and return HTTP 200."""
     settings.INBOUND_EMAIL_RESPONSE_200 = True
     for klass, payload in self._get_payloads_and_parsers():
         settings.INBOUND_EMAIL_PARSER = klass
         request = self.factory.post(self.url, data={})
         response = receive_inbound_email(request)
         self.assertContains(response, u"Unable to parse", status_code=200)
Example #7
0
 def test_parse_error_response_200(self):
     """Test the RequestParseErrors are handled correctly, and return HTTP 200."""
     settings.INBOUND_EMAIL_RESPONSE_200 = True
     for klass, payload in self._get_payloads_and_parsers():
         settings.INBOUND_EMAIL_PARSER = klass
         request = self.factory.post(self.url, data={})
         response = receive_inbound_email(request)
         self.assertContains(response, u"Unable to parse", status_code=200)
Example #8
0
 def test_valid_request(self):
     """Test the RequestParseErrors are handled correctly, and return HTTP 200."""
     for klass, payload in self._get_payloads_and_parsers():
         settings.INBOUND_EMAIL_PARSER = klass
         request = self.factory.post(self.url, data=payload)
         response = receive_inbound_email(request)
         self.assertContains(response,
                             u"Successfully parsed",
                             status_code=200)
Example #9
0
 def test_inbound_request_HEAD_200(self):
     """Return 200 OK to a HEAD request."""
     request = self.factory.head(self.url)
     response = receive_inbound_email(request)
     self.assertEqual(response.status_code, 200)
Example #10
0
 def test_parse_error_response_400(self):
     """Test the RequestParseErrors are handled correctly, and return HTTP 400."""
     settings.INBOUND_EMAIL_RESPONSE_200 = False
     request = self.factory.post(self.url, data={})
     response = receive_inbound_email(request)
     self.assertContains(response, u"Unable to parse", status_code=400)
Example #11
0
 def test_inbound_request_HEAD_200(self):
     """Return 200 OK to a HEAD request."""
     request = self.factory.head(self.url)
     response = receive_inbound_email(request)
     self.assertEqual(response.status_code, 200)
Example #12
0
 def test_parse_error_response_400(self):
     """Test the RequestParseErrors are handled correctly, and return HTTP 400."""
     settings.INBOUND_EMAIL_RESPONSE_200 = False
     request = self.factory.post(self.url, data={})
     response = receive_inbound_email(request)
     self.assertContains(response, u"Unable to parse", status_code=400)