[Commits] (bkirsch) Improved message parsing functionality as part of content model upgrade

commits at osafoundation.org commits at osafoundation.org
Mon Aug 16 16:50:55 PDT 2004


Commit by: bkirsch
Modified files:
chandler/parcels/osaf/mail/common.py 1.2 1.3
chandler/parcels/osaf/mail/imap.py 1.8 1.9
chandler/parcels/osaf/mail/message.py 1.9 1.10
chandler/parcels/osaf/mail/tests/TestMessage.py 1.3 1.4

Log message:
Improved message parsing functionality as part of content model upgrade

ViewCVS links:
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/common.py.diff?r1=text&tr1=1.2&r2=text&tr2=1.3
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/imap.py.diff?r1=text&tr1=1.8&r2=text&tr2=1.9
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/message.py.diff?r1=text&tr1=1.9&r2=text&tr2=1.10
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/tests/TestMessage.py.diff?r1=text&tr1=1.3&r2=text&tr2=1.4

Index: chandler/parcels/osaf/mail/imap.py
diff -u chandler/parcels/osaf/mail/imap.py:1.8 chandler/parcels/osaf/mail/imap.py:1.9
--- chandler/parcels/osaf/mail/imap.py:1.8	Fri Aug 13 14:08:58 2004
+++ chandler/parcels/osaf/mail/imap.py	Mon Aug 16 16:50:54 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.8 $"
-__date__      = "$Date: 2004/08/13 21:08:58 $"
+__revision__  = "$Revision: 1.9 $"
+__date__      = "$Date: 2004/08/16 23:50:54 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -18,6 +18,8 @@
 import logging as logging
 import repository.util.UUID as UUID
 
+#XXX: Need to make sure all the flags are in place to prevent a non-ssl session if 
+#     ssl required
 
 class ChandlerIMAP4Client(imap4.IMAP4Client):
 
@@ -157,7 +159,7 @@
             host    = self.account.host
             port    = self.account.port
             useSSL  = self.account.useSSL
-            portSSL = self.account.portSSL            
+            portSSL = self.account.portSSL
 
             if __debug__:
                 self.printAccount()

Index: chandler/parcels/osaf/mail/common.py
diff -u chandler/parcels/osaf/mail/common.py:1.2 chandler/parcels/osaf/mail/common.py:1.3
--- chandler/parcels/osaf/mail/common.py:1.2	Wed Aug 11 16:37:58 2004
+++ chandler/parcels/osaf/mail/common.py	Mon Aug 16 16:50:54 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.2 $"
-__date__      = "$Date: 2004/08/11 23:37:58 $"
+__revision__  = "$Revision: 1.3 $"
+__date__      = "$Date: 2004/08/16 23:50:54 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -10,6 +10,11 @@
 import email.Message as Message
 import email.Utils as Utils
 
+CHANDLER_USERAGENT = "Open Source Applications Foundation Chandler .4B Release"
+CHANDLER_HEADER_PREFIX = "X-Chandler-"
+ATTACHMENT_BODY_WARNING = "The body of this message consists of Multiple Mime Parts.\nFor .4B Chandler does not support MIME Parts"
+
+MIME_TEXT_PLAIN = "text/plain"
 
 def getChandlerTransportMessage():
     message = """Subject: ***FOR CHANDLER INTERNAL USE - DO NOT DELETE ***

Index: chandler/parcels/osaf/mail/message.py
diff -u chandler/parcels/osaf/mail/message.py:1.9 chandler/parcels/osaf/mail/message.py:1.10
--- chandler/parcels/osaf/mail/message.py:1.9	Fri Aug 13 07:41:01 2004
+++ chandler/parcels/osaf/mail/message.py	Mon Aug 16 16:50:54 2004
@@ -1,17 +1,20 @@
-__revision__  = "$Revision: 1.9 $"
-__date__      = "$Date: 2004/08/13 14:41:01 $"
+__revision__  = "$Revision: 1.10 $"
+__date__      = "$Date: 2004/08/16 23:50:54 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
 import osaf.contentmodel.mail.Mail as Mail
+import repository.persistence.XMLRepositoryView as XMLRepositoryView
 import mx.DateTime as DateTime
 import email as email
 import email.Message as Message
 import email.Utils as Utils
 import re as re
+import common as common
 
 __exp = re.compile("\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z2-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}")
 
+
 def isValidEmailAddress(emailAddress):
     """
     This method tests an email address for valid syntax as defined RFC 822
