Beispiel #1
0
def test_from_mime():
    # create email from separated mime and test if it get reconstructed ok
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=3))
    root_mime.append(text_1)
    attachments = []
    for _ in range(2):
        a = mime.create.attachment("image/png", randStr(size=10), randUnicode(), AttachmentType.INLINE)
        attachments.append(a)
        a.to_string()
        root_mime.append(a)

    text_2 = mime.create.text("plain", randUnicode(length=3))
    root_mime.append(text_2)
    for _ in range(3):
        a = mime.create.attachment("video/mp4", randStr(size=15), randUnicode(), AttachmentType.ATTACHMENT)
        attachments.append(a)
        a.to_string()
        root_mime.append(a)
    root_mime.headers["Message-Id"] = u"<{}>".format(EmailHelpers.newMessageId())
    email = EmailV1.fromMime(root_mime.to_string(), [], {"user_id": u"*****@*****.**", "display_name": u"S B"})

    # check if the attachments have been all separated properly
    body_mime = mime.from_string(email.body.content)
    assert len(attachments) == len(filter(lambda p: p.content_type.value == DUMMY_CONTENT_TYPE , body_mime.parts))
    # check att hashes are properly inserted as filenames
    assert map(lambda a: HexEncode(Sha256Sum(a.to_string())), attachments) == map(lambda p: p.content_disposition[1]["filename"], filter(lambda p: p.content_type.value == DUMMY_CONTENT_TYPE , body_mime.parts))
Beispiel #2
0
def test_parse_mime_regular_attachment():
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=1))
    root_mime.append(text_1)

    for _ in range(3):
        att = mime.create.attachment(u"image/png", randStr(size=12355),
                                     randUnicode(), AttachmentType.ATTACHMENT)
        root_mime.append(att)

    text, html, attachments = parseMime(root_mime)

    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join(
        [u"<div data-preveil-text>{}</div>".format(text_1.body)])
    # all are regular atts
    assert all(
        map(
            lambda att: att.metadata.content_disposition == AttachmentType.
            ATTACHMENT, attachments))
    # all the atts exist w right content
    assert map(lambda att: att.content.content, attachments) == map(
        lambda att_part: att_part.body,
        filter(lambda p: p.content_disposition[0] == AttachmentType.ATTACHMENT,
               root_mime.parts))

    #add some nested atts
    nested_mime = mime.create.multipart("mixed")
    html_1 = mime.create.text("html", randUnicode(length=454))
    nested_mime.append(html_1)
    for _ in range(5):
        att = mime.create.attachment(u"application/octet-stream",
                                     randStr(size=545), randUnicode(length=12),
                                     AttachmentType.ATTACHMENT)
        nested_mime.append(att)

    root_mime.append(nested_mime)

    text, html, attachments = parseMime(root_mime)
    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join(
        [u"<div data-preveil-text>{}</div>".format(text_1.body), html_1.body])
    # all are regular atts
    assert all(
        map(
            lambda att: att.metadata.content_disposition == AttachmentType.
            ATTACHMENT, attachments))
    # all the atts exist w right content
    assert map(lambda att: att.content.content, attachments) == map(
        lambda att_part: att_part.body,
        filter(lambda p: p.content_disposition[0] == AttachmentType.ATTACHMENT,
               root_mime.parts + nested_mime.parts))
Beispiel #3
0
def test_unknown_content_type_with_no_disposition():
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=1332))
    root_mime.append(text_1)

    att = mime.create.attachment(u"image/png", randStr(size=8722),
                                 randUnicode(), AttachmentType.ATTACHMENT)
    root_mime.append(att)

    content = randUnicode(length=1523)
    unknown_part = mime.create.text("plain", content)
    unknown_part.headers["Content-Type"] = mime.message.ContentType(
        u"xx", u"yy")

    root_mime.append(unknown_part)

    text, html, attachments = parseMime(root_mime)

    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join(
        [u"<div data-preveil-text>{}</div>".format(text_1.body)])
    assert len(attachments) == 2
    # assert that the bad content type has translated in as a regular attachment
    assert map(lambda att: att.metadata.content_disposition,
               attachments) == [AttachmentType.ATTACHMENT] * 2
    # all attachment still has the right content
    assert map(lambda att: att.content.content, attachments) == map(
        lambda p: p.body,
        filter(lambda p: p.content_type.value != u"text/plain",
               root_mime.parts))
