source: main/waeup.kwarapoly/trunk/src/waeup/kwarapoly/interswitch/tests.py @ 14066

Last change on this file since 14066 was 14066, checked in by Henrik Bettermann, 8 years ago

Change split amounts for pre applications.

  • Property svn:keywords set to Id
File size: 37.3 KB
Line 
1## $Id: tests.py 14066 2016-08-10 13:04:36Z 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
19import grok
20import pytz
21from datetime import datetime, date, timedelta
22from hurry.workflow.interfaces import IWorkflowState
23from zope.component import createObject, getUtility
24from zope.catalog.interfaces import ICatalog
25from zope.event import notify
26from waeup.kofa.applicants.container import ApplicantsContainer
27from waeup.kofa.university.faculty import Faculty
28from waeup.kofa.university.department import Department
29from waeup.kofa.students.tests.test_browser import StudentsFullSetup
30from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
31from waeup.kofa.configuration import SessionConfiguration
32from waeup.kwarapoly.testing import FunctionalLayer
33from waeup.kwarapoly.students.payments import CustomStudentOnlinePayment
34
35# Also run tests that send requests to external servers?
36#   If you enable this, please make sure the external services
37#   do exist really and are not bothered by being spammed by a test programme.
38EXTERNAL_TESTS = False
39
40def external_test(func):
41    if not EXTERNAL_TESTS:
42        myself = __file__
43        if myself.endswith('.pyc'):
44            myself = myself[:-2]
45        print "WARNING: external tests are skipped!"
46        print "WARNING: edit %s to enable them." % myself
47        return
48    return func
49
50
51class InterswitchTestsStudents(StudentsFullSetup):
52    """Tests for the Interswitch payment gateway.
53    """
54
55    layer = FunctionalLayer
56
57    def setUp(self):
58        super(InterswitchTestsStudents, self).setUp()
59
60        # Create at least one Kwarapoly faculty
61        self.app['faculties']['CPGS'] = Faculty(code='CPGS')
62        self.app['faculties']['CPGS']['dep1'] = Department(code='dep1')
63        self.certificate2 = createObject('waeup.Certificate')
64        self.certificate2.code = u'CERT2'
65        self.certificate2.application_category = 'basic'
66        self.certificate2.study_mode = 'nd_ft'
67        self.certificate2.start_level = 100
68        self.certificate2.end_level = 300
69        self.app['faculties']['CPGS']['dep1'].certificates.addCertificate(
70            self.certificate2)
71        # Set study course attributes of test student
72        self.student['studycourse'].certificate = self.certificate2
73        self.student['studycourse'].current_session = 2004
74        self.student['studycourse'].entry_session = 2004
75        self.student['studycourse'].current_verdict = 'A'
76        self.student['studycourse'].current_level = 100
77        # Set local lga
78        self.student.lga = u'kwara_asa'
79        # Update the catalog
80        notify(grok.ObjectModifiedEvent(self.student))
81
82    def test_schoolfee_ticket_creation(self):
83        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
84        self.browser.open(self.payments_path)
85        IWorkflowState(self.student).setState('cleared')
86        self.browser.open(self.payments_path + '/addop')
87        self.browser.getControl(name="form.p_category").value = ['schoolfee']
88        self.browser.getControl("Create ticket").click()
89        self.assertMatches('...Book and pay for accommodation first...',
90                           self.browser.contents)
91        # We add a fake maint. payment ticket to meet the condition
92        maint_payment = CustomStudentOnlinePayment()
93        self.student['payments']['any_key'] = maint_payment
94        maint_payment.p_category = 'hostel_maintenance'
95        maint_payment.p_state = 'unpaid'
96        maint_payment.p_session = 2004
97        self.browser.getControl(name="form.p_category").value = ['schoolfee']
98        self.browser.getControl("Create ticket").click()
99        self.assertMatches('...Book and pay for accommodation first...',
100                           self.browser.contents)
101        # Ticket must be paid
102        maint_payment.p_state = 'paid'
103        self.browser.getControl(name="form.p_category").value = ['schoolfee']
104        self.browser.getControl("Create ticket").click()
105        self.assertMatches('...ticket created...',
106                           self.browser.contents)
107        ctrl = self.browser.getControl(name='val_id')
108        self.value = ctrl.options[1]
109        self.browser.getLink(self.value).click()
110        self.assertMatches('...Amount Authorized...',
111                           self.browser.contents)
112        self.assertTrue('<span>40700.0</span>' in self.browser.contents)
113        self.payment_url = self.browser.url
114
115
116#    def callback_url(self, payment_url, resp, apprAmt):
117#        return payment_url + (
118#            '/isw_callback?echo=' +
119#            '&resp=%s' +
120#            '&desc=Something went wrong' +
121#            '&txnRef=p1331792385335' +
122#            '&payRef=' + '&retRef=' +
123#            '&cardNum=0' +
124#            '&apprAmt=%s' +
125#            '&url=http://xyz') % (resp, apprAmt)
126
127    def test_interswitch_form(self):
128
129        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
130        self.browser.open(self.payments_path)
131        # In KwaraPoly only returning students can create school fee payment
132        # without haveing paid accommodation fee
133        IWorkflowState(self.student).setState('returning')
134        configuration = createObject('waeup.SessionConfiguration')
135        configuration.academic_session = 2005
136        self.app['configuration'].addSessionConfiguration(configuration)
137        self.browser.open(self.payments_path + '/addop')
138        self.browser.getControl(name="form.p_category").value = ['schoolfee']
139        self.browser.getControl("Create ticket").click()
140        self.assertMatches('...ticket created...', self.browser.contents)
141        ctrl = self.browser.getControl(name='val_id')
142        self.value = ctrl.options[0]
143        self.browser.getLink(self.value).click()
144        self.assertMatches('...Amount Authorized...', self.browser.contents)
145        self.assertTrue(
146            '<span>31500.0</span>' in self.browser.contents)
147        self.payment_url = self.browser.url
148
149        # Manager can access InterswitchForm
150        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
151        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
152        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0)
153        self.browser.getLink("CollegePAY", index=0).click()
154        # Split amounts have been set.
155        self.assertEqual(self.student['payments'][self.value].provider_amt, 1200.0)
156        self.assertEqual(self.student['payments'][self.value].gateway_amt, 300.0)
157        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 1800.0)
158        self.assertMatches('...Total Amount Authorized:...',
159                           self.browser.contents)
160        self.assertTrue(
161            '<input type="hidden" name="amount" value="3150000" />' in
162            self.browser.contents)
163        self.assertTrue(
164            'item_name="School Fee" item_amt="2820000" bank_id="10" acct_num="0106259811"' in
165            self.browser.contents)
166        self.assertTrue(
167            'item_name="Dalash" item_amt="180000" bank_id="117" acct_num="1013196791"' in
168            self.browser.contents)
169        self.assertTrue(
170            'item_name="BT Education" item_amt="120000" bank_id="117" acct_num="1010764827"' in
171            self.browser.contents)
172
173        # Let's do the same for maintenance fee payment
174
175        self.browser.open(self.payments_path)
176        self.browser.open(self.payments_path + '/addop')
177        self.browser.getControl(
178            name="form.p_category").value = ['hostel_maintenance']
179        self.browser.getControl("Create ticket").click()
180        self.assertMatches('...You have not yet booked accommodation...',
181                           self.browser.contents)
182        # Students have to book bed first
183        self.browser.open(self.acco_path)
184        IWorkflowState(self.student).setState('admitted')
185        self.browser.getControl("Book accommodation").click()
186        self.assertFalse('Activation Code:' in self.browser.contents)
187        self.browser.getControl("Create bed ticket").click()
188        # Bed is randomly selected but, since there is only
189        # one bed for this student, we know that ...
190        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
191                           self.browser.contents)
192        self.assertMatches('...ticket created...',
193                           self.browser.contents)
194        self.browser.open(self.payments_path + '/addop')
195        self.browser.getControl(
196            name="form.p_category").value = ['hostel_maintenance']
197        self.browser.getControl("Create ticket").click()
198        self.assertMatches('...ticket created...',
199                           self.browser.contents)
200        ctrl = self.browser.getControl(name='val_id')
201        value = ctrl.options[1]
202        self.browser.getLink(value).click()
203        self.assertMatches('...Amount Authorized...',
204                           self.browser.contents)
205        # Maint fee is taken from the hostel object
206        self.assertTrue('<span>876.0</span>' in self.browser.contents)
207        self.payment_url = self.browser.url
208        # Manager can access InterswitchForm
209        self.browser.getLink("CollegePAY", index=0).click()
210        # Split amounts have been set.
211        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
212        self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0)
213        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
214        # The total amount to be processed by Interswitch
215        # has been reduced by the Interswitch fee of 150 Nairas
216        self.assertMatches('...Total Amount Authorized:...',
217                           self.browser.contents)
218        self.assertTrue(
219            '<input type="hidden" name="pay_item_id" value="102" />' in
220            self.browser.contents)
221        self.assertTrue(
222            '<input type="hidden" name="amount" value="87600" />' in
223            self.browser.contents)
224        self.assertTrue(
225            'item_name="Hostel Maintenance" item_amt="57600" bank_id="10" acct_num="0106259811"' in
226            self.browser.contents)
227
228        # Create carryover ticket
229        self.browser.open(self.payments_path + '/addop')
230        self.browser.getControl(name="form.p_category").value = ['carryover1']
231        self.browser.getControl("Create ticket").click()
232        ctrl = self.browser.getControl(name='val_id')
233        value = ctrl.options[2]
234        self.browser.getLink(value).click()
235        self.assertTrue('<span>6000.0</span>' in self.browser.contents)
236        # Manager can access InterswitchForm
237        self.browser.getLink("CollegePAY", index=0).click()
238        # Split amounts have been set.
239        self.assertEqual(self.student['payments'][self.value].provider_amt, 1200.0)
240        self.assertEqual(self.student['payments'][self.value].gateway_amt, 300.0)
241        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 1800.0)
242        self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in
243                           self.browser.contents)
244        self.assertMatches('...Total Amount Authorized:...',
245                           self.browser.contents)
246        self.assertTrue(
247            '<input type="hidden" name="amount" value="600000" />' in
248            self.browser.contents)
249        self.assertTrue(
250            'item_name="One Carry-Over" item_amt="270000" bank_id="10" acct_num="0106259811"' in
251            self.browser.contents)
252        self.assertTrue(
253            'item_name="Dalash" item_amt="180000" bank_id="117" acct_num="1013196791"' in
254            self.browser.contents)
255        self.assertTrue(
256            'item_name="BT Education" item_amt="120000" bank_id="117" acct_num="1010764827"' in
257            self.browser.contents)
258
259    def test_interswitch_form_new_payment_cats(self):
260        # only a few categories covered
261
262        self.app['configuration']['2004'].certificate_fee = 6800.0
263        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
264        self.browser.open(self.payments_path + '/addop')
265        self.browser.getControl(name="form.p_category").value = ['certificate']
266        self.browser.getControl("Create ticket").click()
267        self.assertMatches('...ticket created...', self.browser.contents)
268        ctrl = self.browser.getControl(name='val_id')
269        value = ctrl.options[0]
270        self.browser.getLink(value).click()
271        self.assertMatches('...Amount Authorized...', self.browser.contents)
272        self.assertTrue('<span>6800.0</span>' in self.browser.contents)
273        self.browser.getLink("CollegePAY", index=0).click()
274        self.assertEqual(self.student['payments'][value].amount_auth, 6800.0)
275        self.assertEqual(self.student['payments'][value].provider_amt, 300.0)
276        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
277        self.assertEqual(self.student['payments'][value].thirdparty_amt, 200.0)
278
279        self.app['configuration']['2004'].transcript_local_fee = 5000.0
280        self.browser.open(self.payments_path + '/addop')
281        self.browser.getControl(name="form.p_category").value = ['transcript_local']
282        self.browser.getControl("Create ticket").click()
283        ctrl = self.browser.getControl(name='val_id')
284        value = ctrl.options[1]
285        self.browser.getLink(value).click()
286        self.assertMatches('...Amount Authorized...', self.browser.contents)
287        self.assertTrue('<span>5000.0</span>' in self.browser.contents)
288        self.payment_url = self.browser.url
289        self.browser.getLink("CollegePAY", index=0).click()
290        self.assertEqual(self.student['payments'][value].amount_auth, 5000.0)
291        self.assertEqual(self.student['payments'][value].provider_amt, 300.0)
292        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
293        self.assertEqual(self.student['payments'][value].thirdparty_amt, 200.0)
294
295        self.app['configuration']['2004'].loss_idcard_fee = 1015.0
296        self.browser.open(self.payments_path + '/addop')
297        self.browser.getControl(name="form.p_category").value = ['loss_idcard']
298        self.browser.getControl("Create ticket").click()
299        ctrl = self.browser.getControl(name='val_id')
300        value = ctrl.options[2]
301        self.browser.getLink(value).click()
302        self.assertMatches('...Amount Authorized...', self.browser.contents)
303        self.assertTrue(
304            '<span>1015.0</span>' in self.browser.contents)
305        self.payment_url = self.browser.url
306        self.browser.getLink("CollegePAY", index=0).click()
307        self.assertEqual(self.student['payments'][value].amount_auth, 1015.0)
308        self.assertEqual(self.student['payments'][value].provider_amt, 9.0)
309        self.assertEqual(self.student['payments'][value].gateway_amt, 15.22)
310        self.assertEqual(self.student['payments'][value].thirdparty_amt, 6.0)
311
312        self.app['configuration']['2004'].loss_examcard_fee = 500.0
313        self.browser.open(self.payments_path + '/addop')
314        self.browser.getControl(name="form.p_category").value = ['loss_examcard']
315        self.browser.getControl("Create ticket").click()
316        ctrl = self.browser.getControl(name='val_id')
317        value = ctrl.options[3]
318        self.browser.getLink(value).click()
319        self.assertMatches('...Amount Authorized...', self.browser.contents)
320        self.assertTrue('<span>500.0</span>' in self.browser.contents)
321        self.payment_url = self.browser.url
322        self.browser.getLink("CollegePAY", index=0).click()
323        self.assertEqual(self.student['payments'][value].amount_auth, 500.0)
324        self.assertEqual(self.student['payments'][value].provider_amt, 4.5)
325        self.assertEqual(self.student['payments'][value].gateway_amt, 7.5)
326        self.assertEqual(self.student['payments'][value].thirdparty_amt, 3)
327
328        self.app['configuration']['2004'].change_inst_fee = 6000.0
329        self.browser.open(self.payments_path + '/addop')
330        self.browser.getControl(name="form.p_category").value = ['change_inst']
331        self.browser.getControl("Create ticket").click()
332        ctrl = self.browser.getControl(name='val_id')
333        value = ctrl.options[4]
334        self.browser.getLink(value).click()
335        self.assertMatches('...Amount Authorized...', self.browser.contents)
336        self.assertTrue('<span>6000.0</span>' in self.browser.contents)
337        self.payment_url = self.browser.url
338        self.browser.getLink("CollegePAY", index=0).click()
339        self.assertEqual(self.student['payments'][value].amount_auth, 6000.0)
340        self.assertEqual(self.student['payments'][value].provider_amt, 300.0)
341        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
342        self.assertEqual(self.student['payments'][value].thirdparty_amt, 200.0)
343
344        self.app['configuration']['2004'].loss_result_fee = 7000.0
345        self.browser.open(self.payments_path + '/addop')
346        self.browser.getControl(name="form.p_category").value = ['loss_result']
347        self.browser.getControl("Create ticket").click()
348        ctrl = self.browser.getControl(name='val_id')
349        value = ctrl.options[5]
350        self.browser.getLink(value).click()
351        self.assertMatches('...Amount Authorized...', self.browser.contents)
352        self.assertTrue('<span>7000.0</span>' in self.browser.contents)
353        self.payment_url = self.browser.url
354        self.browser.getLink("CollegePAY", index=0).click()
355        self.assertEqual(self.student['payments'][value].amount_auth, 7000.0)
356        self.assertEqual(self.student['payments'][value].provider_amt, 300.0)
357        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
358        self.assertEqual(self.student['payments'][value].thirdparty_amt, 200.0)
359
360        self.app['configuration']['2004'].loss_idcard_fee = 1900.0
361        self.browser.open(self.payments_path + '/addop')
362        self.browser.getControl(name="form.p_category").value = ['loss_idcard']
363        self.browser.getControl("Create ticket").click()
364        ctrl = self.browser.getControl(name='val_id')
365        value = ctrl.options[6]
366        self.browser.getLink(value).click()
367        self.assertMatches('...Amount Authorized...', self.browser.contents)
368        self.assertTrue('<span>1900.0</span>' in self.browser.contents)
369        self.payment_url = self.browser.url
370        self.browser.getLink("CollegePAY", index=0).click()
371        self.assertEqual(self.student['payments'][value].amount_auth, 1900.0)
372        self.assertEqual(self.student['payments'][value].provider_amt, 9.0)
373        self.assertEqual(self.student['payments'][value].gateway_amt, 28.5)
374        self.assertEqual(self.student['payments'][value].thirdparty_amt, 6.0)
375
376
377    def test_interswitch_form_ticket_expired(self):
378        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
379        acc_payment = createObject('waeup.StudentOnlinePayment')
380        acc_payment.p_state = 'unpaid'
381        acc_payment.p_category = 'clearance'
382        acc_payment.p_id = 'xyz'
383        acc_payment.pay_item_id = '123'
384        acc_payment.amount_auth = 876.0
385        self.student['payments']['xyz'] = acc_payment
386        self.browser.open(self.payments_path + '/xyz')
387        self.browser.getLink("CollegePAY", index=0).click()
388        self.assertTrue('<input type="hidden" name="pay_item_id" value="000" />'
389                           in self.browser.contents)
390        self.assertMatches('...Total Amount Authorized:...',
391                           self.browser.contents)
392        self.assertEqual(self.student.current_mode, 'nd_ft')
393        self.assertTrue(
394            '<input type="hidden" name="amount" value="87600" />' in
395            self.browser.contents)
396        delta = timedelta(days=8)
397        acc_payment.creation_date -= delta
398        self.browser.open(self.payments_path + '/xyz')
399        self.browser.getLink("CollegePAY", index=0).click()
400        self.assertMatches(
401            '...This payment ticket is too old. Please create a new ticket...',
402            self.browser.contents)
403        delta = timedelta(days=2)
404        acc_payment.creation_date += delta
405        self.browser.open(self.payments_path + '/xyz')
406        self.browser.getLink("CollegePAY", index=0).click()
407        self.assertMatches('...Total Amount Authorized:...',
408                           self.browser.contents)
409
410    @external_test
411    def test_webservice(self):
412        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
413        IWorkflowState(self.student).setState('cleared')
414        # We add a fake maint. payment ticket to meet the condition
415        maint_payment = CustomStudentOnlinePayment()
416        self.student['payments']['any_key'] = maint_payment
417        maint_payment.p_category = 'hostel_maintenance'
418        maint_payment.p_state = 'paid'
419        maint_payment.p_session = 2004
420        # We create the school fee payment ticket
421        self.browser.open(self.payments_path + '/addop')
422        self.browser.getControl(name="form.p_category").value = ['schoolfee']
423        self.browser.getControl("Create ticket").click()
424        self.assertMatches('...ticket created...',
425                           self.browser.contents)
426        ctrl = self.browser.getControl(name='val_id')
427        self.value = ctrl.options[1]
428        self.browser.getLink(self.value).click()
429        self.payment_url = self.browser.url
430        # First we have open InterswitchPageStudent to set provider_amt
431        # and gateway_amt
432        self.browser.open(self.payment_url + '/goto_interswitch')
433        # Now we can call the webservice
434        self.browser.open(self.payment_url + '/request_webservice')
435        self.assertMatches('...Unsuccessful callback...',
436                          self.browser.contents)
437        # The payment is now in state failed ...
438        self.assertMatches('...<span>Failed</span>...',
439                          self.browser.contents)
440        # ... and the catalog has been updated
441        cat = getUtility(ICatalog, name='payments_catalog')
442        results = list(
443            cat.searchResults(p_state=('failed', 'failed')))
444        self.assertEqual(len(results), 1)
445        self.assertEqual(results[0].p_state, 'failed')
446
447        # Let's replace the p_id with a valid p_id of the Kwarapoly
448        # live system. This is definitely not an appropriate
449        # solution for testing, but we have no choice since
450        # Interswitch doesn't provide any interface
451        # for testing.
452        payment = self.student['payments'][self.value]
453        payment.p_id = 'p3543612043224'
454        self.browser.open(self.payment_url + '/request_webservice')
455        self.assertMatches('...Callback amount does not match...',
456                          self.browser.contents)
457        # The payment is now in state failed ...
458        self.assertMatches('...<span>Failed</span>...',
459                          self.browser.contents)
460        # Let's replace the amount autorized with the amount of the
461        # live system payment
462        payment.amount_auth = payment.r_amount_approved
463        self.browser.open(self.payment_url + '/request_webservice')
464        self.assertMatches('...Successful payment...',
465                          self.browser.contents)
466        # The payment is now in state paid ...
467        self.assertMatches('...<span>Paid</span>...',
468                          self.browser.contents)
469        # ... and the catalog has been updated
470        cat = getUtility(ICatalog, name='payments_catalog')
471        results = list(
472            cat.searchResults(p_state=('paid', 'paid')))
473        self.assertEqual(len(results), 1)
474        self.assertEqual(results[0].p_state, 'paid')
475        # Approval is logged in students.log ...
476        logfile = os.path.join(
477            self.app['datacenter'].storage, 'logs', 'students.log')
478        logcontent = open(logfile).read()
479        self.assertTrue(
480            'zope.mgr - '
481            'waeup.kwarapoly.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
482            'W1000000 - successful schoolfee payment: p3543612043224\n'
483            in logcontent)
484        # ... and in payments.log
485        logfile = os.path.join(
486            self.app['datacenter'].storage, 'logs', 'payments.log')
487        logcontent = open(logfile).read()
488        self.assertTrue(
489            '"zope.mgr",W1000000,p3543612043224,schoolfee,'
490            '52100.0,00,1200.0,300.0,1800.0,,,\n'
491            in logcontent)
492
493
494class InterswitchTestsApplicants(ApplicantsFullSetup):
495    """Tests for the Interswitch payment gateway.
496    """
497
498    layer = FunctionalLayer
499
500    def setUp(self):
501        super(InterswitchTestsApplicants, self).setUp()
502        configuration = SessionConfiguration()
503        configuration.academic_session = datetime.now().year - 2
504        self.app['configuration'].addSessionConfiguration(configuration)
505        self.configuration = configuration
506        # Create at least one Kwarapoly faculty
507        self.app['faculties']['CPGS'] = Faculty(code='CPGS')
508        self.app['faculties']['CPGS']['dep1'] = Department(code='dep1')
509        self.certificate2 = createObject('waeup.Certificate')
510        self.certificate2.code = u'CERT2'
511        self.certificate2.application_category = 'ndft'
512        self.certificate2.study_mode = 'nd_ft'
513        self.certificate2.start_level = 100
514        self.certificate2.end_level = 300
515        self.app['faculties']['CPGS']['dep1'].certificates.addCertificate(
516            self.certificate2)
517        self.applicantscontainer.application_category = 'ndft'
518        self.applicant.applicant_id = u'nd_anything'
519       
520        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
521        self.browser.open(self.manage_path)
522        #IWorkflowState(self.student).setState('started')
523        super(InterswitchTestsApplicants, self).fill_correct_values()
524        self.browser.getControl(name="form.course1").value = ['CERT2']
525        self.applicantscontainer.application_fee = 3333.0
526        self.browser.getControl(name="form.nationality").value = ['NG']
527        self.browser.getControl(name="transition").value = ['start']
528        self.browser.getControl("Save").click()
529        self.browser.getControl("Add online").click()
530        self.assertMatches('...ticket created...',
531                           self.browser.contents)
532        self.assertMatches('...Amount Authorized...',
533                           self.browser.contents)
534        self.assertTrue('<span>3333.0</span>' in self.browser.contents)
535        self.payment_url = self.browser.url
536
537
538    def test_interswitch_form(self):
539
540        # Manager can access InterswitchForm
541        self.browser.getLink("CollegePAY", index=0).click()
542        self.assertMatches('...Total Amount Authorized:...',
543                           self.browser.contents)
544        self.assertTrue(
545            '<input type="hidden" name="amount" value="333300" />'
546            in self.browser.contents)
547        self.assertTrue(
548            '<item_detail item_id="1" item_name="Application" '
549            'item_amt="253300" bank_id="10" acct_num="0106259811" />'
550            in self.browser.contents)
551
552        # Meanwhile hndft fee goes to same account
553        self.applicant.applicant_id = u'hnd_anything'
554        self.browser.open(self.manage_path)
555        ctrl = self.browser.getControl(name='val_id')
556        value = ctrl.options[0]
557        self.browser.getLink(value).click()
558        self.browser.getLink("CollegePAY", index=0).click()
559        self.assertTrue(
560            '<item_detail item_id="1" item_name="Application" '
561            'item_amt="253300" bank_id="10" acct_num="0106259811" />'
562            in self.browser.contents)
563        # Commission or bribe?
564        self.assertTrue(
565            '<item_detail item_id="2" item_name="Dalash" item_amt="20000" '
566            'bank_id="117" acct_num="1013196791" />'
567            in self.browser.contents)
568        self.assertTrue(
569            '<item_detail item_id="3" item_name="BT Education" '
570            'item_amt="30000" bank_id="117" acct_num="1010764827" />'
571            in self.browser.contents)
572
573        # prehndft fee goes to another account and has other split data
574        self.applicant.applicant_id = u'prehnd_anything'
575        self.browser.open(self.manage_path)
576        ctrl = self.browser.getControl(name='val_id')
577        value = ctrl.options[0]
578        self.browser.getLink(value).click()
579        self.browser.getLink("CollegePAY", index=0).click()
580        self.assertTrue(
581            '<item_detail item_id="1" item_name="Application" '
582            'item_amt="303300" bank_id="10" acct_num="0106259811" />'
583            in self.browser.contents)
584        self.assertFalse('Dalash' in self.browser.contents)
585        self.assertTrue(
586            '<item_detail item_id="2" item_name="BT Education" '
587            'item_amt="26250" bank_id="117" acct_num="1010764827" />'
588            in self.browser.contents)
589
590        # prejambites fee goes to another account
591        self.applicant.applicant_id = u'prejambites_anything'
592        self.browser.open(self.manage_path)
593        ctrl = self.browser.getControl(name='val_id')
594        value = ctrl.options[0]
595        self.browser.getLink(value).click()
596        self.browser.getLink("CollegePAY", index=0).click()
597        self.assertTrue(
598            '<item_detail item_id="1" item_name="Application" '
599            'item_amt="303300" bank_id="10" acct_num="0106259811" />'
600            in self.browser.contents)
601        self.assertFalse('Dalash' in self.browser.contents)
602        self.assertTrue(
603            '<item_detail item_id="2" item_name="BT Education" '
604            'item_amt="26250" bank_id="117" acct_num="1010764827" />'
605            in self.browser.contents)
606        self.assertTrue(
607            '<input type="hidden" name="pay_item_id" value="104" />'
608            in self.browser.contents)
609
610        # putme goes into the prejambites account
611        self.applicant.applicant_id = u'putme_anything'
612        self.browser.open(self.manage_path)
613        ctrl = self.browser.getControl(name='val_id')
614        value = ctrl.options[0]
615        self.browser.getLink(value).click()
616        self.browser.getLink("CollegePAY", index=0).click()
617        self.assertTrue(
618            '<item_detail item_id="1" item_name="Application" '
619            'item_amt="253300" bank_id="10" acct_num="0106259811" />'
620            in self.browser.contents)
621        self.assertTrue(
622            '<item_detail item_id="2" item_name="Dalash" item_amt="20000" '
623            'bank_id="117" acct_num="1013196791" />'
624            in self.browser.contents)
625        self.assertTrue(
626            '<item_detail item_id="3" item_name="BT Education" '
627            'item_amt="30000" bank_id="117" acct_num="1010764827" />'
628            in self.browser.contents)
629        self.assertTrue(
630            '<input type="hidden" name="pay_item_id" value="104" />'
631            in self.browser.contents)
632
633    def test_interswitch_form_ticket_expired(self):
634        # Manager can access InterswitchForm
635        self.browser.getLink("CollegePAY", index=0).click()
636        self.assertMatches('...Total Amount Authorized:...',
637                           self.browser.contents)
638        self.assertTrue(
639            '<input type="hidden" name="amount" value="333300" />'
640            in self.browser.contents)
641        self.assertTrue(
642            '<item_detail item_id="1" item_name="Application" '
643            'item_amt="253300" bank_id="10" acct_num="0106259811" />'
644            in self.browser.contents)
645        delta = timedelta(days=8)
646        self.applicant.values()[0].creation_date -= delta
647        self.browser.open(self.payment_url)
648        self.browser.getLink("CollegePAY", index=0).click()
649        self.assertMatches(
650            '...This payment ticket is too old. Please create a new ticket...',
651            self.browser.contents)
652
653    def prepare_special_container(self):
654        # Add special application container
655        container_name = u'special%s' % (datetime.now().year - 2)
656        applicantscontainer = ApplicantsContainer()
657        applicantscontainer.code = container_name
658        applicantscontainer.prefix = 'special'
659        applicantscontainer.year = datetime.now().year - 2
660        applicantscontainer.title = u'This is a special app container'
661        applicantscontainer.application_category = 'no'
662        applicantscontainer.mode = 'create'
663        delta = timedelta(days=10)
664        applicantscontainer.startdate = datetime.now(pytz.utc) - delta
665        applicantscontainer.enddate = datetime.now(pytz.utc) + delta
666        applicantscontainer.strict_deadline = True
667        self.app['applicants'][container_name] = applicantscontainer
668        # Add an applicant
669        applicant = createObject('waeup.Applicant')
670        # reg_number is the only field which has to be preset here
671        # because managers are allowed to edit this required field
672        applicant.reg_number = u'12345'
673        applicant.firstname = u'Vorname'
674        applicant.lastname = u'Nachname'
675        applicant.email = 'aa@aa.aa'
676        applicant.special_application = u'transcript_local'
677        applicant.applicant_id = u'special_anything'
678        self.app['applicants'][container_name].addApplicant(applicant)
679        self.special_applicant = applicant
680        self.configuration.transcript_local_fee = 5300.0
681        self.special_manage_path = 'http://localhost/app/applicants/%s/%s/%s' % (
682            container_name, applicant.application_number, 'manage')
683
684    def test_interswitch_form_special(self):
685        self.prepare_special_container()
686        self.browser.open(self.special_manage_path)
687        self.browser.getControl("Add online").click()
688        self.assertMatches('...ticket created...',
689                           self.browser.contents)
690        self.browser.getLink("CollegePAY", index=0).click()
691        self.assertTrue(
692            '<item_detail item_id="1" item_name="ND Transcript (local)" '
693            'item_amt="450000" bank_id="10" acct_num="0106259811" />'
694            in self.browser.contents)
695
696        self.special_applicant.special_application = u'log_book'
697        self.configuration.log_book_fee = 570.5
698        self.browser.open(self.special_manage_path)
699        self.browser.getControl("Add online").click()
700        self.assertMatches('...ticket created...',
701                           self.browser.contents)
702        self.browser.getLink("CollegePAY", index=0).click()
703        # The university gets 570.5 - (1.5% x 570.5) - 3 - 4.5
704        self.assertTrue(
705            '<item_detail item_id="1" item_name="Log Book" '
706            'item_amt="55444" bank_id="10" acct_num="0106259811" />'
707            in self.browser.contents)
708
709
710    @external_test
711    def test_webservice(self):
712        self.prepare_special_container()
713        IWorkflowState(self.special_applicant).setState('started')
714        self.browser.open(self.special_manage_path)
715        self.browser.getControl("Add online").click()
716        payment_url = self.browser.url
717        self.browser.open(payment_url + '/request_webservice')
718        self.assertMatches('...Unsuccessful callback...',
719                          self.browser.contents)
720        # The payment is now in state failed
721        self.assertMatches('...<span>Failed</span>...',
722                          self.browser.contents)
723        # Let's replace the p_id with a valid p_id of the Kwarapoly
724        # live system. This is definitely not an appropriate
725        # solution for testing, but we have no choice since
726        # Interswitch doesn't provide any interface
727        # for testing.
728        p_id = self.special_applicant.keys()[0]
729        payment = self.special_applicant[p_id]
730        payment.p_id = 'p3543612043224'
731        self.browser.open(payment_url + '/request_webservice')
732        self.assertMatches('...Callback amount does not match...',
733                          self.browser.contents)
734        payment.amount_auth = payment.r_amount_approved
735
736        self.browser.open(payment_url + '/request_webservice')
737        self.assertMatches('...Successful payment...',
738                          self.browser.contents)
739        # The payment is now in state paid ...
740        self.assertMatches('...<span>Paid</span>...',
741                          self.browser.contents)
742        # ... and the catalog has been updated
743        cat = getUtility(ICatalog, name='payments_catalog')
744        results = list(
745            cat.searchResults(p_state=('paid', 'paid')))
746        self.assertEqual(len(results), 1)
747        self.assertEqual(results[0].p_state, 'paid')
748        # Approval is logged in applicants.log ...
749        logfile = os.path.join(
750            self.app['datacenter'].storage, 'logs', 'applicants.log')
751        logcontent = open(logfile).read()
752        self.assertTrue(
753            'zope.mgr - waeup.kwarapoly.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageApplicant'
754            ' - special_anything - successful payment: p3543612043224\n'
755            in logcontent)
756        # ... and in payments.log
757        logfile = os.path.join(
758            self.app['datacenter'].storage, 'logs', 'payments.log')
759        logcontent = open(logfile).read()
760        self.assertTrue(
761            '"zope.mgr",special_anything,p3543612043224,transcript_local,52100.0,'
762            '00,0.0,0.0,0.0,,,\n'
763            in logcontent)
764        self.assertEqual(self.applicant.state, 'started')
765        # Special Payment applicant can add new payment
766        self.browser.open(self.edit_path)
767        self.assertTrue('Add online payment ticket' in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.