Exemplo n.º 1
0
    def test_get_text_body_properly_recomposes_line_continuations(self):
        message = Message()
        email_object = self._get_email_object(
            'message_with_long_text_lines.eml')

        message.get_email_object = lambda: email_object

        actual_text = message.text
        expected_text = ('The one of us with a bike pump is far ahead, '
                         'but a man stopped to help us and gave us his pump.')

        self.assertEqual(actual_text, expected_text)
Exemplo n.º 2
0
 def test_receive_conversation_contribution_with_tag_by_mail(self):
     # create incoming mail with tag (#302)
     self.client.post(
         reverse('create-group-conversation', args=(self.group.pk, )), {
             'subject': 'Subject',
             'text': 'Text'
         })
     reply_to = mail.outbox[0].extra_headers['Reply-To']
     msg = Message(
         from_header=self.gestalt.user.email,
         body='Delivered-To: {}\n\nText with #tag'.format(reply_to))
     message_received.send(self, message=msg)
Exemplo n.º 3
0
    def test_get_body_properly_handles_unicode_body(self):
        with open(
                os.path.join(os.path.dirname(__file__),
                             'messages/generic_message.eml')) as f:
            unicode_body = six.u(f.read())

        message = Message()
        message.body = unicode_body

        expected_body = unicode_body
        actual_body = message.get_email_object().as_string()

        self.assertEqual(expected_body, actual_body)
Exemplo n.º 4
0
    def _process_message(self, message: RawMessage) -> Message:
        """
        We are overriding this method only to change receiving of message
        text form by calling str(message.as_bytes(), charset) instead of
        message.as_string() as original method does.
        """
        from campaigns.utils import convert_header_to_unicode

        msg = Message()

        if mailbox_settings['store_original_message']:
            msg.eml.save('%s.eml' % uuid.uuid4(),
                         ContentFile(message.as_string()),
                         save=False)
        msg.mailbox = self
        if 'subject' in message:
            msg.subject = (convert_header_to_unicode(
                message['subject'])[0:255])
        if 'message-id' in message:
            msg.message_id = message['message-id'][0:255].strip()
        if 'from' in message:
            msg.from_header = convert_header_to_unicode(message['from'])
        if 'to' in message:
            msg.to_header = convert_header_to_unicode(message['to'])
        elif 'Delivered-To' in message:
            msg.to_header = convert_header_to_unicode(message['Delivered-To'])
        msg.save()
        dehydrated_message = self._get_dehydrated_message(message, msg)

        try:
            message_context = dehydrated_message.as_string()
        except KeyError as e:
            for charset in filter(None, dehydrated_message.get_charsets()):
                try:
                    message_context = str(dehydrated_message.as_bytes(),
                                          charset)
                    break
                except UnicodeDecodeError:
                    continue
            else:
                raise e

        msg.set_body(message_context)
        if dehydrated_message['in-reply-to']:
            msg.in_reply_to = Message.objects.filter(
                message_id=dehydrated_message['in-reply-to'].strip()).first()
        msg.save()
        return msg
Exemplo n.º 5
0
    def test_message_issue_82_bis(self):
        """ Ensure that the email object is good before and after
        calling _get_dehydrated_message()

        """
        message = self._get_email_object('email_issue_82.eml')

        success = True

        # this is the code of _process_message()
        msg = Message()
        # if STORE_ORIGINAL_MESSAGE:
        #     msg.eml.save('%s.eml' % uuid.uuid4(), ContentFile(message), save=False)
        msg.mailbox = self.mailbox
        if 'subject' in message:
            msg.subject = convert_header_to_unicode(message['subject'])[0:255]
        if 'message-id' in message:
            msg.message_id = message['message-id'][0:255]
        if 'from' in message:
            msg.from_header = convert_header_to_unicode(message['from'])
        if 'to' in message:
            msg.to_header = convert_header_to_unicode(message['to'])
        elif 'Delivered-To' in message:
            msg.to_header = convert_header_to_unicode(message['Delivered-To'])
        msg.save()

        #here the message is ok
        str_msg = message.as_string()
        message = self.mailbox._get_dehydrated_message(message, msg)
        try:
            # here as_string raises UnicodeEncodeError
            str_msg = message.as_string()
        except:
            success = False

        msg.set_body(str_msg)
        if message['in-reply-to']:
            try:
                msg.in_reply_to = Message.objects.filter(
                    message_id=message['in-reply-to']
                )[0]
            except IndexError:
                pass
        msg.save()

        self.assertEqual(True, success)
    def test_message_handles_rehydration_problems(self):
        incoming_email_object = self._get_email_object(
            'message_with_defective_attachment_association.eml', )
        expected_email_object = self._get_email_object(
            'message_with_defective_attachment_association_result.eml', )
        # Note: this is identical to the above, but it appears that
        # while reading-in an e-mail message, we do alter it slightly
        message = Message()
        message.body = incoming_email_object.as_string()

        msg = self.mailbox.process_incoming_message(incoming_email_object)

        actual_email_object = msg.get_email_object()

        self.assertEqual(
            actual_email_object,
            expected_email_object,
        )
Exemplo n.º 7
0
    def test_receive_content_contribution_with_tag_by_mail(self):
        # create incoming mail with tag (#302)
        self.client.post(
            reverse('create-group-article', args=(self.group.slug, )), {
                'title': 'Test',
                'text': 'Test'
            })
        reply_to = mail.outbox[0].extra_headers['Reply-To']
        msg = Message(
            from_header=self.gestalt.user.email,
            body='Delivered-To: {}\n\nText with #tag'.format(reply_to))
        message_received.send(self, message=msg)

        # check, that tag page contains link to article
        r = self.client.get(reverse('tag', args=('tag', )))
        self.assertContains(
            r, 'href="{}"'.format(
                Association.objects.get(
                    content__title='Test').get_absolute_url()))