@@ -24,7 +27,7 @@
     @type addr: C{String}
     @return: C{Boolean}
     """
-    if type(emailAddress) != str:
+    if not isinstance(emailAddress, str):
         return False
 
     emailAddress = emailAddress.strip()
@@ -54,7 +57,8 @@
     @return: C{Boolean}
     """
 
-    if type(emailAddressOne) != str or type(emailAddressTwo) != str:
+    #XXX: There are bugs here because of the weakness of the parseaddr API
+    if not isinstance(emailAddressOne, str) or not isinstance(emailAddressTwo, str):
         return False
 
     emailAddressOne = Utils.parseaddr(emailAddressOne)[1]
@@ -62,6 +66,17 @@
 
     return emailAddressOne.lower() == emailAddressTwo.lower()
 
+def format_addr(emailAddress):
+    if not isinstance(emailAddress, Mail.EmailAddress):
+        return None
+
+    if hasValue(emailAddress.fullName):
+        return emailAddress.fullName + " <" + emailAddress.emailAddress + ">"
+
+    return emailAddress.emailAddress
+
+def createMessageID():
+    return Utils.make_msgid()
 
 def hasValue(value):
     """
@@ -73,30 +88,36 @@
     @type value: C{String}
     @return: C{Boolean}
     """
-    if value is not None:
+    if isinstance(value, str):
         test = value.strip()
         if len(test) > 0:
             return True
 
     return False
 
-# XXX: Relook at this logic
-def format_addr(addr):
-    """
-    This method formats an email address
+def isPlainTextContentType(contentType):
+    if isinstance(contentType, str):
+        contentType = contentType.lower().strip()
 
-    @param addr: The email address list to format
-    @type addr: C{list}
-    @return: C{string}
-    """
-    str = addr[0]
-    if str != None and str != '':
-        str = str + ' '
+        if contentType == common.MIME_TEXT_PLAIN:
+            return True
 
-        str = str + '<' + addr[1] + '>'
-        return str
+    return False
 
-    return addr[1]
+def createChandlerHeader(postfix):
+    if not hasValue(postfix):
+        return None
+
+    return common.CHANDLER_HEASER_PREFIX + postfix
+
+def isChandlerHeader(header):
+    if not hasValue(header):
+        return False
+
+    if header.startswith(common.CHANDLER_HEADER_PREFIX):
+        return True
+
+    return False
 
 def messageTextToKind(messageText):
     """
@@ -111,9 +132,9 @@
     if not isinstance(messageText, str):
         raise TypeError("messageText must be a String")
 
-    return messageObjectToKind(email.message_from_string(messageText))
+    return messageObjectToKind(email.message_from_string(messageText), messageText)
 
-def messageObjectToKind(messageObject):
+def messageObjectToKind(messageObject, messageText = None):
     """
     This method converts a email message string to
     a Chandler C{Mail.MailMessage} object
@@ -128,43 +149,77 @@
 
     m = Mail.MailMessage()
 
+    if messageText is None or type(messageText) != str:
+        messageText = messageObject.as_string()
+
+    # Save the original message text in a text blob
+    m.rfc2822Message = strToText(m, "rfc2822Message", messageText)
+
+    # XXX: Will ned to manually extract the received header and references
+
     if m is None:
         raise TypeError("Repository returned a MailMessage that was None")
 
-    if messageObject['Date'] is not None:
-        m.dateSent = DateTime.mktime(Utils.parsedate(messageObject['Date']))
-        m.dateSentString = messageObject['Date']
+    date = messageObject['Date']
 
+    if date is not None:
+        m.dateSent = DateTime.mktime(Utils.parsedate(date))
+        m.dateSentString = date
+        del messageObject['Date']
+
+    #XXX: Will this fail at the Repository level
     else:
         m.dateSent = None
 
     m.dateReceived = DateTime.now()
 
