source: main/waeup.aaue/trunk/src/waeup/aaue/interswitch/tests.py @ 12729

Last change on this file since 12729 was 12729, checked in by Henrik Bettermann, 10 years ago

Configure complex split payment, see ticket #117.

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