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

Last change on this file since 13406 was 13406, checked in by Henrik Bettermann, 9 years ago

Prepare function for computing the gateway amount dynamically (will be modified on next Monday).

Transaction fee is already embedded in each of the fee items and must this be substracted.

  • Property svn:keywords set to Id
File size: 18.4 KB
Line 
1## $Id: tests.py 13406 2015-11-07 12:05:48Z 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 = 1000.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        self.browser.getControl("Add online").click()
66        self.assertTrue('ticket created' in self.browser.contents)
67        #self.browser.getLink(value).click()
68        self.assertTrue('Amount Authorized' in self.browser.contents)
69        self.assertTrue('<span>1000.0</span>' in self.browser.contents)
70        self.payment_url = self.browser.url
71
72    def test_interswitch_form(self):
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="100000" />'
78            in self.browser.contents)
79        delta = timedelta(days=8)
80        self.applicant.values()[0].creation_date -= delta
81        self.browser.open(self.payment_url)
82        self.browser.getLink("CollegePAY", index=0).click()
83        self.assertTrue(
84            'This payment ticket is too old. Please create a new ticket'
85            in self.browser.contents)
86
87    @external_test
88    def test_webservice(self):
89
90        self.browser.open(self.payment_url + '/request_webservice')
91        self.assertTrue('Unsuccessful callback' in self.browser.contents)
92        # The payment is now in state failed
93        self.assertTrue('<span>Failed</span>' in self.browser.contents)
94
95class InterswitchTestsStudents(StudentsFullSetup):
96    """Tests for the Interswitch payment gateway.
97    """
98
99    layer = FunctionalLayer
100
101    def setUp(self):
102        super(InterswitchTestsStudents, self).setUp()
103
104    def test_interswitch_form_pt_school_fees(self):
105        self.certificate.study_mode = 'ug_pt'
106        self.student['studycourse'].entry_session = 2013
107        self.student['studycourse'].certificate.school_fee_1 = 50250.0
108        self.app['configuration']['2004'].union_fee = 1000.0
109        self.app['configuration']['2004'].welfare_fee = 500.0
110        # Manager can access InterswitchForm
111        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
112        self.browser.open(self.payments_path)
113        IWorkflowState(self.student).setState('cleared')
114        self.student.nationality = u'NG'
115        self.browser.open(self.payments_path + '/addop')
116        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
117        self.browser.getControl("Create ticket").click()
118        self.assertTrue('ticket created' in self.browser.contents)
119        ctrl = self.browser.getControl(name='val_id')
120        self.value = ctrl.options[0]
121        self.browser.getLink(self.value).click()
122        self.assertTrue('Amount Authorized' in self.browser.contents)
123        # 50250 + 1000 + 500 - 500
124        self.assertTrue('<span>51250.0</span>' in self.browser.contents)
125        self.payment_url = self.browser.url
126        self.browser.getLink("CollegePAY", index=0).click()
127        self.assertTrue(
128            'Please pay acceptance fee first' in self.browser.contents)
129        acc_payment = createObject('waeup.StudentOnlinePayment')
130        acc_payment.p_state = 'paid'
131        acc_payment.p_category = 'clearance'
132        self.student['payments']['xyz'] = acc_payment
133        self.browser.getLink("CollegePAY", index=0).click()
134        self.assertTrue('<input type="hidden" name="pay_item_id" value="105" />'
135                        in self.browser.contents)
136        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
137        self.assertEqual(self.student.current_mode, 'ug_pt')
138        self.assertTrue(
139            '<input type="hidden" name="amount" value="5125000" />'
140            in self.browser.contents)
141        self.assertTrue(
142            'item_name="School Fee plus" '
143            'item_amt="4600000" bank_id="7" '
144            'acct_num="1014847058"' in self.browser.contents)
145
146    def test_interswitch_form_regular_school_fees(self):
147        self.certificate.study_mode = 'ug_ft'
148        self.student['studycourse'].entry_session = 2013
149        self.student['studycourse'].certificate.school_fee_1 = 51750.0
150        # Manager can access InterswitchForm
151        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
152        self.browser.open(self.payments_path)
153        IWorkflowState(self.student).setState('cleared')
154        self.student.nationality = u'NG'
155        self.browser.open(self.payments_path + '/addop')
156        self.browser.getControl(name="form.p_category").value = ['schoolfee']
157        self.browser.getControl("Create ticket").click()
158        self.assertTrue('ticket created' in
159                           self.browser.contents)
160        ctrl = self.browser.getControl(name='val_id')
161        self.value = ctrl.options[0]
162        self.browser.getLink(self.value).click()
163        self.assertTrue('Amount Authorized' in
164                           self.browser.contents)
165        self.assertTrue('<span>51750.0</span>' in self.browser.contents)
166        self.payment_url = self.browser.url
167        self.browser.getLink("CollegePAY", index=0).click()
168        self.assertTrue(
169            'Please pay acceptance fee first' in self.browser.contents)
170        acc_payment = createObject('waeup.StudentOnlinePayment')
171        acc_payment.p_state = 'paid'
172        acc_payment.p_category = 'clearance'
173        self.student['payments']['xyz'] = acc_payment
174        self.browser.getLink("CollegePAY", index=0).click()
175        self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />'
176                        in self.browser.contents)
177        self.assertTrue('Total Amount Authorized:'
178                        in self.browser.contents)
179        self.assertEqual(self.student.current_mode, 'ug_ft')
180        self.assertTrue(
181            '<input type="hidden" name="amount" value="5175000" />'
182            in self.browser.contents)
183        self.assertTrue(
184            'item_name="School Fee" item_amt="4750000" bank_id="117" '
185            'acct_num="1010827641"' in self.browser.contents)
186
187
188    def test_interswitch_form_pt_acceptance_fees(self):
189        self.certificate.study_mode = 'ug_pt'
190        self.app['configuration']['2004'].clearance_fee_pt = 40250.0
191        self.app['configuration']['2004'].matric_gown_fee = 2000.0
192        self.app['configuration']['2004'].lapel_fee = 800.0
193        # Manager can access InterswitchForm
194        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
195        self.browser.open(self.payments_path)
196        IWorkflowState(self.student).setState('admitted')
197        self.student.nationality = u'NG'
198        self.browser.open(self.payments_path + '/addop')
199        self.browser.getControl(name="form.p_category").value = ['clearance_incl']
200        self.browser.getControl("Create ticket").click()
201        self.assertTrue('ticket created' in  self.browser.contents)
202        ctrl = self.browser.getControl(name='val_id')
203        self.value = ctrl.options[0]
204        self.browser.getLink(self.value).click()
205        self.assertTrue('Amount Authorized' in self.browser.contents)
206        # 40250 + 2000 + 800 -500
207        self.assertTrue('<span>42550.0</span>' in self.browser.contents)
208        self.payment_url = self.browser.url
209        self.browser.getLink("CollegePAY", index=0).click()
210        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />' in
211                           self.browser.contents)
212        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
213        self.assertTrue(
214            '<input type="hidden" name="amount" value="4255000" />'
215            in self.browser.contents)
216        self.assertTrue(
217            'item_name="Acceptance Fee plus" '
218            'item_amt="4000000" bank_id="117" '
219            'acct_num="1014066976"' in self.browser.contents)
220
221    def test_interswitch_form_regular_acceptance_fees(self):
222        self.certificate.study_mode = 'ug_ft'
223        self.app['configuration']['2004'].clearance_fee = 43050.0
224        # Manager can access InterswitchForm
225        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
226        self.browser.open(self.payments_path)
227        IWorkflowState(self.student).setState('admitted')
228        self.student.nationality = u'NG'
229        self.browser.open(self.payments_path + '/addop')
230        self.browser.getControl(name="form.p_category").value = ['clearance']
231        self.browser.getControl("Create ticket").click()
232        self.assertTrue('ticket created' in self.browser.contents)
233        ctrl = self.browser.getControl(name='val_id')
234        self.value = ctrl.options[0]
235        self.browser.getLink(self.value).click()
236        self.assertTrue('Amount Authorized' in self.browser.contents)
237        self.assertTrue('<span>43050.0</span>' in self.browser.contents)
238        self.payment_url = self.browser.url
239        self.browser.getLink("CollegePAY", index=0).click()
240        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
241                        in self.browser.contents)
242        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
243        self.assertTrue(
244            '<input type="hidden" name="amount" value="4305000" />'
245            in self.browser.contents)
246        self.assertTrue(
247            'item_name="Acceptance Fee" item_amt="4280000" bank_id="117" '
248            'acct_num="1010827641"' in self.browser.contents)
249
250    def test_interswitch_form_welfare_fees(self):
251        self.certificate.study_mode = 'ug_pt'
252        self.app['configuration']['2004'].welfare_fee = 1567.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('admitted')
257        self.student.nationality = u'NG'
258        self.browser.open(self.payments_path + '/addop')
259        self.browser.getControl(name="form.p_category").value = ['welfare']
260        self.browser.getControl("Create ticket").click()
261        self.assertTrue('ticket created' in  self.browser.contents)
262        ctrl = self.browser.getControl(name='val_id')
263        self.value = ctrl.options[0]
264        self.browser.getLink(self.value).click()
265        self.assertTrue('Amount Authorized' in self.browser.contents)
266        self.assertTrue(
267            '<span>1567.0</span>' in self.browser.contents)
268        self.payment_url = self.browser.url
269        self.certificate.study_mode = 'ug_ft'
270        self.browser.getLink("CollegePAY", index=0).click()
271        self.assertTrue('<input type="hidden" name="pay_item_id" value="105" />'
272                           in self.browser.contents)
273        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
274        self.assertTrue(
275            '<input type="hidden" name="amount" value="156700" />'
276            in self.browser.contents)
277        self.assertTrue(
278            'item_name="Student Welfare Assurance Fee" item_amt="131700" '
279            'bank_id="123" acct_num="1006407792"'
280            in self.browser.contents)
281
282    def test_interswitch_form_ticket_expired(self):
283        self.certificate.study_mode = 'ug_pt'
284        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
285        acc_payment = createObject('waeup.StudentOnlinePayment')
286        acc_payment.p_state = 'unpaid'
287        acc_payment.p_category = 'clearance'
288        acc_payment.p_id = 'xyz'
289        acc_payment.pay_item_id = '123'
290        acc_payment.amount_auth = 876.0
291        self.student['payments']['xyz'] = acc_payment
292        self.browser.open(self.payments_path + '/xyz')
293        self.browser.getLink("CollegePAY", index=0).click()
294        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />'
295                        in self.browser.contents)
296        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
297        self.assertEqual(self.student.current_mode, 'ug_pt')
298        self.assertTrue(
299            '<input type="hidden" name="amount" value="87600" />'
300            in self.browser.contents)
301        delta = timedelta(days=8)
302        acc_payment.creation_date -= delta
303        self.browser.open(self.payments_path + '/xyz')
304        self.browser.getLink("CollegePAY", index=0).click()
305        self.assertTrue(
306            'This payment ticket is too old. Please create a new ticket'
307            in self.browser.contents)
308        delta = timedelta(days=2)
309        acc_payment.creation_date += delta
310        self.browser.open(self.payments_path + '/xyz')
311        self.browser.getLink("CollegePAY", index=0).click()
312        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
313
314    @external_test
315    def test_webservice(self):
316        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
317        self.browser.open(self.payments_path)
318        IWorkflowState(self.student).setState('cleared')
319        self.student.nationality = u'NG'
320        self.browser.open(self.payments_path + '/addop')
321        self.browser.getControl(name="form.p_category").value = ['schoolfee']
322        self.browser.getControl("Create ticket").click()
323        self.assertTrue('ticket created' in self.browser.contents)
324        ctrl = self.browser.getControl(name='val_id')
325        self.value = ctrl.options[0]
326        self.browser.getLink(self.value).click()
327        self.payment_url = self.browser.url
328        # First we have to open InterswitchPageStudent to set provider_amt
329        # and gateway_amt
330        self.browser.open(self.payment_url + '/goto_interswitch')
331        # Now we can call the webservice
332        self.browser.open(self.payment_url + '/request_webservice')
333        self.assertTrue('Unsuccessful callback' in self.browser.contents)
334        # The payment is now in state failed ...
335        self.assertTrue('<span>Failed</span>' in self.browser.contents)
336        # ... and the catalog has been updated
337        cat = getUtility(ICatalog, name='payments_catalog')
338        results = list(
339            cat.searchResults(p_state=('failed', 'failed')))
340        self.assertEqual(len(results), 1)
341        self.assertEqual(results[0].p_state, 'failed')
342
343        # Let's replace the p_id with a valid p_id of the AAUE
344        # live system. This is definitely not an appropriate
345        # solution for testing, but we have no choice since
346        # Interswitch doesn't provide any interface
347        # for testing.
348        payment = self.student['payments'][self.value]
349        payment.p_id = 'p4465649308559'
350        self.browser.open(self.payment_url + '/request_webservice')
351        self.assertTrue('Unsuccessful callback: Amount Inconsistency' in
352                          self.browser.contents)
353        # The payment is now in state failed ...
354        self.assertTrue('<span>Failed</span>' in self.browser.contents)
355        # Let's replace the amount autorized with the amount of the
356        # live system payment
357        payment.amount_auth = 60250.0
358        self.browser.open(self.payment_url + '/request_webservice')
359        self.assertTrue('Successful payment' in
360                          self.browser.contents)
361        # The payment is now in state paid ...
362        self.assertTrue('<span>Paid</span>' in self.browser.contents)
363        # ... and the catalog has been updated
364        cat = getUtility(ICatalog, name='payments_catalog')
365        results = list(
366            cat.searchResults(p_state=('paid', 'paid')))
367        self.assertEqual(len(results), 1)
368        self.assertEqual(results[0].p_state, 'paid')
369        # Approval is logged in students.log ...
370        logfile = os.path.join(
371            self.app['datacenter'].storage, 'logs', 'students.log')
372        logcontent = open(logfile).read()
373        self.assertTrue(
374            'zope.mgr - '
375            'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
376            'E1000000 - successful schoolfee payment: p4465649308559\n'
377            in logcontent)
378        # ... and in payments.log
379        logfile = os.path.join(
380            self.app['datacenter'].storage, 'logs', 'payments.log')
381        logcontent = open(logfile).read()
382        self.assertTrue(
383            '"zope.mgr",E1000000,p4465649308559,schoolfee,'
384            '60250.0,00,1500.0,250.0,0.0,,,\n'
385            in logcontent)
Note: See TracBrowser for help on using the repository browser.