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

Last change on this file since 14920 was 14920, checked in by Henrik Bettermann, 7 years ago

Apply Treasury Single Account policy also to AAUE.

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