Exemplo n.º 1
0
    def test_find_bodies_with_bad_mime_tree(self):
        email = factories.EmailFactory()
        body = factories.BodyFactory(data=b"This mail body is searchable")  # build 1 body and use that

        # the root part, multipart/alternative
        root_part = factories.PartListFactory(email=email, body=body)
        factories.HeaderFactory(part=root_part, name="Content-Type", data="multipart/alternative")

        # first text part
        alt1_part = factories.PartListFactory(email=email, body=body, parent=root_part)
        factories.HeaderFactory(part=alt1_part, name="Content-Type", data="text/plain; charset=\"ascii\"")

        # second text part
        alt2_part = factories.PartListFactory(email=email, body=body, parent=root_part)
        factories.HeaderFactory(part=alt2_part, name="Content-Type", data="text/plain; charset=\"ascii\"")

        # make first text part invalid by giving it a child
        alt1_child_part = factories.PartListFactory(email=email, body=body, parent=alt1_part)
        factories.HeaderFactory(part=alt1_child_part, name="Content-Type", data="text/plain; charset=\"ascii\"")

        # find_bodies returns a list of lists, so flatten it out
        bodies = [part for part_list in email_utils.find_bodies(email.get_parts()) for part in part_list]
        # we should only see one part
        self.assertEqual(len(bodies), 1)
        # and it should be a leaf node
        self.assertTrue(bodies[0].is_leaf_node())
        self.assertEqual(bodies[0], alt2_part)
Exemplo n.º 2
0
    def test_encoders_used(self):
        # make message with base64 part, uuencode part, 8bit part, 7bit part,
        # quopri part, and some invalid part
        body_data = b"Hello\n\nHow are you?\n"
        email = factories.EmailFactory(inbox=self.inbox)
        body = factories.BodyFactory(data=body_data)
        first_part = factories.PartListFactory(
            email=email, body=factories.BodyFactory(data=b""))
        factories.HeaderFactory(
            part=first_part,
            name="Content-Type",
            data="multipart/mixed; boundary=\"=-3BRZDE/skgKPPh+RuFa/\"")

        encodings = {
            "base64": check_base64,
            "quoted-printable": check_quopri,
            "uuencode": check_uu,
            "x-uuencode": check_uu,
            "uue": check_uu,
            "x-uue": check_uu,
            "7-bit": check_noop,
            "8-bit": check_noop,
            "9-bit": check_unknown,  # unknown encoding
        }

        for enc in encodings.keys():
            part = factories.PartListFactory(email=email,
                                             parent=first_part,
                                             body=body)
            factories.HeaderFactory(part=part,
                                    name="Content-Type",
                                    data="text/plain; name=\"my-file.txt\"")
            factories.HeaderFactory(part=part,
                                    name="Content-Transfer-Encoding",
                                    data=enc)

        # finally, make a part without content headers
        factories.PartListFactory(email=email, parent=first_part, body=body)

        # and now export
        message_object = make_message(email)

        for message_part in message_object.walk():
            ct = message_part.get("Content-Type", None)
            cte = message_part.get("Content-Transfer-Encoding", None)
            if ct is None:
                # default is to assume 7-bit
                check_noop(message_part, body_data)
                self.assertEqual(message_part.get_payload(decode=True),
                                 body_data)
            elif ct.startswith("multipart/mixed"):
                pass
            else:
                encodings[cte](message_part, body_data)
                self.assertEqual(message_part.get_payload(decode=True),
                                 body_data)
Exemplo n.º 3
0
    def setUp(self):
        super(EmailViewTestCase, self).setUp()

        self.user = factories.UserFactory()
        self.email = factories.EmailFactory(inbox__user=self.user)
        body = factories.BodyFactory(data=BODY)
        part = factories.PartListFactory(email=self.email, body=body)
        factories.HeaderFactory(part=part, name="From")
        factories.HeaderFactory(part=part, name="Subject")
        factories.HeaderFactory(part=part, name="Content-Type", data="text/html; charset=\"utf-8\"")

        login = self.client.login(username=self.user.username, password="******", request=MockRequest(self.user))

        if not login:
            raise Exception("Could not log in")
Exemplo n.º 4
0
    def setUp(self):
        self.user = factories.UserFactory()
        self.inboxes = factories.InboxFactory.create_batch(2, user=self.user)
        self.emails = factories.EmailFactory.create_batch(
            5, inbox=self.inboxes[0])
        self.emails.extend(
            factories.EmailFactory.create_batch(5, inbox=self.inboxes[1]))

        for email in self.emails:
            part = factories.PartListFactory(email=email)
            factories.HeaderFactory(part=part, name="From")
            factories.HeaderFactory(part=part, name="Subject", data="ßssss!")

        self.tmp_dir = tempfile.mkdtemp()
        self.mail_dir = os.path.join(self.tmp_dir, "isdabizda")
        mailbox.Maildir(self.mail_dir)
Exemplo n.º 5
0
    def setUp(self):
        super(BadEmailTestCase, self).setUp()

        self.user = factories.UserFactory()
        self.email = factories.EmailFactory(inbox__user=self.user)
        body = factories.BodyFactory(data=BODY)
        part = factories.PartListFactory(email=self.email, body=body)
        factories.HeaderFactory(part=part, name="From")
        factories.HeaderFactory(part=part, name="Subject")
        factories.HeaderFactory(part=part,
                                name="Content-Type",
                                data="text/html; charset=\"windows-1252\"")
        factories.HeaderFactory(part=part,
                                name="Content-Disposition",
                                data="inline; filename=\"He\n\rl\rlo\nß.jpg\"")

        self.email_metaless = factories.EmailFactory(inbox__user=self.user)
        body = factories.BodyFactory(data=METALESS_BODY)
        part = factories.PartListFactory(email=self.email_metaless, body=body)
        factories.HeaderFactory(part=part, name="From")
        factories.HeaderFactory(part=part, name="Subject")
        factories.HeaderFactory(part=part,
                                name="Content-Type",
                                data="text/html; charset=\"ascii\"")

        login = self.client.login(username=self.user.username,
                                  password="******",
                                  request=MockRequest(self.user))

        if not login:
            raise Exception("Could not log in")
