testERP5PayzenSecurePayment.py 5.77 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak 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
##############################################################################
#
# Copyright (c) 2002-2011 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility 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
# guarantees and support are strongly advised 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
import hashlib
import DateTime
Łukasz Nowak's avatar
Łukasz Nowak committed
31
import random
Łukasz Nowak's avatar
Łukasz Nowak committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

def sha1(s):
  return hashlib.sha1(s).hexdigest()

class TestERP5PayzenSecurePaymentMixin(ERP5TypeTestCase):
  """
  An ERP5 Payzen Secure Payment test case
  """

  def getTitle(self):
    return "ERP5 Payzen Secure Payment"

  def getBusinessTemplateList(self):
    """
    Tuple of Business Templates we need to install
    """
    return ('erp5_base'
          , 'erp5_secure_payment'
          , 'erp5_payzen_secure_payment')

  def afterSetUp(self):
    self.portal = self.getPortalObject()
Łukasz Nowak's avatar
Łukasz Nowak committed
54 55 56 57
    if not self.portal.hasObject('portal_secure_payments'):
      self.portal.manage_addProduct['ERP5SecurePayment'].manage_addTool(
        'ERP5 Secure Payment Tool', None)
      self.stepTic()
Łukasz Nowak's avatar
Łukasz Nowak committed
58
    self.service = self.portal.portal_secure_payments.newContent(
Łukasz Nowak's avatar
Łukasz Nowak committed
59
      portal_type='Payzen Service')
Łukasz Nowak's avatar
Łukasz Nowak committed
60 61 62
    self.stepTic()

class TestERP5PayzenSecurePayment(TestERP5PayzenSecurePaymentMixin):
Łukasz Nowak's avatar
Łukasz Nowak committed
63 64 65 66 67 68
  def afterSetUp(self):
    super(TestERP5PayzenSecurePayment, self).afterSetUp()
    self.service_password = '0123456789012345'
    self.service.edit(service_password=self.service_password)
    self.stepTic()

Łukasz Nowak's avatar
Łukasz Nowak committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  def test_getSignature_dict_simple(self):
    self.assertEqual(
      self.service._getSignature({'key': 'value'}, ['key']),
      sha1('value+' + self.service_password)
    )

  def test_getSignature_dict_key_sort(self):
    self.assertEqual(
      self.service._getSignature({'key': 'value', 'key1': 'value1'}, ['key',
        'key1']),
      sha1('value+value1+' + self.service_password)
    )
    self.assertEqual(
      self.service._getSignature({'key': 'value', 'key1': 'value1'}, ['key1',
        'key']),
      sha1('value1+value+' + self.service_password)
    )

  def test_getSignature_dict_date_as_DateTime(self):
    now = DateTime.DateTime()
    d = {'key': now}
    self.assertEqual(
      self.service._getSignature(d, ['key']),
      sha1(now.strftime('%Y%m%d') + '+' + self.service_password)
    )
    # dict was updated
    self.assertEqual(d['key'], now.strftime('%Y-%m-%dT%H:%M:%S'))

  def test_getSignature_dict_date_as_DateTime_output_date(self):
    now = DateTime.DateTime()
    d = {'key': now}
    self.assertEqual(
      self.service._getSignature(d, ['key'], output_date_format='%Y'),
      sha1(now.strftime('%Y%m%d') + '+' + self.service_password)
    )
    # dict was updated with passed format
    self.assertEqual(d['key'], now.strftime('%Y'))

  def test_getSignature_dict_date_as_DateTime_signature_format(self):
    now = DateTime.DateTime()
    d = {'key': now}
    self.assertEqual(
      self.service._getSignature(d, ['key'], signature_date_format='%Y'),
      sha1(now.strftime('%Y') + '+' + self.service_password)
    )
    # dict was updated with passed format
    self.assertEqual(d['key'], now.strftime('%Y-%m-%dT%H:%M:%S'))

  def test_getSignature_dict_date_as_string(self):
    now = DateTime.DateTime()
    d = {'keyDaTe': now.strftime('%Y-%m-%d %H:%M:%S')}
    self.assertEqual(
      self.service._getSignature(d, ['keyDaTe']),
      sha1(now.strftime('%Y%m%d') + '+' + self.service_password)
    )
    # dict was updated with passed format
    self.assertEqual(d['keyDaTe'], now.strftime('%Y-%m-%dT%H:%M:%S'))
Łukasz Nowak's avatar
Łukasz Nowak committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  def test_navigate(self):
    self.service.edit(
      link_url_string='http://payzen/',
      payzen_vads_action_mode='INTERACTIVE',
      payzen_vads_ctx_mode='TEST',
      payzen_vads_page_action='REGISTER',
      payzen_vads_version='V2',
      service_username='0123456'
    )
    pt_id = str(random.random())
    self.portal.portal_skins.custom.manage_addProduct['PageTemplates']\
                .manage_addPageTemplate(id = pt_id, text='''<tal:block tal:repeat="value here/field_list">key=<tal:block tal:replace="python: value[0]"/> value=<tal:block tal:replace="python: value[1]"/>
</tal:block>''')
    # flush skin cache
    self.portal.changeSkin(None)
    try:
      result = self.service.navigate(pt_id, {"key": 'value'})
      signature = sha1('value+INTERACTIVE+ERP5+TEST+REGISTER+SINGLE+0123456+V2+'
        + self.service_password)
      self.assertEqual(result, """key=vads_site_id value=0123456
key=vads_payment_config value=SINGLE
key=vads_action_mode value=INTERACTIVE
key=vads_contrib value=ERP5
key=vads_page_action value=REGISTER
key=vads_ctx_mode value=TEST
key=key value=value
key=signature value=%s
key=vads_version value=V2
""" % signature)
    finally:
      self.portal.portal_skins.custom.manage_delObjects([pt_id])
      # flush skin cache
      self.portal.changeSkin(None)