1#!/usr/bin/python 2 3import unittest 4import mail, email 5 6class test_data: 7 mail_host = None 8 mail_port = None 9 mail_connect = False 10 mail_from_address = None 11 mail_to_address = None 12 mail_message = None 13 14 15# we define our needed mock SMTP 16class SMTP: 17 def __init__(self, host=None, port=25): 18 test_data.mail_host = host 19 test_data.mail_port = port 20 21 if test_data.mail_host: 22 self.connect(test_data.mail_host, test_data.mail_port) 23 24 25 def connect(self, host, port): 26 test_data.mail_connect = True 27 28 29 def quit(self): 30 test_data.mail_connect = False 31 32 33 def sendmail(self, from_address, to_address, message): 34 test_data.mail_from_address = from_address 35 test_data.mail_to_address = to_address 36 test_data.mail_message = message 37 38 39class mail_test(unittest.TestCase): 40 cached_SMTP = None 41 42 def setUp(self): 43 # now perform the slip 44 self.cached_SMTP = mail.smtplib.SMTP 45 mail.smtplib.SMTP = SMTP 46 47 48 def tearDown(self): 49 # now put things back 50 mail.smtplib.SMTP = self.cached_SMTP 51 52 53 def test_send_message(self): 54 message = email.Message.Message() 55 message["To"] = "you" 56 message["Cc"] = "them" 57 message["From"] = "me" 58 message["Subject"] = "hello" 59 message.set_payload("Hello everybody!") 60 61 mail.send("me", "you", "them", "hello", "Hello everybody!") 62 self.assertEquals("me", test_data.mail_from_address) 63 self.assertEquals(["you","them"], test_data.mail_to_address) 64 self.assertEquals(message.as_string(), test_data.mail_message) 65 66 67# this is so the test can be run in standalone mode 68if __name__ == '__main__': 69 unittest.main() 70