def test_attachment_from_file_storage(): blob = randStream() filename = randUnicode() content_type = "y/x" metadata = AttachmentMetadata(filename, content_type) _file = FileStorage(stream=blob, filename=filename, content_type=content_type) input_file = copy.deepcopy(_file) attachment = Attachment.fromFileStorage(input_file, metadata) assert attachment.content.content == _file.read() assert attachment.metadata.filename == filename assert attachment.metadata.content_type == content_type assert attachment.metadata.content_disposition == AttachmentType.ATTACHMENT # test content_type correction blob = randStream() filename = randUnicode() content_type = "badcontenttype" metadata = AttachmentMetadata(filename, content_type) _file = FileStorage(stream=blob, filename=filename, content_type=content_type) input_file = copy.deepcopy(_file) attachment = Attachment.fromFileStorage(input_file, metadata) assert attachment.content.content == _file.read() assert attachment.metadata.filename == filename assert attachment.metadata.content_type == "application/octet-stream" assert attachment.metadata.content_disposition == AttachmentType.ATTACHMENT
def create_email_v4(sender, tos, ccs, bccs, subject, text, html, attachments, in_reply_to, references, reply_tos=[], flags=[], server_attr=NOT_ASSIGNED(), message_id=None): if message_id is None: message_id = u"<{}>".format(EmailHelpers.newMessageId()) body = EmailHelpers.serializeBody({"text": text, "html": html}) return EmailFactory.new( **{ "server_attr": server_attr, "flags": flags, "subject": subject, "sender": { "user_id": sender.user_id, "display_name": sender.display_name }, "tos": [{ "user_id": to.user_id, "display_name": to.display_name } for to in tos], "ccs": [{ "user_id": cc.user_id, "display_name": cc.display_name } for cc in ccs], "bccs": [{ "user_id": bcc.user_id, "display_name": bcc.display_name } for bcc in bccs], "message_id": message_id, "body": Content(body), "attachments": [ Attachment.fromFileStorage( _file, AttachmentMetadata(_file.filename, _file.content_type)) for _file in attachments ], "in_reply_to": in_reply_to, "reply_tos": [], "references": references, "protocol_version": PROTOCOL_VERSION.V4 })
def test_attachment_metadata(): filename = randUnicode() content_type = randUnicode() content_disposition = randUnicode() content_id = randUnicode() size = random.randint(0, 1000000000) metadata = AttachmentMetadata(filename, content_type, content_disposition, content_id, size) assert metadata.toDict() == { "filename": filename, "content_type" : content_type, "content_id": content_id, "content_disposition": content_disposition, "size": size } metadata = AttachmentMetadata(filename, None, None, None, None) assert metadata.toDict() == { "filename": filename, "content_type" : u"application/octet-stream", "content_id": None, "content_disposition": AttachmentType.ATTACHMENT, "size": None }
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
def create_email_v1(sender, tos, ccs, bccs, subject, text, html, attachments, in_reply_to, references, reply_tos=[], flags=[], server_attr=NOT_ASSIGNED(), message_id=None): if message_id is None: message_id = u"<{}>".format(EmailHelpers.newMessageId()) sender = {"user_id": sender.user_id, "display_name": sender.display_name} tos = [{ "user_id": to.user_id, "display_name": to.display_name } for to in tos] ccs = [{ "user_id": cc.user_id, "display_name": cc.display_name } for cc in ccs] bccs = [{ "user_id": bcc.user_id, "display_name": bcc.display_name } for bcc in bccs] time = None if not isinstance(server_attr, NOT_ASSIGNED): time = server_attr.server_time raw_mime = createMime(text, html, [ Attachment.fromFileStorage( _file, AttachmentMetadata(_file.filename, _file.content_type)) for _file in attachments ], message_id, time, subject, tos, ccs, bccs, reply_tos, sender, in_reply_to, references) return EmailV1.fromMime(raw_mime.to_string(), flags, overwrite_sender=sender)
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
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
def test_attachment_metadata(): filename = randUnicode() content_type = randUnicode() content_disposition = randUnicode() content_id = randUnicode() size = random.randint(0, 1000000000) metadata = AttachmentMetadata(filename, content_type, content_disposition, content_id, size) assert metadata.toDict() == { "filename": filename, "content_type": content_type, "content_id": content_id, "content_disposition": content_disposition, "size": size } metadata = AttachmentMetadata(filename, None, None, None, None) assert metadata.toDict() == { "filename": filename, "content_type": u"application/octet-stream", "content_id": None, "content_disposition": AttachmentType.ATTACHMENT, "size": None }
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