source: main/kofacustom.coewarri/trunk/src/kofacustom/coewarri/interswitch/tests.py @ 16380

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

Fix tests.

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