source: main/waeup.aaue/trunk/src/waeup/aaue/interswitch/tests.py @ 16281

Last change on this file since 16281 was 15867, checked in by Henrik Bettermann, 5 years ago

Reduce transaction charge from 250 to 200.

  • Property svn:keywords set to Id
File size: 23.6 KB
Line 
1## $Id: tests.py 15867 2019-12-05 16:50:14Z 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
19from datetime import datetime, date, timedelta
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
26from waeup.aaue.testing import FunctionalLayer
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 InterswitchTestsApplicants(ApplicantsFullSetup):
45    """Tests for the Interswitch payment gateway.
46    """
47
48    layer = FunctionalLayer
49
50    def setUp(self):
51        super(InterswitchTestsApplicants, self).setUp()
52        certificate = createObject('waeup.Certificate')
53        certificate.code = 'CERT2'
54        certificate.title = 'New Certificate'
55        certificate.application_category = 'basic'
56        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
57            certificate)
58        configuration = SessionConfiguration()
59        configuration.academic_session = datetime.now().year - 2
60        self.app['configuration'].addSessionConfiguration(configuration)
61        self.configuration = configuration
62        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
63        self.browser.open(self.manage_path)
64        #IWorkflowState(self.student).setState('started')
65        super(InterswitchTestsApplicants, self).fill_correct_values()
66        #self.browser.getControl(name="form.programme_type").value = ['regular']
67        self.applicantscontainer.application_fee = 10000.0
68        self.browser.getControl(name="form.nationality").value = ['NG']
69        self.browser.getControl(name="form.course2").value = ['CERT2']
70        self.browser.getControl(name="transition").value = ['start']
71        self.browser.getControl("Save").click()
72
73    def test_interswitch_form_regular(self):
74        self.browser.getControl("Add online").click()
75        self.assertTrue('ticket created' in self.browser.contents)
76        #self.browser.getLink(value).click()
77        self.assertTrue('Amount Authorized' in self.browser.contents)
78        self.assertTrue('<span>10000.0</span>' in self.browser.contents)
79        self.payment_url = self.browser.url
80        # Manager can access InterswitchForm
81        self.browser.getLink("Pay via Interswitch", index=0).click()
82        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
83        self.assertTrue(
84            '<input type="hidden" name="amount" value="1000000" />'
85            in self.browser.contents)
86        self.assertTrue(
87            'item_name="WAeAC" item_amt="200000"'
88            in self.browser.contents)
89        self.assertTrue(
90            'item_name="Application Fee" item_amt="780000" bank_id="117" acct_num="1010835352"'
91            in self.browser.contents)
92        self.assertTrue(
93            '<input type="hidden" name="product_id" value="5845" />'
94            in self.browser.contents)
95        delta = timedelta(days=8)
96        self.applicant.values()[0].creation_date -= delta
97        self.browser.open(self.payment_url)
98        self.browser.getLink("Pay via Interswitch", index=0).click()
99        self.assertTrue(
100            'This payment ticket is too old. Please create a new ticket'
101            in self.browser.contents)
102
103    def test_interswitch_form_pg(self):
104        self.applicant.applicant_id = u'pg_anything'
105        self.browser.getControl("Add online").click()
106        self.assertTrue('ticket created' in self.browser.contents)
107        #self.browser.getLink(value).click()
108        self.assertTrue('Amount Authorized' in self.browser.contents)
109        self.assertTrue('<span>10000.0</span>' in self.browser.contents)
110        self.payment_url = self.browser.url
111        # Manager can access InterswitchForm
112        self.browser.getLink("Pay via Interswitch", index=0).click()
113        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
114        self.assertTrue(
115            '<input type="hidden" name="amount" value="1000000" />'
116            in self.browser.contents)
117        self.assertTrue(
118            'item_name="WAeAC" item_amt="200000"'
119            in self.browser.contents)
120        self.assertTrue(
121            'item_name="Application Fee" item_amt="580000" bank_id="117" acct_num="1010835352"'
122            in self.browser.contents)
123        self.assertTrue(
124            'item_name="PG Handbook" item_amt="200000" bank_id="117" acct_num="1010827641"'
125            in self.browser.contents)
126        self.assertTrue(
127            '<input type="hidden" name="product_id" value="5845" />'
128            in self.browser.contents)
129        delta = timedelta(days=8)
130        self.applicant.values()[0].creation_date -= delta
131        self.browser.open(self.payment_url)
132        self.browser.getLink("Pay via Interswitch", index=0).click()
133        self.assertTrue(
134            'This payment ticket is too old. Please create a new ticket'
135            in self.browser.contents)
136
137    def test_interswitch_form_utme(self):
138        self.applicant.applicant_id = u'utme_anything'
139        self.browser.getControl("Add online").click()
140        self.assertTrue('ticket created' in self.browser.contents)
141        #self.browser.getLink(value).click()
142        self.assertTrue('Amount Authorized' in self.browser.contents)
143        self.assertTrue('<span>10000.0</span>' in self.browser.contents)
144        self.payment_url = self.browser.url
145        # Manager can access InterswitchForm
146        self.browser.getLink("Pay via Interswitch", index=0).click()
147        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
148        self.assertTrue(
149            '<input type="hidden" name="amount" value="1000000" />'
150            in self.browser.contents)
151        self.assertTrue(
152            'item_name="WAeAC" item_amt="200000"'
153            in self.browser.contents)
154        self.assertTrue(
155            'item_name="Application Fee" item_amt="780000" bank_id="117" acct_num="1010835352"'
156            in self.browser.contents)
157        self.assertTrue(
158            '<input type="hidden" name="product_id" value="5845" />'
159            in self.browser.contents)
160        delta = timedelta(days=8)
161        self.applicant.values()[0].creation_date -= delta
162        self.browser.open(self.payment_url)
163        self.browser.getLink("Pay via Interswitch", index=0).click()
164        self.assertTrue(
165            'This payment ticket is too old. Please create a new ticket'
166            in self.browser.contents)
167
168    def test_interswitch_form_bridge(self):
169        self.applicant.applicant_id = u'bridge_xyz'
170        self.browser.getControl("Add online").click()
171        self.assertTrue('ticket created' in self.browser.contents)
172        #self.browser.getLink(value).click()
173        self.assertTrue('Amount Authorized' in self.browser.contents)
174        self.assertTrue('<span>10000.0</span>' in self.browser.contents)
175        self.payment_url = self.browser.url
176        # Manager can access InterswitchForm
177        self.browser.getLink("Pay via Interswitch", index=0).click()
178        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
179        self.assertTrue(
180            '<input type="hidden" name="amount" value="1000000" />'
181            in self.browser.contents)
182        self.assertTrue(
183            'item_name="WAeAC" item_amt="200000"'
184            in self.browser.contents)
185        self.assertTrue(
186            'item_name="Application Fee" item_amt="780000" bank_id="7" acct_num="1014847058"'
187            in self.browser.contents)
188
189    @external_test
190    def test_webservice(self):
191
192        self.browser.open(self.payment_url + '/request_webservice')
193        self.assertTrue('Unsuccessful callback' in self.browser.contents)
194        # The payment is now in state failed
195        self.assertTrue('<span>Failed</span>' in self.browser.contents)
196
197class InterswitchTestsStudents(StudentsFullSetup):
198    """Tests for the Interswitch payment gateway.
199    """
200
201    layer = FunctionalLayer
202
203    def setUp(self):
204        super(InterswitchTestsStudents, self).setUp()
205        self.app['configuration']['2004'].interswitch_enabled = True
206
207    def test_interswitch_form_pt_school_fees(self):
208        self.certificate.study_mode = 'ug_pt'
209        self.student['studycourse'].entry_session = 2013
210        self.student['studycourse'].certificate.school_fee_1 = 50200.0
211        self.app['configuration']['2004'].union_fee = 1000.0
212        self.app['configuration']['2004'].welfare_fee = 500.0
213        # Manager can access InterswitchForm
214        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
215        self.browser.open(self.payments_path)
216        IWorkflowState(self.student).setState('cleared')
217        self.student.nationality = u'NG'
218        self.browser.open(self.payments_path + '/addop')
219        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
220        self.browser.getControl("Create ticket").click()
221        self.assertTrue('ticket created' in self.browser.contents)
222        ctrl = self.browser.getControl(name='val_id')
223        self.value = ctrl.options[0]
224        self.browser.getLink(self.value).click()
225        self.assertTrue('Amount Authorized' in self.browser.contents)
226        # 50200 + 1000 + 500 - 400
227        self.assertTrue('<span>51300.0</span>' in self.browser.contents)
228        self.payment_url = self.browser.url
229        self.browser.getLink("Pay via Interswitch", index=0).click()
230        self.assertTrue(
231            'Please pay acceptance fee first' in self.browser.contents)
232        acc_payment = createObject('waeup.StudentOnlinePayment')
233        acc_payment.p_state = 'paid'
234        acc_payment.p_category = 'clearance'
235        self.student['payments']['xyz'] = acc_payment
236        self.browser.getLink("Pay via Interswitch", index=0).click()
237        self.assertTrue('<input type="hidden" name="pay_item_id" value="105" />'
238                        in self.browser.contents)
239        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
240        self.assertEqual(self.student.current_mode, 'ug_pt')
241        self.assertTrue(
242            '<input type="hidden" name="amount" value="5130000" />'
243            in self.browser.contents)
244        self.assertTrue(
245            'item_name="School Fee Plus" '
246            'item_amt="4810000" bank_id="7" '
247            'acct_num="1014847058"' in self.browser.contents)
248
249    def test_interswitch_form_regular_school_fees(self):
250        self.certificate.study_mode = 'ug_ft'
251        self.student['studycourse'].entry_session = 2013
252        self.student['studycourse'].certificate.school_fee_1 = 51750.0
253        # Manager can access InterswitchForm
254        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
255        self.browser.open(self.payments_path)
256        IWorkflowState(self.student).setState('cleared')
257        self.student.nationality = u'NG'
258        self.browser.open(self.payments_path + '/addop')
259        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
260        self.browser.getControl("Create ticket").click()
261        self.assertTrue('ticket created' in
262                           self.browser.contents)
263        ctrl = self.browser.getControl(name='val_id')
264        self.value = ctrl.options[0]
265        self.browser.getLink(self.value).click()
266        self.assertTrue('Amount Authorized' in
267                           self.browser.contents)
268        self.assertTrue('<span>51750.0</span>' in self.browser.contents)
269        self.payment_url = self.browser.url
270        self.browser.getLink("Pay via Interswitch", index=0).click()
271        self.assertTrue(
272            'Please pay acceptance fee first' in self.browser.contents)
273        acc_payment = createObject('waeup.StudentOnlinePayment')
274        acc_payment.p_state = 'paid'
275        acc_payment.p_category = 'clearance'
276        self.student['payments']['xyz'] = acc_payment
277        self.browser.getLink("Pay via Interswitch", index=0).click()
278        self.assertTrue('<input type="hidden" name="pay_item_id" value="107" />'
279                        in self.browser.contents)
280        self.assertTrue('Total Amount Authorized:'
281                        in self.browser.contents)
282        self.assertEqual(self.student.current_mode, 'ug_ft')
283        self.assertTrue(
284            '<input type="hidden" name="amount" value="5175000" />'
285            in self.browser.contents)
286        self.assertTrue(
287            'item_name="School Fee Plus" item_amt="4755000" bank_id="117" '
288            'acct_num="1010827641"' in self.browser.contents)
289
290    def test_interswitch_form_pt_acceptance_fees(self):
291        self.certificate.study_mode = 'ug_pt'
292        self.app['configuration']['2004'].clearance_fee_ug_pt = 40200.0
293        self.app['configuration']['2004'].matric_gown_fee = 2000.0
294        self.app['configuration']['2004'].lapel_fee = 800.0
295        # Manager can access InterswitchForm
296        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
297        self.browser.open(self.payments_path)
298        IWorkflowState(self.student).setState('admitted')
299        self.student.nationality = u'NG'
300        self.browser.open(self.payments_path + '/addop')
301        self.browser.getControl(name="form.p_category").value = ['clearance_incl']
302        self.browser.getControl("Create ticket").click()
303        self.assertTrue('ticket created' in  self.browser.contents)
304        ctrl = self.browser.getControl(name='val_id')
305        self.value = ctrl.options[0]
306        self.browser.getLink(self.value).click()
307        self.assertTrue('Amount Authorized' in self.browser.contents)
308        # 40200 + 2000 + 800 - 400
309        self.assertTrue('<span>42600.0</span>' in self.browser.contents)
310        self.payment_url = self.browser.url
311        self.browser.getLink("Pay via Interswitch", index=0).click()
312        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />' in
313                           self.browser.contents)
314        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
315        self.assertTrue(
316            '<input type="hidden" name="amount" value="4260000" />'
317            in self.browser.contents)
318        self.assertTrue(
319            'item_name="Acceptance Fee Plus" '
320            'item_amt="4000000" bank_id="7" '
321            'acct_num="1014847058"' in self.browser.contents)
322
323    def test_interswitch_form_regular_acceptance_fees(self):
324        self.certificate.study_mode = 'ug_ft'
325        self.app['configuration']['2004'].clearance_fee = 43050.0
326        # Manager can access InterswitchForm
327        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
328        self.browser.open(self.payments_path)
329        IWorkflowState(self.student).setState('admitted')
330        self.student.nationality = u'NG'
331        self.browser.open(self.payments_path + '/addop')
332        self.browser.getControl(name="form.p_category").value = ['clearance_incl']
333        self.browser.getControl("Create ticket").click()
334        self.assertTrue('ticket created' in self.browser.contents)
335        ctrl = self.browser.getControl(name='val_id')
336        self.value = ctrl.options[0]
337        self.browser.getLink(self.value).click()
338        self.assertTrue('Amount Authorized' in self.browser.contents)
339        self.assertTrue('<span>43050.0</span>' in self.browser.contents)
340        self.payment_url = self.browser.url
341        self.browser.getLink("Pay via Interswitch", index=0).click()
342        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
343                        in self.browser.contents)
344        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
345        self.assertTrue(
346            '<input type="hidden" name="amount" value="4305000" />'
347            in self.browser.contents)
348        self.assertTrue(
349            'item_name="Acceptance Fee Plus" item_amt="4135000" bank_id="117" '
350            'acct_num="1010827641"' in self.browser.contents)
351
352    def test_interswitch_form_concessional_fees(self):
353        self.certificate.study_mode = 'ug_ft'
354        self.app['configuration']['2004'].concessional_fee = 1567.0
355        # Manager can access InterswitchForm
356        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
357        self.browser.open(self.payments_path)
358        IWorkflowState(self.student).setState('admitted')
359        self.student.nationality = u'NG'
360        self.browser.open(self.payments_path + '/addop')
361        self.browser.getControl(name="form.p_category").value = ['concessional']
362        self.browser.getControl("Create ticket").click()
363        self.assertTrue('ticket created' in  self.browser.contents)
364        ctrl = self.browser.getControl(name='val_id')
365        self.value = ctrl.options[0]
366        self.browser.getLink(self.value).click()
367        self.assertTrue('Amount Authorized' in self.browser.contents)
368        self.assertTrue(
369            '<span>1567.0</span>' in self.browser.contents)
370        self.payment_url = self.browser.url
371        self.certificate.study_mode = 'ug_ft'
372        self.browser.getLink("Pay via Interswitch", index=0).click()
373        self.assertTrue('<input type="hidden" name="pay_item_id" value="107" />'
374                           in self.browser.contents)
375        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
376        self.assertTrue(
377            '<input type="hidden" name="amount" value="156700" />'
378            in self.browser.contents)
379        self.assertTrue(
380            'item_name="Concessional Fee" item_amt="136700" '
381            'bank_id="117" acct_num="1010827641"'
382            in self.browser.contents)
383
384    def test_interswitch_form_ticket_expired(self):
385        self.certificate.study_mode = 'ug_pt'
386        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
387        acc_payment = createObject('waeup.StudentOnlinePayment')
388        acc_payment.p_state = 'unpaid'
389        acc_payment.p_session = 2004
390        acc_payment.p_category = 'clearance'
391        acc_payment.p_id = 'xyz'
392        acc_payment.pay_item_id = '123'
393        acc_payment.amount_auth = 876.0
394        self.student['payments']['xyz'] = acc_payment
395        self.browser.open(self.payments_path + '/xyz')
396        self.browser.getLink("Pay via Interswitch", index=0).click()
397        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />'
398                        in self.browser.contents)
399        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
400        self.assertEqual(self.student.current_mode, 'ug_pt')
401        self.assertTrue(
402            '<input type="hidden" name="amount" value="87600" />'
403            in self.browser.contents)
404        delta = timedelta(days=8)
405        acc_payment.creation_date -= delta
406        self.browser.open(self.payments_path + '/xyz')
407        self.browser.getLink("Pay via Interswitch", index=0).click()
408        self.assertTrue(
409            'This payment ticket is too old. Please create a new ticket'
410            in self.browser.contents)
411        delta = timedelta(days=2)
412        acc_payment.creation_date += delta
413        self.browser.open(self.payments_path + '/xyz')
414        self.browser.getLink("Pay via Interswitch", index=0).click()
415        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
416
417    @external_test
418    def test_webservice(self):
419        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
420        self.browser.open(self.payments_path)
421        IWorkflowState(self.student).setState('cleared')
422        self.student.nationality = u'NG'
423        self.browser.open(self.payments_path + '/addop')
424        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
425        self.browser.getControl("Create ticket").click()
426        self.assertTrue('ticket created' in self.browser.contents)
427        ctrl = self.browser.getControl(name='val_id')
428        self.value = ctrl.options[0]
429        self.browser.getLink(self.value).click()
430        self.payment_url = self.browser.url
431        # First we have to open InterswitchPageStudent to set provider_amt
432        # and gateway_amt
433        self.browser.open(self.payment_url + '/goto_interswitch')
434        # Now we can call the webservice
435        self.browser.open(self.payment_url + '/request_webservice')
436        self.assertTrue('Unsuccessful callback' in self.browser.contents)
437        # The payment is now in state failed ...
438        self.assertTrue('<span>Failed</span>' in self.browser.contents)
439        # ... and the catalog has been updated
440        cat = getUtility(ICatalog, name='payments_catalog')
441        results = list(
442            cat.searchResults(p_state=('failed', 'failed')))
443        self.assertEqual(len(results), 1)
444        self.assertEqual(results[0].p_state, 'failed')
445
446        # Let's replace the p_id with a valid p_id of the AAUE
447        # live system. This is definitely not an appropriate
448        # solution for testing, but we have no choice since
449        # Interswitch doesn't provide any interface
450        # for testing.
451        payment = self.student['payments'][self.value]
452        payment.p_id = 'p4465649308559'
453        self.browser.open(self.payment_url + '/request_webservice')
454        self.assertTrue('Unsuccessful callback: Amount Inconsistency' in
455                          self.browser.contents)
456        # The payment is now in state failed ...
457        self.assertTrue('<span>Failed</span>' in self.browser.contents)
458        # Let's replace the amount autorized with the amount of the
459        # live system payment
460        payment.amount_auth = 60250.0
461        self.browser.open(self.payment_url + '/request_webservice')
462        self.assertTrue('Successful payment' in
463                          self.browser.contents)
464        # The payment is now in state paid ...
465        self.assertTrue('<span>Paid</span>' in self.browser.contents)
466        # ... and the catalog has been updated
467        cat = getUtility(ICatalog, name='payments_catalog')
468        results = list(
469            cat.searchResults(p_state=('paid', 'paid')))
470        self.assertEqual(len(results), 1)
471        self.assertEqual(results[0].p_state, 'paid')
472        # Approval is logged in students.log ...
473        logfile = os.path.join(
474            self.app['datacenter'].storage, 'logs', 'students.log')
475        logcontent = open(logfile).read()
476        self.assertTrue(
477            'zope.mgr - '
478            'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
479            'E1000000 - successful schoolfee_incl payment: p4465649308559\n'
480            in logcontent)
481        # ... and in payments.log
482        logfile = os.path.join(
483            self.app['datacenter'].storage, 'logs', 'payments.log')
484        logcontent = open(logfile).read()
485        self.assertTrue(
486            '"zope.mgr",E1000000,p4465649308559,schoolfee_incl,'
487            '60250.0,00,1500.0,250.0,0.0,,,\n'
488            in logcontent)
Note: See TracBrowser for help on using the repository browser.