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

Last change on this file since 16338 was 16338, checked in by Henrik Bettermann, 4 years ago

Reanble GNS pack payment requirements.

  • Property svn:keywords set to Id
File size: 19.0 KB
RevLine 
[10765]1## $Id: tests.py 16338 2020-12-01 07:16:35Z 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
[11271]19from datetime import datetime, date, timedelta
[10765]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
[14889]26from waeup.kofa.university.faculty import Faculty
27from waeup.kofa.university.department import Department
[14716]28from kofacustom.dspg.testing import FunctionalLayer
[10765]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
[14732]35SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
36
[10765]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'
[14875]63        # ND FT Non-Deltan Fresh Student School Fee
64        self.certificate.study_mode = 'nd_ft'
65        self.certificate.school_fee_3 = 30000.0
[10765]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()
[16338]69        self.assertTrue('You have to pay NADESU/SA/SUG and GNS Dues first'
[14938]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
[16309]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
[14938]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()
[10765]86        self.assertMatches('...ticket created...',
87                           self.browser.contents)
[14889]88        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
[10765]89        self.payment_url = self.browser.url
[15719]90        self.browser.getLink("Pay via Interswitch", index=0).click()
[14875]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(
[16013]97            'item_name="School Fee" item_amt="2575000" bank_id="117" acct_num="1012808851"' in
[14875]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()
[14938]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)
[14875]107        self.payment_url = self.browser.url
[15719]108        self.browser.getLink("Pay via Interswitch", index=0).click()
[14938]109        self.assertTrue('<input type="hidden" name="pay_item_id" value="103" />'
110                           in self.browser.contents)
[14875]111        self.assertTrue(
112            '<input type="hidden" name="amount" value="1234500" />' in
[10765]113            self.browser.contents)
[14875]114        self.assertTrue(
[15699]115            'item_name="Acceptance/Screening" item_amt="1159500" bank_id="121" acct_num="0072223654"' in
[14875]116            self.browser.contents)
[10765]117
[12979]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')
[15719]128        self.browser.getLink("Pay via Interswitch", index=0).click()
[14862]129        self.assertMatches('...<input type="hidden" name="pay_item_id" value="103" />...',
[12979]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)
[13020]137        delta = timedelta(days=8)
[12979]138        acc_payment.creation_date -= delta
139        self.browser.open(self.payments_path + '/xyz')
[15719]140        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]141        self.assertMatches(
142            '...This payment ticket is too old. Please create a new ticket...',
143            self.browser.contents)
[13020]144        delta = timedelta(days=2)
[12979]145        acc_payment.creation_date += delta
146        self.browser.open(self.payments_path + '/xyz')
[15719]147        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]148        self.assertMatches('...Total Amount Authorized:...',
149                           self.browser.contents)
150
[14938]151    def test_pt_payments(self):
[14889]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
[14940]174        # dep_sug tickets cannot be created
[14889]175        self.browser.open(self.payments_path + '/addop')
[14938]176        self.browser.getControl(name="form.p_category").value = ['dep_sug']
177        self.browser.getControl("Create ticket").click()
[14940]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()
[15719]184        #self.browser.getLink("Pay via Interswitch", index=0).click()
[14940]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)
[14938]190        # Add school fee ticket
191        self.browser.open(self.payments_path + '/addop')
[14889]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        self.assertTrue('<span>30000.0</span>'  in self.browser.contents)
196        self.payment_url = self.browser.url
[15719]197        self.browser.getLink("Pay via Interswitch", index=0).click()
[15215]198        self.assertTrue('<item_detail item_id="1" item_name="School Fee 1" item_amt="1545000" bank_id="117" acct_num="1015220292" />'
[14889]199            in self.browser.contents)
[15707]200        self.assertTrue('<item_detail item_id="2" item_name="School Fee 2" item_amt="1030000" bank_id="8" acct_num="2034761924" />'
[14889]201            in self.browser.contents)
[15255]202        self.assertTrue('<item_detail item_id="3" item_name="WAEAC" item_amt="280000" bank_id="8" acct_num="2028964403" />'
[14889]203            in self.browser.contents)
[15345]204        self.assertTrue('<item_detail item_id="4" item_name="Technology Fee" item_amt="120000" bank_id="10" acct_num="0032256360" />'
[14964]205            in self.browser.contents)
[14889]206
[14921]207    def test_interswitch_form_dep_sug_payments(self):
208        # Manager can access InterswitchForm
209        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
210        self.browser.open(self.payments_path)
211        IWorkflowState(self.student).setState('cleared')
212        self.browser.open(self.payments_path + '/addop')
213        self.browser.getControl(name="form.p_category").value = ['dep_sug']
214        self.browser.getControl("Create ticket").click()
215        self.assertMatches('...ticket created...', self.browser.contents)
[14938]216        self.assertTrue('<span>3150.0</span>'  in self.browser.contents)
[14921]217        self.payment_url = self.browser.url
[15719]218        self.browser.getLink("Pay via Interswitch", index=0).click()
[14938]219        self.assertTrue('<item_detail item_id="1" item_name="SUG" item_amt="100000" bank_id="11" acct_num="0038079930" />'
[14921]220            in self.browser.contents)
221        self.assertTrue('<item_detail item_id="2" item_name="Students Welfare" item_amt="50000" bank_id="11" acct_num="0037892949" />'
222            in self.browser.contents)
223        self.assertTrue('<item_detail item_id="3" item_name="Anti-Cult Book" item_amt="90000" bank_id="11" acct_num="0037892949" />'
224            in self.browser.contents)
[14938]225        self.assertTrue('<item_detail item_id="4" item_name="NADESSTU" item_amt="50000" bank_id="11" acct_num="0036375968" />'
[14921]226            in self.browser.contents)
227
[10765]228    @external_test
229    def test_webservice(self):
230        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
231        self.browser.open(self.payments_path)
232        IWorkflowState(self.student).setState('cleared')
233        self.student.nationality = u'NG'
234        self.browser.open(self.payments_path + '/addop')
235        self.browser.getControl(name="form.p_category").value = ['schoolfee']
236        self.browser.getControl("Create ticket").click()
237        self.assertMatches('...ticket created...',
238                           self.browser.contents)
239        ctrl = self.browser.getControl(name='val_id')
240        self.value = ctrl.options[0]
241        self.browser.getLink(self.value).click()
242        self.payment_url = self.browser.url
243        # First we have open InterswitchPageStudent to set provider_amt
244        # and gateway_amt
245        self.browser.open(self.payment_url + '/goto_interswitch')
246        # Now we can call the webservice
247        self.browser.open(self.payment_url + '/request_webservice')
248        self.assertMatches('...Unsuccessful callback...',
249                          self.browser.contents)
250        # The payment is now in state failed ...
251        self.assertMatches('...<span>Failed</span>...',
252                          self.browser.contents)
253        # ... and the catalog has been updated
254        cat = getUtility(ICatalog, name='payments_catalog')
255        results = list(
256            cat.searchResults(p_state=('failed', 'failed')))
257        self.assertEqual(len(results), 1)
258        self.assertEqual(results[0].p_state, 'failed')
259
260        # Let's replace the p_id with a valid p_id of the Uniben
261        # live system. This is definitely not an appropriate
262        # solution for testing, but we have no choice since
263        # Interswitch doesn't provide any interface
264        # for testing.
265        payment = self.student['payments'][self.value]
266        payment.p_id = 'p3547789850240'
267        self.browser.open(self.payment_url + '/request_webservice')
268        self.assertMatches('...Callback amount does not match...',
269                          self.browser.contents)
270        # The payment is now in state failed ...
271        self.assertMatches('...<span>Failed</span>...',
272                          self.browser.contents)
273        # Let's replace the amount autorized with the amount of the
274        # live system payment
275        payment.amount_auth = payment.r_amount_approved
276        self.browser.open(self.payment_url + '/request_webservice')
277        self.assertMatches('...Successful payment...',
278                          self.browser.contents)
279        # The payment is now in state paid ...
280        self.assertMatches('...<span>Paid</span>...',
281                          self.browser.contents)
282        # ... and the catalog has been updated
283        cat = getUtility(ICatalog, name='payments_catalog')
284        results = list(
285            cat.searchResults(p_state=('paid', 'paid')))
286        self.assertEqual(len(results), 1)
287        self.assertEqual(results[0].p_state, 'paid')
288        # Approval is logged in students.log ...
289        logfile = os.path.join(
290            self.app['datacenter'].storage, 'logs', 'students.log')
291        logcontent = open(logfile).read()
292        self.assertTrue(
293            'zope.mgr - '
[14716]294            'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
[10765]295            'X1000000 - successful schoolfee payment: p3547789850240\n'
296            in logcontent)
297        # ... and in payments.log
298        logfile = os.path.join(
299            self.app['datacenter'].storage, 'logs', 'payments.log')
300        logcontent = open(logfile).read()
301        self.assertTrue(
302            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
303            '12000.0,00,0.0,150.0,0.0,,,\n'
304            in logcontent)
305
306
307class InterswitchTestsApplicants(ApplicantsFullSetup):
308    """Tests for the Interswitch payment gateway.
309    """
310
311    layer = FunctionalLayer
312
313    def setUp(self):
314        super(InterswitchTestsApplicants, self).setUp()
[11271]315        configuration = SessionConfiguration()
316        configuration.academic_session = datetime.now().year - 2
317        self.app['configuration'].addSessionConfiguration(configuration)
318        self.configuration = configuration
[10765]319        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
320        self.browser.open(self.manage_path)
321        #IWorkflowState(self.student).setState('started')
322        super(InterswitchTestsApplicants, self).fill_correct_values()
323        self.applicantscontainer.application_fee = 1000.0
324        self.browser.getControl(name="form.nationality").value = ['NG']
325        self.browser.getControl(name="transition").value = ['start']
326        self.browser.getControl("Save").click()
[14889]327
328
329    def test_interswitch_form(self):
[10765]330        self.browser.getControl("Add online").click()
[14732]331        self.assertMatches('...passport photo before making payment...',
332                           self.browser.contents)
333        self.browser.open(self.manage_path)
334        super(InterswitchTestsApplicants, self).fill_correct_values()
335        self.browser.getControl(name="form.nationality").value = ['NG']
336        #self.browser.getControl(name="transition").value = ['start']
337        image = open(SAMPLE_IMAGE, 'rb')
338        ctrl = self.browser.getControl(name='form.passport')
339        file_ctrl = ctrl.mech_control
340        file_ctrl.add_file(image, filename='myphoto.jpg')
341        self.browser.getControl("Save").click()
342        self.browser.getControl("Add online").click()
[10765]343        self.assertMatches('...ticket created...',
344                           self.browser.contents)
345        #ctrl = self.browser.getControl(name='val_id')
346        #value = ctrl.options[0]
347        #self.browser.getLink(value).click()
348        self.assertMatches('...Amount Authorized...',
349                           self.browser.contents)
[14889]350        self.assertTrue('span>1000.0</span>' in self.browser.contents)
[10765]351        self.payment_url = self.browser.url
[15719]352        self.browser.getLink("Pay via Interswitch", index=0).click()
[14889]353        self.assertTrue('<input type="hidden" name="amount" value="100000" />'
354            in self.browser.contents)
[13020]355        delta = timedelta(days=8)
[12979]356        self.applicant.values()[0].creation_date -= delta
357        self.browser.open(self.payment_url)
[15719]358        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]359        self.assertMatches(
360            '...This payment ticket is too old. Please create a new ticket...',
361            self.browser.contents)
[14889]362        # ND PT Application Fee
363        self.applicantscontainer.application_category = 'ndpt'
364        self.certificate.application_category = 'ndpt'
365        self.browser.open(self.manage_path)
366        self.browser.getControl("Add online").click()
[15719]367        self.browser.getLink("Pay via Interswitch", index=0).click()
[15098]368        self.assertTrue('<item_detail item_id="1" item_name="Application 1" item_amt="15000" bank_id="117" acct_num="1015220292" />'
[14889]369            in self.browser.contents)
[15707]370        self.assertTrue('<item_detail item_id="2" item_name="Application 2" item_amt="10000" bank_id="8" acct_num="2034761924" />'
[14889]371            in self.browser.contents)
[15255]372        self.assertTrue('<item_detail item_id="3" item_name="WAEAC" item_amt="50000" bank_id="8" acct_num="2028964403" />'
[14889]373            in self.browser.contents)
[10765]374
375    @external_test
376    def test_webservice(self):
377
378        self.browser.open(self.payment_url + '/request_webservice')
379        self.assertMatches('...Unsuccessful callback...',
380                          self.browser.contents)
381        # The payment is now in state failed
382        self.assertMatches('...<span>Failed</span>...',
383                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.