Beispiel #4
0
def test_create_mime_headers_validity():
    text = randUnicode(length=2012)
    html = randUnicode(length=2014)
    attachments = []
    for _ in range(4):
        content = randStr(size=2017)
        _filename = randUnicode()
        f = FileStorage(stream=StringIO.StringIO(content), filename=_filename, content_type="image/jpeg")
        attachments.append(Attachment.fromFileStorage(f, AttachmentMetadata(f.filename, f.content_type)))

    message_id = randUnicode()
    _time = time.time()
    tos = [{"user_id": randUnicode()+"@x.com", "display_name": randUnicode()} for _ in range(12)]
    ccs = [{"user_id": randUnicode()+"@x.com", "display_name": randUnicode()} for _ in range(4)]
    bccs = [{"user_id": randUnicode()+"@x.com", "display_name": randUnicode()} for _ in range(3)]
    references = [randUnicode() for _ in range(5)]
    in_reply_to = randUnicode()
    subject = randUnicode()
    reply_tos = [{"user_id": randUnicode()+"@x.com", "display_name": randUnicode()} for _ in range(2)]
    sender = {"user_id": randUnicode()+"@x.com", "display_name": randUnicode()}
    raw_mime = createMime(text, html, attachments, message_id, _time, subject, tos, ccs, bccs, reply_tos, sender, in_reply_to, references)
    assert message_id == raw_mime.headers.get("Message-Id")
    assert tos == [{"user_id": to.address, "display_name": to.display_name} for to in addresslib.address.parse_list(raw_mime.headers.get("To"))]
    assert ccs == [{"user_id": cc.address, "display_name": cc.display_name} for cc in addresslib.address.parse_list(raw_mime.headers.get("Cc"))]
    assert bccs == [{"user_id": bcc.address, "display_name": bcc.display_name} for bcc in addresslib.address.parse_list(raw_mime.headers.get("Bcc"))]
    assert reply_tos == [{"user_id": rpt.address, "display_name": rpt.display_name} for rpt in addresslib.address.parse_list(raw_mime.headers.get("Reply-To"))]
    assert "{} <{}>".format(sender["display_name"], sender["user_id"]) == raw_mime.headers.get("From")
    assert subject == raw_mime.headers.get("Subject")
    assert references == raw_mime.headers.get("references").split(" ")
    assert in_reply_to == raw_mime.headers.get("in-reply-to")
    assert email.utils.formatdate(_time) == raw_mime.headers.get("Date")
    assert len(re.findall("=\n", raw_mime.to_string())) == 0
Beispiel #5
0
def test_create_mime_with_inline_attachment():
    text = randUnicode(length=1024)
    html = randUnicode(length=1024)
    attachments = []
    for _ in range(4):
        content = randStr(size=4096)
        _filename = randUnicode()
        f = FileStorage(stream=StringIO.StringIO(content), filename=_filename, content_type="image/jpeg")
        att = Attachment.fromFileStorage(f, AttachmentMetadata(f.filename, f.content_type))
        att.metadata.content_disposition = AttachmentType.INLINE
        attachments.append(att)


    raw_mime = createMime(text, html, attachments)
    assert raw_mime.content_type.value == u"multipart/alternative"
    # 1 part is for text/plain content, and one for multipart/related
    assert set(["(multipart/related)", "(text/plain)"]) == set(map(lambda p: str(p), raw_mime.parts))
    assert text == filter(lambda p:str(p) == "(text/plain)", raw_mime.parts)[0].body
    related_part = filter(lambda p: str(p) == "(multipart/related)", raw_mime.parts)[0]
    # one for html and rest inline atts
    assert len(related_part.parts) == len(attachments) + 1
    assert html == filter(lambda p:str(p) == "(text/html)", related_part.parts)[0].body
    attachment_parts = filter(lambda p: p.content_disposition[0] == AttachmentType.INLINE, related_part.parts)
    assert set(map(lambda att: att.metadata.filename, attachments)) == set(map(lambda att_part: att_part.content_disposition[1]["filename"], attachment_parts))
    assert set(map(lambda att: att.content.content, attachments)) == set(map(lambda att_part: att_part.body, attachment_parts))
    assert len(re.findall("=\n", raw_mime.to_string())) == 0
