source: main/waeup.fceokene/trunk/src/waeup/fceokene/interswitch/tests.py @ 10690

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

Add note on payment slips.

  • Property svn:keywords set to Id
File size: 19.3 KB
RevLine 
[7894]1## $Id: tests.py 10529 2013-08-23 10:38:21Z 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##
[9780]18import os
[9711]19from zope.component import getUtility
20from zope.catalog.interfaces import ICatalog
[7894]21from hurry.workflow.interfaces import IWorkflowState
22from waeup.kofa.students.tests.test_browser import StudentsFullSetup
[8266]23from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
24from waeup.kofa.configuration import SessionConfiguration
[8460]25from waeup.fceokene.testing import FunctionalLayer
[7894]26
[7970]27# Also run tests that send requests to external servers?
28#   If you enable this, please make sure the external services
29#   do exist really and are not bothered by being spammed by a test programme.
[9903]30EXTERNAL_TESTS = False
[7970]31
32def external_test(func):
33    if not EXTERNAL_TESTS:
34        myself = __file__
35        if myself.endswith('.pyc'):
36            myself = myself[:-2]
37        print "WARNING: external tests are skipped!"
38        print "WARNING: edit %s to enable them." % myself
39        return
40    return func
41
42
[8266]43class InterswitchTestsStudents(StudentsFullSetup):
[7929]44    """Tests for the Interswitch payment gateway.
[7894]45    """
46
47    layer = FunctionalLayer
48
[7970]49    def setUp(self):
[8266]50        super(InterswitchTestsStudents, self).setUp()
[9943]51        self.certificate.study_mode = 'nce_ft'
[7970]52        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
[7995]53        self.browser.open(self.payments_path)
[7970]54        IWorkflowState(self.student).setState('cleared')
[7995]55        self.browser.open(self.payments_path + '/addop')
[9613]56        self.browser.getControl(name="form.p_category").value = ['schoolfee']
[7970]57        self.browser.getControl("Create ticket").click()
58        self.assertMatches('...ticket created...',
59                           self.browser.contents)
60        ctrl = self.browser.getControl(name='val_id')
[9780]61        self.value = ctrl.options[0]
62        self.browser.getLink(self.value).click()
[7970]63        self.assertMatches('...Amount Authorized...',
64                           self.browser.contents)
[9956]65        self.assertTrue(
[10388]66            '<span>12645.0</span>' in self.browser.contents)
[7970]67        self.payment_url = self.browser.url
68
69
[8263]70#    def callback_url(self, payment_url, resp, apprAmt):
71#        return payment_url + (
72#            '/isw_callback?echo=' +
73#            '&resp=%s' +
74#            '&desc=Something went wrong' +
75#            '&txnRef=p1331792385335' +
76#            '&payRef=' + '&retRef=' +
77#            '&cardNum=0' +
78#            '&apprAmt=%s' +
79#            '&url=http://xyz') % (resp, apprAmt)
[7894]80
[7970]81    def test_interswitch_form(self):
[7894]82
[9780]83        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
84        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
85        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0)
[9613]86        # Manager can access InterswitchForm for the created school fee ticket
[7894]87        self.browser.getLink("CollegePAY", index=0).click()
[9780]88        # Split amounts have been set
89        self.assertEqual(self.student['payments'][self.value].provider_amt, 1600.0)
90        self.assertEqual(self.student['payments'][self.value].gateway_amt, 150.0)
91        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 1400.0)
[7894]92        self.assertMatches('...Total Amount Authorized:...',
93                           self.browser.contents)
[9956]94        self.assertTrue(
[10388]95            '<input type="hidden" name="amount" value="1264500.0" />'
[9956]96            in self.browser.contents)
97        self.assertTrue(
[10388]98            'item_name="School Fee" item_amt="949500" bank_id="117" acct_num="1012044015"'
[9956]99            in self.browser.contents)
100        self.assertTrue(
101            'item_name="FCEOkene Split" item_amt="140000" bank_id="117" acct_num="1012044039"'
102            in self.browser.contents)
103        self.assertTrue(
104            'item_name="BT Education" item_amt="160000" bank_id="31" acct_num="0026781725"'
105            in self.browser.contents)
[7894]106
[9998]107        # Maintenance fee payment
[9613]108
109        self.browser.open(self.payments_path)
110        self.browser.open(self.payments_path + '/addop')
111        self.browser.getControl(
112            name="form.p_category").value = ['hostel_maintenance']
113        self.browser.getControl("Create ticket").click()
114        self.assertMatches('...You have not yet booked accommodation...',
115                           self.browser.contents)
116        # Students have to book bed first
117        self.browser.open(self.acco_path)
118        IWorkflowState(self.student).setState('admitted')
119        self.browser.getLink("Book accommodation").click()
120        self.assertFalse('Activation Code:' in self.browser.contents)
121        self.browser.getControl("Create bed ticket").click()
122        # Bed is randomly selected but, since there is only
123        # one bed for this student, we know that ...
[9998]124        self.assertFalse('Hall 1, Block A, Room 101, Bed A'
125                           in self.browser.contents)
126        self.assertTrue('(see payment slip)'
127                           in self.browser.contents)
128        self.assertTrue('ticket created'
129                           in self.browser.contents)
[9613]130        self.browser.open(self.payments_path + '/addop')
131        self.browser.getControl(
132            name="form.p_category").value = ['hostel_maintenance']
133        self.browser.getControl("Create ticket").click()
134        self.assertMatches('...ticket created...',
135                           self.browser.contents)
136        ctrl = self.browser.getControl(name='val_id')
137        value = ctrl.options[1]
138        self.browser.getLink(value).click()
139        self.assertMatches('...Amount Authorized...',
140                           self.browser.contents)
[9956]141        self.assertTrue(
[10520]142            '<span>4150.0</span>' in self.browser.contents)
[9998]143        # p_item is not unveiled
144        self.assertFalse('Hall 1, Block A, Room 101, Bed A'
145            in self.browser.contents)
146        self.assertTrue('(visible after successful payment)'
147            in self.browser.contents)
[9613]148        self.payment_url = self.browser.url
[9780]149        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
150        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
151        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
[9613]152        # Manager can access InterswitchForm
153        self.browser.getLink("CollegePAY", index=0).click()
[9780]154        # Split amounts have been set
155        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
156        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
157        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
[9613]158        # The total amount to be processed by Interswitch
159        # has been reduced by the Interswitch fee of 150 Nairas
160        self.assertMatches('...Total Amount Authorized:...',
161                           self.browser.contents)
[9956]162        self.assertTrue(
[10520]163            '<input type="hidden" name="amount" value="415000.0" />'
[9956]164            in self.browser.contents)
165        self.assertTrue(
[10520]166            'item_name="Hostel Maintenance Fee" item_amt="400000" bank_id="117" acct_num="1012044132"'
[9956]167            in self.browser.contents)
168        # BT does nor charge a fee for maintenance fee
169        self.assertFalse("BT Education" in self.browser.contents)
[9998]170        # p_item is not unveiled
171        self.assertFalse('Hall 1, Block A, Room 101, Bed A'
172            in self.browser.contents)
173        self.assertTrue('(visible after successful payment)'
174            in self.browser.contents)
175        # If the ticket is paid coordinates are shown
176        self.student['payments'][value].p_state = 'paid'
177        self.browser.open(self.payment_url)
178        self.assertTrue('Hall 1, Block A, Room 101, Bed A'
179            in self.browser.contents)
180        self.assertFalse('(visible after successful payment)'
181            in self.browser.contents)
[9956]182
[9998]183        # Acceptance fee payment
[9956]184
185        self.browser.open(self.payments_path)
186        self.browser.open(self.payments_path + '/addop')
187        self.browser.getControl(
188            name="form.p_category").value = ['clearance']
189        self.browser.getControl("Create ticket").click()
190        self.assertMatches('...ticket created...',
191                           self.browser.contents)
192        ctrl = self.browser.getControl(name='val_id')
193        value = ctrl.options[2]
194        self.browser.getLink(value).click()
195        self.assertMatches('...Amount Authorized...',
196                           self.browser.contents)
197        self.assertTrue(
[10388]198            '<span>3606.0</span>' in self.browser.contents)
[9956]199        self.payment_url = self.browser.url
200        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
201        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
202        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
203        # Manager can access InterswitchForm
204        self.browser.getLink("CollegePAY", index=0).click()
205        # Split amounts have been set
206        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
207        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
208        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
209        # The total amount to be processed by Interswitch
210        # has been reduced by the Interswitch fee of 150 Nairas
211        self.assertMatches('...Total Amount Authorized:...',
212                           self.browser.contents)
213        self.assertTrue(
[10388]214            '<input type="hidden" name="amount" value="360600.0" />' in
[9613]215            self.browser.contents)
[9956]216        self.assertTrue(
[10388]217            'item_name="Acceptance Fee" item_amt="345600" bank_id="8" acct_num="2003670143"'
[9956]218            in self.browser.contents)
[9613]219        # BT does nor charge a fee for maintenance fee
220        self.assertFalse("BT Education" in self.browser.contents)
221
[8263]222#    @external_test
223#    def test_callback(self):
[7970]224
[7894]225        # Manager can call callback manually
[8263]226#        self.browser.open(self.callback_url(self.payment_url, 'XX', '300'))
227#        self.assertMatches('...Unsuccessful callback: Something went wrong...',
228#                          self.browser.contents)
229#        self.assertMatches('...Failed...',
230#                           self.browser.contents)
231#        self.browser.open(self.payment_url + '/isw_callback')
232#        self.assertMatches('...Unsuccessful callback: Incomplete query string...',
233#                          self.browser.contents)
234#        self.assertMatches('...Failed...',
235#                           self.browser.contents)
236#        self.browser.open(self.callback_url(self.payment_url, '00', '300000'))
237#        self.assertMatches('...Wrong amount...',
238#                          self.browser.contents)
239#        self.browser.open(self.callback_url(self.payment_url, '00', '4000000'))
240#        self.assertMatches('...Valid callback received...',
241#                          self.browser.contents)
[7930]242
[7970]243    @external_test
[7930]244    def test_webservice(self):
[9780]245        # First we have open InterswitchPageStudent to set provider_amt
246        # and gateway_amt
247        self.browser.open(self.payment_url + '/goto_interswitch')
248        # Now we can call the webservice
[7970]249        self.browser.open(self.payment_url + '/request_webservice')
[9711]250        #self.assertMatches('...Unsuccessful callback...',
251        #                  self.browser.contents)
[9780]252        self.assertMatches('...Unsuccessful callback...',
[7930]253                          self.browser.contents)
[9711]254        # The payment is now in state failed ...
[8266]255        self.assertMatches('...<span>Failed</span>...',
256                          self.browser.contents)
[9711]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')
[8266]263
[9790]264        # Let's replace the p_id with a valid p_id of the FCEOkene
[9780]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 = 'p3536651296379'
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            'waeup.fceokene.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - '
299            'K1000000 - successful schoolfee payment: p3536651296379\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",K1000000,p3536651296379,schoolfee,'
307            '3150.0,00,1600.0,150.0,1400.0,,,\n'
308            in logcontent)
309
[8266]310class InterswitchTestsApplicants(ApplicantsFullSetup):
311    """Tests for the Interswitch payment gateway.
312    """
313
314    layer = FunctionalLayer
315
316    def setUp(self):
317        super(InterswitchTestsApplicants, self).setUp()
318        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
319        self.browser.open(self.manage_path)
320        #IWorkflowState(self.student).setState('started')
321        super(InterswitchTestsApplicants, self).fill_correct_values()
[8528]322        self.applicantscontainer.application_fee = 1000.0
[9455]323        self.browser.getControl(name="form.nationality").value = ['NG']
[8266]324        self.browser.getControl(name="transition").value = ['start']
325        self.browser.getControl("Save").click()
326        self.browser.getControl("Add online").click()
327        self.assertMatches('...ticket created...',
328                           self.browser.contents)
329        self.assertMatches('...Amount Authorized...',
330                           self.browser.contents)
331        self.assertMatches(
332            '...<span>1000.0</span>...',
333            self.browser.contents)
334        self.payment_url = self.browser.url
[9780]335        self.browser.open(self.manage_path)
336        ctrl = self.browser.getControl(name='val_id')
337        self.value = ctrl.options[0]
[8266]338
339
340    def test_interswitch_form(self):
341
342        # Manager can access InterswitchForm
[9780]343        self.browser.open(self.payment_url)
[8266]344        self.browser.getLink("CollegePAY", index=0).click()
345        self.assertMatches('...Total Amount Authorized:...',
346                           self.browser.contents)
[9956]347        self.assertTrue(
348            '<input type="hidden" name="amount" value="100000.0" />'
349            in self.browser.contents)
[10528]350        self.assertTrue(
351            '<item_detail item_id="1" item_name="application"'
352            ' item_amt="35000" bank_id="117" acct_num="1012445289" />'
353            in self.browser.contents)
[8266]354
[10528]355
[8266]356    @external_test
357    def test_webservice(self):
[9780]358        # First we have open InterswitchPageStudent to set provider_amt
359        # and gateway_amt
360        self.browser.open(self.payment_url + '/goto_interswitch')
361        # Now we can call the webservice
[8266]362        self.browser.open(self.payment_url + '/request_webservice')
363        self.assertMatches('...Unsuccessful callback...',
364                          self.browser.contents)
[9780]365        # The payment is now in state failed ...
[8266]366        self.assertMatches('...<span>Failed</span>...',
367                          self.browser.contents)
[9780]368        # ... and the catalog has been updated
369        cat = getUtility(ICatalog, name='payments_catalog')
370        results = list(
371            cat.searchResults(p_state=('failed', 'failed')))
372        self.assertEqual(len(results), 1)
373        self.assertEqual(results[0].p_state, 'failed')
374
375        # Let's replace the p_id with a valid p_id of the Uniben
376        # live system. This is definitely not an appropriate
377        # solution for testing, but we have no choice since
378        # Interswitch doesn't provide any interface
379        # for testing.
380        payment = self.applicant[self.value]
381        payment.p_id = 'p3536651296379'
382        self.browser.open(self.payment_url + '/request_webservice')
383        self.assertMatches('...Callback amount does not match...',
384                          self.browser.contents)
385        # The payment is now in state failed ...
386        self.assertMatches('...<span>Failed</span>...',
387                          self.browser.contents)
388        # Let's replace the amount autorized with the amount of the
389        # live system payment
390        payment.amount_auth = payment.r_amount_approved
391        self.browser.open(self.payment_url + '/request_webservice')
392        self.assertMatches('...Successful payment...',
393                          self.browser.contents)
394        # The payment is now in state paid ...
395        self.assertMatches('...<span>Paid</span>...',
396                          self.browser.contents)
397        # ... and the catalog has been updated
398        cat = getUtility(ICatalog, name='payments_catalog')
399        results = list(
400            cat.searchResults(p_state=('paid', 'paid')))
401        self.assertEqual(len(results), 1)
402        self.assertEqual(results[0].p_state, 'paid')
403        # Approval is logged in students.log ...
404        logfile = os.path.join(
405            self.app['datacenter'].storage, 'logs', 'applicants.log')
406        logcontent = open(logfile).read()
407        self.assertTrue(
408            'zope.mgr - '
409            'waeup.fceokene.interswitch.browser.InterswitchPaymentRequestWebservicePageApplicant - '
410            '%s - successful payment: p3536651296379\n'
411            % self.applicant.applicant_id in logcontent)
412        # ... and in payments.log
413        logfile = os.path.join(
414            self.app['datacenter'].storage, 'logs', 'payments.log')
415        logcontent = open(logfile).read()
416        self.assertTrue(
417            '"zope.mgr",%s,p3536651296379,application,'
418            '3150.0,00,500.0,150.0,0.0,,,\n' % self.applicant.applicant_id
419            in logcontent)
Note: See TracBrowser for help on using the repository browser.