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

Last change on this file since 13808 was 13678, checked in by Henrik Bettermann, 9 years ago

Parttime and fulltime postgrad students pay different acceptance fees.

  • Property svn:keywords set to Id
File size: 18.9 KB
Line 
1## $Id: tests.py 13678 2016-02-11 10:45:59Z 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 = 10000.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
66    def test_interswitch_form_regular(self):
67        self.browser.getControl("Add online").click()
68        self.assertTrue('ticket created' in self.browser.contents)
69        #self.browser.getLink(value).click()
70        self.assertTrue('Amount Authorized' in self.browser.contents)
71        self.assertTrue('<span>10000.0</span>' in self.browser.contents)
72        self.payment_url = self.browser.url
73        # Manager can access InterswitchForm
74        self.browser.getLink("CollegePAY", index=0).click()
75        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
76        self.assertTrue(
77            '<input type="hidden" name="amount" value="1000000" />'
78            in self.browser.contents)
79        self.assertTrue(
80            'item_name="BT Education" item_amt="100000"'
81            in self.browser.contents)
82        self.assertTrue(
83            'item_name="Application Fee" item_amt="875000" bank_id="117" acct_num="1010835352"'
84            in self.browser.contents)
85        self.assertTrue(
86            '<input type="hidden" name="product_id" value="5845" />'
87            in self.browser.contents)
88        delta = timedelta(days=8)
89        self.applicant.values()[0].creation_date -= delta
90        self.browser.open(self.payment_url)
91        self.browser.getLink("CollegePAY", index=0).click()
92        self.assertTrue(
93            'This payment ticket is too old. Please create a new ticket'
94            in self.browser.contents)
95
96    @external_test
97    def test_webservice(self):
98
99        self.browser.open(self.payment_url + '/request_webservice')
100        self.assertTrue('Unsuccessful callback' in self.browser.contents)
101        # The payment is now in state failed
102        self.assertTrue('<span>Failed</span>' in self.browser.contents)
103
104class InterswitchTestsStudents(StudentsFullSetup):
105    """Tests for the Interswitch payment gateway.
106    """
107
108    layer = FunctionalLayer
109
110    def setUp(self):
111        super(InterswitchTestsStudents, self).setUp()
112
113    def test_interswitch_form_pt_school_fees(self):
114        self.certificate.study_mode = 'ug_pt'
115        self.student['studycourse'].entry_session = 2013
116        self.student['studycourse'].certificate.school_fee_1 = 50250.0
117        self.app['configuration']['2004'].union_fee = 1000.0
118        self.app['configuration']['2004'].welfare_fee = 500.0
119        # Manager can access InterswitchForm
120        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
121        self.browser.open(self.payments_path)
122        IWorkflowState(self.student).setState('cleared')
123        self.student.nationality = u'NG'
124        self.browser.open(self.payments_path + '/addop')
125        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
126        self.browser.getControl("Create ticket").click()
127        self.assertTrue('ticket created' in self.browser.contents)
128        ctrl = self.browser.getControl(name='val_id')
129        self.value = ctrl.options[0]
130        self.browser.getLink(self.value).click()
131        self.assertTrue('Amount Authorized' in self.browser.contents)
132        # 50250 + 1000 + 500 -500
133        self.assertTrue('<span>51250.0</span>' in self.browser.contents)
134        self.payment_url = self.browser.url
135        self.browser.getLink("CollegePAY", index=0).click()
136        self.assertTrue(
137            'Please pay acceptance fee first' in self.browser.contents)
138        acc_payment = createObject('waeup.StudentOnlinePayment')
139        acc_payment.p_state = 'paid'
140        acc_payment.p_category = 'clearance'
141        self.student['payments']['xyz'] = acc_payment
142        self.browser.getLink("CollegePAY", index=0).click()
143        self.assertTrue('<input type="hidden" name="pay_item_id" value="105" />'
144                        in self.browser.contents)
145        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
146        self.assertEqual(self.student.current_mode, 'ug_pt')
147        self.assertTrue(
148            '<input type="hidden" name="amount" value="5125000" />'
149            in self.browser.contents)
150        self.assertTrue(
151            'item_name="School Fee Plus" '
152            'item_amt="4600000" bank_id="7" '
153            'acct_num="1014847058"' in self.browser.contents)
154
155    def test_interswitch_form_regular_school_fees(self):
156        self.certificate.study_mode = 'ug_ft'
157        self.student['studycourse'].entry_session = 2013
158        self.student['studycourse'].certificate.school_fee_1 = 51750.0
159        # Manager can access InterswitchForm
160        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
161        self.browser.open(self.payments_path)
162        IWorkflowState(self.student).setState('cleared')
163        self.student.nationality = u'NG'
164        self.browser.open(self.payments_path + '/addop')
165        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
166        self.browser.getControl("Create ticket").click()
167        self.assertTrue('ticket created' in
168                           self.browser.contents)
169        ctrl = self.browser.getControl(name='val_id')
170        self.value = ctrl.options[0]
171        self.browser.getLink(self.value).click()
172        self.assertTrue('Amount Authorized' in
173                           self.browser.contents)
174        self.assertTrue('<span>51750.0</span>' in self.browser.contents)
175        self.payment_url = self.browser.url
176        self.browser.getLink("CollegePAY", index=0).click()
177        self.assertTrue(
178            'Please pay acceptance fee first' in self.browser.contents)
179        acc_payment = createObject('waeup.StudentOnlinePayment')
180        acc_payment.p_state = 'paid'
181        acc_payment.p_category = 'clearance'
182        self.student['payments']['xyz'] = acc_payment
183        self.browser.getLink("CollegePAY", index=0).click()
184        self.assertTrue('<input type="hidden" name="pay_item_id" value="107" />'
185                        in self.browser.contents)
186        self.assertTrue('Total Amount Authorized:'
187                        in self.browser.contents)
188        self.assertEqual(self.student.current_mode, 'ug_ft')
189        self.assertTrue(
190            '<input type="hidden" name="amount" value="5175000" />'
191            in self.browser.contents)
192        self.assertTrue(
193            'item_name="School Fee Plus" item_amt="4750000" bank_id="117" '
194            'acct_num="1010827641"' in self.browser.contents)
195
196
197    def test_interswitch_form_pt_acceptance_fees(self):
198        self.certificate.study_mode = 'ug_pt'
199        self.app['configuration']['2004'].clearance_fee_ug_pt = 40250.0
200        self.app['configuration']['2004'].matric_gown_fee = 2000.0
201        self.app['configuration']['2004'].lapel_fee = 800.0
202        # Manager can access InterswitchForm
203        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
204        self.browser.open(self.payments_path)
205        IWorkflowState(self.student).setState('admitted')
206        self.student.nationality = u'NG'
207        self.browser.open(self.payments_path + '/addop')
208        self.browser.getControl(name="form.p_category").value = ['clearance_incl']
209        self.browser.getControl("Create ticket").click()
210        self.assertTrue('ticket created' in  self.browser.contents)
211        ctrl = self.browser.getControl(name='val_id')
212        self.value = ctrl.options[0]
213        self.browser.getLink(self.value).click()
214        self.assertTrue('Amount Authorized' in self.browser.contents)
215        # 40250 + 2000 + 800 - 500
216        self.assertTrue('<span>42550.0</span>' in self.browser.contents)
217        self.payment_url = self.browser.url
218        self.browser.getLink("CollegePAY", index=0).click()
219        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />' in
220                           self.browser.contents)
221        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
222        self.assertTrue(
223            '<input type="hidden" name="amount" value="4255000" />'
224            in self.browser.contents)
225        self.assertTrue(
226            'item_name="Acceptance Fee Plus" '
227            'item_amt="4000000" bank_id="117" '
228            'acct_num="1014066976"' in self.browser.contents)
229
230    def test_interswitch_form_regular_acceptance_fees(self):
231        self.certificate.study_mode = 'ug_ft'
232        self.app['configuration']['2004'].clearance_fee = 43050.0
233        # Manager can access InterswitchForm
234        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
235        self.browser.open(self.payments_path)
236        IWorkflowState(self.student).setState('admitted')
237        self.student.nationality = u'NG'
238        self.browser.open(self.payments_path + '/addop')
239        self.browser.getControl(name="form.p_category").value = ['clearance_incl']
240        self.browser.getControl("Create ticket").click()
241        self.assertTrue('ticket created' in self.browser.contents)
242        ctrl = self.browser.getControl(name='val_id')
243        self.value = ctrl.options[0]
244        self.browser.getLink(self.value).click()
245        self.assertTrue('Amount Authorized' in self.browser.contents)
246        self.assertTrue('<span>43050.0</span>' in self.browser.contents)
247        self.payment_url = self.browser.url
248        self.browser.getLink("CollegePAY", index=0).click()
249        self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />'
250                        in self.browser.contents)
251        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
252        self.assertTrue(
253            '<input type="hidden" name="amount" value="4305000" />'
254            in self.browser.contents)
255        self.assertTrue(
256            'item_name="Acceptance Fee Plus" item_amt="4280000" bank_id="117" '
257            'acct_num="1010827641"' in self.browser.contents)
258
259    def test_interswitch_form_concessional_fees(self):
260        self.certificate.study_mode = 'ug_pt'
261        self.app['configuration']['2004'].concessional_fee = 1567.0
262        # Manager can access InterswitchForm
263        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
264        self.browser.open(self.payments_path)
265        IWorkflowState(self.student).setState('admitted')
266        self.student.nationality = u'NG'
267        self.browser.open(self.payments_path + '/addop')
268        self.browser.getControl(name="form.p_category").value = ['concessional']
269        self.browser.getControl("Create ticket").click()
270        self.assertTrue('ticket created' in  self.browser.contents)
271        ctrl = self.browser.getControl(name='val_id')
272        self.value = ctrl.options[0]
273        self.browser.getLink(self.value).click()
274        self.assertTrue('Amount Authorized' in self.browser.contents)
275        self.assertTrue(
276            '<span>1567.0</span>' in self.browser.contents)
277        self.payment_url = self.browser.url
278        self.certificate.study_mode = 'ug_ft'
279        self.browser.getLink("CollegePAY", index=0).click()
280        self.assertTrue('<input type="hidden" name="pay_item_id" value="107" />'
281                           in self.browser.contents)
282        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
283        self.assertTrue(
284            '<input type="hidden" name="amount" value="156700" />'
285            in self.browser.contents)
286        self.assertTrue(
287            'item_name="Concessional Fee" item_amt="131700" '
288            'bank_id="117" acct_num="1010835352"'
289            in self.browser.contents)
290
291    def test_interswitch_form_ticket_expired(self):
292        self.certificate.study_mode = 'ug_pt'
293        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
294        acc_payment = createObject('waeup.StudentOnlinePayment')
295        acc_payment.p_state = 'unpaid'
296        acc_payment.p_session = 2004
297        acc_payment.p_category = 'clearance'
298        acc_payment.p_id = 'xyz'
299        acc_payment.pay_item_id = '123'
300        acc_payment.amount_auth = 876.0
301        self.student['payments']['xyz'] = acc_payment
302        self.browser.open(self.payments_path + '/xyz')
303        self.browser.getLink("CollegePAY", index=0).click()
304        self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />'
305                        in self.browser.contents)
306        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
307        self.assertEqual(self.student.current_mode, 'ug_pt')
308        self.assertTrue(
309            '<input type="hidden" name="amount" value="87600" />'
310            in self.browser.contents)
311        delta = timedelta(days=8)
312        acc_payment.creation_date -= delta
313        self.browser.open(self.payments_path + '/xyz')
314        self.browser.getLink("CollegePAY", index=0).click()
315        self.assertTrue(
316            'This payment ticket is too old. Please create a new ticket'
317            in self.browser.contents)
318        delta = timedelta(days=2)
319        acc_payment.creation_date += delta
320        self.browser.open(self.payments_path + '/xyz')
321        self.browser.getLink("CollegePAY", index=0).click()
322        self.assertTrue('Total Amount Authorized:' in self.browser.contents)
323
324    @external_test
325    def test_webservice(self):
326        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
327        self.browser.open(self.payments_path)
328        IWorkflowState(self.student).setState('cleared')
329        self.student.nationality = u'NG'
330        self.browser.open(self.payments_path + '/addop')
331        self.browser.getControl(name="form.p_category").value = ['schoolfee_incl']
332        self.browser.getControl("Create ticket").click()
333        self.assertTrue('ticket created' in self.browser.contents)
334        ctrl = self.browser.getControl(name='val_id')
335        self.value = ctrl.options[0]
336        self.browser.getLink(self.value).click()
337        self.payment_url = self.browser.url
338        # First we have to open InterswitchPageStudent to set provider_amt
339        # and gateway_amt
340        self.browser.open(self.payment_url + '/goto_interswitch')
341        # Now we can call the webservice
342        self.browser.open(self.payment_url + '/request_webservice')
343        self.assertTrue('Unsuccessful callback' in self.browser.contents)
344        # The payment is now in state failed ...
345        self.assertTrue('<span>Failed</span>' in self.browser.contents)
346        # ... and the catalog has been updated
347        cat = getUtility(ICatalog, name='payments_catalog')
348        results = list(
349            cat.searchResults(p_state=('failed', 'failed')))
350        self.assertEqual(len(results), 1)
351        self.assertEqual(results[0].p_state, 'failed')
352
353        # Let's replace the p_id with a valid p_id of the AAUE
354        # live system. This is definitely not an appropriate
355        # solution for testing, but we have no choice since
356        # Interswitch doesn't provide any interface
357        # for testing.
358        payment = self.student['payments'][self.value]
359        payment.p_id = 'p4465649308559'
360        self.browser.open(self.payment_url + '/request_webservice')
361        self.assertTrue('Unsuccessful callback: Amount Inconsistency' in
362                          self.browser.contents)
363        # The payment is now in state failed ...
364        self.assertTrue('<span>Failed</span>' in self.browser.contents)
365        # Let's replace the amount autorized with the amount of the
366        # live system payment
367        payment.amount_auth = 60250.0
368        self.browser.open(self.payment_url + '/request_webservice')
369        self.assertTrue('Successful payment' in
370                          self.browser.contents)
371        # The payment is now in state paid ...
372        self.assertTrue('<span>Paid</span>' in self.browser.contents)
373        # ... and the catalog has been updated
374        cat = getUtility(ICatalog, name='payments_catalog')
375        results = list(
376            cat.searchResults(p_state=('paid', 'paid')))
377        self.assertEqual(len(results), 1)
378        self.assertEqual(results[0].p_state, 'paid')
379        # Approval is logged in students.log ...
380        logfile = os.path.join(
381            self.app['datacenter'].storage, 'logs', 'students.log')
382        logcontent = open(logfile).read()
383        self.assertTrue(
384            'zope.mgr - '
385            'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
386            'E1000000 - successful schoolfee_incl payment: p4465649308559\n'
387            in logcontent)
388        # ... and in payments.log
389        logfile = os.path.join(
390            self.app['datacenter'].storage, 'logs', 'payments.log')
391        logcontent = open(logfile).read()
392        self.assertTrue(
393            '"zope.mgr",E1000000,p4465649308559,schoolfee_incl,'
394            '60250.0,00,1500.0,250.0,0.0,,,\n'
395            in logcontent)
Note: See TracBrowser for help on using the repository browser.