Beispiel #6
0
def test_parse_mime_inline_attachment():
    root_mime = mime.create.multipart("related")
    html_1 = mime.create.text("html", randUnicode(length=10))
    root_mime.append(html_1)

    for _ in range(6):
        att = mime.create.attachment(u"image/png", randStr(size=235),
                                     randUnicode(), AttachmentType.INLINE)
        root_mime.append(att)

    text, html, attachments = parseMime(root_mime)

    assert text == u""
    #the 3 attachments should be inline and have content id assigned to them
    assert len(attachments) == 6
    assert all([
        att.metadata.content_disposition == AttachmentType.INLINE
        for att in attachments
    ])
    matches = re.findall("<div data-preveil-inline>(.*?)</div>",
                         html,
                         flags=re.DOTALL)
    # all are inline
    assert len(matches) == len(attachments)
    # make sure all the content ids are included
    content_ids = [re.findall('src="(.*?)"', img)[0] for img in matches]
    content_ids = [re.subn("cid:", "<", ci)[0] + ">" for ci in content_ids]
    assert content_ids == [att.metadata.content_id for att in attachments]

    # test when inline atts have content_id
    root_mime = mime.create.multipart("alternative")
    text_1 = mime.create.text("plain", randUnicode(length=19))
    root_mime.append(text_1)
    related_mime = mime.create.multipart("related")
    html_1 = mime.create.text("html", randUnicode(length=15))
    related_mime.append(html_1)
    att = mime.create.attachment(u"application/octet-stream",
                                 randStr(size=421), randUnicode(),
                                 AttachmentType.INLINE)
    cid = EmailHelpers.newMessageId()
    att.headers["Content-Id"] = cid
    related_mime.append(att)
    root_mime.append(related_mime)
    text, html, attachments = parseMime(root_mime)
    assert len(attachments) == 1
    assert cid == attachments[0].metadata.content_id
    assert len(re.findall("data-preveil-inline", html)) == 0
Beispiel #7
0
def test_from_mime():
    # create email from separated mime and test if it get reconstructed ok
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=3))
    root_mime.append(text_1)
    attachments = []
    for _ in range(2):
        a = mime.create.attachment("image/png", randStr(size=10),
                                   randUnicode(), AttachmentType.INLINE)
        attachments.append(a)
        a.to_string()
        root_mime.append(a)

    text_2 = mime.create.text("plain", randUnicode(length=3))
    root_mime.append(text_2)
    for _ in range(3):
        a = mime.create.attachment("video/mp4", randStr(size=15),
                                   randUnicode(), AttachmentType.ATTACHMENT)
        attachments.append(a)
        a.to_string()
        root_mime.append(a)
    root_mime.headers["Message-Id"] = u"<{}>".format(
        EmailHelpers.newMessageId())
    email = EmailV1.fromMime(root_mime.to_string(), [],
                             overwrite_sender={
                                 "user_id": u"*****@*****.**",
                                 "display_name": u"S B"
                             })

    # check if the attachments have been all separated properly
    body_mime = mime.from_string(email.body.content)
    assert len(attachments) == len(
        filter(lambda p: p.content_type.value == DUMMY_CONTENT_TYPE,
               body_mime.parts))
    # check att hashes are properly inserted as filenames
    assert map(lambda a: HexEncode(Sha256Sum(a.to_string())),
               attachments) == map(
                   lambda p: p.content_disposition[1]["filename"],
                   filter(lambda p: p.content_type.value == DUMMY_CONTENT_TYPE,
                          body_mime.parts))
