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

Last change on this file since 15836 was 15773, checked in by Henrik Bettermann, 5 years ago

Adjust to Nigeria package.

  • Property svn:keywords set to Id
File size: 11.7 KB
Line 
1## $Id: tests.py 15773 2019-11-07 17:45:43Z 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 ticket").click()
63        self.assertMatches('...ticket 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 ticket").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 setUp(self):
210        super(InterswitchTestsApplicants, self).setUp()
211        configuration = SessionConfiguration()
212        configuration.academic_session = datetime.now().year - 2
213        self.app['configuration'].addSessionConfiguration(configuration)
214        self.configuration = configuration
215        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
216        self.browser.open(self.manage_path)
217        #IWorkflowState(self.student).setState('started')
218        super(InterswitchTestsApplicants, self).fill_correct_values()
219        self.applicantscontainer.application_fee = 1000.0
220        self.browser.getControl(name="form.nationality").value = ['NG']
221        self.browser.getControl(name="transition").value = ['start']
222        self.browser.getControl("Save").click()
223        self.browser.getControl("Add online").click()
224        self.assertMatches('...ticket created...',
225                           self.browser.contents)
226        #ctrl = self.browser.getControl(name='val_id')
227        #value = ctrl.options[0]
228        #self.browser.getLink(value).click()
229        self.assertMatches('...Amount Authorized...',
230                           self.browser.contents)
231        self.assertMatches(
232            '...<span>1000.0</span>...',
233            self.browser.contents)
234        self.payment_url = self.browser.url
235
236
237    def test_interswitch_form(self):
238        # Manager can access InterswitchForm
239        self.browser.getLink("Pay via Interswitch", index=0).click()
240        self.assertMatches('...Total Amount Authorized:...',
241                           self.browser.contents)
242        self.assertMatches(
243            '...<input type="hidden" name="amount" value="100000" />...',
244            self.browser.contents)
245        delta = timedelta(days=8)
246        self.applicant.values()[0].creation_date -= delta
247        self.browser.open(self.payment_url)
248        self.browser.getLink("Pay via Interswitch", index=0).click()
249        self.assertMatches(
250            '...This payment ticket is too old. Please create a new ticket...',
251            self.browser.contents)
252
253    @external_test
254    def test_webservice(self):
255
256        self.browser.open(self.payment_url + '/request_webservice')
257        self.assertMatches('...Unsuccessful callback...',
258                          self.browser.contents)
259        # The payment is now in state failed
260        self.assertMatches('...<span>Failed</span>...',
261                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.