source: main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/interswitch/tests.py @ 18036

Last change on this file since 18036 was 18036, checked in by Henrik Bettermann, 18 hours ago

Remove some bugs.

  • Property svn:keywords set to Id
File size: 15.5 KB
Line 
1## $Id: tests.py 18036 2025-03-11 19:29:27Z 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, date, timedelta
20from zope.component import createObject, 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 kofacustom.iuokada.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
54    def test_interswitch_form(self):
55        self.app['configuration']['2004'].registration_fresh_fee = 7000.0
56        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
57        self.browser.open(self.payments_path)
58        IWorkflowState(self.student).setState('cleared')
59        self.student.nationality = u'NG'
60        self.browser.open(self.payments_path + '/addop')
61        self.browser.getControl(name="form.p_category").value = ['registration_fresh']
62        self.browser.getControl(name="form.p_option").value = ['first']
63        self.browser.getControl("Create payment").click()
64        self.assertTrue('Payment created' in self.browser.contents)
65        self.browser.open(self.payments_path)
66        ctrl = self.browser.getControl(name='val_id')
67        self.value = ctrl.options[0]
68        self.browser.getLink(self.value).click()
69        self.assertTrue(
70            '<span>7000.0</span>' in self.browser.contents)
71        self.payment_url = self.browser.url
72        self.browser.getLink("Pay via Interswitch", index=0).click()
73        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in
74                           self.browser.contents)
75        self.assertEqual(self.student.current_mode, 'ug_ft')
76        # 250 gateway charge have been added
77        payment = self.student['payments'][self.value]
78        self.assertEqual(payment.amount_auth, 7250)
79        self.assertEqual(payment.net_amt, 7000)
80        self.assertTrue(
81            '<input type="hidden" name="amount" value="725000" />' in
82            self.browser.contents)
83        # WAeAC charge have been substracted
84        self.assertTrue(
85            'item_name="Registration Fee (Fresh)" item_amt="200000" bank_id="8" acct_num="2021420049"' in
86            self.browser.contents)
87        self.assertTrue(
88            'item_name="WAeAC" item_amt="500000" bank_id="31" acct_num="0773411069"' in
89            self.browser.contents)
90
91    def test_interswitch_required_combi(self):
92        # Student is a fresh non-science student
93        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
94        self.browser.open(self.payments_path)
95        IWorkflowState(self.student).setState('cleared')
96        self.student.nationality = u'NG'
97        self.browser.open(self.payments_path + '/addop')
98        self.browser.getControl(name="form.p_category").value = ['schoolfee']
99        self.browser.getControl(name="form.p_option").value = ['first']
100        self.browser.getControl("Create payment").click()
101        self.assertTrue('Make either single payments or make a' in self.browser.contents)
102        self.browser.getControl(name="form.p_category").value = ['required_combi']
103        self.browser.getControl("Create payment").click()
104        self.assertTrue('Student Health Insurance undefined.' in self.browser.contents)
105        self.app['configuration']['2004'].registration_fresh_fee = 10000.0
106        self.app['configuration']['2004'].book_fee = 10000.0
107        self.app['configuration']['2004'].develop_fee = 10000.0
108        self.app['configuration']['2004'].parentsconsult_fee = 10000.0
109        self.app['configuration']['2004'].municipal_fresh_fee = 10000.0
110        self.app['configuration']['2004'].matric_fee = 10000.0
111        self.app['configuration']['2004'].waecneco_fee = 10000.0
112        self.app['configuration']['2004'].jambver_fee = 10000.0
113        self.app['configuration']['2004'].health_insurance_fee = 10000.0
114        self.app['configuration']['2004'].id_card_fee = 10000.0
115        self.app['configuration']['2004'].medical_screening_fee = 10000.0
116        self.browser.getControl(name="form.p_category").value = ['required_combi']
117        self.browser.getControl("Create payment").click()
118        self.assertTrue('Payment created' in self.browser.contents)
119        self.browser.open(self.payments_path)
120        ctrl = self.browser.getControl(name='val_id')
121        self.value = ctrl.options[0]
122        self.browser.getLink(self.value).click()
123        self.assertTrue(
124            '<span>110000.0</span>' in self.browser.contents)
125        self.payment_url = self.browser.url
126        self.browser.getLink("Pay via Interswitch", index=0).click()
127        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in
128                           self.browser.contents)
129        self.assertEqual(self.student.current_mode, 'ug_ft')
130        # 250 gateway charge have been added
131        payment = self.student['payments'][self.value]
132        self.assertEqual(payment.amount_auth, 110250)
133        self.assertEqual(payment.net_amt, 110000)
134        self.assertTrue(
135            '<input type="hidden" name="amount" value="11025000" />' in
136            self.browser.contents)
137        self.assertTrue(
138            'item_id="6" item_name="registration_fresh" item_amt="500000" bank_id="31" acct_num="0040621193"' in
139            self.browser.contents)
140        self.assertTrue(
141            'item_id="12" item_name="parentsconsult" item_amt="1000000" bank_id="117" acct_num="1228747029"' in
142            self.browser.contents)
143
144    def test_interswitch_form_ticket_expired(self):
145        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
146        acc_payment = createObject('waeup.StudentOnlinePayment')
147        acc_payment.p_state = 'unpaid'
148        acc_payment.p_category = 'registration'
149        acc_payment.p_id = 'xyz'
150        acc_payment.pay_item_id = '123'
151        acc_payment.amount_auth = 1000.0
152        self.student['payments']['xyz'] = acc_payment
153        self.browser.open(self.payments_path + '/xyz')
154        self.browser.getLink("Pay via Interswitch", index=0).click()
155        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in
156                           self.browser.contents)
157        self.assertMatches('...Total Amount Authorized:...',
158                           self.browser.contents)
159        self.assertEqual(self.student.current_mode, 'ug_ft')
160        # 250 gateway charge have been added
161        self.assertTrue(
162            '<input type="hidden" name="amount" value="125000" />' in
163            self.browser.contents)
164        self.assertEqual(acc_payment.amount_auth, 1250)
165        self.assertEqual(acc_payment.net_amt, 1000)
166        delta = timedelta(days=8)
167        acc_payment.creation_date -= delta
168        self.browser.open(self.payments_path + '/xyz')
169        self.browser.getLink("Pay via Interswitch", index=0).click()
170        self.assertMatches(
171            '...This payment ticket is too old. Please create a new ticket...',
172            self.browser.contents)
173        delta = timedelta(days=2)
174        acc_payment.creation_date += delta
175        self.browser.open(self.payments_path + '/xyz')
176        self.browser.getLink("Pay via Interswitch", index=0).click()
177        self.assertMatches('...Total Amount Authorized:...',
178                           self.browser.contents)
179
180    @external_test
181    def test_webservice(self):
182        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
183        self.browser.open(self.payments_path)
184        IWorkflowState(self.student).setState('cleared')
185        self.student.nationality = u'NG'
186        self.browser.open(self.payments_path + '/addop')
187        self.browser.getControl(name="form.p_category").value = ['schoolfee']
188        self.browser.getControl("Create payment").click()
189        self.assertMatches('...ticket created...',
190                           self.browser.contents)
191        ctrl = self.browser.getControl(name='val_id')
192        self.value = ctrl.options[0]
193        self.browser.getLink(self.value).click()
194        self.payment_url = self.browser.url
195        # First we have open InterswitchPageStudent to set provider_amt
196        # and gateway_amt
197        self.browser.open(self.payment_url + '/goto_interswitch')
198        # Now we can call the webservice
199        self.browser.open(self.payment_url + '/request_webservice')
200        self.assertMatches('...Unsuccessful callback...',
201                          self.browser.contents)
202        # The payment is now in state failed ...
203        self.assertMatches('...<span>Failed</span>...',
204                          self.browser.contents)
205        # ... and the catalog has been updated
206        cat = getUtility(ICatalog, name='payments_catalog')
207        results = list(
208            cat.searchResults(p_state=('failed', 'failed')))
209        self.assertEqual(len(results), 1)
210        self.assertEqual(results[0].p_state, 'failed')
211
212        # Let's replace the p_id with a valid p_id of the Uniben
213        # live system. This is definitely not an appropriate
214        # solution for testing, but we have no choice since
215        # Interswitch doesn't provide any interface
216        # for testing.
217        payment = self.student['payments'][self.value]
218        payment.p_id = 'p3547789850240'
219        self.browser.open(self.payment_url + '/request_webservice')
220        self.assertMatches('...Callback amount does not match...',
221                          self.browser.contents)
222        # The payment is now in state failed ...
223        self.assertMatches('...<span>Failed</span>...',
224                          self.browser.contents)
225        # Let's replace the amount autorized with the amount of the
226        # live system payment
227        payment.amount_auth = payment.r_amount_approved
228        self.browser.open(self.payment_url + '/request_webservice')
229        self.assertMatches('...Successful payment...',
230                          self.browser.contents)
231        # The payment is now in state paid ...
232        self.assertMatches('...<span>Paid</span>...',
233                          self.browser.contents)
234        # ... and the catalog has been updated
235        cat = getUtility(ICatalog, name='payments_catalog')
236        results = list(
237            cat.searchResults(p_state=('paid', 'paid')))
238        self.assertEqual(len(results), 1)
239        self.assertEqual(results[0].p_state, 'paid')
240        # Approval is logged in students.log ...
241        logfile = os.path.join(
242            self.app['datacenter'].storage, 'logs', 'students.log')
243        logcontent = open(logfile).read()
244        self.assertTrue(
245            'zope.mgr - '
246            'kofacustom.iuokada.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
247            'X1000000 - successful schoolfee payment: p3547789850240\n'
248            in logcontent)
249        # ... and in payments.log
250        logfile = os.path.join(
251            self.app['datacenter'].storage, 'logs', 'payments.log')
252        logcontent = open(logfile).read()
253        self.assertTrue(
254            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
255            '12000.0,00,0.0,150.0,0.0,,,\n'
256            in logcontent)
257
258
259class InterswitchTestsApplicants(ApplicantsFullSetup):
260    """Tests for the Interswitch payment gateway.
261    """
262
263    layer = FunctionalLayer
264
265    def fill_correct_values(self):
266        # Fill the edit form with suitable values
267        self.browser.getControl(name="form.firstname").value = 'John'
268        self.browser.getControl(name="form.middlename").value = 'Anthony'
269        self.browser.getControl(name="form.lastname").value = 'Tester'
270        self.browser.getControl(name="custom.course1").value = ['CERT1']
271        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
272        self.browser.getControl(name="form.sex").value = ['m']
273        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
274        self.browser.getControl(name="form.perm_address").value = 'Adresse'
275
276    def setUp(self):
277        super(InterswitchTestsApplicants, self).setUp()
278        configuration = SessionConfiguration()
279        configuration.academic_session = datetime.now().year - 2
280        self.app['configuration'].addSessionConfiguration(configuration)
281        self.configuration = configuration
282        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
283        self.browser.open(self.manage_path)
284        #IWorkflowState(self.student).setState('started')
285        self.fill_correct_values()
286        self.applicantscontainer.application_fee = 1000.0
287        self.browser.getControl(name="form.nationality").value = ['NG']
288        self.browser.getControl(name="transition").value = ['start']
289        self.browser.getControl("Save").click()
290        self.browser.getControl("Add online").click()
291        self.assertMatches('...Payment created...',
292                           self.browser.contents)
293        #ctrl = self.browser.getControl(name='val_id')
294        #value = ctrl.options[0]
295        #self.browser.getLink(value).click()
296        self.assertMatches('...Amount Authorized...',
297                           self.browser.contents)
298        self.assertMatches(
299            '...<span>1000.0</span>...',
300            self.browser.contents)
301        self.payment_url = self.browser.url
302
303
304    def test_interswitch_form(self):
305        # Manager can access InterswitchForm
306        self.browser.getLink("Pay via Interswitch", index=0).click()
307        self.assertMatches('...Total Amount Authorized:...',
308                           self.browser.contents)
309        self.assertMatches(
310            '...<input type="hidden" name="amount" value="100000" />...',
311            self.browser.contents)
312        delta = timedelta(days=8)
313        self.applicant.values()[0].creation_date -= delta
314        self.browser.open(self.payment_url)
315        self.browser.getLink("Pay via Interswitch", index=0).click()
316        self.assertMatches(
317            '...This payment ticket is too old. Please create a new ticket...',
318            self.browser.contents)
319
320    @external_test
321    def test_webservice(self):
322
323        self.browser.open(self.payment_url + '/request_webservice')
324        self.assertMatches('...Unsuccessful callback...',
325                          self.browser.contents)
326        # The payment is now in state failed
327        self.assertMatches('...<span>Failed</span>...',
328                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.