From 6fe739fec22ce7a9bf03a61f6c51006ca77a5bbb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Calonne?= <aurel@nexedi.com>
Date: Tue, 8 Jan 2008 13:31:50 +0000
Subject: [PATCH] add tests for attachement in mail without using dms module

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18628 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/tests/testCRM.py                 | 116 ++++++++++++++++++
 .../crm_emails/sample_attachment.txt          |   1 +
 2 files changed, 117 insertions(+)
 create mode 100644 product/ERP5/tests/test_data/crm_emails/sample_attachment.txt

diff --git a/product/ERP5/tests/testCRM.py b/product/ERP5/tests/testCRM.py
index fa2fff459a..15b8ac5965 100644
--- a/product/ERP5/tests/testCRM.py
+++ b/product/ERP5/tests/testCRM.py
@@ -555,6 +555,122 @@ class TestCRMMailSend(ERP5TypeTestCase):
     self.assertEqual(answer_event.getTextContent(), '> This is an advertisement mail.')
 
 
+  def test_MailAttachmentFileWithoutDMS(self):
+    """
+    Make sure that file document is correctly attached in email
+    """
+    # Add a document on a person which will be attached.
+
+    def add_document(filename, id, container, portal_type):
+      f = openTestFile(filename)
+      document = container.newContent(id=id, portal_type=portal_type)
+      document.edit(file=f, reference=filename)
+      return document
+
+    # txt
+    document_txt = add_document('sample_attachment.txt', '2',
+                                self.portal.person_module['me'], 'File')
+
+    get_transaction().commit()
+    self.tic()
+    get_transaction().commit()
+
+    # Add a ticket
+    ticket = self.portal.campaign_module.newContent(id='1',
+                                                    portal_type='Campaign',
+                                                    title='Advertisement')
+    # Create a event
+    ticket.Ticket_newEvent(portal_type='Mail Message',
+                           title='Our new product',
+                           description='Buy this now!',
+                           direction='outgoing')
+
+    # Set sender and attach a document to the event.
+    event = self.portal.event_module.objectValues()[0]
+    event.edit(source='person_module/me',
+               destination='person_module/sender',
+               aggregate=document_txt.getRelativeUrl(),
+               text_content='This is an advertisement mail.')
+
+    mail_text = event.send(download=True)
+
+    # Check mail text.
+    message = email.message_from_string(mail_text)
+    part = None
+    for i in message.get_payload():
+      if i.get_content_type()=='text/plain':
+        part = i
+    self.assertEqual(part.get_payload(decode=True), event.getTextContent())
+
+    # Check attachment
+    # txt
+    self.assert_('sample_attachment.txt' in 
+                 [i.get_filename() for i in message.get_payload()])
+    part = None
+    for i in message.get_payload():
+      if i.get_filename()=='sample_attachment.txt':
+        part = i
+    self.assert_(len(part.get_payload(decode=True))>0)
+
+
+
+  def test_MailAttachmentImageWithoutDMS(self):
+    """
+    Make sure that image document is correctly attached in email without dms
+    """
+    # Add a document on a person which will be attached.
+
+    def add_document(filename, id, container, portal_type):
+      f = openTestFile(filename)
+      document = container.newContent(id=id, portal_type=portal_type)
+      document.edit(file=f, reference=filename)
+      return document
+
+    # gif
+    document_gif = add_document('sample_attachment.gif', '1',
+                                self.portal.person_module['me'], 'Image')
+
+    get_transaction().commit()
+    self.tic()
+    get_transaction().commit()
+
+    # Add a ticket
+    ticket = self.portal.campaign_module.newContent(id='1',
+                                                    portal_type='Campaign',
+                                                    title='Advertisement')
+    # Create a event
+    ticket.Ticket_newEvent(portal_type='Mail Message',
+                           title='Our new product',
+                           description='Buy this now!',
+                           direction='outgoing')
+
+    # Set sender and attach a document to the event.
+    event = self.portal.event_module.objectValues()[0]
+    event.edit(source='person_module/me',
+               destination='person_module/sender',
+               aggregate=document_gif.getRelativeUrl(),
+               text_content='This is an advertisement mail.')
+
+    mail_text = event.send(download=True)
+
+    # Check mail text.
+    message = email.message_from_string(mail_text)
+    part = None
+    for i in message.get_payload():
+      if i.get_content_type()=='text/plain':
+        part = i
+    self.assertEqual(part.get_payload(decode=True), event.getTextContent())
+
+    # Check attachment
+    # gif
+    self.assert_('sample_attachment.gif' in 
+                 [i.get_filename() for i in message.get_payload()])
+    part = None
+    for i in message.get_payload():
+      if i.get_filename()=='sample_attachment.gif':
+        part = i
+    self.assertEqual(part.get_payload(decode=True), str(document_gif.getData()))
+
 def test_suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestCRMMailIngestion))
diff --git a/product/ERP5/tests/test_data/crm_emails/sample_attachment.txt b/product/ERP5/tests/test_data/crm_emails/sample_attachment.txt
new file mode 100644
index 0000000000..e09445457a
--- /dev/null
+++ b/product/ERP5/tests/test_data/crm_emails/sample_attachment.txt
@@ -0,0 +1 @@
+sample text file
-- 
2.30.9