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

Last change on this file since 17949 was 17946, checked in by Henrik Bettermann, 3 weeks ago

Remove SU fee from school fee again. So the work was for nothing.

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