Beispiel #8
0
def test_parse_mime_inline_attachment():
    root_mime = mime.create.multipart("related")
    html_1 = mime.create.text("html", randUnicode(length=10))
    root_mime.append(html_1)

    for _ in range(6):
        att = mime.create.attachment(u"image/png", randStr(size=235), randUnicode(), AttachmentType.INLINE)
        root_mime.append(att)

    text, html, attachments = parseMime(root_mime)

    assert text == u""
    #the 3 attachments should be inline and have content id assigned to them
    assert len(attachments) == 6
    assert all([att.metadata.content_disposition == AttachmentType.INLINE for att in attachments])
    matches = re.findall("<div data-preveil-inline>(.*?)</div>", html, flags=re.DOTALL)
    # all are inline
    assert len(matches) == len(attachments)
    # make sure all the content ids are included
    content_ids = [re.findall('src="(.*?)"',img)[0] for img in matches]
    content_ids = [re.subn("cid:", "<", ci)[0] + ">" for ci in content_ids]
    assert content_ids == [att.metadata.content_id for att in attachments]

    # test when inline atts have content_id
    root_mime = mime.create.multipart("alternative")
    text_1 = mime.create.text("plain", randUnicode(length=19))
    root_mime.append(text_1)
    related_mime = mime.create.multipart("related")
    html_1 = mime.create.text("html", randUnicode(length=15))
    related_mime.append(html_1)
    att = mime.create.attachment(u"application/octet-stream", randStr(size=421), randUnicode(), AttachmentType.INLINE)
    cid = EmailHelpers.newMessageId()
    att.headers["Content-Id"] = cid
    related_mime.append(att)
    root_mime.append(related_mime)
    text, html, attachments = parseMime(root_mime)
    assert len(attachments) == 1
    assert cid == attachments[0].metadata.content_id
    assert len(re.findall("data-preveil-inline", html)) == 0
Beispiel #9
0
def test_attachment_to_mime():
    filename = randUnicode()
    content_type = "image/png"
    content_disposition = randUnicode()
    content_id = randUnicode()
    metadata = AttachmentMetadata(filename, content_type, content_disposition, content_id)
    blob = randStr(size=1024)

    attachement = Attachment(metadata, Content(blob))
    att_mime = attachement.toMime()
    assert att_mime.headers.get("Content-Id") == content_id
    assert att_mime.headers.get("Content-Disposition") == (content_disposition, {"filename": filename})
    assert att_mime.headers.get("Content-Type") == content_type
    assert att_mime.body == blob
Beispiel #10
0
def test_parse_mime_regular_attachment():
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=1))
    root_mime.append(text_1)

    for _ in range(3):
        att = mime.create.attachment(u"image/png", randStr(size=12355), randUnicode(), AttachmentType.ATTACHMENT)
        root_mime.append(att)

    text, html, attachments = parseMime(root_mime)

    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join([u"<div data-preveil-text>{}</div>".format(text_1.body)])
    # all are regular atts
    assert all(map(lambda att: att.metadata.content_disposition == AttachmentType.ATTACHMENT, attachments))
    # all the atts exist w right content
    assert map(lambda att: att.content.content, attachments) == map(lambda att_part: att_part.body, filter(lambda p: p.content_disposition[0] == AttachmentType.ATTACHMENT, root_mime.parts))

    #add some nested atts
    nested_mime = mime.create.multipart("mixed")
    html_1 = mime.create.text("html", randUnicode(length=454))
    nested_mime.append(html_1)
    for _ in range(5):
        att = mime.create.attachment(u"application/octet-stream", randStr(size=545), randUnicode(length=12), AttachmentType.ATTACHMENT)
        nested_mime.append(att)

    root_mime.append(nested_mime)

    text, html, attachments = parseMime(root_mime)
    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join([u"<div data-preveil-text>{}</div>".format(text_1.body), html_1.body])
    # all are regular atts
    assert all(map(lambda att: att.metadata.content_disposition == AttachmentType.ATTACHMENT, attachments))
    # all the atts exist w right content
    assert map(lambda att: att.content.content, attachments) == map(lambda att_part: att_part.body, filter(lambda p: p.content_disposition[0] == AttachmentType.ATTACHMENT, root_mime.parts + nested_mime.parts))
