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

Last change on this file since 16082 was 16082, checked in by Henrik Bettermann, 4 years ago

Adjust test.

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