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

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

Changes according to r9775.

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