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

Last change on this file since 18135 was 18135, checked in by Henrik Bettermann, 14 hours ago

Minor modifications requested in #252

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