source: main/kofacustom.edocons/trunk/src/kofacustom/edocons/interswitch/tests.py @ 17415

Last change on this file since 17415 was 17386, checked in by Henrik Bettermann, 19 months ago

Fix test.

  • Property svn:keywords set to Id
File size: 13.3 KB
Line 
1## $Id: tests.py 17386 2023-04-20 06:01:11Z 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.edocons.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        # Manager can access InterswitchForm
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 = ['schoolfee']
62        self.browser.getControl("Create ticket").click()
63        self.assertMatches('...ticket created...',
64                           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.assertMatches('...Amount Authorized...',
70                           self.browser.contents)
71        self.assertTrue(
72            '<span>40000.0</span>' in self.browser.contents)
73        self.payment_url = self.browser.url
74        self.browser.getLink("Pay via Interswitch CollegePAY", index=0).click()
75        self.assertMatches('...<input type="hidden" name="pay_item_id" value="Default_Payable_MX26002" />...',
76                           self.browser.contents)
77        self.assertEqual(self.student.current_mode, 'ug_ft')
78        self.assertTrue(
79            '<input type="hidden" name="amount" value="4030000" />' in
80            self.browser.contents)
81        self.assertTrue(
82            'item_name="School Fee (total amount)" item_amt="4000000" bank_id="31" acct_num="1489560452"' in
83            self.browser.contents)
84        self.browser.open(self.payments_path + '/addop')
85        self.browser.getControl(name="form.p_category").value = ['schoolfee_1']
86        self.browser.getControl("Create ticket").click()
87        self.assertMatches('...ticket created...',
88                           self.browser.contents)
89        self.browser.open(self.payments_path)
90        ctrl = self.browser.getControl(name='val_id')
91        self.value = ctrl.options[1]
92        self.browser.getLink(self.value).click()
93        self.assertMatches('...Amount Authorized...',
94                           self.browser.contents)
95        self.assertTrue(
96            '<span>27727.0</span>' in self.browser.contents)
97        self.payment_url = self.browser.url
98        self.browser.getLink("Pay via Interswitch CollegePAY", index=0).click()
99        self.assertMatches('...<input type="hidden" name="pay_item_id" value="Default_Payable_MX26002" />...',
100                           self.browser.contents)
101        self.assertEqual(self.student.current_mode, 'ug_ft')
102        self.assertTrue(
103            '<input type="hidden" name="amount" value="2802700" />' in
104            self.browser.contents)
105        self.assertTrue(
106            'item_name="School Fee (1st instalment)" item_amt="2772700" bank_id="31" acct_num="1489560452"' in
107            self.browser.contents)
108
109    def test_interswitch_form_ticket_expired(self):
110        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
111        acc_payment = createObject('waeup.StudentOnlinePayment')
112        acc_payment.p_state = 'unpaid'
113        acc_payment.p_category = 'schoolfee'
114        acc_payment.p_id = 'xyz'
115        acc_payment.pay_item_id = '123'
116        acc_payment.amount_auth = 1000.0
117        self.student['payments']['xyz'] = acc_payment
118        self.browser.open(self.payments_path + '/xyz')
119        self.browser.getLink("Pay via Interswitch CollegePAY", index=0).click()
120        self.assertMatches('...<input type="hidden" name="pay_item_id" value="Default_Payable_MX26002" />...',
121                           self.browser.contents)
122        self.assertMatches('...Total Amount Authorized:...',
123                           self.browser.contents)
124        self.assertEqual(self.student.current_mode, 'ug_ft')
125        self.assertMatches(
126            '...<input type="hidden" name="amount" value="115000" />...',
127            self.browser.contents)
128        delta = timedelta(days=8)
129        acc_payment.creation_date -= delta
130        self.browser.open(self.payments_path + '/xyz')
131        self.browser.getLink("Pay via Interswitch", index=0).click()
132        self.assertMatches(
133            '...This payment ticket is too old. Please create a new ticket...',
134            self.browser.contents)
135        delta = timedelta(days=2)
136        acc_payment.creation_date += delta
137        self.browser.open(self.payments_path + '/xyz')
138        self.browser.getLink("Pay via Interswitch", index=0).click()
139        self.assertMatches('...Total Amount Authorized:...',
140                           self.browser.contents)
141
142    @external_test
143    def test_webservice(self):
144        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
145        self.browser.open(self.payments_path)
146        IWorkflowState(self.student).setState('cleared')
147        self.student.nationality = u'NG'
148        self.browser.open(self.payments_path + '/addop')
149        self.browser.getControl(name="form.p_category").value = ['schoolfee']
150        self.browser.getControl("Create ticket").click()
151        self.assertMatches('...ticket created...',
152                           self.browser.contents)
153        ctrl = self.browser.getControl(name='val_id')
154        self.value = ctrl.options[0]
155        self.browser.getLink(self.value).click()
156        self.payment_url = self.browser.url
157        # First we have open InterswitchPageStudent to set provider_amt
158        # and gateway_amt
159        self.browser.open(self.payment_url + '/goto_interswitch')
160        # Now we can call the webservice
161        self.browser.open(self.payment_url + '/request_webservice')
162        self.assertMatches('...Unsuccessful callback...',
163                          self.browser.contents)
164        # The payment is now in state failed ...
165        self.assertMatches('...<span>Failed</span>...',
166                          self.browser.contents)
167        # ... and the catalog has been updated
168        cat = getUtility(ICatalog, name='payments_catalog')
169        results = list(
170            cat.searchResults(p_state=('failed', 'failed')))
171        self.assertEqual(len(results), 1)
172        self.assertEqual(results[0].p_state, 'failed')
173
174        # Let's replace the p_id with a valid p_id of the Uniben
175        # live system. This is definitely not an appropriate
176        # solution for testing, but we have no choice since
177        # Interswitch doesn't provide any interface
178        # for testing.
179        payment = self.student['payments'][self.value]
180        payment.p_id = 'p3547789850240'
181        self.browser.open(self.payment_url + '/request_webservice')
182        self.assertMatches('...Callback amount does not match...',
183                          self.browser.contents)
184        # The payment is now in state failed ...
185        self.assertMatches('...<span>Failed</span>...',
186                          self.browser.contents)
187        # Let's replace the amount autorized with the amount of the
188        # live system payment
189        payment.amount_auth = payment.r_amount_approved
190        self.browser.open(self.payment_url + '/request_webservice')
191        self.assertMatches('...Successful payment...',
192                          self.browser.contents)
193        # The payment is now in state paid ...
194        self.assertMatches('...<span>Paid</span>...',
195                          self.browser.contents)
196        # ... and the catalog has been updated
197        cat = getUtility(ICatalog, name='payments_catalog')
198        results = list(
199            cat.searchResults(p_state=('paid', 'paid')))
200        self.assertEqual(len(results), 1)
201        self.assertEqual(results[0].p_state, 'paid')
202        # Approval is logged in students.log ...
203        logfile = os.path.join(
204            self.app['datacenter'].storage, 'logs', 'students.log')
205        logcontent = open(logfile).read()
206        self.assertTrue(
207            'zope.mgr - '
208            'kofacustom.edocons.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
209            'X1000000 - successful schoolfee payment: p3547789850240\n'
210            in logcontent)
211        # ... and in payments.log
212        logfile = os.path.join(
213            self.app['datacenter'].storage, 'logs', 'payments.log')
214        logcontent = open(logfile).read()
215        self.assertTrue(
216            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
217            '12000.0,00,0.0,150.0,0.0,,,\n'
218            in logcontent)
219
220
221class InterswitchTestsApplicants(ApplicantsFullSetup):
222    """Tests for the Interswitch payment gateway.
223    """
224
225    layer = FunctionalLayer
226
227    def setUp(self):
228        super(InterswitchTestsApplicants, self).setUp()
229        configuration = SessionConfiguration()
230        configuration.academic_session = datetime.now().year - 2
231        self.app['configuration'].addSessionConfiguration(configuration)
232        self.configuration = configuration
233        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
234        self.browser.open(self.manage_path)
235        #IWorkflowState(self.student).setState('started')
236        self.browser.getControl(name="form.firstname").value = 'John'
237        self.browser.getControl(name="form.middlename").value = 'Anthony'
238        self.browser.getControl(name="form.lastname").value = 'Tester'
239        #self.browser.getControl(name="form.course1").value = ['CERT1']
240        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
241        self.browser.getControl(name="form.sex").value = ['m']
242        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
243        self.applicantscontainer.application_fee = 1000.0
244        self.browser.getControl(name="form.nationality").value = ['NG']
245        self.browser.getControl(name="form.lga").value = ['zamfara_maru']
246        self.browser.getControl(name="transition").value = ['start']
247        self.browser.getControl("Save").click()
248        self.browser.getControl("Add online").click()
249        self.assertMatches('...ticket created...',
250                           self.browser.contents)
251        #ctrl = self.browser.getControl(name='val_id')
252        #value = ctrl.options[0]
253        #self.browser.getLink(value).click()
254        self.assertMatches('...Amount Authorized...',
255                           self.browser.contents)
256        self.assertMatches(
257            '...<span>1000.0</span>...',
258            self.browser.contents)
259        self.payment_url = self.browser.url
260
261
262    def test_interswitch_form(self):
263        # Manager can access InterswitchForm
264        self.browser.getLink("Pay via Interswitch", index=0).click()
265        self.assertMatches('...Total Amount Authorized:...',
266                           self.browser.contents)
267        self.assertMatches(
268            '...<input type="hidden" name="amount" value="100000" />...',
269            self.browser.contents)
270        delta = timedelta(days=8)
271        self.applicant.values()[0].creation_date -= delta
272        self.browser.open(self.payment_url)
273        self.browser.getLink("Pay via Interswitch", index=0).click()
274        self.assertMatches(
275            '...This payment ticket is too old. Please create a new ticket...',
276            self.browser.contents)
277
278    @external_test
279    def test_webservice(self):
280
281        self.browser.open(self.payment_url + '/request_webservice')
282        self.assertMatches('...Unsuccessful callback...',
283                          self.browser.contents)
284        # The payment is now in state failed
285        self.assertMatches('...<span>Failed</span>...',
286                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.