source: main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/interswitch/tests.py @ 16326

Last change on this file since 16326 was 16292, checked in by Henrik Bettermann, 4 years ago

Redesign personal data slip completely.

  • Property svn:keywords set to Id
File size: 12.4 KB
RevLine 
[10765]1## $Id: tests.py 16292 2020-10-30 12:36:49Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18import os
[11271]19from datetime import datetime, date, timedelta
[10765]20from zope.component import createObject, getUtility
21from zope.catalog.interfaces import ICatalog
22from hurry.workflow.interfaces import IWorkflowState
23from waeup.kofa.students.tests.test_browser import StudentsFullSetup
24from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
25from waeup.kofa.configuration import SessionConfiguration
[15563]26from kofacustom.iuokada.testing import FunctionalLayer
[10765]27
28# Also run tests that send requests to external servers?
29#   If you enable this, please make sure the external services
30#   do exist really and are not bothered by being spammed by a test programme.
31EXTERNAL_TESTS = False
32
33def external_test(func):
34    if not EXTERNAL_TESTS:
35        myself = __file__
36        if myself.endswith('.pyc'):
37            myself = myself[:-2]
38        print "WARNING: external tests are skipped!"
39        print "WARNING: edit %s to enable them." % myself
40        return
41    return func
42
43
44class InterswitchTestsStudents(StudentsFullSetup):
45    """Tests for the Interswitch payment gateway.
46    """
47
48    layer = FunctionalLayer
49
50    def setUp(self):
51        super(InterswitchTestsStudents, self).setUp()
[15656]52        self.app['configuration']['2004'].interswitch_enabled = True
[10765]53
54    def test_interswitch_form(self):
[16270]55        self.app['configuration']['2004'].registration_fee = 7000.0
[10765]56        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
57        self.browser.open(self.payments_path)
58        IWorkflowState(self.student).setState('cleared')
59        self.student.nationality = u'NG'
60        self.browser.open(self.payments_path + '/addop')
[16137]61        self.browser.getControl(name="form.p_category").value = ['registration']
[15883]62        self.browser.getControl("Create payment").click()
[16137]63        self.assertTrue('Payment created' in self.browser.contents)
[16270]64        self.browser.open(self.payments_path)
[10765]65        ctrl = self.browser.getControl(name='val_id')
66        self.value = ctrl.options[0]
67        self.browser.getLink(self.value).click()
[14874]68        self.assertTrue(
[16270]69            '<span>7000.0</span>' in self.browser.contents)
[10765]70        self.payment_url = self.browser.url
[15704]71        self.browser.getLink("Pay via Interswitch", index=0).click()
[16137]72        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in
[10765]73                           self.browser.contents)
74        self.assertEqual(self.student.current_mode, 'ug_ft')
[15773]75        # 250 gateway charge have been added
76        payment = self.student['payments'][self.value]
[16137]77        self.assertEqual(payment.amount_auth, 7250)
[16270]78        self.assertEqual(payment.net_amt, 7000)
[14874]79        self.assertTrue(
[16137]80            '<input type="hidden" name="amount" value="725000" />' in
[10765]81            self.browser.contents)
[16270]82        # WAeAC charge have been substracted
[14874]83        self.assertTrue(
[16137]84            'item_name="Registration Fee" item_amt="200000" bank_id="117" acct_num="1011050158"' in
[10765]85            self.browser.contents)
[16270]86        self.assertTrue(
87            'item_name="WAeAC" item_amt="500000" bank_id="31" acct_num="0773411069"' in
88            self.browser.contents)
[10765]89
[12979]90    def test_interswitch_form_ticket_expired(self):
91        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
92        acc_payment = createObject('waeup.StudentOnlinePayment')
93        acc_payment.p_state = 'unpaid'
[15773]94        acc_payment.p_category = 'registration'
[12979]95        acc_payment.p_id = 'xyz'
96        acc_payment.pay_item_id = '123'
[15773]97        acc_payment.amount_auth = 1000.0
[12979]98        self.student['payments']['xyz'] = acc_payment
99        self.browser.open(self.payments_path + '/xyz')
[15704]100        self.browser.getLink("Pay via Interswitch", index=0).click()
[15773]101        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in
[12979]102                           self.browser.contents)
103        self.assertMatches('...Total Amount Authorized:...',
104                           self.browser.contents)
105        self.assertEqual(self.student.current_mode, 'ug_ft')
[16270]106        # 250 gateway charge have been added
[15773]107        self.assertTrue(
[16270]108            '<input type="hidden" name="amount" value="125000" />' in
[12979]109            self.browser.contents)
[16270]110        self.assertEqual(acc_payment.amount_auth, 1250)
[15773]111        self.assertEqual(acc_payment.net_amt, 1000)
[13020]112        delta = timedelta(days=8)
[12979]113        acc_payment.creation_date -= delta
114        self.browser.open(self.payments_path + '/xyz')
[15704]115        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]116        self.assertMatches(
117            '...This payment ticket is too old. Please create a new ticket...',
118            self.browser.contents)
[13020]119        delta = timedelta(days=2)
[12979]120        acc_payment.creation_date += delta
121        self.browser.open(self.payments_path + '/xyz')
[15704]122        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]123        self.assertMatches('...Total Amount Authorized:...',
124                           self.browser.contents)
125
[10765]126    @external_test
127    def test_webservice(self):
128        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
129        self.browser.open(self.payments_path)
130        IWorkflowState(self.student).setState('cleared')
131        self.student.nationality = u'NG'
132        self.browser.open(self.payments_path + '/addop')
133        self.browser.getControl(name="form.p_category").value = ['schoolfee']
[15883]134        self.browser.getControl("Create payment").click()
[10765]135        self.assertMatches('...ticket created...',
136                           self.browser.contents)
137        ctrl = self.browser.getControl(name='val_id')
138        self.value = ctrl.options[0]
139        self.browser.getLink(self.value).click()
140        self.payment_url = self.browser.url
141        # First we have open InterswitchPageStudent to set provider_amt
142        # and gateway_amt
143        self.browser.open(self.payment_url + '/goto_interswitch')
144        # Now we can call the webservice
145        self.browser.open(self.payment_url + '/request_webservice')
146        self.assertMatches('...Unsuccessful callback...',
147                          self.browser.contents)
148        # The payment is now in state failed ...
149        self.assertMatches('...<span>Failed</span>...',
150                          self.browser.contents)
151        # ... and the catalog has been updated
152        cat = getUtility(ICatalog, name='payments_catalog')
153        results = list(
154            cat.searchResults(p_state=('failed', 'failed')))
155        self.assertEqual(len(results), 1)
156        self.assertEqual(results[0].p_state, 'failed')
157
158        # Let's replace the p_id with a valid p_id of the Uniben
159        # live system. This is definitely not an appropriate
160        # solution for testing, but we have no choice since
161        # Interswitch doesn't provide any interface
162        # for testing.
163        payment = self.student['payments'][self.value]
164        payment.p_id = 'p3547789850240'
165        self.browser.open(self.payment_url + '/request_webservice')
166        self.assertMatches('...Callback amount does not match...',
167                          self.browser.contents)
168        # The payment is now in state failed ...
169        self.assertMatches('...<span>Failed</span>...',
170                          self.browser.contents)
171        # Let's replace the amount autorized with the amount of the
172        # live system payment
173        payment.amount_auth = payment.r_amount_approved
174        self.browser.open(self.payment_url + '/request_webservice')
175        self.assertMatches('...Successful payment...',
176                          self.browser.contents)
177        # The payment is now in state paid ...
178        self.assertMatches('...<span>Paid</span>...',
179                          self.browser.contents)
180        # ... and the catalog has been updated
181        cat = getUtility(ICatalog, name='payments_catalog')
182        results = list(
183            cat.searchResults(p_state=('paid', 'paid')))
184        self.assertEqual(len(results), 1)
185        self.assertEqual(results[0].p_state, 'paid')
186        # Approval is logged in students.log ...
187        logfile = os.path.join(
188            self.app['datacenter'].storage, 'logs', 'students.log')
189        logcontent = open(logfile).read()
190        self.assertTrue(
191            'zope.mgr - '
[15563]192            'kofacustom.iuokada.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
[10765]193            'X1000000 - successful schoolfee payment: p3547789850240\n'
194            in logcontent)
195        # ... and in payments.log
196        logfile = os.path.join(
197            self.app['datacenter'].storage, 'logs', 'payments.log')
198        logcontent = open(logfile).read()
199        self.assertTrue(
200            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
201            '12000.0,00,0.0,150.0,0.0,,,\n'
202            in logcontent)
203
204
205class InterswitchTestsApplicants(ApplicantsFullSetup):
206    """Tests for the Interswitch payment gateway.
207    """
208
209    layer = FunctionalLayer
210
[16082]211    def fill_correct_values(self):
212        # Fill the edit form with suitable values
213        self.browser.getControl(name="form.firstname").value = 'John'
214        self.browser.getControl(name="form.middlename").value = 'Anthony'
215        self.browser.getControl(name="form.lastname").value = 'Tester'
216        self.browser.getControl(name="custom.course1").value = ['CERT1']
217        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
218        self.browser.getControl(name="form.sex").value = ['m']
219        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
[16292]220        self.browser.getControl(name="form.perm_address").value = 'Adresse'
[16082]221
[10765]222    def setUp(self):
223        super(InterswitchTestsApplicants, self).setUp()
[11271]224        configuration = SessionConfiguration()
225        configuration.academic_session = datetime.now().year - 2
226        self.app['configuration'].addSessionConfiguration(configuration)
227        self.configuration = configuration
[10765]228        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
229        self.browser.open(self.manage_path)
230        #IWorkflowState(self.student).setState('started')
[16082]231        self.fill_correct_values()
[10765]232        self.applicantscontainer.application_fee = 1000.0
233        self.browser.getControl(name="form.nationality").value = ['NG']
234        self.browser.getControl(name="transition").value = ['start']
235        self.browser.getControl("Save").click()
236        self.browser.getControl("Add online").click()
[15883]237        self.assertMatches('...Payment created...',
[10765]238                           self.browser.contents)
239        #ctrl = self.browser.getControl(name='val_id')
240        #value = ctrl.options[0]
241        #self.browser.getLink(value).click()
242        self.assertMatches('...Amount Authorized...',
243                           self.browser.contents)
244        self.assertMatches(
245            '...<span>1000.0</span>...',
246            self.browser.contents)
247        self.payment_url = self.browser.url
248
249
250    def test_interswitch_form(self):
251        # Manager can access InterswitchForm
[15704]252        self.browser.getLink("Pay via Interswitch", index=0).click()
[10765]253        self.assertMatches('...Total Amount Authorized:...',
254                           self.browser.contents)
255        self.assertMatches(
[11813]256            '...<input type="hidden" name="amount" value="100000" />...',
[10765]257            self.browser.contents)
[13020]258        delta = timedelta(days=8)
[12979]259        self.applicant.values()[0].creation_date -= delta
260        self.browser.open(self.payment_url)
[15704]261        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]262        self.assertMatches(
263            '...This payment ticket is too old. Please create a new ticket...',
264            self.browser.contents)
[10765]265
266    @external_test
267    def test_webservice(self):
268
269        self.browser.open(self.payment_url + '/request_webservice')
270        self.assertMatches('...Unsuccessful callback...',
271                          self.browser.contents)
272        # The payment is now in state failed
273        self.assertMatches('...<span>Failed</span>...',
274                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.