source: main/waeup.uniben/trunk/src/waeup/uniben/interswitch/tests.py @ 11684

Last change on this file since 11684 was 11645, checked in by Henrik Bettermann, 11 years ago

Add test for previous revision.

  • Property svn:keywords set to Id
File size: 20.8 KB
RevLine 
[7894]1## $Id: tests.py 11645 2014-05-14 05:00:26Z 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##
[9775]18import os
[10836]19from datetime import datetime
[9713]20from zope.component import createObject, getUtility
21from zope.catalog.interfaces import ICatalog
[7894]22from hurry.workflow.interfaces import IWorkflowState
23from waeup.kofa.students.tests.test_browser import StudentsFullSetup
[8266]24from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
25from waeup.kofa.configuration import SessionConfiguration
[8020]26from waeup.uniben.testing import FunctionalLayer
[7894]27
[7970]28# Also run tests that send requests to external servers?
29#   If you enable this, please make sure the external services
30#   do exist really and are not bothered by being spammed by a test programme.
[9831]31EXTERNAL_TESTS = False
[7970]32
33def external_test(func):
34    if not EXTERNAL_TESTS:
35        myself = __file__
36        if myself.endswith('.pyc'):
37            myself = myself[:-2]
38        print "WARNING: external tests are skipped!"
39        print "WARNING: edit %s to enable them." % myself
40        return
41    return func
42
43
[8266]44class InterswitchTestsStudents(StudentsFullSetup):
[7929]45    """Tests for the Interswitch payment gateway.
[7894]46    """
47
48    layer = FunctionalLayer
49
[7970]50    def setUp(self):
[8266]51        super(InterswitchTestsStudents, self).setUp()
[7970]52        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
[7995]53        self.browser.open(self.payments_path)
[7970]54        IWorkflowState(self.student).setState('cleared')
[9006]55        self.student.nationality = u'NG'
[7995]56        self.browser.open(self.payments_path + '/addop')
[9384]57        self.browser.getControl(name="form.p_category").value = ['schoolfee']
[7970]58        self.browser.getControl("Create ticket").click()
[9435]59        self.assertMatches('...ticket created...',
60                           self.browser.contents)
61        ctrl = self.browser.getControl(name='val_id')
[9775]62        self.value = ctrl.options[0]
63        self.browser.getLink(self.value).click()
[9435]64        self.assertMatches('...Amount Authorized...',
65                           self.browser.contents)
66        self.assertMatches(
67            '...<span>40000.0</span>...',
68            self.browser.contents)
69        self.payment_url = self.browser.url
[7970]70
[8263]71#    def callback_url(self, payment_url, resp, apprAmt):
72#        return payment_url + (
73#            '/isw_callback?echo=' +
74#            '&resp=%s' +
75#            '&desc=Something went wrong' +
76#            '&txnRef=p1331792385335' +
77#            '&payRef=' + '&retRef=' +
78#            '&cardNum=0' +
79#            '&apprAmt=%s' +
80#            '&url=http://xyz') % (resp, apprAmt)
[7894]81
[7970]82    def test_interswitch_form(self):
[7894]83        # Manager can access InterswitchForm
84        self.browser.getLink("CollegePAY", index=0).click()
[8266]85        # The total amount to be processed by Interswitch
86        # has been reduced by the Interswitch fee of 150 Nairas
[9389]87        self.assertMatches('...<input type="hidden" name="pay_item_id" value="5700" />...',
88                           self.browser.contents)
[9384]89        self.assertMatches('...Total Amount Authorized:...',
90                           self.browser.contents)
[9460]91        self.assertEqual(self.student.current_mode, 'ug_ft')
[7894]92        self.assertMatches(
[8276]93            '...<input type="hidden" name="amount" value="4000000.0" />...',
[7894]94            self.browser.contents)
[9384]95        self.assertMatches(
[9460]96            '...item_name="School Fee" item_amt="3835000" bank_id="8" acct_num="2017506430"...',
[9384]97            self.browser.contents)
98        self.assertMatches(
99            '...item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1010764827"...',
100            self.browser.contents)
[7894]101
[9520]102        # Create school fee ticket for returning students. Payment is made
103        # for next session.
104        current_payment_key = self.student['payments'].keys()[0]
[9389]105        self.certificate.study_mode = u'ug_pt'
106        IWorkflowState(self.student).setState('returning')
107        configuration = createObject('waeup.SessionConfiguration')
108        configuration.academic_session = 2005
109        self.app['configuration'].addSessionConfiguration(configuration)
[9384]110        self.browser.open(self.payments_path + '/addop')
[9389]111        self.browser.getControl(name="form.p_category").value = ['schoolfee']
[9384]112        self.browser.getControl("Create ticket").click()
[9570]113
114        ## Next session payment can't be made ...
115        #self.assertMatches(
116        #    '...You have not yet paid your current/active session...',
117        #    self.browser.contents)
118        ## current session payment must be approved first.
119        #self.student['payments'][current_payment_key].approve()
120        #self.browser.open(self.payments_path + '/addop')
121        #self.browser.getControl(name="form.p_category").value = ['schoolfee']
122        #self.browser.getControl("Create ticket").click()
123
[9384]124        ctrl = self.browser.getControl(name='val_id')
125        value = ctrl.options[1]
[9775]126        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
127        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
[9384]128        self.browser.getLink(value).click()
[9389]129        self.browser.getLink("CollegePAY", index=0).click()
[9775]130        # Split amounts have been set.
131        self.assertEqual(self.student['payments'][value].provider_amt, 1500.0)
132        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
[9389]133        self.assertMatches('...<input type="hidden" name="pay_item_id" value="5701" />...',
[9384]134                           self.browser.contents)
135        self.assertMatches(
[9389]136            '...<input type="hidden" name="amount" value="2000000.0" />...',
137            self.browser.contents)
138        self.assertMatches(
[9460]139            '...item_name="School Fee" item_amt="1835000" bank_id="16" acct_num="0122009929"...',
[9389]140            self.browser.contents)
141        self.assertMatches(
142            '...item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1010764827"...',
143            self.browser.contents)
144
145        # Create clearance fee ticket
[10902]146        #self.browser.open(self.payments_path + '/addop')
147        #self.browser.getControl(name="form.p_category").value = ['clearance']
148        #self.browser.getControl("Create ticket").click()
149        #ctrl = self.browser.getControl(name='val_id')
150        #value = ctrl.options[2]
151        #self.browser.getLink(value).click()
152        #self.assertMatches(
153        #    '...<span>45000.0</span>...',
154        #    self.browser.contents)
155        ## Manager can access InterswitchForm
156        #self.browser.getLink("CollegePAY", index=0).click()
157        #self.assertEqual(self.student['payments'][value].provider_amt, 1500.0)
158        #self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
159        #self.assertMatches('...<input type="hidden" name="pay_item_id" value="5702" />...',
160        #                   self.browser.contents)
161        #self.assertMatches('...Total Amount Authorized:...',
162        #                   self.browser.contents)
163        #self.assertMatches(
164        #    '...<input type="hidden" name="amount" value="4500000.0" />...',
165        #    self.browser.contents)
166        #self.assertMatches(
167        #    '...item_name="Acceptance Fee" item_amt="4335000" bank_id="7" acct_num="1003475516"...',
168        #    self.browser.contents)
169        #self.assertMatches(
170        #    '...item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1010764827"...',
171        #    self.browser.contents)
[9384]172
[9515]173        # Create gown fee ticket
174        configuration.maint_fee = 987.0
175        self.app['configuration']['2004'].gown_fee = 234.0
176        self.browser.open(self.payments_path + '/addop')
177        self.browser.getControl(name="form.p_category").value = ['gown']
178        self.browser.getControl("Create ticket").click()
179        ctrl = self.browser.getControl(name='val_id')
[10902]180        value = ctrl.options[2]
[9515]181        self.browser.getLink(value).click()
182        self.assertMatches(
183            '...<span>234.0</span>...',
184            self.browser.contents)
185        # Manager can access InterswitchForm
186        self.browser.getLink("CollegePAY", index=0).click()
[9775]187        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
188        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
[9515]189        self.assertMatches('...<input type="hidden" name="pay_item_id" value="5704" />...',
190                           self.browser.contents)
191        self.assertMatches('...Total Amount Authorized:...',
192                           self.browser.contents)
193        self.assertMatches(
194            '...<input type="hidden" name="amount" value="23400.0" />...',
195            self.browser.contents)
196        self.assertMatches(
[11478]197            '...<item_detail item_id="1" item_name="Gown Hire Fee" item_amt="8400" bank_id="16" acct_num="0122011401" />...',
[9515]198            self.browser.contents)
199        self.assertFalse(
200            'item_name="BT Education"' in self.browser.contents)
[9384]201
[9727]202        # Create temp maint fee ticket
203        self.browser.open(self.payments_path + '/addop')
204        self.browser.getControl(name="form.p_category").value = ['tempmaint_1']
205        self.browser.getControl("Create ticket").click()
206        ctrl = self.browser.getControl(name='val_id')
[10902]207        value = ctrl.options[3]
[9727]208        self.browser.getLink(value).click()
209        self.assertMatches(
[9728]210            '...<span>8150.0</span>...',
[9727]211            self.browser.contents)
212        # Manager can access InterswitchForm
213        self.browser.getLink("CollegePAY", index=0).click()
[9775]214        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
215        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
[9727]216        self.assertMatches('...<input type="hidden" name="pay_item_id" value="5705" />...',
217                           self.browser.contents)
218        self.assertMatches('...Total Amount Authorized:...',
219                           self.browser.contents)
220        self.assertMatches(
[9728]221            '...<input type="hidden" name="amount" value="815000.0" />...',
[9727]222            self.browser.contents)
223        self.assertMatches(
[11478]224            '...<item_detail item_id="1" item_name="Hostel Maintenance Fee" item_amt="800000" bank_id="17" acct_num="0009598925" />...',
[9727]225            self.browser.contents)
[9728]226        self.assertFalse(
227            'item_name="BT Education"' in self.browser.contents)
[9727]228
[9863]229        # Create previous session fee ticket
230        configuration = createObject('waeup.SessionConfiguration')
231        configuration.academic_session = 2003
232        configuration.clearance_fee = 3456.0
233        self.app['configuration'].addSessionConfiguration(configuration)
234        self.student['studycourse'].entry_session = 2002
[10902]235        #self.browser.open(self.payments_path + '/addpp')
236        #self.browser.getControl(name="form.p_category").value = ['clearance']
237        #self.browser.getControl(name="form.p_session").value = ['2003']
238        #self.browser.getControl(name="form.p_level").value = ['300']
239        #self.browser.getControl("Create ticket").click()
240        #ctrl = self.browser.getControl(name='val_id')
241        #value = ctrl.options[5]
242        #self.browser.getLink(value).click()
243        #self.assertMatches(
244        #    '...<span>45000.0</span>...',
245        #    self.browser.contents)
246        ## Manager can access InterswitchForm
247        #self.browser.getLink("CollegePAY", index=0).click()
248        #self.assertEqual(self.student['payments'][value].provider_amt, 1500.0)
249        #self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
250        #self.assertMatches('...<input type="hidden" name="pay_item_id" value="5702" />...',
251        #                   self.browser.contents)
252        #self.assertMatches('...Total Amount Authorized:...',
253        #                   self.browser.contents)
254        ##self.assertMatches(
255        #    '...<input type="hidden" name="amount" value="4500000.0" />...',
256        #    self.browser.contents)
257        #self.assertMatches(
258        #    '...<item_detail item_id="1" item_name="Acceptance Fee" item_amt="4335000" bank_id="7" acct_num="1003475516" />...',
259        #    self.browser.contents)
[9863]260
[9869]261        # Create balance payment ticket
262        self.browser.open(self.payments_path + '/addbp')
263        self.browser.getControl(name="form.p_category").value = ['schoolfee']
264        self.browser.getControl(name="form.balance_session").value = ['2003']
265        self.browser.getControl(name="form.balance_level").value = ['300']
266        self.browser.getControl(name="form.balance_amount").value = '200'
267        self.browser.getControl("Create ticket").click()
268        ctrl = self.browser.getControl(name='val_id')
[10902]269        value = ctrl.options[4]
[9869]270        self.browser.getLink(value).click()
271        self.assertMatches(
272            '...<span>200.0</span>...',
273            self.browser.contents)
274        # Manager can access InterswitchForm
275        self.browser.getLink("CollegePAY", index=0).click()
276        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
277        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
278        self.assertMatches('...<input type="hidden" name="pay_item_id" value="5701" />...',
279                           self.browser.contents)
280        self.assertMatches(
281            '...<input type="hidden" name="amount" value="20000.0" />...',
282            self.browser.contents)
283        self.assertMatches(
284            '...item_name="School Fee" item_amt="5000" bank_id="16" acct_num="0122009929"...',
285            self.browser.contents)
286        self.assertFalse(
287            'item_name="BT Education"' in self.browser.contents)
[9863]288
[8263]289#    @external_test
290#    def test_callback(self):
[7970]291
[7894]292        # Manager can call callback manually
[8263]293#        self.browser.open(self.callback_url(self.payment_url, 'XX', '300'))
294#        self.assertMatches('...Unsuccessful callback: Something went wrong...',
295#                          self.browser.contents)
296#        self.assertMatches('...Failed...',
297#                           self.browser.contents)
298#        self.browser.open(self.payment_url + '/isw_callback')
299#        self.assertMatches('...Unsuccessful callback: Incomplete query string...',
300#                          self.browser.contents)
301#        self.assertMatches('...Failed...',
302#                           self.browser.contents)
303#        self.browser.open(self.callback_url(self.payment_url, '00', '300000'))
304#        self.assertMatches('...Wrong amount...',
305#                          self.browser.contents)
306#        self.browser.open(self.callback_url(self.payment_url, '00', '4000000'))
307#        self.assertMatches('...Valid callback received...',
308#                          self.browser.contents)
[7930]309
[11645]310    def test_pay_twice(self):
311        # Create second ticket
312        self.browser.open(self.payments_path + '/addop')
313        self.browser.getControl(name="form.p_category").value = ['schoolfee']
314        self.browser.getControl("Create ticket").click()
315        # Pay first ticket
316        self.student['payments'][self.value].approve()
317        # Try to pay second ticket
318        self.browser.open(self.payments_path)
319        ctrl = self.browser.getControl(name='val_id')
320        value = ctrl.options[1]
321        self.browser.getLink(value).click()
322        self.browser.getLink("CollegePAY", index=0).click()
323        self.assertMatches(
324            '...alert alert-danger">This type of payment has already been made...',
325            self.browser.contents)
326
[7970]327    @external_test
[7930]328    def test_webservice(self):
[9775]329        # First we have open InterswitchPageStudent to set provider_amt
330        # and gateway_amt
331        self.browser.open(self.payment_url + '/goto_interswitch')
332        # Now we can call the webservice
[7970]333        self.browser.open(self.payment_url + '/request_webservice')
[7930]334        self.assertMatches('...Unsuccessful callback...',
335                          self.browser.contents)
[9713]336        # The payment is now in state failed ...
[8266]337        self.assertMatches('...<span>Failed</span>...',
338                          self.browser.contents)
[9713]339        # ... and the catalog has been updated
340        cat = getUtility(ICatalog, name='payments_catalog')
341        results = list(
342            cat.searchResults(p_state=('failed', 'failed')))
343        self.assertEqual(len(results), 1)
344        self.assertEqual(results[0].p_state, 'failed')
[8266]345
[9775]346        # Let's replace the p_id with a valid p_id of the Uniben
347        # live system. This is definitely not an appropriate
348        # solution for testing, but we have no choice since
349        # Interswitch doesn't provide any interface
350        # for testing.
351        payment = self.student['payments'][self.value]
352        payment.p_id = 'p3547789850240'
353        self.browser.open(self.payment_url + '/request_webservice')
354        self.assertMatches('...Callback amount does not match...',
355                          self.browser.contents)
356        # The payment is now in state failed ...
357        self.assertMatches('...<span>Failed</span>...',
358                          self.browser.contents)
359        # Let's replace the amount autorized with the amount of the
360        # live system payment
361        payment.amount_auth = payment.r_amount_approved
362        self.browser.open(self.payment_url + '/request_webservice')
363        self.assertMatches('...Successful payment...',
364                          self.browser.contents)
365        # The payment is now in state paid ...
366        self.assertMatches('...<span>Paid</span>...',
367                          self.browser.contents)
368        # ... and the catalog has been updated
369        cat = getUtility(ICatalog, name='payments_catalog')
370        results = list(
371            cat.searchResults(p_state=('paid', 'paid')))
372        self.assertEqual(len(results), 1)
373        self.assertEqual(results[0].p_state, 'paid')
374        # Approval is logged in students.log ...
375        logfile = os.path.join(
376            self.app['datacenter'].storage, 'logs', 'students.log')
377        logcontent = open(logfile).read()
378        self.assertTrue(
379            'zope.mgr - '
[11633]380            'waeup.uniben.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
[9775]381            'B1000000 - successful schoolfee payment: p3547789850240\n'
382            in logcontent)
383        # ... and in payments.log
384        logfile = os.path.join(
385            self.app['datacenter'].storage, 'logs', 'payments.log')
386        logcontent = open(logfile).read()
387        self.assertTrue(
388            '"zope.mgr",B1000000,p3547789850240,schoolfee,'
389            '12000.0,00,1500.0,150.0,0.0,,,\n'
390            in logcontent)
391
392
[8266]393class InterswitchTestsApplicants(ApplicantsFullSetup):
394    """Tests for the Interswitch payment gateway.
395    """
396
397    layer = FunctionalLayer
398
399    def setUp(self):
400        super(InterswitchTestsApplicants, self).setUp()
[10836]401        configuration = SessionConfiguration()
402        configuration.academic_session = datetime.now().year - 2
403        self.app['configuration'].addSessionConfiguration(configuration)
[8266]404        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
405        self.browser.open(self.manage_path)
406        #IWorkflowState(self.student).setState('started')
407        super(InterswitchTestsApplicants, self).fill_correct_values()
[8526]408        self.applicantscontainer.application_fee = 1000.0
[9453]409        self.browser.getControl(name="form.nationality").value = ['NG']
[8266]410        self.browser.getControl(name="transition").value = ['start']
411        self.browser.getControl("Save").click()
412        self.browser.getControl("Add online").click()
413        self.assertMatches('...ticket created...',
414                           self.browser.contents)
[8281]415        #ctrl = self.browser.getControl(name='val_id')
416        #value = ctrl.options[0]
417        #self.browser.getLink(value).click()
[10836]418        self.payment_url = self.browser.url
419
420    def test_interswitch_form(self):
[8266]421        self.assertMatches('...Amount Authorized...',
422                           self.browser.contents)
423        self.assertMatches(
424            '...<span>1000.0</span>...',
425            self.browser.contents)
426        # Manager can access InterswitchForm
427        self.browser.getLink("CollegePAY", index=0).click()
428        self.assertMatches('...Total Amount Authorized:...',
429                           self.browser.contents)
430        self.assertMatches(
[8276]431            '...<input type="hidden" name="amount" value="100000.0" />...',
[8266]432            self.browser.contents)
433
434    @external_test
435    def test_webservice(self):
436
437        self.browser.open(self.payment_url + '/request_webservice')
438        self.assertMatches('...Unsuccessful callback...',
439                          self.browser.contents)
440        # The payment is now in state failed
441        self.assertMatches('...<span>Failed</span>...',
442                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.