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

Last change on this file since 15303 was 15303, checked in by Henrik Bettermann, 6 years ago

Deactivate Interswitch tests. FCEOkene does no longer use this service.

  • Property svn:keywords set to Id
File size: 22.4 KB
Line 
1## $Id: tests.py 15303 2019-01-24 14:33: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, timedelta, date
20from zope.component import 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.fceokene.testing import FunctionalLayer
27
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.
31EXTERNAL_TESTS = False
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
44class InterswitchTestsStudents(StudentsFullSetup):
45    """Tests for the Interswitch payment gateway.
46    """
47
48    layer = FunctionalLayer
49
50    def setUp(self):
51        super(InterswitchTestsStudents, self).setUp()
52        self.app['configuration']['2004'].interswitch_enabled = True
53        self.certificate.study_mode = 'pd_ft'
54        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
55        self.browser.open(self.payments_path)
56        IWorkflowState(self.student).setState('cleared')
57        self.browser.open(self.payments_path + '/addop')
58        self.browser.getControl(name="form.p_category").value = ['schoolfee']
59        self.browser.getControl("Create ticket").click()
60        self.assertMatches('...ticket created...',
61                           self.browser.contents)
62        ctrl = self.browser.getControl(name='val_id')
63        self.value = ctrl.options[0]
64        self.browser.getLink(self.value).click()
65        self.assertMatches('...Amount Authorized...',
66                           self.browser.contents)
67        self.assertTrue(
68            '<span>70450.0</span>' in self.browser.contents)
69        self.payment_url = self.browser.url
70        self.payment = self.student['payments'][self.value]
71
72
73#    def callback_url(self, payment_url, resp, apprAmt):
74#        return payment_url + (
75#            '/isw_callback?echo=' +
76#            '&resp=%s' +
77#            '&desc=Something went wrong' +
78#            '&txnRef=p1331792385335' +
79#            '&payRef=' + '&retRef=' +
80#            '&cardNum=0' +
81#            '&apprAmt=%s' +
82#            '&url=http://xyz') % (resp, apprAmt)
83
84    def deactivated_test_interswitch_form(self):
85
86        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
87        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
88        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0)
89        # Manager can access InterswitchForm for the created school fee ticket
90        self.browser.getLink("CollegePAY", index=0).click()
91        # Split amounts have been set
92        self.assertEqual(self.student['payments'][self.value].provider_amt, 1600.0)
93        self.assertEqual(self.student['payments'][self.value].gateway_amt, 150.0)
94        self.assertMatches('...Total Amount Authorized:...',
95                           self.browser.contents)
96        self.assertTrue(
97            '<input type="hidden" name="amount" value="7045000" />'
98            in self.browser.contents)
99        self.assertTrue(
100            'item_name="School Fee" item_amt="6870000" bank_id="8" acct_num="2003670143"'
101            in self.browser.contents)
102        self.assertTrue(
103            'item_name="WAeAC" item_amt="160000" bank_id="31" acct_num="0773411069"'
104            in self.browser.contents)
105
106        # Third semester payment
107        self.certificate.study_mode = 'nce_ft'
108        self.app['configuration']['2004'].third_semester_fee = 10000.0
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 = ['third_semester']
113        self.browser.getControl("Create ticket").click()
114        ctrl = self.browser.getControl(name='val_id')
115        value = ctrl.options[1]
116        self.browser.getLink(value).click()
117        self.browser.getLink("CollegePAY", index=0).click()
118        self.assertTrue(
119            '<input type="hidden" name="amount" value="808800" />'
120            in self.browser.contents)
121        self.assertTrue(
122            'item_name="NCE Third Semester Fee" item_amt="633800" bank_id="8" acct_num="2003670143"'
123            in self.browser.contents)
124        self.assertTrue(
125            'item_name="WAeAC" item_amt="160000" bank_id="31" acct_num="0773411069"'
126            in self.browser.contents)
127
128        # Maintenance fee payment
129        self.certificate.study_mode = 'nce_ft'
130        self.browser.open(self.payments_path)
131        self.browser.open(self.payments_path + '/addop')
132        self.browser.getControl(
133            name="form.p_category").value = ['hostel_maintenance']
134        self.browser.getControl("Create ticket").click()
135        self.assertMatches('...You have not yet booked accommodation...',
136                           self.browser.contents)
137        # Students have to book bed first
138        self.browser.open(self.acco_path)
139        IWorkflowState(self.student).setState('admitted')
140        self.browser.getControl("Book accommodation").click()
141        self.assertFalse('Activation Code:' in self.browser.contents)
142        self.browser.getControl("Create bed ticket").click()
143        # Bed is randomly selected but, since there is only
144        # one bed for this student, we know that ...
145        self.assertFalse('Hall 1, Block A, Room 101, Bed A'
146                           in self.browser.contents)
147        self.assertTrue('(see payment slip)'
148                           in self.browser.contents)
149        self.assertTrue('ticket created'
150                           in self.browser.contents)
151        self.browser.open(self.payments_path + '/addop')
152        self.browser.getControl(
153            name="form.p_category").value = ['hostel_maintenance']
154        self.browser.getControl("Create ticket").click()
155        self.assertMatches('...ticket created...',
156                           self.browser.contents)
157        ctrl = self.browser.getControl(name='val_id')
158        value = ctrl.options[2]
159        self.browser.getLink(value).click()
160        self.assertMatches('...Amount Authorized...',
161                           self.browser.contents)
162        self.assertTrue(
163            '<span>1026.0</span>' in self.browser.contents)
164        # p_item is not unveiled
165        self.assertFalse('Hall 1, Block A, Room 101, Bed A'
166            in self.browser.contents)
167        self.assertTrue('(visible after successful payment)'
168            in self.browser.contents)
169        self.payment_url = self.browser.url
170        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
171        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
172        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
173        # Manager can access InterswitchForm
174        self.browser.getLink("CollegePAY", index=0).click()
175        # Split amounts have been set
176        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
177        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
178        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
179        # The total amount to be processed by Interswitch
180        # has been reduced by the Interswitch fee of 150 Nairas
181        self.assertMatches('...Total Amount Authorized:...',
182                           self.browser.contents)
183        self.assertTrue(
184            '<input type="hidden" name="amount" value="102600" />'
185            in self.browser.contents)
186        self.assertTrue(
187            'item_name="Hostel Maintenance Fee" item_amt="87600" bank_id="8" acct_num="2003670143"'
188            in self.browser.contents)
189        # BT does nor charge a fee for maintenance fee
190        self.assertFalse("BT Education" in self.browser.contents)
191        # p_item is not unveiled
192        self.assertFalse('Hall 1, Block A, Room 101, Bed A'
193            in self.browser.contents)
194        self.assertTrue('(visible after successful payment)'
195            in self.browser.contents)
196        # If the ticket is paid coordinates are shown
197        self.student['payments'][value].p_state = 'paid'
198        self.browser.open(self.payment_url)
199        self.assertTrue('Hall 1, Block A, Room 101, Bed A'
200            in self.browser.contents)
201        self.assertFalse('(visible after successful payment)'
202            in self.browser.contents)
203
204        # Acceptance fee payment
205
206        self.browser.open(self.payments_path)
207        self.browser.open(self.payments_path + '/addop')
208        self.browser.getControl(
209            name="form.p_category").value = ['clearance']
210        self.browser.getControl("Create ticket").click()
211        self.assertMatches('...ticket created...',
212                           self.browser.contents)
213        ctrl = self.browser.getControl(name='val_id')
214        value = ctrl.options[3]
215        self.browser.getLink(value).click()
216        self.assertMatches('...Amount Authorized...',
217                           self.browser.contents)
218        self.assertTrue(
219            '<span>3606.0</span>' in self.browser.contents)
220        self.payment_url = self.browser.url
221        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
222        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
223        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
224        # Manager can access InterswitchForm
225        self.browser.getLink("CollegePAY", index=0).click()
226        # Split amounts have been set
227        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
228        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
229        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
230        # The total amount to be processed by Interswitch
231        # has been reduced by the Interswitch fee of 150 Nairas
232        self.assertMatches('...Total Amount Authorized:...',
233                           self.browser.contents)
234        self.assertTrue(
235            '<input type="hidden" name="amount" value="360600" />' in
236            self.browser.contents)
237        self.assertTrue(
238            'item_name="Acceptance Fee" item_amt="345600" bank_id="8" acct_num="2003670143"'
239            in self.browser.contents)
240        # BT does nor charge a fee for maintenance fee
241        self.assertFalse("BT Education" in self.browser.contents)
242
243    def deactivated_test_interswitch_form_ticket_expired(self):
244        # Manager can access InterswitchForm
245        self.browser.getLink("CollegePAY", index=0).click()
246        self.assertMatches('...<input type="hidden" name="pay_item_id" value="8302" />...',
247                           self.browser.contents)
248        self.assertMatches('...Total Amount Authorized:...',
249                           self.browser.contents)
250        self.assertEqual(self.student.current_mode, 'pd_ft')
251        self.assertMatches(
252            '...<input type="hidden" name="amount" value="4000000" />...',
253            self.browser.contents)
254        delta = timedelta(days=8)
255        self.payment.creation_date -= delta
256        self.browser.open(self.payment_url)
257        self.browser.getLink("CollegePAY", index=0).click()
258        self.assertMatches(
259            '...This payment ticket is too old. Please create a new ticket...',
260            self.browser.contents)
261        delta = timedelta(days=2)
262        self.payment.creation_date += delta
263        self.browser.open(self.payment_url)
264        self.browser.getLink("CollegePAY", index=0).click()
265        self.assertMatches('...Total Amount Authorized:...',
266                           self.browser.contents)
267
268#    @external_test
269#    def test_callback(self):
270
271        # Manager can call callback manually
272#        self.browser.open(self.callback_url(self.payment_url, 'XX', '300'))
273#        self.assertMatches('...Unsuccessful callback: Something went wrong...',
274#                          self.browser.contents)
275#        self.assertMatches('...Failed...',
276#                           self.browser.contents)
277#        self.browser.open(self.payment_url + '/isw_callback')
278#        self.assertMatches('...Unsuccessful callback: Incomplete query string...',
279#                          self.browser.contents)
280#        self.assertMatches('...Failed...',
281#                           self.browser.contents)
282#        self.browser.open(self.callback_url(self.payment_url, '00', '300000'))
283#        self.assertMatches('...Wrong amount...',
284#                          self.browser.contents)
285#        self.browser.open(self.callback_url(self.payment_url, '00', '4000000'))
286#        self.assertMatches('...Valid callback received...',
287#                          self.browser.contents)
288
289    @external_test
290    def test_webservice(self):
291        # First we have open InterswitchPageStudent to set provider_amt
292        # and gateway_amt
293        self.browser.open(self.payment_url + '/goto_interswitch')
294        # Now we can call the webservice
295        self.browser.open(self.payment_url + '/request_webservice')
296        #self.assertMatches('...Unsuccessful callback...',
297        #                  self.browser.contents)
298        self.assertMatches('...Unsuccessful callback...',
299                          self.browser.contents)
300        # The payment is now in state failed ...
301        self.assertMatches('...<span>Failed</span>...',
302                          self.browser.contents)
303        # ... and the catalog has been updated
304        cat = getUtility(ICatalog, name='payments_catalog')
305        results = list(
306            cat.searchResults(p_state=('failed', 'failed')))
307        self.assertEqual(len(results), 1)
308        self.assertEqual(results[0].p_state, 'failed')
309
310        # Let's replace the p_id with a valid p_id of the FCEOkene
311        # live system. This is definitely not an appropriate
312        # solution for testing, but we have no choice since
313        # Interswitch doesn't provide any interface
314        # for testing.
315        payment = self.student['payments'][self.value]
316        payment.p_id = 'p3536651296379'
317        self.browser.open(self.payment_url + '/request_webservice')
318        self.assertMatches('...Callback amount does not match...',
319                          self.browser.contents)
320        # The payment is now in state failed ...
321        self.assertMatches('...<span>Failed</span>...',
322                          self.browser.contents)
323        # Let's replace the amount autorized with the amount of the
324        # live system payment
325        payment.amount_auth = payment.r_amount_approved
326        self.browser.open(self.payment_url + '/request_webservice')
327        self.assertMatches('...Successful payment...',
328                          self.browser.contents)
329        # The payment is now in state paid ...
330        self.assertMatches('...<span>Paid</span>...',
331                          self.browser.contents)
332        # ... and the catalog has been updated
333        cat = getUtility(ICatalog, name='payments_catalog')
334        results = list(
335            cat.searchResults(p_state=('paid', 'paid')))
336        self.assertEqual(len(results), 1)
337        self.assertEqual(results[0].p_state, 'paid')
338        # Approval is logged in students.log ...
339        logfile = os.path.join(
340            self.app['datacenter'].storage, 'logs', 'students.log')
341        logcontent = open(logfile).read()
342        self.assertTrue(
343            'zope.mgr - '
344            'waeup.fceokene.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
345            'K1000000 - successful schoolfee payment: p3536651296379\n'
346            in logcontent)
347        # ... and in payments.log
348        logfile = os.path.join(
349            self.app['datacenter'].storage, 'logs', 'payments.log')
350        logcontent = open(logfile).read()
351        self.assertTrue(
352            '"zope.mgr",K1000000,p3536651296379,schoolfee,'
353            '3150.0,00,1600.0,150.0,1400.0,,,\n'
354            in logcontent)
355
356class InterswitchTestsApplicants(ApplicantsFullSetup):
357    """Tests for the Interswitch payment gateway.
358    """
359
360    layer = FunctionalLayer
361
362    def setUp(self):
363        super(InterswitchTestsApplicants, self).setUp()
364        configuration = SessionConfiguration()
365        configuration.academic_session = datetime.now().year - 2
366        self.app['configuration'].addSessionConfiguration(configuration)
367        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
368        self.browser.open(self.manage_path)
369        #IWorkflowState(self.student).setState('started')
370        super(InterswitchTestsApplicants, self).fill_correct_values()
371        self.applicantscontainer.application_fee = 1000.0
372        self.browser.getControl(name="form.nationality").value = ['NG']
373        self.browser.getControl(name="transition").value = ['start']
374        self.browser.getControl("Save").click()
375
376    def deactivated_test_interswitch_form(self):
377        self.browser.getControl("Add online").click()
378        self.assertMatches('...ticket created...',
379                           self.browser.contents)
380        self.assertMatches(
381            '...<span>1000.0</span>...',
382            self.browser.contents)
383        self.payment_url = self.browser.url
384        self.browser.open(self.manage_path)
385        ctrl = self.browser.getControl(name='val_id')
386        self.value = ctrl.options[0]
387        # Manager can access InterswitchForm
388        self.browser.open(self.payment_url)
389        self.browser.getLink("CollegePAY", index=0).click()
390        self.assertMatches('...Total Amount Authorized:...',
391                           self.browser.contents)
392        self.assertTrue(
393            '<input type="hidden" name="amount" value="100000" />'
394            in self.browser.contents)
395        self.assertTrue(
396            '<item_detail item_id="1" item_name="application"'
397            ' item_amt="35000" bank_id="8" acct_num="2003670143" />'
398            in self.browser.contents)
399        delta = timedelta(days=8)
400        self.applicant[self.value].creation_date -= delta
401        self.browser.open(self.payment_url)
402        self.browser.getLink("CollegePAY", index=0).click()
403        self.assertMatches(
404            '...This payment ticket is too old. Please create a new ticket...',
405            self.browser.contents)
406
407    @external_test
408    def test_webservice(self):
409        self.browser.getControl("Add online").click()
410        self.assertMatches('...ticket created...',
411                           self.browser.contents)
412        self.assertMatches(
413            '...<span>1000.0</span>...',
414            self.browser.contents)
415        self.payment_url = self.browser.url
416        self.browser.open(self.manage_path)
417        ctrl = self.browser.getControl(name='val_id')
418        self.value = ctrl.options[0]
419        # First we have open InterswitchPageStudent to set provider_amt
420        # and gateway_amt
421        self.browser.open(self.payment_url + '/goto_interswitch')
422        # Now we can call the webservice
423        self.browser.open(self.payment_url + '/request_webservice')
424        self.assertMatches('...Unsuccessful callback...',
425                          self.browser.contents)
426        # The payment is now in state failed ...
427        self.assertMatches('...<span>Failed</span>...',
428                          self.browser.contents)
429        # ... and the catalog has been updated
430        cat = getUtility(ICatalog, name='payments_catalog')
431        results = list(
432            cat.searchResults(p_state=('failed', 'failed')))
433        self.assertEqual(len(results), 1)
434        self.assertEqual(results[0].p_state, 'failed')
435
436        # Let's replace the p_id with a valid p_id of the Uniben
437        # live system. This is definitely not an appropriate
438        # solution for testing, but we have no choice since
439        # Interswitch doesn't provide any interface
440        # for testing.
441        payment = self.applicant[self.value]
442        payment.p_id = 'p3536651296379'
443        self.browser.open(self.payment_url + '/request_webservice')
444        self.assertMatches('...Callback amount does not match...',
445                          self.browser.contents)
446        # The payment is now in state failed ...
447        self.assertMatches('...<span>Failed</span>...',
448                          self.browser.contents)
449        # Let's replace the amount autorized with the amount of the
450        # live system payment
451        payment.amount_auth = payment.r_amount_approved
452        self.browser.open(self.payment_url + '/request_webservice')
453        self.assertMatches('...Successful payment...',
454                          self.browser.contents)
455        # The payment is now in state paid ...
456        self.assertMatches('...<span>Paid</span>...',
457                          self.browser.contents)
458        # ... and the catalog has been updated
459        cat = getUtility(ICatalog, name='payments_catalog')
460        results = list(
461            cat.searchResults(p_state=('paid', 'paid')))
462        self.assertEqual(len(results), 1)
463        self.assertEqual(results[0].p_state, 'paid')
464        # Approval is logged in students.log ...
465        logfile = os.path.join(
466            self.app['datacenter'].storage, 'logs', 'applicants.log')
467        logcontent = open(logfile).read()
468        self.assertTrue(
469            'zope.mgr - '
470            'waeup.fceokene.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageApplicant - '
471            '%s - successful payment: p3536651296379\n'
472            % self.applicant.applicant_id in logcontent)
473        # ... and in payments.log
474        logfile = os.path.join(
475            self.app['datacenter'].storage, 'logs', 'payments.log')
476        logcontent = open(logfile).read()
477        self.assertTrue(
478            '"zope.mgr",%s,p3536651296379,application,'
479            '3150.0,00,500.0,150.0,0.0,,,\n' % self.applicant.applicant_id
480            in logcontent)
Note: See TracBrowser for help on using the repository browser.