Beispiel #11
0
def test_invalid_content_disposition():
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=1653))
    root_mime.append(text_1)

    att = mime.create.attachment(u"image/png", randStr(size=2052), randUnicode(), u"invalidCD")
    root_mime.append(att)

    text, html, attachments = parseMime(root_mime)

    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join([u"<div data-preveil-text>{}</div>".format(text_1.body)])
    assert len(attachments) == 1
    # assert that the bad CD has translated as a regular attachment
    assert attachments[0].metadata.content_disposition == AttachmentType.ATTACHMENT
    # all attachment still has the right content
    assert attachments[0].content.content == filter(lambda p: p.content_disposition[0] == u"invalidCD", root_mime.parts)[0].body
Beispiel #12
0
def test_attachment_to_mime():
    filename = randUnicode()
    content_type = "image/png"
    content_disposition = randUnicode()
    content_id = randUnicode()
    metadata = AttachmentMetadata(filename, content_type, content_disposition,
                                  content_id)
    blob = randStr(size=1024)

    attachement = Attachment(metadata, Content(blob))
    att_mime = attachement.toMime()
    assert att_mime.headers.get("Content-Id") == content_id
    assert att_mime.headers.get("Content-Disposition") == (content_disposition,
                                                           {
                                                               "filename":
                                                               filename
                                                           })
    assert att_mime.headers.get("Content-Type") == content_type
    assert att_mime.body == blob
Beispiel #13
0
def test_create_mime_with_regular_attachment():
    text = randUnicode(length=1024)
    html = randUnicode(length=1024)
    attachments = []
    for _ in range(4):
        content = randStr(size=4096)
        _filename = randUnicode()
        f = FileStorage(stream=StringIO.StringIO(content),
                        filename=_filename,
                        content_type="image/jpeg")
        attachments.append(
            Attachment.fromFileStorage(
                f, AttachmentMetadata(f.filename, f.content_type)))

    raw_mime = createMime(text, html, attachments)
    assert raw_mime.content_type.value == u"multipart/mixed"
    # 1 part is for text/html content, and one for each attachment
    assert len(raw_mime.parts) == len(attachments) + 1
    body_parts = filter(
        lambda p: p.content_type.value == u"multipart/alternative",
        raw_mime.parts)
    assert len(body_parts) == 1
    body_part = body_parts[0]
    assert set(["(text/html)",
                "(text/plain)"]) == set(map(lambda p: str(p), body_part.parts))
    attachment_parts = filter(
        lambda p: p.content_disposition[0] == AttachmentType.ATTACHMENT,
        raw_mime.parts)
    assert set(map(lambda att: att.metadata.content_disposition,
                   attachments)) == set(
                       map(lambda att_part: att_part.content_disposition[0],
                           attachment_parts))
    assert set(map(lambda att: att.metadata.filename, attachments)) == set(
        map(lambda att_part: att_part.content_disposition[1]["filename"],
            attachment_parts))
    assert set(map(lambda att: att.content.content, attachments)) == set(
        map(lambda att_part: att_part.body, attachment_parts))
    assert len(re.findall("=\n", raw_mime.to_string())) == 0
