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

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

Deduct tech fee.

  • Property svn:keywords set to Id
File size: 19.0 KB
Line 
1## $Id: tests.py 14961 2018-03-07 12:48:34Z 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 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        self.browser.open(self.payments_path + '/addop')
78        self.browser.getControl(name="form.p_category").value = ['schoolfee']
79        self.browser.getControl("Create ticket").click()
80        self.assertMatches('...ticket created...',
81                           self.browser.contents)
82        ctrl = self.browser.getControl(name='val_id')
83        self.value = ctrl.options[1]
84        self.browser.getLink(self.value).click()
85        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
86        self.payment_url = self.browser.url
87        self.browser.getLink("CollegePAY", index=0).click()
88        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
89            in self.browser.contents)
90        self.assertTrue(
91            '<input type="hidden" name="amount" value="3000000" />' in
92            self.browser.contents)
93        self.assertTrue(
94            'item_name="School Fee" item_amt="2575000" bank_id="11" acct_num="0010761873"' in
95            self.browser.contents)
96        # ND FT Non-Deltan Fresh Student Acceptance Fee
97        self.app['configuration']['2004'].clearance_fee = 12345.0
98        self.browser.open(self.payments_path + '/addop')
99        self.browser.getControl(name="form.p_category").value = ['clearance']
100        self.browser.getControl("Create ticket").click()
101        self.assertTrue('ticket created' in self.browser.contents)
102        ctrl = self.browser.getControl(name='val_id')
103        self.value = ctrl.options[2]
104        self.browser.getLink(self.value).click()
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("CollegePAY", 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" item_amt="1159500" bank_id="8" acct_num="2004402644"' 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("CollegePAY", 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("CollegePAY", 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("CollegePAY", 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        # dep_sug tickets cannot be created
175        self.browser.open(self.payments_path + '/addop')
176        self.browser.getControl(name="form.p_category").value = ['dep_sug']
177        self.browser.getControl("Create ticket").click()
178        self.assertTrue('Amount could not be determined' in self.browser.contents)
179        #self.assertTrue('ticket created' in self.browser.contents)
180        #ctrl = self.browser.getControl(name='val_id')
181        #dep_sug_pid = ctrl.options[0]
182        #self.assertTrue(self.student['payments'][dep_sug_pid].amount_auth == 1650)
183        #self.browser.getLink(dep_sug_pid).click()
184        #self.browser.getLink("CollegePAY", index=0).click()
185        #self.student['payments'][dep_sug_pid].p_state = 'paid'
186        #self.assertTrue(
187        #    '<item_detail item_id="1" item_name="Students Welfare" '
188        #    'item_amt="50000" bank_id="11" acct_num="0037892949" />'
189        #    in self.browser.contents)
190        # Add school fee ticket
191        self.browser.open(self.payments_path + '/addop')
192        self.browser.getControl(name="form.p_category").value = ['schoolfee']
193        self.browser.getControl("Create ticket").click()
194        self.assertMatches('...ticket created...', self.browser.contents)
195        ctrl = self.browser.getControl(name='val_id')
196        self.value = ctrl.options[0]
197        self.browser.getLink(self.value).click()
198        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
199        self.payment_url = self.browser.url
200        self.browser.getLink("CollegePAY", index=0).click()
201        self.assertTrue('<item_detail item_id="1" item_name="School Fee 1" item_amt="1545000" bank_id="117" acct_num="1012808851" />'
202            in self.browser.contents)
203        self.assertTrue('<item_detail item_id="2" item_name="School Fee 2" item_amt="1030000" bank_id="123" acct_num="1002883141" />'
204            in self.browser.contents)
205        self.assertTrue('<item_detail item_id="3" item_name="BT Education" item_amt="280000" bank_id="8" acct_num="2028964403" />'
206            in self.browser.contents)
207
208    def test_interswitch_form_dep_sug_payments(self):
209        # Manager can access InterswitchForm
210        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
211        self.browser.open(self.payments_path)
212        IWorkflowState(self.student).setState('cleared')
213        self.browser.open(self.payments_path + '/addop')
214        self.browser.getControl(name="form.p_category").value = ['dep_sug']
215        self.browser.getControl("Create ticket").click()
216        self.assertMatches('...ticket created...', self.browser.contents)
217        ctrl = self.browser.getControl(name='val_id')
218        self.value = ctrl.options[0]
219        self.browser.getLink(self.value).click()
220        self.assertTrue('<span>3150.0</span>'  in self.browser.contents)
221        self.payment_url = self.browser.url
222        self.browser.getLink("CollegePAY", index=0).click()
223        self.assertTrue('<item_detail item_id="1" item_name="SUG" item_amt="100000" bank_id="11" acct_num="0038079930" />'
224            in self.browser.contents)
225        self.assertTrue('<item_detail item_id="2" item_name="Students Welfare" item_amt="50000" bank_id="11" acct_num="0037892949" />'
226            in self.browser.contents)
227        self.assertTrue('<item_detail item_id="3" item_name="Anti-Cult Book" item_amt="90000" bank_id="11" acct_num="0037892949" />'
228            in self.browser.contents)
229        self.assertTrue('<item_detail item_id="4" item_name="NADESSTU" item_amt="50000" bank_id="11" acct_num="0036375968" />'
230            in self.browser.contents)
231
232    @external_test
233    def test_webservice(self):
234        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
235        self.browser.open(self.payments_path)
236        IWorkflowState(self.student).setState('cleared')
237        self.student.nationality = u'NG'
238        self.browser.open(self.payments_path + '/addop')
239        self.browser.getControl(name="form.p_category").value = ['schoolfee']
240        self.browser.getControl("Create ticket").click()
241        self.assertMatches('...ticket created...',
242                           self.browser.contents)
243        ctrl = self.browser.getControl(name='val_id')
244        self.value = ctrl.options[0]
245        self.browser.getLink(self.value).click()
246        self.payment_url = self.browser.url
247        # First we have open InterswitchPageStudent to set provider_amt
248        # and gateway_amt
249        self.browser.open(self.payment_url + '/goto_interswitch')
250        # Now we can call the webservice
251        self.browser.open(self.payment_url + '/request_webservice')
252        self.assertMatches('...Unsuccessful callback...',
253                          self.browser.contents)
254        # The payment is now in state failed ...
255        self.assertMatches('...<span>Failed</span>...',
256                          self.browser.contents)
257        # ... and the catalog has been updated
258        cat = getUtility(ICatalog, name='payments_catalog')
259        results = list(
260            cat.searchResults(p_state=('failed', 'failed')))
261        self.assertEqual(len(results), 1)
262        self.assertEqual(results[0].p_state, 'failed')
263
264        # Let's replace the p_id with a valid p_id of the Uniben
265        # live system. This is definitely not an appropriate
266        # solution for testing, but we have no choice since
267        # Interswitch doesn't provide any interface
268        # for testing.
269        payment = self.student['payments'][self.value]
270        payment.p_id = 'p3547789850240'
271        self.browser.open(self.payment_url + '/request_webservice')
272        self.assertMatches('...Callback amount does not match...',
273                          self.browser.contents)
274        # The payment is now in state failed ...
275        self.assertMatches('...<span>Failed</span>...',
276                          self.browser.contents)
277        # Let's replace the amount autorized with the amount of the
278        # live system payment
279        payment.amount_auth = payment.r_amount_approved
280        self.browser.open(self.payment_url + '/request_webservice')
281        self.assertMatches('...Successful payment...',
282                          self.browser.contents)
283        # The payment is now in state paid ...
284        self.assertMatches('...<span>Paid</span>...',
285                          self.browser.contents)
286        # ... and the catalog has been updated
287        cat = getUtility(ICatalog, name='payments_catalog')
288        results = list(
289            cat.searchResults(p_state=('paid', 'paid')))
290        self.assertEqual(len(results), 1)
291        self.assertEqual(results[0].p_state, 'paid')
292        # Approval is logged in students.log ...
293        logfile = os.path.join(
294            self.app['datacenter'].storage, 'logs', 'students.log')
295        logcontent = open(logfile).read()
296        self.assertTrue(
297            'zope.mgr - '
298            'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
299            'X1000000 - successful schoolfee payment: p3547789850240\n'
300            in logcontent)
301        # ... and in payments.log
302        logfile = os.path.join(
303            self.app['datacenter'].storage, 'logs', 'payments.log')
304        logcontent = open(logfile).read()
305        self.assertTrue(
306            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
307            '12000.0,00,0.0,150.0,0.0,,,\n'
308            in logcontent)
309
310
311class InterswitchTestsApplicants(ApplicantsFullSetup):
312    """Tests for the Interswitch payment gateway.
313    """
314
315    layer = FunctionalLayer
316
317    def setUp(self):
318        super(InterswitchTestsApplicants, self).setUp()
319        configuration = SessionConfiguration()
320        configuration.academic_session = datetime.now().year - 2
321        self.app['configuration'].addSessionConfiguration(configuration)
322        self.configuration = configuration
323        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
324        self.browser.open(self.manage_path)
325        #IWorkflowState(self.student).setState('started')
326        super(InterswitchTestsApplicants, self).fill_correct_values()
327        self.applicantscontainer.application_fee = 1000.0
328        self.browser.getControl(name="form.nationality").value = ['NG']
329        self.browser.getControl(name="transition").value = ['start']
330        self.browser.getControl("Save").click()
331
332
333    def test_interswitch_form(self):
334        self.browser.getControl("Add online").click()
335        self.assertMatches('...passport photo before making payment...',
336                           self.browser.contents)
337        self.browser.open(self.manage_path)
338        super(InterswitchTestsApplicants, self).fill_correct_values()
339        self.browser.getControl(name="form.nationality").value = ['NG']
340        #self.browser.getControl(name="transition").value = ['start']
341        image = open(SAMPLE_IMAGE, 'rb')
342        ctrl = self.browser.getControl(name='form.passport')
343        file_ctrl = ctrl.mech_control
344        file_ctrl.add_file(image, filename='myphoto.jpg')
345        self.browser.getControl("Save").click()
346        self.browser.getControl("Add online").click()
347        self.assertMatches('...ticket created...',
348                           self.browser.contents)
349        #ctrl = self.browser.getControl(name='val_id')
350        #value = ctrl.options[0]
351        #self.browser.getLink(value).click()
352        self.assertMatches('...Amount Authorized...',
353                           self.browser.contents)
354        self.assertTrue('span>1000.0</span>' in self.browser.contents)
355        self.payment_url = self.browser.url
356        self.browser.getLink("CollegePAY", index=0).click()
357        self.assertTrue('<input type="hidden" name="amount" value="100000" />'
358            in self.browser.contents)
359        delta = timedelta(days=8)
360        self.applicant.values()[0].creation_date -= delta
361        self.browser.open(self.payment_url)
362        self.browser.getLink("CollegePAY", index=0).click()
363        self.assertMatches(
364            '...This payment ticket is too old. Please create a new ticket...',
365            self.browser.contents)
366        # ND PT Application Fee
367        self.applicantscontainer.application_category = 'ndpt'
368        self.certificate.application_category = 'ndpt'
369        self.browser.open(self.manage_path)
370        self.browser.getControl("Add online").click()
371        self.browser.getLink("CollegePAY", index=0).click()
372        self.assertTrue('<item_detail item_id="1" item_name="Application 1" item_amt="15000" bank_id="117" acct_num="1015220292" />'
373            in self.browser.contents)
374        self.assertTrue('<item_detail item_id="2" item_name="Application 2" item_amt="10000" bank_id="123" acct_num="1002883141" />'
375            in self.browser.contents)
376        self.assertTrue('<item_detail item_id="3" item_name="BT Education" item_amt="50000" bank_id="8" acct_num="2028964403" />'
377            in self.browser.contents)
378
379    @external_test
380    def test_webservice(self):
381
382        self.browser.open(self.payment_url + '/request_webservice')
383        self.assertMatches('...Unsuccessful callback...',
384                          self.browser.contents)
385        # The payment is now in state failed
386        self.assertMatches('...<span>Failed</span>...',
387                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.