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

Last change on this file since 9954 was 9951, checked in by Henrik Bettermann, 12 years ago

Change school fee bank accounts.

  • Property svn:keywords set to Id
File size: 16.4 KB
Line 
1## $Id: tests.py 9951 2013-02-15 08:03:44Z 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 zope.component import getUtility
20from zope.catalog.interfaces import ICatalog
21from hurry.workflow.interfaces import IWorkflowState
22from waeup.kofa.students.tests.test_browser import StudentsFullSetup
23from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
24from waeup.kofa.configuration import SessionConfiguration
25from waeup.fceokene.testing import FunctionalLayer
26
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.
30EXTERNAL_TESTS = False
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
43class InterswitchTestsStudents(StudentsFullSetup):
44    """Tests for the Interswitch payment gateway.
45    """
46
47    layer = FunctionalLayer
48
49    def setUp(self):
50        super(InterswitchTestsStudents, self).setUp()
51        self.certificate.study_mode = 'nce_ft'
52        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
53        self.browser.open(self.payments_path)
54        IWorkflowState(self.student).setState('cleared')
55        self.browser.open(self.payments_path + '/addop')
56        self.browser.getControl(name="form.p_category").value = ['schoolfee']
57        self.browser.getControl("Create ticket").click()
58        self.assertMatches('...ticket created...',
59                           self.browser.contents)
60        ctrl = self.browser.getControl(name='val_id')
61        self.value = ctrl.options[0]
62        self.browser.getLink(self.value).click()
63        self.assertMatches('...Amount Authorized...',
64                           self.browser.contents)
65        self.assertMatches(
66            '...<span>12495.0</span>...',
67            self.browser.contents)
68        self.payment_url = self.browser.url
69
70
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)
81
82    def test_interswitch_form(self):
83
84        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
85        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
86        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0)
87        # Manager can access InterswitchForm for the created school fee ticket
88        self.browser.getLink("CollegePAY", index=0).click()
89        # Split amounts have been set
90        self.assertEqual(self.student['payments'][self.value].provider_amt, 1600.0)
91        self.assertEqual(self.student['payments'][self.value].gateway_amt, 150.0)
92        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 1400.0)
93        self.assertMatches('...Total Amount Authorized:...',
94                           self.browser.contents)
95        self.assertMatches(
96            '...<input type="hidden" name="amount" value="1249500.0" />...',
97            self.browser.contents)
98        self.assertMatches(
99            '...item_name="School Fee" item_amt="934800" bank_id="117" acct_num="1012044015"...',
100            self.browser.contents)
101        self.assertMatches(
102            '...item_name="FCEOkene Split" item_amt="140000" bank_id="117" acct_num="1012044039"...',
103            self.browser.contents)
104        self.assertMatches(
105            '...item_name="BT Education" item_amt="160000" bank_id="31" acct_num="0026781725"...',
106            self.browser.contents)
107
108        # Let's do the same for maintenance fee payment
109
110        self.browser.open(self.payments_path)
111        self.browser.open(self.payments_path + '/addop')
112        self.browser.getControl(
113            name="form.p_category").value = ['hostel_maintenance']
114        self.browser.getControl("Create ticket").click()
115        self.assertMatches('...You have not yet booked accommodation...',
116                           self.browser.contents)
117        # Students have to book bed first
118        self.browser.open(self.acco_path)
119        IWorkflowState(self.student).setState('admitted')
120        self.browser.getLink("Book accommodation").click()
121        self.assertFalse('Activation Code:' in self.browser.contents)
122        self.browser.getControl("Create bed ticket").click()
123        # Bed is randomly selected but, since there is only
124        # one bed for this student, we know that ...
125        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
126                           self.browser.contents)
127        self.assertMatches('...ticket created...',
128                           self.browser.contents)
129        self.browser.open(self.payments_path + '/addop')
130        self.browser.getControl(
131            name="form.p_category").value = ['hostel_maintenance']
132        self.browser.getControl("Create ticket").click()
133        self.assertMatches('...ticket created...',
134                           self.browser.contents)
135        ctrl = self.browser.getControl(name='val_id')
136        value = ctrl.options[1]
137        self.browser.getLink(value).click()
138        self.assertMatches('...Amount Authorized...',
139                           self.browser.contents)
140        self.assertMatches(
141            '...<span>4150.0</span>...',
142            self.browser.contents)
143        self.payment_url = self.browser.url
144        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
145        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
146        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
147        # Manager can access InterswitchForm
148        self.browser.getLink("CollegePAY", index=0).click()
149        # Split amounts have been set
150        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
151        self.assertEqual(self.student['payments'][value].gateway_amt, 150.0)
152        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
153        # The total amount to be processed by Interswitch
154        # has been reduced by the Interswitch fee of 150 Nairas
155        self.assertMatches('...Total Amount Authorized:...',
156                           self.browser.contents)
157        self.assertMatches(
158            '...<input type="hidden" name="amount" value="400000.0" />...',
159            self.browser.contents)
160        self.assertMatches(
161            '...item_name="Hostel Maintenance Fee" item_amt="385000" bank_id="117" acct_num="1012044132"...',
162            self.browser.contents)
163        # BT does nor charge a fee for maintenance fee
164        self.assertFalse("BT Education" in self.browser.contents)
165
166#    @external_test
167#    def test_callback(self):
168
169        # Manager can call callback manually
170#        self.browser.open(self.callback_url(self.payment_url, 'XX', '300'))
171#        self.assertMatches('...Unsuccessful callback: Something went wrong...',
172#                          self.browser.contents)
173#        self.assertMatches('...Failed...',
174#                           self.browser.contents)
175#        self.browser.open(self.payment_url + '/isw_callback')
176#        self.assertMatches('...Unsuccessful callback: Incomplete query string...',
177#                          self.browser.contents)
178#        self.assertMatches('...Failed...',
179#                           self.browser.contents)
180#        self.browser.open(self.callback_url(self.payment_url, '00', '300000'))
181#        self.assertMatches('...Wrong amount...',
182#                          self.browser.contents)
183#        self.browser.open(self.callback_url(self.payment_url, '00', '4000000'))
184#        self.assertMatches('...Valid callback received...',
185#                          self.browser.contents)
186
187    @external_test
188    def test_webservice(self):
189        # First we have open InterswitchPageStudent to set provider_amt
190        # and gateway_amt
191        self.browser.open(self.payment_url + '/goto_interswitch')
192        # Now we can call the webservice
193        self.browser.open(self.payment_url + '/request_webservice')
194        #self.assertMatches('...Unsuccessful callback...',
195        #                  self.browser.contents)
196        self.assertMatches('...Unsuccessful callback...',
197                          self.browser.contents)
198        # The payment is now in state failed ...
199        self.assertMatches('...<span>Failed</span>...',
200                          self.browser.contents)
201        # ... and the catalog has been updated
202        cat = getUtility(ICatalog, name='payments_catalog')
203        results = list(
204            cat.searchResults(p_state=('failed', 'failed')))
205        self.assertEqual(len(results), 1)
206        self.assertEqual(results[0].p_state, 'failed')
207
208        # Let's replace the p_id with a valid p_id of the FCEOkene
209        # live system. This is definitely not an appropriate
210        # solution for testing, but we have no choice since
211        # Interswitch doesn't provide any interface
212        # for testing.
213        payment = self.student['payments'][self.value]
214        payment.p_id = 'p3536651296379'
215        self.browser.open(self.payment_url + '/request_webservice')
216        self.assertMatches('...Callback amount does not match...',
217                          self.browser.contents)
218        # The payment is now in state failed ...
219        self.assertMatches('...<span>Failed</span>...',
220                          self.browser.contents)
221        # Let's replace the amount autorized with the amount of the
222        # live system payment
223        payment.amount_auth = payment.r_amount_approved
224        self.browser.open(self.payment_url + '/request_webservice')
225        self.assertMatches('...Successful payment...',
226                          self.browser.contents)
227        # The payment is now in state paid ...
228        self.assertMatches('...<span>Paid</span>...',
229                          self.browser.contents)
230        # ... and the catalog has been updated
231        cat = getUtility(ICatalog, name='payments_catalog')
232        results = list(
233            cat.searchResults(p_state=('paid', 'paid')))
234        self.assertEqual(len(results), 1)
235        self.assertEqual(results[0].p_state, 'paid')
236        # Approval is logged in students.log ...
237        logfile = os.path.join(
238            self.app['datacenter'].storage, 'logs', 'students.log')
239        logcontent = open(logfile).read()
240        self.assertTrue(
241            'zope.mgr - '
242            'waeup.fceokene.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - '
243            'K1000000 - successful schoolfee payment: p3536651296379\n'
244            in logcontent)
245        # ... and in payments.log
246        logfile = os.path.join(
247            self.app['datacenter'].storage, 'logs', 'payments.log')
248        logcontent = open(logfile).read()
249        self.assertTrue(
250            '"zope.mgr",K1000000,p3536651296379,schoolfee,'
251            '3150.0,00,1600.0,150.0,1400.0,,,\n'
252            in logcontent)
253
254class InterswitchTestsApplicants(ApplicantsFullSetup):
255    """Tests for the Interswitch payment gateway.
256    """
257
258    layer = FunctionalLayer
259
260    def setUp(self):
261        super(InterswitchTestsApplicants, self).setUp()
262        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
263        self.browser.open(self.manage_path)
264        #IWorkflowState(self.student).setState('started')
265        super(InterswitchTestsApplicants, self).fill_correct_values()
266        self.applicantscontainer.application_fee = 1000.0
267        self.browser.getControl(name="form.nationality").value = ['NG']
268        self.browser.getControl(name="transition").value = ['start']
269        self.browser.getControl("Save").click()
270        self.browser.getControl("Add online").click()
271        self.assertMatches('...ticket created...',
272                           self.browser.contents)
273        self.assertMatches('...Amount Authorized...',
274                           self.browser.contents)
275        self.assertMatches(
276            '...<span>1000.0</span>...',
277            self.browser.contents)
278        self.payment_url = self.browser.url
279        self.browser.open(self.manage_path)
280        ctrl = self.browser.getControl(name='val_id')
281        self.value = ctrl.options[0]
282
283
284    def test_interswitch_form(self):
285
286        # Manager can access InterswitchForm
287        self.browser.open(self.payment_url)
288        self.browser.getLink("CollegePAY", index=0).click()
289        self.assertMatches('...Total Amount Authorized:...',
290                           self.browser.contents)
291        self.assertMatches(
292            '...<input type="hidden" name="amount" value="100000.0" />...',
293            self.browser.contents)
294
295    @external_test
296    def test_webservice(self):
297        # First we have open InterswitchPageStudent to set provider_amt
298        # and gateway_amt
299        self.browser.open(self.payment_url + '/goto_interswitch')
300        # Now we can call the webservice
301        self.browser.open(self.payment_url + '/request_webservice')
302        self.assertMatches('...Unsuccessful callback...',
303                          self.browser.contents)
304        # The payment is now in state failed ...
305        self.assertMatches('...<span>Failed</span>...',
306                          self.browser.contents)
307        # ... and the catalog has been updated
308        cat = getUtility(ICatalog, name='payments_catalog')
309        results = list(
310            cat.searchResults(p_state=('failed', 'failed')))
311        self.assertEqual(len(results), 1)
312        self.assertEqual(results[0].p_state, 'failed')
313
314        # Let's replace the p_id with a valid p_id of the Uniben
315        # live system. This is definitely not an appropriate
316        # solution for testing, but we have no choice since
317        # Interswitch doesn't provide any interface
318        # for testing.
319        payment = self.applicant[self.value]
320        payment.p_id = 'p3536651296379'
321        self.browser.open(self.payment_url + '/request_webservice')
322        self.assertMatches('...Callback amount does not match...',
323                          self.browser.contents)
324        # The payment is now in state failed ...
325        self.assertMatches('...<span>Failed</span>...',
326                          self.browser.contents)
327        # Let's replace the amount autorized with the amount of the
328        # live system payment
329        payment.amount_auth = payment.r_amount_approved
330        self.browser.open(self.payment_url + '/request_webservice')
331        self.assertMatches('...Successful payment...',
332                          self.browser.contents)
333        # The payment is now in state paid ...
334        self.assertMatches('...<span>Paid</span>...',
335                          self.browser.contents)
336        # ... and the catalog has been updated
337        cat = getUtility(ICatalog, name='payments_catalog')
338        results = list(
339            cat.searchResults(p_state=('paid', 'paid')))
340        self.assertEqual(len(results), 1)
341        self.assertEqual(results[0].p_state, 'paid')
342        # Approval is logged in students.log ...
343        logfile = os.path.join(
344            self.app['datacenter'].storage, 'logs', 'applicants.log')
345        logcontent = open(logfile).read()
346        self.assertTrue(
347            'zope.mgr - '
348            'waeup.fceokene.interswitch.browser.InterswitchPaymentRequestWebservicePageApplicant - '
349            '%s - successful payment: p3536651296379\n'
350            % self.applicant.applicant_id in logcontent)
351        # ... and in payments.log
352        logfile = os.path.join(
353            self.app['datacenter'].storage, 'logs', 'payments.log')
354        logcontent = open(logfile).read()
355        self.assertTrue(
356            '"zope.mgr",%s,p3536651296379,application,'
357            '3150.0,00,500.0,150.0,0.0,,,\n' % self.applicant.applicant_id
358            in logcontent)
Note: See TracBrowser for help on using the repository browser.