-    if messageObject['Subject'] is None:
-        m.subject = ""
-    else:
-        m.subject = messageObject['Subject']
+    __assignToKind(m, messageObject, 'Subject', 'String', 'subject')
+    __assignToKind(m, messageObject, 'Content-Type', 'String', 'contentType')
+    __assignToKind(m, messageObject, 'Content-Length', 'String', 'contentLength')
+    __assignToKind(m, messageObject, 'Content-Transfer-Encoding', 'String', 'contentTransferEncoding')
+    __assignToKind(m, messageObject, 'Mime-Version', 'String', 'mimeVersion')
+    __assignToKind(m, messageObject, 'Message-ID', 'String', 'messageId')
+    __assignToKind(m, messageObject, 'Return-Path', 'String', 'returnPath')
+    __assignToKind(m, messageObject, 'In-Reply-To', 'String', 'inReplyTo')
+    __assignToKind(m, messageObject, 'From', 'EmailAddress', 'fromAddress')
+    __assignToKind(m, messageObject, 'Reply-To', 'EmailAddress', 'replyToAddress')
+    __assignToKind(m.toAddress, messageObject, 'To', 'EmailAddressList')
+    __assignToKind(m.ccAddress, messageObject, 'Cc', 'EmailAddressList')
+    __assignToKind(m.bccAddress, messageObject, 'Bcc', 'EmailAddressList')
+    __assignToKind(m.references, messageObject, 'References', 'StringList')
+    __assignToKind(m.received, messageObject, 'Received', 'StringList')
+
+    m.chandlerHeaders = {}
+    m.additionalHeaders = {}
+
+    for (key, val) in messageObject.items():
+
+        if isChandlerHeader(key):
+            m.chandlerHeaders[key] =  val
+
+        else:
+            m.additionalHeaders[key] = val
+
+        try:
+            del messageObject[key]
+
+        except KeyError:
+            print "KEY ERROR Here"
+
+    if messageObject.is_multipart():
+        mimeParts = messageObject.get_payload()
+        found = False
+
+        for mimePart in mimeParts:
+            if isPlainTextContentType(mimePart.get_content_type()):
+                m.body = strToText(m, "body",  mimePart.get_payload())
+                found = True
 
-    m.fromAddress = Mail.EmailAddress()
-    m.fromAddress.emailAddress = format_addr(Utils.parseaddr(messageObject['From']))
+        if not found:
+            m.body = strToText(m, "body", common.ATTACHMENT_BODY_WARNING)
 
-    m.toAddress = []
-    for addr in Utils.getaddresses(messageObject.get_all('To', [])):
-        ea = Mail.EmailAddress()
-        ea.emailAddress = format_addr(addr)
-        m.toAddress.append(ea)
-
-    m.ccAddress = []
-    for addr in Utils.getaddresses(messageObject.get_all('Cc', [])):
-        ea = Mail.EmailAddress()
-        ea.emailAddress = format_addr(addr)
-        m.ccAddress.append(ea)
-
-    m.bccAddress = []
-    for addr in Utils.getaddresses(messageObject.get_all('Bcc', [])):
-        ea = Mail.EmailAddress()
-        ea.emailAddress = format_addr(addr)
-        m.bccAddress.append(ea)
+    else:
+        m.body = strToText(m, "body", messageObject.get_payload())
 
     return m
 
@@ -181,44 +236,67 @@
     if not isinstance(mailMessage, Mail.MailMessage):
         raise TypeError("mailMessage must be an instance of Kind Mail.MailMessage")
 
-
+    #XXX: To do figure out in relpy to / recieved  / references logic
     messageObject = Message.Message()
 
-    messageObject['Date'] = mailMessage.dateSentString
-    #Utils.formatdate(mailMessage.dateSent.ticks(), True)
+    if hasValue(mailMessage.messageId):
+       __populateParam(messageObject, 'Message-ID', mailMessage.messageId)
+    else:
+        messageObject['Message-ID'] = createMessageID()
+
+    __populateParam(messageObject, 'Date', mailMessage.dateSentString)
+    __populateParam(messageObject, 'Subject', mailMessage.subject)
+    __populateParam(messageObject, 'Content-Type', mailMessage.contentType)
+    __populateParam(messageObject, 'Content-Length', mailMessage.contentLength)
+    __populateParam(messageObject, 'Content-Transfer-Encoding', mailMessage.contentTransferEncoding)
+    __populateParam(messageObject, 'MIME-Version', mailMessage.mimeVersion)
+    __populateParam(messageObject, 'Return-Path', mailMessage.returnPath)
+    __populateParam(messageObject, 'In-Reply-To', mailMessage.inReplyTo)
+
+    if len(mailMessage.references) > 0:
+        messageObject['References'] = " ".join(mailMessage.references)
+
+    #XXX: This will be a special case because we need multiple received headers
+    if len(mailMessage.received) > 0:
+        messageObject['Received'] = " ".join(mailMessage.received)
+
+    for (key, val) in mailMessage.chandlerHeaders:
+        messageObject[key] = val
+
+    for (key, val) in mailMessage.additionalHeaders:
+        messageObject[key] = val
+
+    if mailMessage.body is not None:
+        messageObject.set_payload(textToStr(mailMessage.body))
+
 
