source: main/kofacustom.dspg/trunk/src/kofacustom/dspg/interswitch/tests.py @ 14932

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

Implement "Departmental and SUG dues" payment (not yet required).

Catch traceback when transferring students.

  • Property svn:keywords set to Id
File size: 17.6 KB
Line 
1## $Id: tests.py 14921 2017-12-07 16:58:30Z 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.kofa.university.faculty import Faculty
27from waeup.kofa.university.department import Department
28from kofacustom.dspg.testing import FunctionalLayer
29
30# Also run tests that send requests to external servers?
31#   If you enable this, please make sure the external services
32#   do exist really and are not bothered by being spammed by a test programme.
33EXTERNAL_TESTS = False
34
35SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
36
37def external_test(func):
38    if not EXTERNAL_TESTS:
39        myself = __file__
40        if myself.endswith('.pyc'):
41            myself = myself[:-2]
42        print "WARNING: external tests are skipped!"
43        print "WARNING: edit %s to enable them." % myself
44        return
45    return func
46
47
48class InterswitchTestsStudents(StudentsFullSetup):
49    """Tests for the Interswitch payment gateway.
50    """
51
52    layer = FunctionalLayer
53
54    def setUp(self):
55        super(InterswitchTestsStudents, self).setUp()
56
57    def test_interswitch_form(self):
58        # Manager can access InterswitchForm
59        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
60        self.browser.open(self.payments_path)
61        IWorkflowState(self.student).setState('cleared')
62        self.student.nationality = u'NG'
63        # ND FT Non-Deltan Fresh Student School Fee
64        self.certificate.study_mode = 'nd_ft'
65        self.certificate.school_fee_3 = 30000.0
66        self.browser.open(self.payments_path + '/addop')
67        self.browser.getControl(name="form.p_category").value = ['schoolfee']
68        self.browser.getControl("Create ticket").click()
69        self.assertMatches('...ticket created...',
70                           self.browser.contents)
71        ctrl = self.browser.getControl(name='val_id')
72        self.value = ctrl.options[0]
73        self.browser.getLink(self.value).click()
74        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
75        self.payment_url = self.browser.url
76        self.browser.getLink("CollegePAY", index=0).click()
77        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
78            in self.browser.contents)
79        self.assertTrue(
80            '<input type="hidden" name="amount" value="3000000" />' in
81            self.browser.contents)
82        self.assertTrue(
83            'item_name="School Fee" item_amt="2695000" bank_id="31" acct_num="0026235493"' in
84            self.browser.contents)
85        # ND FT Non-Deltan Fresh Student Acceptance Fee
86        self.app['configuration']['2004'].clearance_fee = 12345.0
87        self.browser.open(self.payments_path + '/addop')
88        self.browser.getControl(name="form.p_category").value = ['clearance']
89        self.browser.getControl("Create ticket").click()
90        self.assertMatches('...ticket created...',
91                           self.browser.contents)
92        ctrl = self.browser.getControl(name='val_id')
93        self.value = ctrl.options[1]
94        self.browser.getLink(self.value).click()
95        self.assertMatches('...Amount Authorized...',
96                           self.browser.contents)
97        self.assertMatches(
98            '...<span>12345.0</span>...',
99            self.browser.contents)
100        self.payment_url = self.browser.url
101        self.browser.getLink("CollegePAY", index=0).click()
102        self.assertTrue('<input type="hidden" name="pay_item_id" value="103" />' in
103                           self.browser.contents)
104        self.assertTrue(
105            '<input type="hidden" name="amount" value="1234500" />' in
106            self.browser.contents)
107        self.assertTrue(
108            'item_name="Acceptance" item_amt="1159500" bank_id="8" acct_num="2004402644"' in
109            self.browser.contents)
110
111    def test_interswitch_form_ticket_expired(self):
112        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
113        acc_payment = createObject('waeup.StudentOnlinePayment')
114        acc_payment.p_state = 'unpaid'
115        acc_payment.p_category = 'clearance'
116        acc_payment.p_id = 'xyz'
117        acc_payment.pay_item_id = '123'
118        acc_payment.amount_auth = 876.0
119        self.student['payments']['xyz'] = acc_payment
120        self.browser.open(self.payments_path + '/xyz')
121        self.browser.getLink("CollegePAY", index=0).click()
122        self.assertMatches('...<input type="hidden" name="pay_item_id" value="103" />...',
123                           self.browser.contents)
124        self.assertMatches('...Total Amount Authorized:...',
125                           self.browser.contents)
126        self.assertEqual(self.student.current_mode, 'ug_ft')
127        self.assertMatches(
128            '...<input type="hidden" name="amount" value="87600" />...',
129            self.browser.contents)
130        delta = timedelta(days=8)
131        acc_payment.creation_date -= delta
132        self.browser.open(self.payments_path + '/xyz')
133        self.browser.getLink("CollegePAY", index=0).click()
134        self.assertMatches(
135            '...This payment ticket is too old. Please create a new ticket...',
136            self.browser.contents)
137        delta = timedelta(days=2)
138        acc_payment.creation_date += delta
139        self.browser.open(self.payments_path + '/xyz')
140        self.browser.getLink("CollegePAY", index=0).click()
141        self.assertMatches('...Total Amount Authorized:...',
142                           self.browser.contents)
143
144    def test_interswitch_form_pt_payments(self):
145        # Add SPAT certificate
146        certificate = createObject('waeup.Certificate')
147        certificate.code = u'CERT2'
148        certificate.application_category = 'basic' # can be anything
149        certificate.start_level = 100
150        certificate.end_level = 500
151        certificate.study_mode = u'ug_ft' # can be anything
152        self.app['faculties']['SPAT'] = Faculty(code=u'SPAT')
153        self.app['faculties']['SPAT']['dep2'] = Department(code=u'dep2')
154        department = self.app['faculties']['SPAT']['dep2']
155        self.app['faculties']['SPAT']['dep2'].certificates.addCertificate(
156            certificate)
157        certificate.school_fee_3 = 30000.0
158        self.student['studycourse'].certificate = certificate
159        # Manager can access InterswitchForm
160        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
161        self.browser.open(self.payments_path)
162        IWorkflowState(self.student).setState('cleared')
163        self.student.nationality = u'NG'
164        # ND PT Non-Deltan Fresh Student School Fee
165        self.certificate.study_mode = 'nd_ft'
166        self.certificate.school_fee_3 = 30000.0
167        self.browser.open(self.payments_path + '/addop')
168        self.browser.getControl(name="form.p_category").value = ['schoolfee']
169        self.browser.getControl("Create ticket").click()
170        self.assertMatches('...ticket created...', self.browser.contents)
171        ctrl = self.browser.getControl(name='val_id')
172        self.value = ctrl.options[0]
173        self.browser.getLink(self.value).click()
174        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
175        self.payment_url = self.browser.url
176        self.browser.getLink("CollegePAY", index=0).click()
177        self.assertTrue('<item_detail item_id="1" item_name="School Fee 1" item_amt="1617000" bank_id="117" acct_num="1012808851" />'
178            in self.browser.contents)
179        self.assertTrue('<item_detail item_id="2" item_name="School Fee 2" item_amt="1078000" bank_id="123" acct_num="1002883141" />'
180            in self.browser.contents)
181        self.assertTrue('<item_detail item_id="3" item_name="BT Education" item_amt="280000" bank_id="8" acct_num="2028964403" />'
182            in self.browser.contents)
183
184    def test_interswitch_form_dep_sug_payments(self):
185        # Manager can access InterswitchForm
186        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
187        self.browser.open(self.payments_path)
188        IWorkflowState(self.student).setState('cleared')
189        self.browser.open(self.payments_path + '/addop')
190        self.browser.getControl(name="form.p_category").value = ['dep_sug']
191        self.browser.getControl("Create ticket").click()
192        self.assertMatches('...ticket created...', self.browser.contents)
193        ctrl = self.browser.getControl(name='val_id')
194        self.value = ctrl.options[0]
195        self.browser.getLink(self.value).click()
196        self.assertTrue('<span>2900.0</span>'  in self.browser.contents)
197        self.payment_url = self.browser.url
198        self.browser.getLink("CollegePAY", index=0).click()
199        self.assertTrue('<item_detail item_id="1" item_name="SUG" item_amt="75000" bank_id="11" acct_num="0038079930" />'
200            in self.browser.contents)
201        self.assertTrue('<item_detail item_id="2" item_name="Students Welfare" item_amt="50000" bank_id="11" acct_num="0037892949" />'
202            in self.browser.contents)
203        self.assertTrue('<item_detail item_id="3" item_name="Anti-Cult Book" item_amt="90000" bank_id="11" acct_num="0037892949" />'
204            in self.browser.contents)
205        self.assertTrue('<item_detail item_id="4" item_name="NADESSTU " item_amt="50000" bank_id="11" acct_num="0036375968" />'
206            in self.browser.contents)
207
208    @external_test
209    def test_webservice(self):
210        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
211        self.browser.open(self.payments_path)
212        IWorkflowState(self.student).setState('cleared')
213        self.student.nationality = u'NG'
214        self.browser.open(self.payments_path + '/addop')
215        self.browser.getControl(name="form.p_category").value = ['schoolfee']
216        self.browser.getControl("Create ticket").click()
217        self.assertMatches('...ticket created...',
218                           self.browser.contents)
219        ctrl = self.browser.getControl(name='val_id')
220        self.value = ctrl.options[0]
221        self.browser.getLink(self.value).click()
222        self.payment_url = self.browser.url
223        # First we have open InterswitchPageStudent to set provider_amt
224        # and gateway_amt
225        self.browser.open(self.payment_url + '/goto_interswitch')
226        # Now we can call the webservice
227        self.browser.open(self.payment_url + '/request_webservice')
228        self.assertMatches('...Unsuccessful callback...',
229                          self.browser.contents)
230        # The payment is now in state failed ...
231        self.assertMatches('...<span>Failed</span>...',
232                          self.browser.contents)
233        # ... and the catalog has been updated
234        cat = getUtility(ICatalog, name='payments_catalog')
235        results = list(
236            cat.searchResults(p_state=('failed', 'failed')))
237        self.assertEqual(len(results), 1)
238        self.assertEqual(results[0].p_state, 'failed')
239
240        # Let's replace the p_id with a valid p_id of the Uniben
241        # live system. This is definitely not an appropriate
242        # solution for testing, but we have no choice since
243        # Interswitch doesn't provide any interface
244        # for testing.
245        payment = self.student['payments'][self.value]
246        payment.p_id = 'p3547789850240'
247        self.browser.open(self.payment_url + '/request_webservice')
248        self.assertMatches('...Callback amount does not match...',
249                          self.browser.contents)
250        # The payment is now in state failed ...
251        self.assertMatches('...<span>Failed</span>...',
252                          self.browser.contents)
253        # Let's replace the amount autorized with the amount of the
254        # live system payment
255        payment.amount_auth = payment.r_amount_approved
256        self.browser.open(self.payment_url + '/request_webservice')
257        self.assertMatches('...Successful payment...',
258                          self.browser.contents)
259        # The payment is now in state paid ...
260        self.assertMatches('...<span>Paid</span>...',
261                          self.browser.contents)
262        # ... and the catalog has been updated
263        cat = getUtility(ICatalog, name='payments_catalog')
264        results = list(
265            cat.searchResults(p_state=('paid', 'paid')))
266        self.assertEqual(len(results), 1)
267        self.assertEqual(results[0].p_state, 'paid')
268        # Approval is logged in students.log ...
269        logfile = os.path.join(
270            self.app['datacenter'].storage, 'logs', 'students.log')
271        logcontent = open(logfile).read()
272        self.assertTrue(
273            'zope.mgr - '
274            'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
275            'X1000000 - successful schoolfee payment: p3547789850240\n'
276            in logcontent)
277        # ... and in payments.log
278        logfile = os.path.join(
279            self.app['datacenter'].storage, 'logs', 'payments.log')
280        logcontent = open(logfile).read()
281        self.assertTrue(
282            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
283            '12000.0,00,0.0,150.0,0.0,,,\n'
284            in logcontent)
285
286
287class InterswitchTestsApplicants(ApplicantsFullSetup):
288    """Tests for the Interswitch payment gateway.
289    """
290
291    layer = FunctionalLayer
292
293    def setUp(self):
294        super(InterswitchTestsApplicants, self).setUp()
295        configuration = SessionConfiguration()
296        configuration.academic_session = datetime.now().year - 2
297        self.app['configuration'].addSessionConfiguration(configuration)
298        self.configuration = configuration
299        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
300        self.browser.open(self.manage_path)
301        #IWorkflowState(self.student).setState('started')
302        super(InterswitchTestsApplicants, self).fill_correct_values()
303        self.applicantscontainer.application_fee = 1000.0
304        self.browser.getControl(name="form.nationality").value = ['NG']
305        self.browser.getControl(name="transition").value = ['start']
306        self.browser.getControl("Save").click()
307
308
309    def test_interswitch_form(self):
310        self.browser.getControl("Add online").click()
311        self.assertMatches('...passport photo before making payment...',
312                           self.browser.contents)
313        self.browser.open(self.manage_path)
314        super(InterswitchTestsApplicants, self).fill_correct_values()
315        self.browser.getControl(name="form.nationality").value = ['NG']
316        #self.browser.getControl(name="transition").value = ['start']
317        image = open(SAMPLE_IMAGE, 'rb')
318        ctrl = self.browser.getControl(name='form.passport')
319        file_ctrl = ctrl.mech_control
320        file_ctrl.add_file(image, filename='myphoto.jpg')
321        self.browser.getControl("Save").click()
322        self.browser.getControl("Add online").click()
323        self.assertMatches('...ticket created...',
324                           self.browser.contents)
325        #ctrl = self.browser.getControl(name='val_id')
326        #value = ctrl.options[0]
327        #self.browser.getLink(value).click()
328        self.assertMatches('...Amount Authorized...',
329                           self.browser.contents)
330        self.assertTrue('span>1000.0</span>' in self.browser.contents)
331        self.payment_url = self.browser.url
332        self.browser.getLink("CollegePAY", index=0).click()
333        self.assertTrue('<input type="hidden" name="amount" value="100000" />'
334            in self.browser.contents)
335        delta = timedelta(days=8)
336        self.applicant.values()[0].creation_date -= delta
337        self.browser.open(self.payment_url)
338        self.browser.getLink("CollegePAY", index=0).click()
339        self.assertMatches(
340            '...This payment ticket is too old. Please create a new ticket...',
341            self.browser.contents)
342        # ND PT Application Fee
343        self.applicantscontainer.application_category = 'ndpt'
344        self.certificate.application_category = 'ndpt'
345        self.browser.open(self.manage_path)
346        self.browser.getControl("Add online").click()
347        self.browser.getLink("CollegePAY", index=0).click()
348        self.assertTrue('<item_detail item_id="1" item_name="Application 1" item_amt="15000" bank_id="117" acct_num="1015220292" />'
349            in self.browser.contents)
350        self.assertTrue('<item_detail item_id="2" item_name="Application 2" item_amt="10000" bank_id="123" acct_num="1002883141" />'
351            in self.browser.contents)
352        self.assertTrue('<item_detail item_id="3" item_name="BT Education" item_amt="50000" bank_id="8" acct_num="2028964403" />'
353            in self.browser.contents)
354
355    @external_test
356    def test_webservice(self):
357
358        self.browser.open(self.payment_url + '/request_webservice')
359        self.assertMatches('...Unsuccessful callback...',
360                          self.browser.contents)
361        # The payment is now in state failed
362        self.assertMatches('...<span>Failed</span>...',
363                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.