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

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

Configure school fee and acceptance fee payments.

  • Property svn:keywords set to Id
File size: 13.3 KB
Line 
1## $Id: tests.py 14875 2017-10-24 08:43:15Z 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        # ND FT Non-Deltan Fresh Student School Fee
62        self.certificate.study_mode = 'nd_ft'
63        self.certificate.school_fee_3 = 30000.0
64        self.browser.open(self.payments_path + '/addop')
65        self.browser.getControl(name="form.p_category").value = ['schoolfee']
66        self.browser.getControl("Create ticket").click()
67        self.assertMatches('...ticket created...',
68                           self.browser.contents)
69        ctrl = self.browser.getControl(name='val_id')
70        self.value = ctrl.options[0]
71        self.browser.getLink(self.value).click()
72        self.assertMatches('...Amount Authorized...',
73                           self.browser.contents)
74        self.assertMatches(
75            '...<span>30000.0</span>...',
76            self.browser.contents)
77        self.payment_url = self.browser.url
78        self.browser.getLink("CollegePAY", index=0).click()
79        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
80            in self.browser.contents)
81        self.assertTrue(
82            '<input type="hidden" name="amount" value="3000000" />' in
83            self.browser.contents)
84        self.assertTrue(
85            'item_name="School Fee" item_amt="2695000" bank_id="31" acct_num="0026235493"' in
86            self.browser.contents)
87        # ND FT Non-Deltan Fresh Student Acceptance Fee
88        self.app['configuration']['2004'].clearance_fee = 12345.0
89        self.browser.open(self.payments_path + '/addop')
90        self.browser.getControl(name="form.p_category").value = ['clearance']
91        self.browser.getControl("Create ticket").click()
92        self.assertMatches('...ticket created...',
93                           self.browser.contents)
94        ctrl = self.browser.getControl(name='val_id')
95        self.value = ctrl.options[1]
96        self.browser.getLink(self.value).click()
97        self.assertMatches('...Amount Authorized...',
98                           self.browser.contents)
99        self.assertMatches(
100            '...<span>12345.0</span>...',
101            self.browser.contents)
102        self.payment_url = self.browser.url
103        self.browser.getLink("CollegePAY", index=0).click()
104        self.assertTrue('<input type="hidden" name="pay_item_id" value="103" />' in
105                           self.browser.contents)
106        self.assertTrue(
107            '<input type="hidden" name="amount" value="1234500" />' in
108            self.browser.contents)
109        self.assertTrue(
110            'item_name="Acceptance" item_amt="1159500" bank_id="8" acct_num="2004402644"' in
111            self.browser.contents)
112
113    def test_interswitch_form_ticket_expired(self):
114        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
115        acc_payment = createObject('waeup.StudentOnlinePayment')
116        acc_payment.p_state = 'unpaid'
117        acc_payment.p_category = 'clearance'
118        acc_payment.p_id = 'xyz'
119        acc_payment.pay_item_id = '123'
120        acc_payment.amount_auth = 876.0
121        self.student['payments']['xyz'] = acc_payment
122        self.browser.open(self.payments_path + '/xyz')
123        self.browser.getLink("CollegePAY", index=0).click()
124        self.assertMatches('...<input type="hidden" name="pay_item_id" value="103" />...',
125                           self.browser.contents)
126        self.assertMatches('...Total Amount Authorized:...',
127                           self.browser.contents)
128        self.assertEqual(self.student.current_mode, 'ug_ft')
129        self.assertMatches(
130            '...<input type="hidden" name="amount" value="87600" />...',
131            self.browser.contents)
132        delta = timedelta(days=8)
133        acc_payment.creation_date -= delta
134        self.browser.open(self.payments_path + '/xyz')
135        self.browser.getLink("CollegePAY", index=0).click()
136        self.assertMatches(
137            '...This payment ticket is too old. Please create a new ticket...',
138            self.browser.contents)
139        delta = timedelta(days=2)
140        acc_payment.creation_date += delta
141        self.browser.open(self.payments_path + '/xyz')
142        self.browser.getLink("CollegePAY", index=0).click()
143        self.assertMatches('...Total Amount Authorized:...',
144                           self.browser.contents)
145
146    @external_test
147    def test_webservice(self):
148        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
149        self.browser.open(self.payments_path)
150        IWorkflowState(self.student).setState('cleared')
151        self.student.nationality = u'NG'
152        self.browser.open(self.payments_path + '/addop')
153        self.browser.getControl(name="form.p_category").value = ['schoolfee']
154        self.browser.getControl("Create ticket").click()
155        self.assertMatches('...ticket created...',
156                           self.browser.contents)
157        ctrl = self.browser.getControl(name='val_id')
158        self.value = ctrl.options[0]
159        self.browser.getLink(self.value).click()
160        self.payment_url = self.browser.url
161        # First we have open InterswitchPageStudent to set provider_amt
162        # and gateway_amt
163        self.browser.open(self.payment_url + '/goto_interswitch')
164        # Now we can call the webservice
165        self.browser.open(self.payment_url + '/request_webservice')
166        self.assertMatches('...Unsuccessful callback...',
167                          self.browser.contents)
168        # The payment is now in state failed ...
169        self.assertMatches('...<span>Failed</span>...',
170                          self.browser.contents)
171        # ... and the catalog has been updated
172        cat = getUtility(ICatalog, name='payments_catalog')
173        results = list(
174            cat.searchResults(p_state=('failed', 'failed')))
175        self.assertEqual(len(results), 1)
176        self.assertEqual(results[0].p_state, 'failed')
177
178        # Let's replace the p_id with a valid p_id of the Uniben
179        # live system. This is definitely not an appropriate
180        # solution for testing, but we have no choice since
181        # Interswitch doesn't provide any interface
182        # for testing.
183        payment = self.student['payments'][self.value]
184        payment.p_id = 'p3547789850240'
185        self.browser.open(self.payment_url + '/request_webservice')
186        self.assertMatches('...Callback amount does not match...',
187                          self.browser.contents)
188        # The payment is now in state failed ...
189        self.assertMatches('...<span>Failed</span>...',
190                          self.browser.contents)
191        # Let's replace the amount autorized with the amount of the
192        # live system payment
193        payment.amount_auth = payment.r_amount_approved
194        self.browser.open(self.payment_url + '/request_webservice')
195        self.assertMatches('...Successful payment...',
196                          self.browser.contents)
197        # The payment is now in state paid ...
198        self.assertMatches('...<span>Paid</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=('paid', 'paid')))
204        self.assertEqual(len(results), 1)
205        self.assertEqual(results[0].p_state, 'paid')
206        # Approval is logged in students.log ...
207        logfile = os.path.join(
208            self.app['datacenter'].storage, 'logs', 'students.log')
209        logcontent = open(logfile).read()
210        self.assertTrue(
211            'zope.mgr - '
212            'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
213            'X1000000 - successful schoolfee payment: p3547789850240\n'
214            in logcontent)
215        # ... and in payments.log
216        logfile = os.path.join(
217            self.app['datacenter'].storage, 'logs', 'payments.log')
218        logcontent = open(logfile).read()
219        self.assertTrue(
220            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
221            '12000.0,00,0.0,150.0,0.0,,,\n'
222            in logcontent)
223
224
225class InterswitchTestsApplicants(ApplicantsFullSetup):
226    """Tests for the Interswitch payment gateway.
227    """
228
229    layer = FunctionalLayer
230
231    def setUp(self):
232        super(InterswitchTestsApplicants, self).setUp()
233        configuration = SessionConfiguration()
234        configuration.academic_session = datetime.now().year - 2
235        self.app['configuration'].addSessionConfiguration(configuration)
236        self.configuration = configuration
237        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
238        self.browser.open(self.manage_path)
239        #IWorkflowState(self.student).setState('started')
240        super(InterswitchTestsApplicants, self).fill_correct_values()
241        self.applicantscontainer.application_fee = 1000.0
242        self.browser.getControl(name="form.nationality").value = ['NG']
243        self.browser.getControl(name="transition").value = ['start']
244        self.browser.getControl("Save").click()
245        self.browser.getControl("Add online").click()
246        self.assertMatches('...passport photo before making payment...',
247                           self.browser.contents)
248        self.browser.open(self.manage_path)
249        super(InterswitchTestsApplicants, self).fill_correct_values()
250        self.browser.getControl(name="form.nationality").value = ['NG']
251        #self.browser.getControl(name="transition").value = ['start']
252        image = open(SAMPLE_IMAGE, 'rb')
253        ctrl = self.browser.getControl(name='form.passport')
254        file_ctrl = ctrl.mech_control
255        file_ctrl.add_file(image, filename='myphoto.jpg')
256        self.browser.getControl("Save").click()
257        self.browser.getControl("Add online").click()
258        self.assertMatches('...ticket created...',
259                           self.browser.contents)
260        #ctrl = self.browser.getControl(name='val_id')
261        #value = ctrl.options[0]
262        #self.browser.getLink(value).click()
263        self.assertMatches('...Amount Authorized...',
264                           self.browser.contents)
265        self.assertMatches(
266            '...<span>1000.0</span>...',
267            self.browser.contents)
268        self.payment_url = self.browser.url
269
270
271    def test_interswitch_form(self):
272        # Manager can access InterswitchForm
273        self.browser.getLink("CollegePAY", index=0).click()
274        self.assertMatches('...Total Amount Authorized:...',
275                           self.browser.contents)
276        self.assertMatches(
277            '...<input type="hidden" name="amount" value="100000" />...',
278            self.browser.contents)
279        delta = timedelta(days=8)
280        self.applicant.values()[0].creation_date -= delta
281        self.browser.open(self.payment_url)
282        self.browser.getLink("CollegePAY", index=0).click()
283        self.assertMatches(
284            '...This payment ticket is too old. Please create a new ticket...',
285            self.browser.contents)
286
287    @external_test
288    def test_webservice(self):
289
290        self.browser.open(self.payment_url + '/request_webservice')
291        self.assertMatches('...Unsuccessful callback...',
292                          self.browser.contents)
293        # The payment is now in state failed
294        self.assertMatches('...<span>Failed</span>...',
295                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.