Exemplo n.º 6
0
    def setUp(self):
        self.user = factories.UserFactory()

        login = self.client.login(username=self.user.username,
                                  password="******",
                                  request=MockRequest(self.user))

        if not login:
            raise Exception("Could not log in")

        self.emails = factories.EmailFactory.create_batch(
            30, inbox__user=self.user)
        self.not_mine = factories.EmailFactory.create()

        for email in self.emails:
            part = factories.PartListFactory(email=email)
            factories.HeaderFactory(part=part, name="From")
            factories.HeaderFactory(part=part, name="Subject")
Exemplo n.º 7
0
 def test_name_in_cd(self):
     factories.HeaderFactory(part=self.part, name="Content-Disposition", data="inline; filename=\"Växjö.jpg\"")
     url = urlresolvers.reverse("email-attachment", kwargs={"attachmentid": self.part.id})
     response = self.client.get(url)
     self.assertEqual(response.status_code, 200)
     if six.PY3:
         self.assertEqual(response["Content-Disposition"], "attachment; filename=\"Växjö.jpg\"")
     else:
         self.assertEqual(response["Content-Disposition"], "attachment; filename=\"Växjö.jpg\"".encode("utf-8"))
Exemplo n.º 8
0
 def test_name_in_cd(self):
     factories.HeaderFactory(part=self.part,
                             name="Content-Disposition",
                             data="inline; filename=\"Växjö.jpg\"")
     url = urls.reverse("email-attachment",
                        kwargs={"attachmentid": self.part.id})
     response = self.client.get(url)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(
         response["Content-Disposition"],
         "attachment; filename=\"Vaxjo.jpg\"; filename*=UTF-8''V%C3%A4xj%C3%B6.jpg"
     )
Exemplo n.º 9
0
    def test_encoders_used(self):
        # make message with base64 part, uuencode part, 8bit part, 7bit part,
        # quopri part, and some invalid part
        unicode_body_data = "Hello\n\nHow are you?\nPó på pə pë\n".encode()
        ascii_body_data = "Hello\n\nHow are you?\n".encode()
        email = factories.EmailFactory(inbox=self.inbox)
        unicode_body = factories.BodyFactory(data=unicode_body_data)
        ascii_body = factories.BodyFactory(data=ascii_body_data)
        first_part = factories.PartListFactory(
            email=email, body=factories.BodyFactory(data=b""))
        factories.HeaderFactory(
            part=first_part,
            name="Content-Type",
            data="multipart/mixed; boundary=\"=-3BRZDE/skgKPPh+RuFa/\"")

        unicode_encodings = {
            "base64": check_base64,
            "quoted-printable": check_quopri,
            "uuencode": check_uu,
            "x-uuencode": check_uu,
            "uue": check_uu,
            "x-uue": check_uu,
        }
        ascii_encodings = {
            "7-bit": check_noop,
            "8-bit": check_noop,
            "9-bit": check_unknown,  # unknown encoding
        }
        encodings = {}
        encodings.update(unicode_encodings)
        encodings.update(ascii_encodings)

        for enc in unicode_encodings.keys():
            part = factories.PartListFactory(email=email,
                                             parent=first_part,
                                             body=unicode_body)
            factories.HeaderFactory(part=part,
                                    name="Content-Type",
                                    data="text/plain; name=\"my-file.txt\"")
            factories.HeaderFactory(part=part,
                                    name="Content-Transfer-Encoding",
                                    data=enc)

        for enc in ascii_encodings.keys():
            part = factories.PartListFactory(email=email,
                                             parent=first_part,
                                             body=ascii_body)
            factories.HeaderFactory(part=part,
                                    name="Content-Type",
                                    data="text/plain; name=\"my-file.txt\"")
            factories.HeaderFactory(part=part,
                                    name="Content-Transfer-Encoding",
                                    data=enc)

        # finally, make a part without content headers
        factories.PartListFactory(email=email,
                                  parent=first_part,
                                  body=ascii_body)

        # and now export
        message_object = make_message(email)

        for message_part in message_object.walk():
            ct = message_part.get("Content-Type", None)
            cte = message_part.get("Content-Transfer-Encoding", None)
            if ct is None:
                # default is to assume 7-bit
                check_noop(message_part, ascii_body_data)
                self.assertEqual(message_part.get_payload(decode=True),
                                 ascii_body_data)
            elif ct.startswith("multipart/mixed"):
                pass
            elif cte in ascii_encodings:
                encodings[cte](message_part, ascii_body_data)
                self.assertEqual(message_part.get_payload(decode=True),
                                 ascii_body_data)
            elif cte in unicode_encodings:
                encodings[cte](message_part, unicode_body_data)
                self.assertEqual(message_part.get_payload(decode=True),
                                 unicode_body_data)
            else:
                raise AssertionError(
                    "Unknown Content-Type or Content-Type-Encoding")

        # check that we can decode the whole lot in one go
        output_bytes = message_object.as_string().encode("ascii")
        self.assertNotEqual(len(output_bytes), 0)
        self.assertEqual(
            output_bytes.count(b"text/plain; name=\"my-file.txt\""),
            len(encodings))