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

Last change on this file since 17660 was 16965, checked in by Henrik Bettermann, 2 years ago

Fix test.

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