-    messageObject['Date Received'] = Utils.formatdate(mailMessage.dateReceived.ticks(), True)
-    messageObject['Subject'] = mailMessage.subject
+    if mailMessage.fromAddress is not None:
+        messageObject['From'] = format_addr(mailMessage.fromAddress)
 
-    messageObject['From'] = mailMessage.fromAddress.emailAddress
+    if mailMessage.replyToAddress is not None:
+        messageObject["Reply-To"] = format_addr(mailMessage.replyToAddress)
 
     to = []
 
     for address in mailMessage.toAddress:
-        to.append(address.emailAddress)
+        to.append(format_addr(address))
 
     messageObject['To'] = ", ".join(to)
 
-    del to
-
     cc = []
 
     for address in mailMessage.ccAddress:
-        cc.append(address.emailAddress)
+        cc.append(format_addr(address))
 
     messageObject['Cc'] = ", ".join(cc)
 
-    del cc
-
     bcc = []
 
     for address in mailMessage.bccAddress:
-        bcc.append(address.emailAddress)
+        bcc.append(format_addr(address))
 
     messageObject['Bcc'] = ", ".join(bcc)
 
-    del bcc
-
     return messageObject
 
 
@@ -238,3 +316,70 @@
     messageObject = kindToMessageObject(mailMessage)
 
     return messageObject.as_string()
+
+
+def strToText(contentItem, attribute, string):
+    if not isinstance(string, str):
+        return None
+
+    return contentItem.getAttributeAspect(attribute, 'type').makeValue(string, indexed=False)
+
+
+
+def textToStr(text):
+    if not isinstance(text, XMLRepositoryView.XMLText):
+        return False
+
+    reader = text.getReader()
+    string = reader.read()
+    reader.close()
+
+    return string
+
+
+def __populateParam(messageObject, param, var):
+    if hasValue(var):
+        messageObject[param] = var
+
+def __assignToKind(kindVar, messageObject, key, type, attr = None):
+
+    if type == "String":
+        if messageObject[key] is not None:
+            setattr(kindVar, attr, messageObject[key])
+
+    # XXX: This logic will need to be expanded
+    elif type == "StringList":
+        if messageObject[key] is not None:
+            kindVar.append(messageObject[key])
+
+    elif type == "EmailAddress":
+        if messageObject[key] is not None:
+            ea = Mail.EmailAddress()
+            addr = Utils.parseaddr(messageObject[key])
+
+            ea.emailAddress = addr[1]
+
+            if hasValue(addr[0]):
+                ea.fullName = addr[0]
+
+            setattr(kindVar, attr, ea)
+
+    elif type == "EmailAddressList":
+        if messageObject[key] is not None:
+            for addr in Utils.getaddresses(messageObject.get_all(key, [])):
+                ea = Mail.EmailAddress()
+
+                ea.emailAddress = addr[1]
+
+                if hasValue(addr[0]):
+                    ea.fullName = addr[0]
+
+                kindVar.append(ea)
+    else:
+        print "Header slipped through"
+
+    try:
+       del messageObject[key]
+
+    except KeyError:
+        print "Key Error Thrown"

Index: chandler/parcels/osaf/mail/tests/TestMessage.py
diff -u chandler/parcels/osaf/mail/tests/TestMessage.py:1.3 chandler/parcels/osaf/mail/tests/TestMessage.py:1.4
--- chandler/parcels/osaf/mail/tests/TestMessage.py:1.3	Fri Aug 13 07:41:02 2004
+++ chandler/parcels/osaf/mail/tests/TestMessage.py	Mon Aug 16 16:50:54 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.3 $"
-__date__      = "$Date: 2004/08/13 14:41:02 $"
+__revision__  = "$Revision: 1.4 $"
+__date__      = "$Date: 2004/08/16 23:50:54 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -19,19 +19,25 @@
 
 class MessageTest(MailTestCase.MailTestCase):
 
