better attachment testing twisty
authorMark Hammond <mhammond@skippinet.com.au>
Mon, 06 Apr 2009 11:12:07 +1000
branchtwisty
changeset 158 37a8ed4fa05c0175fa774cf2bf60de098645a338
parent 157 45600ab47a25fd868d8943503ff3bc93279d6d37
child 159 d181fa823fbbdcb27eb72395bd4e29ed98eb7e95
push id1
push userroot
push dateWed, 08 Apr 2009 01:46:05 +0000
better attachment testing
server/python/junius/proto/test/__init__.py
--- a/server/python/junius/proto/test/__init__.py
+++ b/server/python/junius/proto/test/__init__.py
@@ -1,11 +1,12 @@
 # This is an implementation of a 'test' protocol.
 import logging
 from twisted.internet import defer, error
+from email.message import Message
 
 logger = logging.getLogger(__name__)
 
 from ...proc import base
 
 class TestMessageProvider(object):
     def __init__(self, account, conductor):
         self.account = account
@@ -31,45 +32,68 @@ class TestMessageProvider(object):
 
     def check_test_message(self, i):
         logger.debug("seeing if message with ID %d exists", i)
         return self.doc_model.open_document("msg", str(i), "proto/test"
                         ).addCallback(self.process_test_message, i)
 
     def process_test_message(self, existing_doc, doc_num):
         if existing_doc is None:
+            # make an attachment for testing purposes.
+            attachments = {"raw-attach" : {"content_type" : 'application/octet-stream',
+                                        "data" : 'test\0blob'
+                                        }
+            }
             doc = dict(
               storage_key=doc_num,
+              _attachments=attachments,
               )
             self.bulk_docs.append((str(doc_num), doc, 'proto/test'))
         else:
             logger.info("Skipping test message with ID %d - already exists",
                         doc_num)
             # we are done.
 
     def saved_bulk_messages(self, result, n):
         logger.debug("Finished saving %d test messages in bulk", n)
         # done
 
 # A 'converter' - takes a proto/test as input and creates a
 # 'raw/message/rfc822' as output.
 class TestConverter(base.ConverterBase):
     def convert(self, doc):
+        # for the sake of testing, we fetch the raw attachment just to compare
+        # its value.
+        return self.doc_model.open_attachment(doc['_id'], "raw-attach",
+                  ).addCallback(self._cb_got_attachment, doc)
+
+    def _cb_got_attachment(self, attach_content, doc):
+        if attach_content != 'test\0blob':
+            raise RuntimeError(attach_content)
+
         me = doc['storage_key']
+        # Use the email package to construct a synthetic rfc822 stream.
+        msg = Message()
         headers = {'from': 'From: from%(storage_key)d@test.com',
                    'subject' : 'This is test document %(storage_key)d',
         }
         for h in headers:
-            headers[h] = headers[h] % doc
+            msg[h] = headers[h] % doc
 
         body = u"Hello, this is test message %(storage_key)d (with extended \xa9haracter!)" % doc
-        # make an attachment for testing purposes.
-        attachments = {"attach1" : {"content_type" : 'application/octet-stream',
-                                    "data" : 'test\0blob'
-                                    }
-                      }
-        new_doc = dict(headers=headers, body=body, _attachments=attachments)
+        msg.set_payload(body.encode('utf-8'), 'utf-8')
+        #attachments = {"rfc822" : {"content_type" : 'message',
+        #                           "data" : msg.get_payload(),
+        #                         }
+        #              }
+        new_doc = {}
+        new_doc['body'] = body
+        #new_doc['_attachments'] = attachments
+        new_doc['multipart'] = False
+        new_doc['headers'] = {}
+        for hn, hv in msg.items():
+            new_doc['headers'][hn.lower()] = hv
         return new_doc
 
 
 class TestAccount(base.AccountBase):
   def startSync(self, conductor):
     return TestMessageProvider(self, conductor).attach()