Beispiel #14
0
def test_invalid_content_disposition():
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=1653))
    root_mime.append(text_1)

    att = mime.create.attachment(u"image/png", randStr(size=2052),
                                 randUnicode(), u"invalidCD")
    root_mime.append(att)

    text, html, attachments = parseMime(root_mime)

    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join(
        [u"<div data-preveil-text>{}</div>".format(text_1.body)])
    assert len(attachments) == 1
    # assert that the bad CD has translated as a regular attachment
    assert attachments[
        0].metadata.content_disposition == AttachmentType.ATTACHMENT
    # all attachment still has the right content
    assert attachments[0].content.content == filter(
        lambda p: p.content_disposition[0] == u"invalidCD",
        root_mime.parts)[0].body
Beispiel #15
0
def test_create_mime_with_regular_attachment():
    text = randUnicode(length=1024)
    html = randUnicode(length=1024)
    attachments = []
    for _ in range(4):
        content = randStr(size=4096)
        _filename = randUnicode()
        f = FileStorage(stream=StringIO.StringIO(content), filename=_filename, content_type="image/jpeg")
        attachments.append(Attachment.fromFileStorage(f, AttachmentMetadata(f.filename, f.content_type)))

    raw_mime = createMime(text, html, attachments)
    assert raw_mime.content_type.value == u"multipart/mixed"
    # 1 part is for text/html content, and one for each attachment
    assert len(raw_mime.parts) == len(attachments) + 1
    body_parts = filter(lambda p: p.content_type.value == u"multipart/alternative", raw_mime.parts)
    assert len(body_parts) == 1
    body_part = body_parts[0]
    assert set(["(text/html)", "(text/plain)"]) == set(map(lambda p: str(p), body_part.parts))
    attachment_parts = filter(lambda p: p.content_disposition[0] == AttachmentType.ATTACHMENT, raw_mime.parts)
    assert set(map(lambda att: att.metadata.content_disposition, attachments)) == set(map(lambda att_part: att_part.content_disposition[0], attachment_parts))
    assert set(map(lambda att: att.metadata.filename, attachments)) == set(map(lambda att_part: att_part.content_disposition[1]["filename"], attachment_parts))
    assert set(map(lambda att: att.content.content, attachments)) == set(map(lambda att_part: att_part.body, attachment_parts))
    assert len(re.findall("=\n", raw_mime.to_string())) == 0
Beispiel #16
0
def test_create_mime_with_inline_attachment():
    text = randUnicode(length=1024)
    html = randUnicode(length=1024)
    attachments = []
    for _ in range(4):
        content = randStr(size=4096)
        _filename = randUnicode()
        f = FileStorage(stream=StringIO.StringIO(content),
                        filename=_filename,
                        content_type="image/jpeg")
        att = Attachment.fromFileStorage(
            f, AttachmentMetadata(f.filename, f.content_type))
        att.metadata.content_disposition = AttachmentType.INLINE
        attachments.append(att)

    raw_mime = createMime(text, html, attachments)
    assert raw_mime.content_type.value == u"multipart/alternative"
    # 1 part is for text/plain content, and one for multipart/related
    assert set(["(multipart/related)",
                "(text/plain)"]) == set(map(lambda p: str(p), raw_mime.parts))
    assert text == filter(lambda p: str(p) == "(text/plain)",
                          raw_mime.parts)[0].body
    related_part = filter(lambda p: str(p) == "(multipart/related)",
                          raw_mime.parts)[0]
    # one for html and rest inline atts
    assert len(related_part.parts) == len(attachments) + 1
    assert html == filter(lambda p: str(p) == "(text/html)",
                          related_part.parts)[0].body
    attachment_parts = filter(
        lambda p: p.content_disposition[0] == AttachmentType.INLINE,
        related_part.parts)
    assert set(map(lambda att: att.metadata.filename, attachments)) == set(
        map(lambda att_part: att_part.content_disposition[1]["filename"],
            attachment_parts))
    assert set(map(lambda att: att.content.content, attachments)) == set(
        map(lambda att_part: att_part.body, attachment_parts))
    assert len(re.findall("=\n", raw_mime.to_string())) == 0
