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

Last change on this file since 18034 was 18034, checked in by Henrik Bettermann, 5 hours ago

Implement required combi split payments.

  • Property svn:keywords set to Id
File size: 15.6 KB
Line 
1## $Id: tests.py 18034 2025-03-10 16:40:53Z 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        print self.browser.contents
135        self.assertTrue(
136            '<input type="hidden" name="amount" value="11025000" />' in
137            self.browser.contents)
138        self.assertTrue(
139            'item_id="6" item_name="registration_fresh" item_amt="500000" bank_id="31" acct_num="0040621193"' in
140            self.browser.contents)
141        self.assertTrue(
142            'item_id="12" item_name="parentsconsult" item_amt="1000000" bank_id="117" acct_num="1228747029"' in
143            self.browser.contents)
144
145    def test_interswitch_form_ticket_expired(self):
146        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
147        acc_payment = createObject('waeup.StudentOnlinePayment')
148        acc_payment.p_state = 'unpaid'
149        acc_payment.p_category = 'registration'
150        acc_payment.p_id = 'xyz'
151        acc_payment.pay_item_id = '123'
152        acc_payment.amount_auth = 1000.0
153        self.student['payments']['xyz'] = acc_payment
154        self.browser.open(self.payments_path + '/xyz')
155        self.browser.getLink("Pay via Interswitch", index=0).click()
156        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in
157                           self.browser.contents)
158        self.assertMatches('...Total Amount Authorized:...',
159                           self.browser.contents)
160        self.assertEqual(self.student.current_mode, 'ug_ft')
161        # 250 gateway charge have been added
162        self.assertTrue(
163            '<input type="hidden" name="amount" value="125000" />' in
164            self.browser.contents)
165        self.assertEqual(acc_payment.amount_auth, 1250)
166        self.assertEqual(acc_payment.net_amt, 1000)
167        delta = timedelta(days=8)
168        acc_payment.creation_date -= delta
169        self.browser.open(self.payments_path + '/xyz')
170        self.browser.getLink("Pay via Interswitch", index=0).click()
171        self.assertMatches(
172            '...This payment ticket is too old. Please create a new ticket...',
173            self.browser.contents)
174        delta = timedelta(days=2)
175        acc_payment.creation_date += delta
176        self.browser.open(self.payments_path + '/xyz')
177        self.browser.getLink("Pay via Interswitch", index=0).click()
178        self.assertMatches('...Total Amount Authorized:...',
179                           self.browser.contents)
180
181    @external_test
182    def test_webservice(self):
183        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
184        self.browser.open(self.payments_path)
185        IWorkflowState(self.student).setState('cleared')
186        self.student.nationality = u'NG'
187        self.browser.open(self.payments_path + '/addop')
188        self.browser.getControl(name="form.p_category").value = ['schoolfee']
189        self.browser.getControl("Create payment").click()
190        self.assertMatches('...ticket created...',
191                           self.browser.contents)
192        ctrl = self.browser.getControl(name='val_id')
193        self.value = ctrl.options[0]
194        self.browser.getLink(self.value).click()
195        self.payment_url = self.browser.url
196        # First we have open InterswitchPageStudent to set provider_amt
197        # and gateway_amt
198        self.browser.open(self.payment_url + '/goto_interswitch')
199        # Now we can call the webservice
200        self.browser.open(self.payment_url + '/request_webservice')
201        self.assertMatches('...Unsuccessful callback...',
202                          self.browser.contents)
203        # The payment is now in state failed ...
204        self.assertMatches('...<span>Failed</span>...',
205                          self.browser.contents)
206        # ... and the catalog has been updated
207        cat = getUtility(ICatalog, name='payments_catalog')
208        results = list(
209            cat.searchResults(p_state=('failed', 'failed')))
210        self.assertEqual(len(results), 1)
211        self.assertEqual(results[0].p_state, 'failed')
212
213        # Let's replace the p_id with a valid p_id of the Uniben
214        # live system. This is definitely not an appropriate
215        # solution for testing, but we have no choice since
216        # Interswitch doesn't provide any interface
217        # for testing.
218        payment = self.student['payments'][self.value]
219        payment.p_id = 'p3547789850240'
220        self.browser.open(self.payment_url + '/request_webservice')
221        self.assertMatches('...Callback amount does not match...',
222                          self.browser.contents)
223        # The payment is now in state failed ...
224        self.assertMatches('...<span>Failed</span>...',
225                          self.browser.contents)
226        # Let's replace the amount autorized with the amount of the
227        # live system payment
228        payment.amount_auth = payment.r_amount_approved
229        self.browser.open(self.payment_url + '/request_webservice')
230        self.assertMatches('...Successful payment...',
231                          self.browser.contents)
232        # The payment is now in state paid ...
233        self.assertMatches('...<span>Paid</span>...',
234                          self.browser.contents)
235        # ... and the catalog has been updated
236        cat = getUtility(ICatalog, name='payments_catalog')
237        results = list(
238            cat.searchResults(p_state=('paid', 'paid')))
239        self.assertEqual(len(results), 1)
240        self.assertEqual(results[0].p_state, 'paid')
241        # Approval is logged in students.log ...
242        logfile = os.path.join(
243            self.app['datacenter'].storage, 'logs', 'students.log')
244        logcontent = open(logfile).read()
245        self.assertTrue(
246            'zope.mgr - '
247            'kofacustom.iuokada.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
248            'X1000000 - successful schoolfee payment: p3547789850240\n'
249            in logcontent)
250        # ... and in payments.log
251        logfile = os.path.join(
252            self.app['datacenter'].storage, 'logs', 'payments.log')
253        logcontent = open(logfile).read()
254        self.assertTrue(
255            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
256            '12000.0,00,0.0,150.0,0.0,,,\n'
257            in logcontent)
258
259
260class InterswitchTestsApplicants(ApplicantsFullSetup):
261    """Tests for the Interswitch payment gateway.
262    """
263
264    layer = FunctionalLayer
265
266    def fill_correct_values(self):
267        # Fill the edit form with suitable values
268        self.browser.getControl(name="form.firstname").value = 'John'
269        self.browser.getControl(name="form.middlename").value = 'Anthony'
270        self.browser.getControl(name="form.lastname").value = 'Tester'
271        self.browser.getControl(name="custom.course1").value = ['CERT1']
272        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
273        self.browser.getControl(name="form.sex").value = ['m']
274        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
275        self.browser.getControl(name="form.perm_address").value = 'Adresse'
276
277    def setUp(self):
278        super(InterswitchTestsApplicants, self).setUp()
279        configuration = SessionConfiguration()
280        configuration.academic_session = datetime.now().year - 2
281        self.app['configuration'].addSessionConfiguration(configuration)
282        self.configuration = configuration
283        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
284        self.browser.open(self.manage_path)
285        #IWorkflowState(self.student).setState('started')
286        self.fill_correct_values()
287        self.applicantscontainer.application_fee = 1000.0
288        self.browser.getControl(name="form.nationality").value = ['NG']
289        self.browser.getControl(name="transition").value = ['start']
290        self.browser.getControl("Save").click()
291        self.browser.getControl("Add online").click()
292        self.assertMatches('...Payment created...',
293                           self.browser.contents)
294        #ctrl = self.browser.getControl(name='val_id')
295        #value = ctrl.options[0]
296        #self.browser.getLink(value).click()
297        self.assertMatches('...Amount Authorized...',
298                           self.browser.contents)
299        self.assertMatches(
300            '...<span>1000.0</span>...',
301            self.browser.contents)
302        self.payment_url = self.browser.url
303
304
305    def test_interswitch_form(self):
306        # Manager can access InterswitchForm
307        self.browser.getLink("Pay via Interswitch", index=0).click()
308        self.assertMatches('...Total Amount Authorized:...',
309                           self.browser.contents)
310        self.assertMatches(
311            '...<input type="hidden" name="amount" value="100000" />...',
312            self.browser.contents)
313        delta = timedelta(days=8)
314        self.applicant.values()[0].creation_date -= delta
315        self.browser.open(self.payment_url)
316        self.browser.getLink("Pay via Interswitch", index=0).click()
317        self.assertMatches(
318            '...This payment ticket is too old. Please create a new ticket...',
319            self.browser.contents)
320
321    @external_test
322    def test_webservice(self):
323
324        self.browser.open(self.payment_url + '/request_webservice')
325        self.assertMatches('...Unsuccessful callback...',
326                          self.browser.contents)
327        # The payment is now in state failed
328        self.assertMatches('...<span>Failed</span>...',
329                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.