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

Last change on this file since 17937 was 17937, checked in by Henrik Bettermann, 5 weeks ago

Fix xml.

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