source: main/waeup.uniben/trunk/src/waeup/uniben/interswitch/tests.py @ 14358

Last change on this file since 14358 was 14237, checked in by Henrik Bettermann, 8 years ago

Clearance fee and provider amount do increase only for students starting in 2016.

  • Property svn:keywords set to Id
File size: 25.0 KB
Line 
1## $Id: tests.py 14237 2016-10-27 21:07:05Z 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.uniben.configuration import CustomSessionConfiguration
26from waeup.uniben.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
33SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
34
35def external_test(func):
36    if not EXTERNAL_TESTS:
37        myself = __file__
38        if myself.endswith('.pyc'):
39            myself = myself[:-2]
40        print "WARNING: external tests are skipped!"
41        print "WARNING: edit %s to enable them." % myself
42        return
43    return func
44
45
46class InterswitchTestsStudents(StudentsFullSetup):
47    """Tests for the Interswitch payment gateway.
48    """
49
50    layer = FunctionalLayer
51
52    def setUp(self):
53        super(InterswitchTestsStudents, self).setUp()
54        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
55        self.browser.open(self.payments_path)
56        IWorkflowState(self.student).setState('cleared')
57        self.student.nationality = u'NG'
58        self.browser.open(self.payments_path + '/addop')
59        self.browser.getControl(name="form.p_category").value = ['schoolfee']
60        self.browser.getControl("Create ticket").click()
61        self.assertMatches('...ticket created...',
62                           self.browser.contents)
63        ctrl = self.browser.getControl(name='val_id')
64        self.value = ctrl.options[0]
65        self.browser.getLink(self.value).click()
66        self.assertMatches('...Amount Authorized...',
67                           self.browser.contents)
68        self.assertTrue(
69            '<span>40000.0</span>' in self.browser.contents)
70        self.payment_url = self.browser.url
71
72#    def callback_url(self, payment_url, resp, apprAmt):
73#        return payment_url + (
74#            '/isw_callback?echo=' +
75#            '&resp=%s' +
76#            '&desc=Something went wrong' +
77#            '&txnRef=p1331792385335' +
78#            '&payRef=' + '&retRef=' +
79#            '&cardNum=0' +
80#            '&apprAmt=%s' +
81#            '&url=http://xyz') % (resp, apprAmt)
82
83    def test_interswitch_form(self):
84        # Manager can access InterswitchForm
85        self.browser.getLink("CollegePAY", index=0).click()
86        # The total amount to be processed by Interswitch
87        # has been reduced by the Interswitch fee of 150 Nairas
88        self.assertTrue('<input type="hidden" name="pay_item_id" value="5700" />'
89                           in self.browser.contents)
90        self.assertTrue('Total Amount Authorized:'
91                           in self.browser.contents)
92        self.assertEqual(self.student.current_mode, 'ug_ft')
93        self.assertTrue(
94            '<input type="hidden" name="amount" value="4000000" />'
95            in self.browser.contents)
96        self.assertTrue(
97            'item_name="School Fee" item_amt="3835000" bank_id="8" acct_num="2017506430"'
98            in self.browser.contents)
99        self.assertTrue(
100            'item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1014261520"'
101            in self.browser.contents)
102
103        # Create school fee ticket for returning students. Payment is made
104        # for next session.
105        current_payment_key = self.student['payments'].keys()[0]
106        self.certificate.study_mode = u'ug_pt'
107        IWorkflowState(self.student).setState('returning')
108        configuration = createObject('waeup.SessionConfiguration')
109        configuration.academic_session = 2005
110        self.app['configuration'].addSessionConfiguration(configuration)
111        self.browser.open(self.payments_path + '/addop')
112        self.browser.getControl(name="form.p_category").value = ['schoolfee']
113        self.browser.getControl("Create ticket").click()
114
115        ## Next session payment can't be made ...
116        #self.assertMatches(
117        #    '...You have not yet paid your current/active session...',
118        #    self.browser.contents)
119        ## current session payment must be approved first.
120        #self.student['payments'][current_payment_key].approve()
121        #self.browser.open(self.payments_path + '/addop')
122        #self.browser.getControl(name="form.p_category").value = ['schoolfee']
123        #self.browser.getControl("Create ticket").click()
124
125        ctrl = self.browser.getControl(name='val_id')
126        value = ctrl.options[1]
127        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
128        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
129        self.browser.getLink(value).click()
130        self.browser.getLink("CollegePAY", index=0).click()
131        # Split amounts have been set.
132        self.assertEqual(self.student['payments'][value].provider_amt, 1500.0)
133        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
134        self.assertTrue('<input type="hidden" name="pay_item_id" value="5701" />'
135                           in self.browser.contents)
136        self.assertTrue(
137            '<input type="hidden" name="amount" value="2000000" />'
138            in self.browser.contents)
139        self.assertTrue(
140            'item_name="School Fee" item_amt="1835000" bank_id="8" acct_num="2017506430"'
141            in self.browser.contents)
142        self.assertTrue(
143            'item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1014261520"'
144            in self.browser.contents)
145
146        # Create clearance fee ticket
147        #self.browser.open(self.payments_path + '/addop')
148        #self.browser.getControl(name="form.p_category").value = ['clearance']
149        #self.browser.getControl("Create ticket").click()
150        #ctrl = self.browser.getControl(name='val_id')
151        #value = ctrl.options[2]
152        #self.browser.getLink(value).click()
153        #self.assertMatches(
154        #    '...<span>45000.0</span>...',
155        #    self.browser.contents)
156        ## Manager can access InterswitchForm
157        #self.browser.getLink("CollegePAY", index=0).click()
158        #self.assertEqual(self.student['payments'][value].provider_amt, 1500.0)
159        #self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
160        #self.assertMatches('...<input type="hidden" name="pay_item_id" value="5702" />...',
161        #                   self.browser.contents)
162        #self.assertMatches('...Total Amount Authorized:...',
163        #                   self.browser.contents)
164        #self.assertMatches(
165        #    '...<input type="hidden" name="amount" value="4500000.0" />...',
166        #    self.browser.contents)
167        #self.assertMatches(
168        #    '...item_name="Acceptance Fee" item_amt="4335000" bank_id="7" acct_num="1003475516"...',
169        #    self.browser.contents)
170        #self.assertMatches(
171        #    '...item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1010764827"...',
172        #    self.browser.contents)
173
174        # Create gown fee ticket
175        configuration.maint_fee = 987.0
176        self.app['configuration']['2004'].gown_fee = 234.0
177        self.browser.open(self.payments_path + '/addop')
178        self.browser.getControl(name="form.p_category").value = ['gown']
179        self.browser.getControl("Create ticket").click()
180        ctrl = self.browser.getControl(name='val_id')
181        value = ctrl.options[2]
182        self.browser.getLink(value).click()
183        self.assertTrue(
184            '<span>234.0</span>',
185            self.browser.contents)
186        # Manager can access InterswitchForm
187        self.browser.getLink("CollegePAY", index=0).click()
188        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
189        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
190        self.assertTrue('<input type="hidden" name="pay_item_id" value="5704" />'
191                           in self.browser.contents)
192        self.assertTrue('Total Amount Authorized:'
193                           in self.browser.contents)
194        self.assertTrue(
195            '<input type="hidden" name="amount" value="23400" />'
196            in self.browser.contents)
197        self.assertTrue(
198            '<item_detail item_id="1" item_name="Gown Hire Fee" item_amt="8400" bank_id="8" acct_num="2017506430" />'
199            in self.browser.contents)
200        self.assertFalse(
201            'item_name="BT Education"' in self.browser.contents)
202
203        # Create hostel application ticket
204        #self.browser.open(self.payments_path + '/addop')
205        #self.browser.getControl(name="form.p_category").value = ['hostel_application']
206        #self.browser.getControl("Create ticket").click()
207        #ctrl = self.browser.getControl(name='val_id')
208        #value = ctrl.options[3]
209        #self.browser.getLink(value).click()
210        #self.assertTrue(
211        #    '<span>1000.0</span>' in self.browser.contents)
212        #self.student['payments'][value].approve()
213
214        # Create temp maint fee ticket
215        #self.browser.open(self.payments_path + '/addop')
216        #self.browser.getControl(name="form.p_category").value = ['tempmaint_1']
217        #self.browser.getControl("Create ticket").click()
218        #ctrl = self.browser.getControl(name='val_id')
219        #value = ctrl.options[4]
220        #self.browser.getLink(value).click()
221        #self.assertTrue(
222        #    '<span>8150.0</span>' in self.browser.contents)
223        # Manager can access InterswitchForm
224        #self.browser.getLink("CollegePAY", index=0).click()
225        #self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
226        #self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
227        #self.assertTrue('<input type="hidden" name="pay_item_id" value="5705" />'
228        #                 in self.browser.contents)
229        #self.assertMatches('...Total Amount Authorized:...',
230        #                   self.browser.contents)
231        #self.assertTrue(
232        #    '<input type="hidden" name="amount" value="815000" />'
233        #    in self.browser.contents)
234        #self.assertTrue(
235        #    '<item_detail item_id="1" item_name="Hostel Maintenance Fee" item_amt="800000" bank_id="129" acct_num="0014419432" />'
236        #    in self.browser.contents)
237        #self.assertFalse(
238        #    'item_name="BT Education"' in self.browser.contents)
239
240        # Create previous session fee ticket
241        configuration = createObject('waeup.SessionConfiguration')
242        configuration.academic_session = 2003
243        configuration.clearance_fee = 3456.0
244        self.app['configuration'].addSessionConfiguration(configuration)
245        self.student['studycourse'].entry_session = 2002
246        #self.browser.open(self.payments_path + '/addpp')
247        #self.browser.getControl(name="form.p_category").value = ['clearance']
248        #self.browser.getControl(name="form.p_session").value = ['2003']
249        #self.browser.getControl(name="form.p_level").value = ['300']
250        #self.browser.getControl("Create ticket").click()
251        #ctrl = self.browser.getControl(name='val_id')
252        #value = ctrl.options[5]
253        #self.browser.getLink(value).click()
254        #self.assertMatches(
255        #    '...<span>45000.0</span>...',
256        #    self.browser.contents)
257        ## Manager can access InterswitchForm
258        #self.browser.getLink("CollegePAY", index=0).click()
259        #self.assertEqual(self.student['payments'][value].provider_amt, 1500.0)
260        #self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
261        #self.assertMatches('...<input type="hidden" name="pay_item_id" value="5702" />...',
262        #                   self.browser.contents)
263        #self.assertMatches('...Total Amount Authorized:...',
264        #                   self.browser.contents)
265        ##self.assertMatches(
266        #    '...<input type="hidden" name="amount" value="4500000.0" />...',
267        #    self.browser.contents)
268        #self.assertMatches(
269        #    '...<item_detail item_id="1" item_name="Acceptance Fee" item_amt="4335000" bank_id="7" acct_num="1003475516" />...',
270        #    self.browser.contents)
271
272        # Create balance payment ticket
273        self.browser.open(self.payments_path + '/addbp')
274        self.browser.getControl(name="form.p_category").value = ['schoolfee']
275        self.browser.getControl(name="form.balance_session").value = ['2003']
276        self.browser.getControl(name="form.balance_level").value = ['300']
277        self.browser.getControl(name="form.balance_amount").value = '200'
278        self.browser.getControl("Create ticket").click()
279        ctrl = self.browser.getControl(name='val_id')
280        value = ctrl.options[3]
281        self.browser.getLink(value).click()
282        self.assertTrue(
283            '<span>200.0</span>' in self.browser.contents)
284        # Manager can access InterswitchForm
285        self.browser.getLink("CollegePAY", index=0).click()
286        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
287        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
288        self.assertTrue('<input type="hidden" name="pay_item_id" value="5701" />'
289                           in self.browser.contents)
290        self.assertTrue(
291            '<input type="hidden" name="amount" value="20000" />'
292            in self.browser.contents)
293        self.assertTrue(
294            'item_name="School Fee" item_amt="5000" bank_id="8" acct_num="2017506430"'
295            in self.browser.contents)
296        self.assertFalse(
297            'item_name="BT Education"' in self.browser.contents)
298
299        # Create JUPEB fee ticket
300        self.app['configuration']['2004'].jupeb_fee = 345.0
301        self.browser.open(self.payments_path + '/addop')
302        self.browser.getControl(name="form.p_category").value = ['jupeb']
303        self.browser.getControl("Create ticket").click()
304        ctrl = self.browser.getControl(name='val_id')
305        value = ctrl.options[4]
306        self.browser.getLink(value).click()
307        self.assertTrue(
308            '<span>345.0</span>',
309            self.browser.contents)
310        # Manager can access InterswitchForm
311        self.browser.getLink("CollegePAY", index=0).click()
312        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
313        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
314        self.assertTrue('<input type="hidden" name="pay_item_id" value="5717" />'
315                           in self.browser.contents)
316        self.assertTrue('Total Amount Authorized:'
317                           in self.browser.contents)
318        self.assertTrue(
319            '<input type="hidden" name="amount" value="34500" />'
320            in self.browser.contents)
321        self.assertTrue(
322            '<item_detail item_id="1" item_name="JUPEB Examination Fee" item_amt="19500" bank_id="8" acct_num="2017506430" />'
323            in self.browser.contents)
324        self.assertFalse(
325            'item_name="BT Education"' in self.browser.contents)
326
327#    @external_test
328#    def test_callback(self):
329
330        # Manager can call callback manually
331#        self.browser.open(self.callback_url(self.payment_url, 'XX', '300'))
332#        self.assertMatches('...Unsuccessful callback: Something went wrong...',
333#                          self.browser.contents)
334#        self.assertMatches('...Failed...',
335#                           self.browser.contents)
336#        self.browser.open(self.payment_url + '/isw_callback')
337#        self.assertMatches('...Unsuccessful callback: Incomplete query string...',
338#                          self.browser.contents)
339#        self.assertMatches('...Failed...',
340#                           self.browser.contents)
341#        self.browser.open(self.callback_url(self.payment_url, '00', '300000'))
342#        self.assertMatches('...Wrong amount...',
343#                          self.browser.contents)
344#        self.browser.open(self.callback_url(self.payment_url, '00', '4000000'))
345#        self.assertMatches('...Valid callback received...',
346#                          self.browser.contents)
347
348    def test_interswitch_form_ticket_expired(self):
349        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
350        acc_payment = createObject('waeup.StudentOnlinePayment')
351        acc_payment.p_state = 'unpaid'
352        acc_payment.p_category = 'clearance'
353        acc_payment.p_id = 'xyz'
354        acc_payment.pay_item_id = '123'
355        acc_payment.amount_auth = 876.0
356        self.student['payments']['xyz'] = acc_payment
357        self.browser.open(self.payments_path + '/xyz')
358        self.browser.getLink("CollegePAY", index=0).click()
359        self.assertTrue('<input type="hidden" name="pay_item_id" value="5702" />'
360                           in self.browser.contents)
361        self.assertTrue('Total Amount Authorized:'
362                           in self.browser.contents)
363        self.assertEqual(self.student.current_mode, 'ug_ft')
364        self.assertTrue(
365            '<input type="hidden" name="amount" value="87600" />'
366            in self.browser.contents)
367        delta = timedelta(days=8)
368        acc_payment.creation_date -= delta
369        self.browser.open(self.payments_path + '/xyz')
370        self.browser.getLink("CollegePAY", index=0).click()
371        self.assertMatches(
372            '...This payment ticket is too old. Please create a new ticket...',
373            self.browser.contents)
374        delta = timedelta(days=2)
375        acc_payment.creation_date += delta
376        self.browser.open(self.payments_path + '/xyz')
377        self.browser.getLink("CollegePAY", index=0).click()
378        self.assertMatches('...Total Amount Authorized:...',
379                           self.browser.contents)
380
381    def test_pay_twice(self):
382        # Create second ticket
383        self.browser.open(self.payments_path + '/addop')
384        self.browser.getControl(name="form.p_category").value = ['schoolfee']
385        self.browser.getControl("Create ticket").click()
386        # Pay first ticket
387        self.student['payments'][self.value].approve()
388        # Try to pay second ticket
389        self.browser.open(self.payments_path)
390        ctrl = self.browser.getControl(name='val_id')
391        value = ctrl.options[1]
392        self.browser.getLink(value).click()
393        self.browser.getLink("CollegePAY", index=0).click()
394        self.assertMatches(
395            '...alert alert-danger">This type of payment has already been made...',
396            self.browser.contents)
397
398    @external_test
399    def test_webservice(self):
400        # First we have open InterswitchPageStudent to set provider_amt
401        # and gateway_amt
402        self.browser.open(self.payment_url + '/goto_interswitch')
403        # Now we can call the webservice
404        self.browser.open(self.payment_url + '/request_webservice')
405        self.assertMatches('...Unsuccessful callback...',
406                          self.browser.contents)
407        # The payment is now in state failed ...
408        self.assertMatches('...<span>Failed</span>...',
409                          self.browser.contents)
410        # ... and the catalog has been updated
411        cat = getUtility(ICatalog, name='payments_catalog')
412        results = list(
413            cat.searchResults(p_state=('failed', 'failed')))
414        self.assertEqual(len(results), 1)
415        self.assertEqual(results[0].p_state, 'failed')
416
417        # Let's replace the p_id with a valid p_id of the Uniben
418        # live system. This is definitely not an appropriate
419        # solution for testing, but we have no choice since
420        # Interswitch doesn't provide any interface
421        # for testing.
422        payment = self.student['payments'][self.value]
423        payment.p_id = 'p3547789850240'
424        self.browser.open(self.payment_url + '/request_webservice')
425        self.assertMatches('...Callback amount does not match...',
426                          self.browser.contents)
427        # The payment is now in state failed ...
428        self.assertMatches('...<span>Failed</span>...',
429                          self.browser.contents)
430        # Let's replace the amount autorized with the amount of the
431        # live system payment
432        payment.amount_auth = payment.r_amount_approved
433        self.browser.open(self.payment_url + '/request_webservice')
434        self.assertMatches('...Successful payment...',
435                          self.browser.contents)
436        # The payment is now in state paid ...
437        self.assertMatches('...<span>Paid</span>...',
438                          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=('paid', 'paid')))
443        self.assertEqual(len(results), 1)
444        self.assertEqual(results[0].p_state, 'paid')
445        # Approval is logged in students.log ...
446        logfile = os.path.join(
447            self.app['datacenter'].storage, 'logs', 'students.log')
448        logcontent = open(logfile).read()
449        self.assertTrue(
450            'zope.mgr - '
451            'waeup.uniben.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
452            'B1000000 - successful schoolfee payment: p3547789850240\n'
453            in logcontent)
454        # ... and in payments.log
455        logfile = os.path.join(
456            self.app['datacenter'].storage, 'logs', 'payments.log')
457        logcontent = open(logfile).read()
458        self.assertTrue(
459            '"zope.mgr",B1000000,p3547789850240,schoolfee,'
460            '12000.0,00,1500.0,150.0,0.0,,,\n'
461            in logcontent)
462
463
464class InterswitchTestsApplicants(ApplicantsFullSetup):
465    """Tests for the Interswitch payment gateway.
466    """
467
468    layer = FunctionalLayer
469
470    def setUp(self):
471        super(InterswitchTestsApplicants, self).setUp()
472        configuration = CustomSessionConfiguration()
473        configuration.academic_session = datetime.now().year - 2
474        self.applicantscontainer.application_fee = 1000.0
475        self.app['configuration'].addSessionConfiguration(configuration)
476        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
477        self.browser.open(self.manage_path)
478        #IWorkflowState(self.student).setState('started')
479        super(InterswitchTestsApplicants, self).fill_correct_values()
480        self.browser.getControl("Add online").click()
481        self.assertMatches('...passport photo before making payment...',
482                           self.browser.contents)
483        self.browser.open(self.manage_path)
484        super(InterswitchTestsApplicants, self).fill_correct_values()
485        #self.browser.getControl(name="form.nationality").value = ['NG']
486        #self.browser.getControl(name="transition").value = ['start']
487        image = open(SAMPLE_IMAGE, 'rb')
488        ctrl = self.browser.getControl(name='form.passport')
489        file_ctrl = ctrl.mech_control
490        file_ctrl.add_file(image, filename='myphoto.jpg')
491        self.browser.getControl("Save").click()
492        self.browser.getControl("Add online").click()
493        self.assertMatches('...ticket created...',
494                           self.browser.contents)
495        #ctrl = self.browser.getControl(name='val_id')
496        #value = ctrl.options[0]
497        #self.browser.getLink(value).click()
498        self.payment_url = self.browser.url
499
500    def test_interswitch_form(self):
501        self.assertMatches('...Amount Authorized...',
502                           self.browser.contents)
503        self.assertTrue(
504            '<span>1000.0</span>' in self.browser.contents)
505        # Manager can access InterswitchForm
506        self.browser.getLink("CollegePAY", index=0).click()
507        self.assertMatches('...Total Amount Authorized:...',
508                           self.browser.contents)
509        self.assertTrue(
510            '<input type="hidden" name="amount" value="100000" />'
511            in self.browser.contents)
512        delta = timedelta(days=8)
513        self.applicant.values()[0].creation_date -= delta
514        self.browser.open(self.payment_url)
515        self.browser.getLink("CollegePAY", index=0).click()
516        self.assertMatches(
517            '...This payment ticket is too old. Please create a new ticket...',
518            self.browser.contents)
519
520    @external_test
521    def test_webservice(self):
522
523        self.browser.open(self.payment_url + '/request_webservice')
524        self.assertMatches('...Unsuccessful callback...',
525                          self.browser.contents)
526        # The payment is now in state failed
527        self.assertMatches('...<span>Failed</span>...',
528                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.