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

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

Add Interswitch school fee components for testing.

File size: 9.7 KB
Line 
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)
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
108    def test_interswitch_form(self):
109        self.app['configuration']['2004'].school_fee_1 = 6666.0
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(
126            '...<span>6666.0</span>...',
127            self.browser.contents)
128        self.payment_url = self.browser.url
129        self.browser.getLink("CollegePAY", index=0).click()
130        # The total amount to be processed by Interswitch
131        # has been reduced by the Interswitch fee of 150 Nairas
132        self.assertMatches('...<input type="hidden" name="pay_item_id" value="101" />...',
133                           self.browser.contents)
134        self.assertMatches('...Total Amount Authorized:...',
135                           self.browser.contents)
136        self.assertEqual(self.student.current_mode, 'ug_ft')
137        self.assertMatches(
138            '...<input type="hidden" name="amount" value="666600" />...',
139            self.browser.contents)
140        self.assertMatches(
141            '...item_name="School Fee" item_amt="641600" bank_id="00" acct_num="00000000"...',
142            self.browser.contents)
143
144    @external_test
145    def test_webservice(self):
146        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
147        self.browser.open(self.payments_path)
148        IWorkflowState(self.student).setState('cleared')
149        self.student.nationality = u'NG'
150        self.browser.open(self.payments_path + '/addop')
151        self.browser.getControl(name="form.p_category").value = ['schoolfee']
152        self.browser.getControl("Create ticket").click()
153        self.assertMatches('...ticket created...',
154                           self.browser.contents)
155        ctrl = self.browser.getControl(name='val_id')
156        self.value = ctrl.options[0]
157        self.browser.getLink(self.value).click()
158        self.payment_url = self.browser.url
159        # First we have open InterswitchPageStudent to set provider_amt
160        # and gateway_amt
161        self.browser.open(self.payment_url + '/goto_interswitch')
162        # Now we can call the webservice
163        self.browser.open(self.payment_url + '/request_webservice')
164        self.assertMatches('...Unsuccessful callback...',
165                          self.browser.contents)
166        # The payment is now in state failed ...
167        self.assertMatches('...<span>Failed</span>...',
168                          self.browser.contents)
169        # ... and the catalog has been updated
170        cat = getUtility(ICatalog, name='payments_catalog')
171        results = list(
172            cat.searchResults(p_state=('failed', 'failed')))
173        self.assertEqual(len(results), 1)
174        self.assertEqual(results[0].p_state, 'failed')
175
176        # Let's replace the p_id with a valid p_id of the Uniben
177        # live system. This is definitely not an appropriate
178        # solution for testing, but we have no choice since
179        # Interswitch doesn't provide any interface
180        # for testing.
181        payment = self.student['payments'][self.value]
182        payment.p_id = 'p3547789850240'
183        self.browser.open(self.payment_url + '/request_webservice')
184        self.assertMatches('...Callback amount does not match...',
185                          self.browser.contents)
186        # The payment is now in state failed ...
187        self.assertMatches('...<span>Failed</span>...',
188                          self.browser.contents)
189        # Let's replace the amount autorized with the amount of the
190        # live system payment
191        payment.amount_auth = payment.r_amount_approved
192        self.browser.open(self.payment_url + '/request_webservice')
193        self.assertMatches('...Successful payment...',
194                          self.browser.contents)
195        # The payment is now in state paid ...
196        self.assertMatches('...<span>Paid</span>...',
197                          self.browser.contents)
198        # ... and the catalog has been updated
199        cat = getUtility(ICatalog, name='payments_catalog')
200        results = list(
201            cat.searchResults(p_state=('paid', 'paid')))
202        self.assertEqual(len(results), 1)
203        self.assertEqual(results[0].p_state, 'paid')
204        # Approval is logged in students.log ...
205        logfile = os.path.join(
206            self.app['datacenter'].storage, 'logs', 'students.log')
207        logcontent = open(logfile).read()
208        self.assertTrue(
209            'zope.mgr - '
210            'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
211            'X1000000 - successful schoolfee payment: p3547789850240\n'
212            in logcontent)
213        # ... and in payments.log
214        logfile = os.path.join(
215            self.app['datacenter'].storage, 'logs', 'payments.log')
216        logcontent = open(logfile).read()
217        self.assertTrue(
218            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
219            '12000.0,00,0.0,150.0,0.0,,,\n'
220            in logcontent)
Note: See TracBrowser for help on using the repository browser.