Beispiel #17
0
def test_unknown_content_type_with_no_disposition():
    root_mime = mime.create.multipart("mixed")
    text_1 = mime.create.text("plain", randUnicode(length=1332))
    root_mime.append(text_1)

    att = mime.create.attachment(u"image/png", randStr(size=8722), randUnicode(), AttachmentType.ATTACHMENT)
    root_mime.append(att)

    content = randUnicode(length=1523)
    unknown_part = mime.create.text("plain", content)
    unknown_part.headers["Content-Type"] = mime.message.ContentType(u"xx", u"yy")

    root_mime.append(unknown_part)

    text, html, attachments = parseMime(root_mime)

    assert text == text_1.body
    # non alternate text gets added to html
    assert html == u"<br>".join([u"<div data-preveil-text>{}</div>".format(text_1.body)])
    assert len(attachments) == 2
    # assert that the bad content type has translated in as a regular attachment
    assert map(lambda att: att.metadata.content_disposition, attachments) == [AttachmentType.ATTACHMENT]*2
    # all attachment still has the right content
    assert map(lambda att: att.content.content, attachments) == map(lambda p: p.body, filter(lambda p: p.content_type.value != u"text/plain", root_mime.parts))
Beispiel #18
0
def test_create_mime_headers_validity():
    text = randUnicode(length=2012)
    html = randUnicode(length=2014)
    attachments = []
    for _ in range(4):
        content = randStr(size=2017)
        _filename = randUnicode()
        f = FileStorage(stream=StringIO.StringIO(content),
                        filename=_filename,
                        content_type="image/jpeg")
        attachments.append(
            Attachment.fromFileStorage(
                f, AttachmentMetadata(f.filename, f.content_type)))

    message_id = randUnicode()
    _time = time.time()
    tos = [{
        "user_id": randUnicode() + "@x.com",
        "display_name": randUnicode()
    } for _ in range(12)]
    ccs = [{
        "user_id": randUnicode() + "@x.com",
        "display_name": randUnicode()
    } for _ in range(4)]
    bccs = [{
        "user_id": randUnicode() + "@x.com",
        "display_name": randUnicode()
    } for _ in range(3)]
    references = [randUnicode() for _ in range(5)]
    in_reply_to = randUnicode()
    subject = randUnicode()
    reply_tos = [{
        "user_id": randUnicode() + "@x.com",
        "display_name": randUnicode()
    } for _ in range(2)]
    sender = {
        "user_id": randUnicode() + "@x.com",
        "display_name": randUnicode()
    }
    raw_mime = createMime(text, html, attachments, message_id, _time, subject,
                          tos, ccs, bccs, reply_tos, sender, in_reply_to,
                          references)
    assert message_id == raw_mime.headers.get("Message-Id")
    assert tos == [{
        "user_id": to.address,
        "display_name": to.display_name
    } for to in addresslib.address.parse_list(raw_mime.headers.get("To"))]
    assert ccs == [{
        "user_id": cc.address,
        "display_name": cc.display_name
    } for cc in addresslib.address.parse_list(raw_mime.headers.get("Cc"))]
    assert bccs == [{
        "user_id": bcc.address,
        "display_name": bcc.display_name
    } for bcc in addresslib.address.parse_list(raw_mime.headers.get("Bcc"))]
    assert reply_tos == [{
        "user_id": rpt.address,
        "display_name": rpt.display_name
    }
                         for rpt in addresslib.address.parse_list(
                             raw_mime.headers.get("Reply-To"))]
    assert "{} <{}>".format(sender["display_name"],
                            sender["user_id"]) == raw_mime.headers.get("From")
    assert subject == raw_mime.headers.get("Subject")
    assert references == raw_mime.headers.get("references").split(" ")
    assert in_reply_to == raw_mime.headers.get("in-reply-to")
    assert email.utils.formatdate(_time) == raw_mime.headers.get("Date")
    assert len(re.findall("=\n", raw_mime.to_string())) == 0