source: main/kofacustom.unidel/trunk/src/kofacustom/unidel/interswitch/tests.py @ 17613

Last change on this file since 17613 was 17599, checked in by Henrik Bettermann, 15 months ago

Reconfigure clearance (acceptance) fee.

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