source: main/kofacustom.dspg/trunk/src/kofacustom/dspg/interswitch/tests.py @ 14862

Last change on this file since 14862 was 14862, checked in by Henrik Bettermann, 7 years ago

Prepare student payments. Not yet readily configured. MAC is missing. Bank account numbers contradictory. pay_item_ids are not yet assigned.

  • Property svn:keywords set to Id
File size: 12.1 KB
Line 
1## $Id: tests.py 14862 2017-10-06 09:19:26Z 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.dspg.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
33SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
34
35def external_test(func):
36    if not EXTERNAL_TESTS:
37        myself = __file__
38        if myself.endswith('.pyc'):
39            myself = myself[:-2]
40        print "WARNING: external tests are skipped!"
41        print "WARNING: edit %s to enable them." % myself
42        return
43    return func
44
45
46class InterswitchTestsStudents(StudentsFullSetup):
47    """Tests for the Interswitch payment gateway.
48    """
49
50    layer = FunctionalLayer
51
52    def setUp(self):
53        super(InterswitchTestsStudents, self).setUp()
54
55    def test_interswitch_form(self):
56        # Manager can access InterswitchForm
57        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
58        self.browser.open(self.payments_path)
59        IWorkflowState(self.student).setState('cleared')
60        self.student.nationality = u'NG'
61        self.browser.open(self.payments_path + '/addop')
62        self.browser.getControl(name="form.p_category").value = ['schoolfee']
63        self.browser.getControl("Create ticket").click()
64        self.assertMatches('...ticket created...',
65                           self.browser.contents)
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        self.assertMatches(
72            '...<span>40000.0</span>...',
73            self.browser.contents)
74        self.payment_url = self.browser.url
75        self.browser.getLink("CollegePAY", index=0).click()
76        self.assertMatches('...<input type="hidden" name="pay_item_id" value="102" />...',
77                           self.browser.contents)
78        self.assertMatches('...Total Amount Authorized:...',
79                           self.browser.contents)
80        self.assertEqual(self.student.current_mode, 'ug_ft')
81        self.assertMatches(
82            '...<input type="hidden" name="amount" value="4000000" />...',
83            self.browser.contents)
84        self.assertMatches(
85            '...item_name="School Fee" item_amt="4000000" bank_id="11" acct_num="0010761873"...',
86            self.browser.contents)
87
88    def test_interswitch_form_ticket_expired(self):
89        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
90        acc_payment = createObject('waeup.StudentOnlinePayment')
91        acc_payment.p_state = 'unpaid'
92        acc_payment.p_category = 'clearance'
93        acc_payment.p_id = 'xyz'
94        acc_payment.pay_item_id = '123'
95        acc_payment.amount_auth = 876.0
96        self.student['payments']['xyz'] = acc_payment
97        self.browser.open(self.payments_path + '/xyz')
98        self.browser.getLink("CollegePAY", index=0).click()
99        self.assertMatches('...<input type="hidden" name="pay_item_id" value="103" />...',
100                           self.browser.contents)
101        self.assertMatches('...Total Amount Authorized:...',
102                           self.browser.contents)
103        self.assertEqual(self.student.current_mode, 'ug_ft')
104        self.assertMatches(
105            '...<input type="hidden" name="amount" value="87600" />...',
106            self.browser.contents)
107        delta = timedelta(days=8)
108        acc_payment.creation_date -= delta
109        self.browser.open(self.payments_path + '/xyz')
110        self.browser.getLink("CollegePAY", index=0).click()
111        self.assertMatches(
112            '...This payment ticket is too old. Please create a new ticket...',
113            self.browser.contents)
114        delta = timedelta(days=2)
115        acc_payment.creation_date += delta
116        self.browser.open(self.payments_path + '/xyz')
117        self.browser.getLink("CollegePAY", index=0).click()
118        self.assertMatches('...Total Amount Authorized:...',
119                           self.browser.contents)
120
121    @external_test
122    def test_webservice(self):
123        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
124        self.browser.open(self.payments_path)
125        IWorkflowState(self.student).setState('cleared')
126        self.student.nationality = u'NG'
127        self.browser.open(self.payments_path + '/addop')
128        self.browser.getControl(name="form.p_category").value = ['schoolfee']
129        self.browser.getControl("Create ticket").click()
130        self.assertMatches('...ticket created...',
131                           self.browser.contents)
132        ctrl = self.browser.getControl(name='val_id')
133        self.value = ctrl.options[0]
134        self.browser.getLink(self.value).click()
135        self.payment_url = self.browser.url
136        # First we have open InterswitchPageStudent to set provider_amt
137        # and gateway_amt
138        self.browser.open(self.payment_url + '/goto_interswitch')
139        # Now we can call the webservice
140        self.browser.open(self.payment_url + '/request_webservice')
141        self.assertMatches('...Unsuccessful callback...',
142                          self.browser.contents)
143        # The payment is now in state failed ...
144        self.assertMatches('...<span>Failed</span>...',
145                          self.browser.contents)
146        # ... and the catalog has been updated
147        cat = getUtility(ICatalog, name='payments_catalog')
148        results = list(
149            cat.searchResults(p_state=('failed', 'failed')))
150        self.assertEqual(len(results), 1)
151        self.assertEqual(results[0].p_state, 'failed')
152
153        # Let's replace the p_id with a valid p_id of the Uniben
154        # live system. This is definitely not an appropriate
155        # solution for testing, but we have no choice since
156        # Interswitch doesn't provide any interface
157        # for testing.
158        payment = self.student['payments'][self.value]
159        payment.p_id = 'p3547789850240'
160        self.browser.open(self.payment_url + '/request_webservice')
161        self.assertMatches('...Callback amount does not match...',
162                          self.browser.contents)
163        # The payment is now in state failed ...
164        self.assertMatches('...<span>Failed</span>...',
165                          self.browser.contents)
166        # Let's replace the amount autorized with the amount of the
167        # live system payment
168        payment.amount_auth = payment.r_amount_approved
169        self.browser.open(self.payment_url + '/request_webservice')
170        self.assertMatches('...Successful payment...',
171                          self.browser.contents)
172        # The payment is now in state paid ...
173        self.assertMatches('...<span>Paid</span>...',
174                          self.browser.contents)
175        # ... and the catalog has been updated
176        cat = getUtility(ICatalog, name='payments_catalog')
177        results = list(
178            cat.searchResults(p_state=('paid', 'paid')))
179        self.assertEqual(len(results), 1)
180        self.assertEqual(results[0].p_state, 'paid')
181        # Approval is logged in students.log ...
182        logfile = os.path.join(
183            self.app['datacenter'].storage, 'logs', 'students.log')
184        logcontent = open(logfile).read()
185        self.assertTrue(
186            'zope.mgr - '
187            'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
188            'X1000000 - successful schoolfee payment: p3547789850240\n'
189            in logcontent)
190        # ... and in payments.log
191        logfile = os.path.join(
192            self.app['datacenter'].storage, 'logs', 'payments.log')
193        logcontent = open(logfile).read()
194        self.assertTrue(
195            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
196            '12000.0,00,0.0,150.0,0.0,,,\n'
197            in logcontent)
198
199
200class InterswitchTestsApplicants(ApplicantsFullSetup):
201    """Tests for the Interswitch payment gateway.
202    """
203
204    layer = FunctionalLayer
205
206    def setUp(self):
207        super(InterswitchTestsApplicants, self).setUp()
208        configuration = SessionConfiguration()
209        configuration.academic_session = datetime.now().year - 2
210        self.app['configuration'].addSessionConfiguration(configuration)
211        self.configuration = configuration
212        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
213        self.browser.open(self.manage_path)
214        #IWorkflowState(self.student).setState('started')
215        super(InterswitchTestsApplicants, self).fill_correct_values()
216        self.applicantscontainer.application_fee = 1000.0
217        self.browser.getControl(name="form.nationality").value = ['NG']
218        self.browser.getControl(name="transition").value = ['start']
219        self.browser.getControl("Save").click()
220        self.browser.getControl("Add online").click()
221        self.assertMatches('...passport photo before making payment...',
222                           self.browser.contents)
223        self.browser.open(self.manage_path)
224        super(InterswitchTestsApplicants, self).fill_correct_values()
225        self.browser.getControl(name="form.nationality").value = ['NG']
226        #self.browser.getControl(name="transition").value = ['start']
227        image = open(SAMPLE_IMAGE, 'rb')
228        ctrl = self.browser.getControl(name='form.passport')
229        file_ctrl = ctrl.mech_control
230        file_ctrl.add_file(image, filename='myphoto.jpg')
231        self.browser.getControl("Save").click()
232        self.browser.getControl("Add online").click()
233        self.assertMatches('...ticket created...',
234                           self.browser.contents)
235        #ctrl = self.browser.getControl(name='val_id')
236        #value = ctrl.options[0]
237        #self.browser.getLink(value).click()
238        self.assertMatches('...Amount Authorized...',
239                           self.browser.contents)
240        self.assertMatches(
241            '...<span>1000.0</span>...',
242            self.browser.contents)
243        self.payment_url = self.browser.url
244
245
246    def test_interswitch_form(self):
247        # Manager can access InterswitchForm
248        self.browser.getLink("CollegePAY", index=0).click()
249        self.assertMatches('...Total Amount Authorized:...',
250                           self.browser.contents)
251        self.assertMatches(
252            '...<input type="hidden" name="amount" value="100000" />...',
253            self.browser.contents)
254        delta = timedelta(days=8)
255        self.applicant.values()[0].creation_date -= delta
256        self.browser.open(self.payment_url)
257        self.browser.getLink("CollegePAY", index=0).click()
258        self.assertMatches(
259            '...This payment ticket is too old. Please create a new ticket...',
260            self.browser.contents)
261
262    @external_test
263    def test_webservice(self):
264
265        self.browser.open(self.payment_url + '/request_webservice')
266        self.assertMatches('...Unsuccessful callback...',
267                          self.browser.contents)
268        # The payment is now in state failed
269        self.assertMatches('...<span>Failed</span>...',
270                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.