testDeferredStyle.py 5.22 KB
Newer Older
Nicolas Delaby's avatar
Nicolas Delaby committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Nicolas Delaby <nicolas@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest
import transaction
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Form.Selection import Selection
from Testing import ZopeTestCase
from Products.ERP5Type.tests.utils import DummyMailHost
from AccessControl import getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager
37
from Products.ERP5OOo.tests.utils import Validator
Nicolas Delaby's avatar
Nicolas Delaby committed
38 39
import email

40

Nicolas Delaby's avatar
Nicolas Delaby committed
41
class TestDeferredStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
42 43 44
  """Tests deferred styles for ERP5."""
  skin = content_type = None
  recipient_email_address = 'invalid@example.com'
45
  attachment_file_extension = ''
Nicolas Delaby's avatar
Nicolas Delaby committed
46 47 48 49
  username = 'bob'
  password = 'bobpwd'
  first_name = 'Bob'

Nicolas Delaby's avatar
Nicolas Delaby committed
50 51 52
  def getTitle(self):
    return 'Test Deferred Style'

Nicolas Delaby's avatar
Nicolas Delaby committed
53
  def getBusinessTemplateList(self):
54 55
    return ('erp5_base', 'erp5_ods_style',
            'erp5_odt_style', 'erp5_deferred_style',)
Nicolas Delaby's avatar
Nicolas Delaby committed
56 57 58 59 60 61 62 63 64

  def afterSetUp(self):
    self.login()
    if not self.skin:
      raise NotImplementedError('Subclasses must define skin')

    person_module = self.portal.person_module
    if person_module._getOb('pers', None) is None:
      person = person_module.newContent(id='pers', portal_type='Person',
65 66 67 68
                                        reference=self.username,
                                        first_name=self.first_name,
                                        password=self.password,
                                        default_email_text=self.recipient_email_address)
Nicolas Delaby's avatar
Nicolas Delaby committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      assignment = person.newContent(portal_type='Assignment')
      assignment.open()

    # replace MailHost
    if 'MailHost' in self.portal.objectIds():
      self.portal.manage_delObjects(['MailHost'])
    self.portal._setObject('MailHost', DummyMailHost('MailHost'))
    transaction.commit()
    self.tic()

  def loginAsUser(self, username):
    uf = self.portal.acl_users
    uf.zodb_roles.assignRoleToPrincipal('Manager', username)
    user = uf.getUserById(username).__of__(uf)
    newSecurityManager(None, user)

  def test_skin_selection(self):
86
    self.assertTrue('Deferred' in self.portal.portal_skins.getSkinSelections())
Nicolas Delaby's avatar
Nicolas Delaby committed
87

88
  def test_report_view(self):
Nicolas Delaby's avatar
Nicolas Delaby committed
89
    self.loginAsUser('bob')
90 91 92 93
    self.portal.changeSkin('Deferred')
    response = self.publish(
        '/%s/person_module/pers/Base_viewHistory?deferred_portal_skin=%s'
        % (self.portal.getId(), self.skin), '%s:%s' % (self.username, self.password))
Nicolas Delaby's avatar
Nicolas Delaby committed
94 95 96 97 98 99 100 101 102 103
    transaction.commit()
    self.tic()
    last_message = self.portal.MailHost._last_message
    self.assertNotEquals((), last_message)
    mfrom, mto, message_text = last_message
    self.assertEquals('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
    mail_message = email.message_from_string(message_text)
    for part in mail_message.walk():
      content_type = part.get_content_type()
      file_name = part.get_filename()
104 105
      # "History" is the title of Base_viewHistory form
      if file_name == 'History%s' % self.attachment_file_extension:
106
        self.assertEquals(content_type, self.content_type)
107
        self.assertEquals('attachment; filename="History%s"' %
108
                                self.attachment_file_extension,
109
                          part.get('Content-Disposition'))
110 111 112 113
        data = part.get_payload(decode=True)
        error_list = Validator().validate(data)
        if error_list:
          self.fail(''.join(error_list))
Nicolas Delaby's avatar
Nicolas Delaby committed
114
        break
115
    else:
Nicolas Delaby's avatar
Nicolas Delaby committed
116 117
      self.fail('Attachment not found in email')

118 119 120 121

class TestODSDeferredStyle(TestDeferredStyle):
  skin = 'ODS'
  content_type = 'application/vnd.oasis.opendocument.spreadsheet'
122
  attachment_file_extension = '.ods'
123 124 125 126 127


class TestODTDeferredStyle(TestDeferredStyle):
  skin = 'ODT'
  content_type = 'application/vnd.oasis.opendocument.text'
128
  attachment_file_extension = '.odt'
129 130


Nicolas Delaby's avatar
Nicolas Delaby committed
131 132
def test_suite():
  suite = unittest.TestSuite()
133 134
  suite.addTest(unittest.makeSuite(TestODSDeferredStyle))
  suite.addTest(unittest.makeSuite(TestODTDeferredStyle))
Nicolas Delaby's avatar
Nicolas Delaby committed
135 136
  return suite