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

Last change on this file since 16563 was 16562, checked in by Henrik Bettermann, 3 years ago

Fix tests.

  • Property svn:keywords set to Id
File size: 18.6 KB
Line 
1## $Id: tests.py 16562 2021-08-03 09:13:33Z 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.assertTrue('You have to pay NADESU/SA/SUG and GNS Dues first'
70            in self.browser.contents)
71        payment = createObject('waeup.StudentOnlinePayment')
72        payment.p_category = u'dep_sug'
73        payment.p_session = self.student.current_session
74        payment.p_id = u'anyid'
75        payment.p_state = u'paid'
76        self.student['payments']['anykey'] = payment
77        payment2 = createObject('waeup.StudentOnlinePayment')
78        payment2.p_category = u'gns_1'
79        payment2.p_session = self.student.current_session
80        payment2.p_id = u'anyid2'
81        payment2.p_state = u'paid'
82        self.student['payments']['anykey2'] = payment2
83        self.browser.open(self.payments_path + '/addop')
84        self.browser.getControl(name="form.p_category").value = ['schoolfee']
85        self.browser.getControl("Create ticket").click()
86        self.assertMatches('...ticket created...',
87                           self.browser.contents)
88        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
89        self.payment_url = self.browser.url
90        self.browser.getLink("Pay via Interswitch", index=0).click()
91        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
92            in self.browser.contents)
93        self.assertTrue(
94            '<input type="hidden" name="amount" value="3000000" />' in
95            self.browser.contents)
96        self.assertTrue(
97            'item_name="School Fee" item_amt="2575000" bank_id="8" acct_num="2004145840"' in
98            self.browser.contents)
99        # ND FT Non-Deltan Fresh Student Acceptance Fee
100        self.app['configuration']['2004'].clearance_fee = 12345.0
101        self.browser.open(self.payments_path + '/addop')
102        self.browser.getControl(name="form.p_category").value = ['clearance']
103        self.browser.getControl("Create ticket").click()
104        self.assertTrue('ticket created' in self.browser.contents)
105        self.assertTrue('Amount Authorized' in self.browser.contents)
106        self.assertTrue('<span>12345.0</span>' in self.browser.contents)
107        self.payment_url = self.browser.url
108        self.browser.getLink("Pay via Interswitch", index=0).click()
109        self.assertTrue('<input type="hidden" name="pay_item_id" value="103" />'
110                           in self.browser.contents)
111        self.assertTrue(
112            '<input type="hidden" name="amount" value="1234500" />' in
113            self.browser.contents)
114        self.assertTrue(
115            'item_name="Acceptance/Screening" item_amt="1159500" bank_id="121" acct_num="0072223654"' in
116            self.browser.contents)
117
118    def test_interswitch_form_ticket_expired(self):
119        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
120        acc_payment = createObject('waeup.StudentOnlinePayment')
121        acc_payment.p_state = 'unpaid'
122        acc_payment.p_category = 'clearance'
123        acc_payment.p_id = 'xyz'
124        acc_payment.pay_item_id = '123'
125        acc_payment.amount_auth = 876.0
126        self.student['payments']['xyz'] = acc_payment
127        self.browser.open(self.payments_path + '/xyz')
128        self.browser.getLink("Pay via Interswitch", index=0).click()
129        self.assertMatches('...<input type="hidden" name="pay_item_id" value="103" />...',
130                           self.browser.contents)
131        self.assertMatches('...Total Amount Authorized:...',
132                           self.browser.contents)
133        self.assertEqual(self.student.current_mode, 'ug_ft')
134        self.assertMatches(
135            '...<input type="hidden" name="amount" value="87600" />...',
136            self.browser.contents)
137        delta = timedelta(days=8)
138        acc_payment.creation_date -= delta
139        self.browser.open(self.payments_path + '/xyz')
140        self.browser.getLink("Pay via Interswitch", index=0).click()
141        self.assertMatches(
142            '...This payment ticket is too old. Please create a new ticket...',
143            self.browser.contents)
144        delta = timedelta(days=2)
145        acc_payment.creation_date += delta
146        self.browser.open(self.payments_path + '/xyz')
147        self.browser.getLink("Pay via Interswitch", index=0).click()
148        self.assertMatches('...Total Amount Authorized:...',
149                           self.browser.contents)
150
151    def test_pt_payments(self):
152        # Add SPAT certificate
153        certificate = createObject('waeup.Certificate')
154        certificate.code = u'CERT2'
155        certificate.application_category = 'basic' # can be anything
156        certificate.start_level = 100
157        certificate.end_level = 500
158        certificate.study_mode = u'ug_ft' # can be anything
159        self.app['faculties']['SPAT'] = Faculty(code=u'SPAT')
160        self.app['faculties']['SPAT']['dep2'] = Department(code=u'dep2')
161        department = self.app['faculties']['SPAT']['dep2']
162        self.app['faculties']['SPAT']['dep2'].certificates.addCertificate(
163            certificate)
164        certificate.school_fee_3 = 30000.0
165        self.student['studycourse'].certificate = certificate
166        # Manager can access InterswitchForm
167        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
168        self.browser.open(self.payments_path)
169        IWorkflowState(self.student).setState('cleared')
170        self.student.nationality = u'NG'
171        # ND PT Non-Deltan Fresh Student School Fee
172        self.certificate.study_mode = 'nd_ft'
173        self.certificate.school_fee_3 = 30000.0
174        # Add dep_sug and gns tickets.
175        payment = createObject('waeup.StudentOnlinePayment')
176        payment.p_category = u'dep_sug'
177        payment.p_session = self.student.current_session
178        payment.p_id = u'anyid'
179        payment.p_state = u'paid'
180        self.student['payments']['anykey'] = payment
181        payment2 = createObject('waeup.StudentOnlinePayment')
182        payment2.p_category = u'gns_1'
183        payment2.p_session = self.student.current_session
184        payment2.p_id = u'anyid2'
185        payment2.p_state = u'paid'
186        self.student['payments']['anykey2'] = payment2
187        # Add school fee ticket
188        self.browser.open(self.payments_path + '/addop')
189        self.browser.getControl(name="form.p_category").value = ['schoolfee']
190        self.browser.getControl("Create ticket").click()
191        self.assertMatches('...ticket created...', self.browser.contents)
192        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
193        self.payment_url = self.browser.url
194        self.browser.getLink("Pay via Interswitch", index=0).click()
195        self.assertTrue('<item_detail item_id="1" item_name="School Fee 1" item_amt="1545000" bank_id="388" acct_num="1556004203" />'
196            in self.browser.contents)
197        self.assertTrue('<item_detail item_id="2" item_name="School Fee 2" item_amt="1030000" bank_id="8" acct_num="2034761924" />'
198            in self.browser.contents)
199        self.assertTrue('<item_detail item_id="3" item_name="WAEAC" item_amt="280000" bank_id="8" acct_num="2028964403" />'
200            in self.browser.contents)
201        self.assertTrue('<item_detail item_id="4" item_name="Technology Fee" item_amt="120000" bank_id="10" acct_num="0032256360" />'
202            in self.browser.contents)
203
204    def test_interswitch_form_dep_sug_payments(self):
205        # Manager can access InterswitchForm
206        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
207        self.browser.open(self.payments_path)
208        IWorkflowState(self.student).setState('cleared')
209        self.browser.open(self.payments_path + '/addop')
210        self.browser.getControl(name="form.p_category").value = ['dep_sug']
211        self.browser.getControl("Create ticket").click()
212        self.assertMatches('...ticket created...', self.browser.contents)
213        self.assertTrue('<span>3150.0</span>'  in self.browser.contents)
214        self.payment_url = self.browser.url
215        self.browser.getLink("Pay via Interswitch", index=0).click()
216        self.assertTrue('<item_detail item_id="1" item_name="SUG" item_amt="100000" bank_id="11" acct_num="0038079930" />'
217            in self.browser.contents)
218        self.assertTrue('<item_detail item_id="2" item_name="Students Welfare" item_amt="50000" bank_id="11" acct_num="0037892949" />'
219            in self.browser.contents)
220        self.assertTrue('<item_detail item_id="3" item_name="Anti-Cult Book" item_amt="90000" bank_id="11" acct_num="0037892949" />'
221            in self.browser.contents)
222        self.assertTrue('<item_detail item_id="4" item_name="NADESSTU" item_amt="50000" bank_id="11" acct_num="0036375968" />'
223            in self.browser.contents)
224
225    @external_test
226    def test_webservice(self):
227        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
228        self.browser.open(self.payments_path)
229        IWorkflowState(self.student).setState('cleared')
230        self.student.nationality = u'NG'
231        self.browser.open(self.payments_path + '/addop')
232        self.browser.getControl(name="form.p_category").value = ['schoolfee']
233        self.browser.getControl("Create ticket").click()
234        self.assertMatches('...ticket created...',
235                           self.browser.contents)
236        ctrl = self.browser.getControl(name='val_id')
237        self.value = ctrl.options[0]
238        self.browser.getLink(self.value).click()
239        self.payment_url = self.browser.url
240        # First we have open InterswitchPageStudent to set provider_amt
241        # and gateway_amt
242        self.browser.open(self.payment_url + '/goto_interswitch')
243        # Now we can call the webservice
244        self.browser.open(self.payment_url + '/request_webservice')
245        self.assertMatches('...Unsuccessful callback...',
246                          self.browser.contents)
247        # The payment is now in state failed ...
248        self.assertMatches('...<span>Failed</span>...',
249                          self.browser.contents)
250        # ... and the catalog has been updated
251        cat = getUtility(ICatalog, name='payments_catalog')
252        results = list(
253            cat.searchResults(p_state=('failed', 'failed')))
254        self.assertEqual(len(results), 1)
255        self.assertEqual(results[0].p_state, 'failed')
256
257        # Let's replace the p_id with a valid p_id of the Uniben
258        # live system. This is definitely not an appropriate
259        # solution for testing, but we have no choice since
260        # Interswitch doesn't provide any interface
261        # for testing.
262        payment = self.student['payments'][self.value]
263        payment.p_id = 'p3547789850240'
264        self.browser.open(self.payment_url + '/request_webservice')
265        self.assertMatches('...Callback amount does not match...',
266                          self.browser.contents)
267        # The payment is now in state failed ...
268        self.assertMatches('...<span>Failed</span>...',
269                          self.browser.contents)
270        # Let's replace the amount autorized with the amount of the
271        # live system payment
272        payment.amount_auth = payment.r_amount_approved
273        self.browser.open(self.payment_url + '/request_webservice')
274        self.assertMatches('...Successful payment...',
275                          self.browser.contents)
276        # The payment is now in state paid ...
277        self.assertMatches('...<span>Paid</span>...',
278                          self.browser.contents)
279        # ... and the catalog has been updated
280        cat = getUtility(ICatalog, name='payments_catalog')
281        results = list(
282            cat.searchResults(p_state=('paid', 'paid')))
283        self.assertEqual(len(results), 1)
284        self.assertEqual(results[0].p_state, 'paid')
285        # Approval is logged in students.log ...
286        logfile = os.path.join(
287            self.app['datacenter'].storage, 'logs', 'students.log')
288        logcontent = open(logfile).read()
289        self.assertTrue(
290            'zope.mgr - '
291            'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
292            'X1000000 - successful schoolfee payment: p3547789850240\n'
293            in logcontent)
294        # ... and in payments.log
295        logfile = os.path.join(
296            self.app['datacenter'].storage, 'logs', 'payments.log')
297        logcontent = open(logfile).read()
298        self.assertTrue(
299            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
300            '12000.0,00,0.0,150.0,0.0,,,\n'
301            in logcontent)
302
303
304class InterswitchTestsApplicants(ApplicantsFullSetup):
305    """Tests for the Interswitch payment gateway.
306    """
307
308    layer = FunctionalLayer
309
310    def setUp(self):
311        super(InterswitchTestsApplicants, self).setUp()
312        configuration = SessionConfiguration()
313        configuration.academic_session = datetime.now().year - 2
314        self.app['configuration'].addSessionConfiguration(configuration)
315        self.configuration = configuration
316        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
317        self.browser.open(self.manage_path)
318        #IWorkflowState(self.student).setState('started')
319        super(InterswitchTestsApplicants, self).fill_correct_values()
320        self.applicantscontainer.application_fee = 1000.0
321        self.browser.getControl(name="form.nationality").value = ['NG']
322        self.browser.getControl(name="transition").value = ['start']
323        self.browser.getControl("Save").click()
324
325
326    def test_interswitch_form(self):
327        self.browser.getControl("Add online").click()
328        self.assertMatches('...passport photo before making payment...',
329                           self.browser.contents)
330        self.browser.open(self.manage_path)
331        super(InterswitchTestsApplicants, self).fill_correct_values()
332        self.browser.getControl(name="form.nationality").value = ['NG']
333        #self.browser.getControl(name="transition").value = ['start']
334        image = open(SAMPLE_IMAGE, 'rb')
335        ctrl = self.browser.getControl(name='form.passport')
336        file_ctrl = ctrl.mech_control
337        file_ctrl.add_file(image, filename='myphoto.jpg')
338        self.browser.getControl("Save").click()
339        self.browser.getControl("Add online").click()
340        self.assertMatches('...ticket created...',
341                           self.browser.contents)
342        #ctrl = self.browser.getControl(name='val_id')
343        #value = ctrl.options[0]
344        #self.browser.getLink(value).click()
345        self.assertMatches('...Amount Authorized...',
346                           self.browser.contents)
347        self.assertTrue('span>1000.0</span>' in self.browser.contents)
348        self.payment_url = self.browser.url
349        self.browser.getLink("Pay via Interswitch", index=0).click()
350        self.assertTrue('<input type="hidden" name="amount" value="100000" />'
351            in self.browser.contents)
352        delta = timedelta(days=8)
353        self.applicant.values()[0].creation_date -= delta
354        self.browser.open(self.payment_url)
355        self.browser.getLink("Pay via Interswitch", index=0).click()
356        self.assertMatches(
357            '...This payment ticket is too old. Please create a new ticket...',
358            self.browser.contents)
359        # ND PT Application Fee
360        self.applicantscontainer.application_category = 'ndpt'
361        self.certificate.application_category = 'ndpt'
362        self.browser.open(self.manage_path)
363        self.browser.getControl("Add online").click()
364        self.browser.getLink("Pay via Interswitch", index=0).click()
365        self.assertTrue('<item_detail item_id="1" item_name="Application 1" item_amt="15000" bank_id="117" acct_num="1015220292" />'
366            in self.browser.contents)
367        self.assertTrue('<item_detail item_id="2" item_name="Application 2" item_amt="10000" bank_id="8" acct_num="2034761924" />'
368            in self.browser.contents)
369        self.assertTrue('<item_detail item_id="3" item_name="WAEAC" item_amt="50000" bank_id="8" acct_num="2028964403" />'
370            in self.browser.contents)
371
372    @external_test
373    def test_webservice(self):
374
375        self.browser.open(self.payment_url + '/request_webservice')
376        self.assertMatches('...Unsuccessful callback...',
377                          self.browser.contents)
378        # The payment is now in state failed
379        self.assertMatches('...<span>Failed</span>...',
380                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.