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

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

School fee instalment payment (2nd version)

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