-    __mail = """To:test at testuser.com, John Johnson <john at home.com>
+    __mail = """To: test at testuser.com, John Johnson <john at home.com>
 Message-Id: <E1Bu9Jy-0007u1-9d at test.com>
 From: bill at home.net
 Cc: jake at now.com
 Bcc: don at we.com
 Date: Mon, 9 Aug 2004 13:55:15 -0700
+Content-Length: 75
+Content-Transfer-Encoding: us-ascii
+Mime-Version: 1.0
+Received: from [192.168.101.37] (w002.z065106067.sjc-ca.dsl.cnc.net [65.106.67.2]) by kahuna.osafoundation.org (8.12.8/8.12.8) with ESMTP id i7GKWWpo017020; Mon, 16 Aug 2004 13:32:32 -0700
+In-Reply-To: <2EE66978-EFB1-11D8-8048-000A95CA1ECC at osafoundation.org>
+References: <9CF0AF12-ED6F-11D8-B611-000A95B076C2 at osafoundation.org> <7542F892-EF9F-11D8-8048-000A95CA1ECC at osafoundation.org> <07A5D499-EFA1-11D8-9F44-000A95D9289E at osafoundation.org> <2EE66978-EFB1-11D8-8048-000A95CA1ECC at osafoundation.org>
 Subject: test mail
