def test_to_message_raises_encoding_error(self): base = encoding.MailBase() base.attach_text("hello", ctype="text/plain") base["Content-Type"] = "text/plain" # this is just broken with self.assertRaises(encoding.EncodingError): encoding.to_message(base)
def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' assert_equal(m['To'], "testing@localhost") assert_equal(m['TO'], m['To']) assert_equal(m['to'], m['To']) assert_equal(m['Subject'], the_subject) assert_equal(m['subject'], m['Subject']) assert_equal(m['sUbjeCt'], m['Subject']) msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) assert_equal(m.items(), m2.items()) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) assert ("To" in m) assert ("Bob" not in m) assert_equal(m["To"], "testing@localhost") m["To"] = "somebody@localhost" assert_equal(m["To"], "somebody@localhost") assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To']) assert_equal(m.items(), [('Subject', the_subject), ('Mime-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('To', 'somebody@localhost')]) assert_equal(m.get_all("Subject"), [the_subject]) assert_equal(m.get_all("Bob"), []) # appending headers m.append_header("subject", "something else") assert_equal(m["Subject"], the_subject) assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To', "Subject"]) assert_equal(m.items(), [('Subject', the_subject), ('Mime-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('To', 'somebody@localhost'), ('Subject', "something else")]) assert_equal(m.get_all("Subject"), [the_subject, "something else"]) # reset subject header m["Subject"] = the_subject for key, item in zip(m.keys(), m.items()): assert_equal(key, item[0]) assert key in m del m[key] assert key not in m assert key not in [i[0] for i in m.items()]
def __init__(self, To=None, From=None, Subject=None, Body=None, Html=None): self.Body = Body self.Html = Html self.base = encoding.MailBase([('To', To), ('From', From), ('Subject', Subject)]) self.multipart = self.Body and self.Html self.attachments = []
def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject assert m['To'] == "testing@localhost" assert m['TO'] == m['To'] assert m['to'] == m['To'] assert m['Subject'] == the_subject assert m['subject'] == m['Subject'] assert m['sUbjeCt'] == m['Subject'] msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) for k in m.keys(): assert k in m del m[k] assert not k in m
def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' assert m['To'] == "testing@localhost" assert m['TO'] == m['To'] assert m['to'] == m['To'] assert m['Subject'] == the_subject assert m['subject'] == m['Subject'] assert m['sUbjeCt'] == m['Subject'] msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) for k in m.keys(): assert k in m del m[k] assert k not in m
def test_odd_content_type_with_charset(): mail = encoding.MailBase() mail.body = u"p\xf6stal".encode('utf-8') mail.content_encoding['Content-Type'] = ('application/plain', {'charset': 'utf-8'}) msg = encoding.to_string(mail) assert msg
def test_attach_file(): mail = encoding.MailBase() png = open("tests/salmon.png", "rb").read() mail.attach_file("salmon.png", png, "image/png", "attachment") msg = encoding.to_message(mail) payload = msg.get_payload(0) assert_equal(payload.get_payload(decode=True), png) assert_equal(payload.get_filename(), "salmon.png")
def test_attach_file(self): mail = encoding.MailBase() with open("tests/data/salmon.png", "rb") as file_obj: png = file_obj.read() mail.attach_file("salmon.png", png, "image/png", "attachment") msg = encoding.to_message(mail) payload = msg.get_payload(0) self.assertEqual(payload.get_payload(decode=True), png) self.assertEqual(payload.get_filename(), "salmon.png")
def test_attach_text(): mail = encoding.MailBase() mail.attach_text("This is some text.", 'text/plain') msg = encoding.to_message(mail) assert_equal(msg.get_payload(0).get_payload(), "This is some text.") assert encoding.to_string(mail) mail.attach_text("<html><body><p>Hi there.</p></body></html>", "text/html") msg = encoding.to_message(mail) assert_equal(len(msg.get_payload()), 2) assert encoding.to_string(mail)
def test_multiple_headers(self): msg = encoding.MailBase() msg["To"] = "somedude@localhost" msg["From"] = "nobody@localhost" assert ("From" in msg) self.assertEqual(msg["From"], "nobody@localhost") msg["From"] = "somebody@localhost" self.assertEqual(msg["From"], "somebody@localhost") self.assertEqual(msg.keys(), ["To", "From"]) # appending headers msg.append_header("To", "*****@*****.**") self.assertEqual(msg["To"], "somedude@localhost") self.assertEqual(msg.keys(), ["To", "From", "To"])
def test_normalized_headers(self): base = encoding.MailBase() # __setitem__ will normalize headers when they're added base["subJect"] = "test" self.assertEqual(base["Subject"], "test") self.assertEqual(base.mime_part["Subject"], "test") self.assertEqual(base.keys(), ["Subject"]) self.assertEqual(base.mime_part.keys(), ["Subject"]) # if the header wasn't added via __setitem__, it should still be returned # to Salmon users in a normalized form base.mime_part["fRom"] = "*****@*****.**" self.assertEqual(base["From"], "*****@*****.**") self.assertEqual(base.mime_part["fRom"], "*****@*****.**") self.assertEqual(base.keys(), ["Subject", "From"]) self.assertEqual(base.mime_part.keys(), ["Subject", "fRom"])
def test_attach_file_bad_content_type(self): mail = encoding.MailBase() with self.assertRaises(encoding.EncodingError): mail.attach_file("salmon.png", "1234", "IMAGe/png", "attachment")
def test_attach_text_bad_content_type(self): mail = encoding.MailBase() with self.assertRaises(encoding.EncodingError): mail.attach_text("This is some text.", 'text/pLAIn')