source: main/kofacustom.edopoly/trunk/src/kofacustom/edopoly/interswitch/tests.py @ 17808

Last change on this file since 17808 was 17703, checked in by Henrik Bettermann, 9 months ago

Ticket #60

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