+Content-Type: text/plain
 
 This is the body"""
 
     __addresses = [None, "    ", "john", "sd$%dsd at dsd-fffd!.com", "bill.jones at tc.unernet.com"]
 
-
     def __getMessageObject(self):
         if self.__messageObject is None:
            self.__messageObject = email.message_from_string(self.__mail)
@@ -41,52 +47,67 @@
     def __getMessageText(self):
         return self.__mail
 
-    ###Add in Body read
     def __getMailMessage(self):
         if self.__mailMessage is not None:
             return self.__mailMessage
 
         m = Mail.MailMessage()
-        m.replyAddress = Mail.EmailAddress()
-        m.replyAddress.emailAddress = message.format_addr(Utils.parseaddr("bill at home.net"))
+        m.fromAddress = Mail.EmailAddress()
+        m.fromAddress.emailAddress = "bill at home.net"
 
         toOne = Mail.EmailAddress()
-        toOne.emailAddress = message.format_addr(Utils.parseaddr("test at testuser.com"))
+        toOne.emailAddress = "test at testuser.com"
 
         toTwo = Mail.EmailAddress()
-        toTwo.emailAddress = message.format_addr(Utils.parseaddr("John Johnson <john at home.com>"))
+        toTwo.emailAddress = "john at home.com"
+        toTwo.fullName = "John Johnson"
 
         m.toAddress = []
         m.toAddress.append(toOne)
         m.toAddress.append(toTwo)
 
         ccOne = Mail.EmailAddress()
-        ccOne.emailAddress = message.format_addr(Utils.parseaddr("jake at now.com"))
+        ccOne.emailAddress = "jake at now.com"
 
         m.ccAddress = []
         m.ccAddress.append(ccOne)
 
         bccOne = Mail.EmailAddress()
-        bccOne.emailAddress = message.format_addr(Utils.parseaddr("don at we.com"))
+        bccOne.emailAddress = "don at we.com"
 
         m.bccAddress = []
         m.bccAddress.append(bccOne)
 
         m.subject = "test mail"
+        dateString = "Mon, 9 Aug 2004 13:55:15 -0700"
+        m.contentLength = "75"
+        m.contentType = "text/plain"
+        m.contentTransferEncoding = "us-ascii"
+        m.mimeVersion = "1.0"
+        m.inReplyTo = "<2EE66978-EFB1-11D8-8048-000A95CA1ECC at osafoundation.org>"
+
+        #XXX: This will need to be updated to handle multiple received headers
+        m.received.append("from [192.168.101.37] (w002.z065106067.sjc-ca.dsl.cnc.net [65.106.67.2]) by kahuna.osafoundation.org (8.12.8/8.12.8) with ESMTP id i7GKWWpo017020; Mon, 16 Aug 2004 13:32:32 -0700")
 
-        date = Utils.parsedate("Mon, 9 Aug 2004 13:55:15 -0700")
+        #XXX: This will need to be updated laterQ
+        m.references.append("<9CF0AF12-ED6F-11D8-B611-000A95B076C2 at osafoundation.org> <7542F892-EF9F-11D8-8048-000A95CA1ECC at osafoundation.org> <07A5D499-EFA1-11D8-9F44-000A95D9289E at osafoundation.org> <2EE66978-EFB1-11D8-8048-000A95CA1ECC at osafoundation.org>")
 
-        m.dateSent = MXDateTime.mktime(date)
+        m.dateSent = MXDateTime.mktime(Utils.parsedate(dateString))
+        m.dateSentString = dateString
         m.dateReceived = MXDateTime.now()
+        m.body = message.strToText(m, "body", "This is the body")
+        m.rfc2822Message = message.strToText(m, "rfc2822Message", self.__mail)
 
         self.__mailMessage = m
 
         return self.__mailMessage
 
     def __compareEmailAddressLists(self, adOne, adTwo):
+
         self.assertNotEqual(adOne, XMLRefDict.XMLRefDict)
         self.assertNotEqual(adTwo, XMLRefDict.XMLRefDict)
-        self.assertEqual(len(adOne), len(adTwo))
+
+        self.assertEquals(len(adOne), len(adTwo))
 
         tempDict = {}
         tempList = []
@@ -102,7 +123,7 @@
                 tempList.append(address.emailAddress)
 
         if len(tempList) > 0:
-            str = "Email Addresses do not match: ", ", ".join(tempList)
+            print "Email Addresses do not match:", ", ".join(tempList)
             self.fail(str)
 
     def __compareDateTimes(self, dOne, dTwo):
@@ -110,7 +131,7 @@
         self.assertNotEqual(dOne, None)
         self.assertNotEqual(dTwo, None)
 
-        self.assertEqual(dOne.strftime(), dTwo.strftime())
+        self.assertEquals(dOne.strftime(), dTwo.strftime())
 
     def __compareMailMessages(self, mOne, mTwo):
         self.assertNotEqual(mOne, None)
@@ -119,11 +140,23 @@
         self.__compareEmailAddressLists(mOne.toAddress, mTwo.toAddress)
         self.__compareEmailAddressLists(mOne.ccAddress, mTwo.ccAddress)
         self.__compareEmailAddressLists(mOne.bccAddress, mTwo.bccAddress)
+
         self.__compareDateTimes(mOne.dateSent, mTwo.dateSent)
 
+        self.assertEquals(mOne.fromAddress.emailAddress, mTwo.fromAddress.emailAddress)
+
         self.assertEquals(mOne.subject, mTwo.subject)
+        self.assertEquals(mOne.contentLength, mTwo.contentLength)
+        self.assertEquals(mOne.contentType, mTwo.contentType)
+        self.assertEquals(mOne.contentTransferEncoding, mTwo.contentTransferEncoding)
+        self.assertEquals(mOne.mimeVersion, mTwo.mimeVersion)
+        self.assertEquals(mOne.inReplyTo, mTwo.inReplyTo)
+        ##Need to fix list compare logic
+        #self.assertListEquals(mOne.received, mTwo.received)
+        #self.assertListEquals(mOne.references, mTwo.references)
+        self.assertEquals(message.textToStr(mOne.body), message.textToStr(mTwo.body))
+        self.assertEquals(message.textToStr(mOne.rfc2822Message), message.textToStr(mTwo.rfc2822Message))
 
-        ###Add in body test
 
     def __compareMessageObjects(self, mOne, mTwo):
         self.assertNotEqual(mOne, None)
@@ -133,6 +166,15 @@
         self.assertEquals(mOne['To'], mTwo['To'])
         self.assertEquals(mOne['Cc'], mTwo['Cc'])
         self.assertEquals(mOne['Bcc'], mTwo['Bcc'])
+        self.assertEquals(mOne['Content-Length'], mTwo['Content-Length'])
+        self.assertEquals(mOne['Content-Type'], mTwo['Content-Type'])
+        self.assertEquals(mOne['Content-Transfer-Encoding'], mTwo['Content-Transfer-Encoding'])
+        self.assertEquals(mOne['Mime-Version'], mTwo['Mime-Version'])
+        ## A tab is getting inserted causing to fali need to look in to
+        #self.assertEquals(mOne['Received'], mTwo['Received'])
+        #self.assertEquals(mOne['References'], mTwo['References'])
+        self.assertEquals(mOne['In-Reply-To'], mTwo['In-Reply-To'])
+        self.assertEquals(mOne['Subject'], mTwo['Subject'])
 
 
         dOne = Utils.parsedate(mOne['Date'])
@@ -142,23 +184,34 @@
             if dOne[i] != dTwo[i]:
                self.fail("Dates do not match %s != %s" % (mOne['Date'], mTwo['Date']))
 
-        self.assertEquals(mOne['Subject'], mTwo['Subject'])
+        self.assertEquals(mOne.get_payload(), mTwo.get_payload())
 
+    #XXX: This needs work
+    def assertListEquals(self, list1, list2):
+         self.assertEquals(len(list1), len(list2))
 
-    def testEmailValidation(self):
-         self.assertEquals(message.isValidEmailAddress(self.__addresses[0]), False)
-         self.assertEquals(message.isValidEmailAddress(self.__addresses[1]), False)
-         self.assertEquals(message.isValidEmailAddress(self.__addresses[2]), False)
-         self.assertEquals(message.isValidEmailAddress(self.__addresses[3]), False)
-         self.assertEquals(message.isValidEmailAddress(self.__addresses[4]), True)
+         size = len(list1)
 
-    def testMessageTextToKind(self):
-        """Conditions:
-           1. Only strings can be passed to messageTextToKind
-           2. Should return a Mail.MailMessage object containing the
-              values passed
-        """
+         for i in range(size):
+             self.assertEquals(list1[i], list2[i])
+
+    #XXX:needs woek
+    def assertDictEquals(dict1, dict2):
+         self.assertEquals(dict1, dict)
+         self.assertEquals(dict2, dict)
 
+         self.assertEquals(len(dict1), len(dict2))
+
+         l1 = dict1.keys()
+         l2 = dict2.keys()
+
+         size = len(l1)
+
+         for i in range(size):
+             self.assertEquals(l1[i], l2[i])
+             self.assertEquals(dict1[l1[i]], dict2[l2[i]])
+
+    def testMessageTextToKind(self):
         self.assertRaises(TypeError, message.messageTextToKind, None)
 
         mailKind = message.messageTextToKind(self.__getMessageText())
@@ -167,28 +220,18 @@
 
         self.__compareMailMessages(mailKind, self.__getMailMessage())
 
-
     def testMessageObjectToKind(self):
-        """Conditions:
-           1. Only email.Message objects can be passed to messageObjectToKind
-           2. Should return a Mail.MailMessage object containing the    values passed
-        """
 
         self.assertRaises(TypeError, message.messageObjectToKind, "Error")
 
-        mailKind = message.messageObjectToKind(self.__getMessageObject())
+        mailKind = message.messageObjectToKind(self.__getMessageObject(), self.__mail)
 
         self.assertNotEqual(mailKind, None)
 
         self.__compareMailMessages(mailKind, self.__getMailMessage())
 
-
     def testKindToMessageText(self):
-        """Conditions:
-           1. Only Mail.MailMessage objects  can be passed to kindToMessageText
-           2. Should return a string object containing the
-              values passed
-        """
+
         self.assertRaises(TypeError, message.kindToMessageText, "Error")
 
         mailText = message.kindToMessageText(self.__getMailMessage())
@@ -196,13 +239,7 @@
 
         self.__compareMessageObjects(mailObject, self.__getMessageObject())
 
-
     def testKindToMessageObject(self):
-        """Conditions:
-           1. Only Mail.MailMessage objects  can be passed to kindToMessageObject
-           2. Should return a email.Message object containing the
-              values passed
-        """
 
         self.assertRaises(TypeError, message.kindToMessageObject, "Error")
 
@@ -210,6 +247,13 @@
 
         self.__compareMessageObjects(messageObject, self.__getMessageObject())
 
+    def testEmailValidation(self):
+         self.assertEquals(message.isValidEmailAddress(self.__addresses[0]), False)
+         self.assertEquals(message.isValidEmailAddress(self.__addresses[1]), False)
+         self.assertEquals(message.isValidEmailAddress(self.__addresses[2]), False)
+         self.assertEquals(message.isValidEmailAddress(self.__addresses[3]), False)
+         self.assertEquals(message.isValidEmailAddress(self.__addresses[4]), True)
+
     def setUp(self):
         super(MessageTest, self).setUp()
         self.loadParcel("http://osafoundation.org/parcels/osaf/contentmodel/mail")
@@ -217,5 +261,4 @@
         self.__mailMessage = None
 
 if __name__ == "__main__":
-   # unittest.main()
-   pass
+   unittest.main()



More information about the Commits mailing list