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

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

revert to the old Interswitch surcharge.

  • Property svn:keywords set to Id
File size: 18.5 KB
Line 
1## $Id: tests.py 13438 2015-11-11 19:49:12Z 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_incl']
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 Plus" 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_incl']
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 Plus" item_amt="4280000" bank_id="117" '
248            'acct_num="1010827641"' in self.browser.contents)
249
250    def test_interswitch_form_concession_fees(self):
251        self.certificate.study_mode = 'ug_pt'
252        self.app['configuration']['2004'].concession_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 = ['concessional']
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="107" />'
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="Concessional Fee" item_amt="131700" '
279            'bank_id="117" acct_num="1010835352"'
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_session = 2004
288        acc_payment.p_category = 'clearance'
289        acc_payment.p_id = 'xyz'
290        acc_payment.pay_item_id = '123'
291        acc_payment.amount_auth = 876.0
292        self.student['payments']['xyz'] = acc_payment
293        self.browser.open(self.payments_path + '/xyz')
294        self.browser.getLink("CollegePAY", index=0).click()
295        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />'
296                        in self.browser.contents)
297        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
298        self.assertEqual(self.student.current_mode, 'ug_pt')
299        self.assertTrue(
300            '<input type="hidden" name="amount" value="87600" />'
301            in self.browser.contents)
302        delta = timedelta(days=8)
303        acc_payment.creation_date -= delta
304        self.browser.open(self.payments_path + '/xyz')
305        self.browser.getLink("CollegePAY", index=0).click()
306        self.assertTrue(
307            'This payment ticket is too old. Please create a new ticket'
308            in self.browser.contents)
309        delta = timedelta(days=2)
310        acc_payment.creation_date += delta
311        self.browser.open(self.payments_path + '/xyz')
312        self.browser.getLink("CollegePAY", index=0).click()
313        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
314
315    @external_test
316    def test_webservice(self):
317        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
318        self.browser.open(self.payments_path)
319        IWorkflowState(self.student).setState('cleared')
320        self.student.nationality = u'NG'
321        self.browser.open(self.payments_path + '/addop')
322        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
323        self.browser.getControl("Create ticket").click()
324        self.assertTrue('ticket created' in self.browser.contents)
325        ctrl = self.browser.getControl(name='val_id')
326        self.value = ctrl.options[0]
327        self.browser.getLink(self.value).click()
328        self.payment_url = self.browser.url
329        # First we have to open InterswitchPageStudent to set provider_amt
330        # and gateway_amt
331        self.browser.open(self.payment_url + '/goto_interswitch')
332        # Now we can call the webservice
333        self.browser.open(self.payment_url + '/request_webservice')
334        self.assertTrue('Unsuccessful callback' in self.browser.contents)
335        # The payment is now in state failed ...
336        self.assertTrue('<span>Failed</span>' in self.browser.contents)
337        # ... and the catalog has been updated
338        cat = getUtility(ICatalog, name='payments_catalog')
339        results = list(
340            cat.searchResults(p_state=('failed', 'failed')))
341        self.assertEqual(len(results), 1)
342        self.assertEqual(results[0].p_state, 'failed')
343
344        # Let's replace the p_id with a valid p_id of the AAUE
345        # live system. This is definitely not an appropriate
346        # solution for testing, but we have no choice since
347        # Interswitch doesn't provide any interface
348        # for testing.
349        payment = self.student['payments'][self.value]
350        payment.p_id = 'p4465649308559'
351        self.browser.open(self.payment_url + '/request_webservice')
352        self.assertTrue('Unsuccessful callback: Amount Inconsistency' in
353                          self.browser.contents)
354        # The payment is now in state failed ...
355        self.assertTrue('<span>Failed</span>' in self.browser.contents)
356        # Let's replace the amount autorized with the amount of the
357        # live system payment
358        payment.amount_auth = 60250.0
359        self.browser.open(self.payment_url + '/request_webservice')
360        self.assertTrue('Successful payment' in
361                          self.browser.contents)
362        # The payment is now in state paid ...
363        self.assertTrue('<span>Paid</span>' in self.browser.contents)
364        # ... and the catalog has been updated
365        cat = getUtility(ICatalog, name='payments_catalog')
366        results = list(
367            cat.searchResults(p_state=('paid', 'paid')))
368        self.assertEqual(len(results), 1)
369        self.assertEqual(results[0].p_state, 'paid')
370        # Approval is logged in students.log ...
371        logfile = os.path.join(
372            self.app['datacenter'].storage, 'logs', 'students.log')
373        logcontent = open(logfile).read()
374        self.assertTrue(
375            'zope.mgr - '
376            'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
377            'E1000000 - successful schoolfee payment: p4465649308559\n'
378            in logcontent)
379        # ... and in payments.log
380        logfile = os.path.join(
381            self.app['datacenter'].storage, 'logs', 'payments.log')
382        logcontent = open(logfile).read()
383        self.assertTrue(
384            '"zope.mgr",E1000000,p4465649308559,schoolfee,'
385            '60250.0,00,1500.0,250.0,0.0,,,\n'
386            in logcontent)
Note: See TracBrowser for help on using the repository browser.