def test_compose_email_based_on(self): m = IMSMessageParser(_class=IMSMessageComposer).message_from_file(email_html_attachment) m.set_subject("hello 123") d = IMSMessageParser().message_from_string(str(m)) self.assertEqual("hello 123", d.subject) self.assertEqual([False, True, True], d.is_equal(IMSMessageParser().message_from_file(email_html_attachment)))
def test_compose_a_html_email_with_attachment(self): c = IMSMessageComposer() c.set_subject("plain and html body") c.add_plain_body("hello world") c.add_html_body("<h1>hello world<h1>") attach = os.path.join(tempfile.mkdtemp(), "hello.txt") with open(attach, 'w') as f: f.write("hello") c.append_attachment(attach) os.remove(attach) d = IMSMessageParser().message_from_string(str(c)) self.assertTrue(d.is_equal(IMSMessageParser().message_from_file(email_html_attachment)))
def test_parse_email_with_html_body_and_attachment(self): m = IMSMessageParser().message_from_file(email_html_attachment) self.assertEqual('plain and html body', m.subject) self.assertIn('hello world', m.body_plain) self.assertIn('<h1>hello world<h1>', m.body_html) self.assertDictEqual({'hello.txt': "hello"}, m.attachments) self.assertTrue(m.is_html_body_contain("<h1>hello world<h1>")) self.assertTrue(m.is_contain_attachment("hello.txt")) self.assertFalse(m.is_contain_attachment("hello01.txt")) self.assertTrue(m.is_attachment_contain("hello.txt", "hello"))
def test_parse_plain_mail(self): m = IMSMessageParser().message_from_file(email_plain) self.assertEqual('<*****@*****.**>', m.messageId) self.assertEqual('*****@*****.**', m.get_header_value('To')) self.assertEqual('Sender <*****@*****.**>', m.get_header_value('From')) self.assertEqual('test email', m.subject) self.assertIn('test email', m.body_plain) self.assertIsNone(m.body_html) self.assertTrue(m.is_header_value_contain("From", "*****@*****.**")) self.assertTrue(m.is_plain_body_contain("test email"))
def test_message_from_string(self): with open(email_plain, 'r') as f: m = IMSMessageParser().message_from_string(f.read()) self.assertIsInstance(m, IMSMessage)
def test_message_from_file(self): m = IMSMessageParser().message_from_file(email_plain) self.assertIsInstance(m, IMSMessage)
def test_compose_a_plain_email(self): c = IMSMessageComposer() c.set_subject("test email") c.add_plain_body("test email") d = IMSMessageParser().message_from_string(str(c)) self.assertTrue(d.is_equal(IMSMessageParser().message_from_file(email_plain)))