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

Last change on this file since 16982 was 16326, checked in by Henrik Bettermann, 4 years ago

Adjust tests.

  • Property svn:keywords set to Id
File size: 13.1 KB
RevLine 
[10765]1## $Id: tests.py 16326 2020-11-21 08:35:09Z 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(
[15301]64            'Pay ICT 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
85
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()
[10765]89        self.assertMatches('...ticket created...',
90                           self.browser.contents)
91        self.assertMatches('...Amount Authorized...',
92                           self.browser.contents)
[14874]93        self.assertTrue(
94            '<span>40000.0</span>' in self.browser.contents)
[10765]95        self.payment_url = self.browser.url
[15718]96        self.browser.getLink("Pay via Interswitch", index=0).click()
[15015]97        self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in
[10765]98                           self.browser.contents)
99        self.assertEqual(self.student.current_mode, 'ug_ft')
[14874]100        self.assertTrue(
101            '<input type="hidden" name="amount" value="4000000" />' in
[10765]102            self.browser.contents)
[14874]103        self.assertTrue(
[15105]104            'item_name="Tuition/Exams Fee" item_amt="3975000" bank_id="121" acct_num="0068241848"' in
[10765]105            self.browser.contents)
[15015]106        # Clearance Fee
107        self.browser.open(self.payments_path + '/addop')
108        self.browser.getControl(name="form.p_category").value = ['clearance']
109        self.browser.getControl("Create ticket").click()
110        self.assertMatches('...ticket created...',
111                           self.browser.contents)
112        self.assertTrue(
113            '<span>3456.0</span>' in self.browser.contents)
114        self.payment_url = self.browser.url
[15718]115        self.browser.getLink("Pay via Interswitch", index=0).click()
[15015]116        self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in
117                           self.browser.contents)
118        self.assertTrue(
119            '<input type="hidden" name="amount" value="345600" />' in
120            self.browser.contents)
121        self.assertTrue(
[15030]122            'item_name="Acceptance Fee" item_amt="170600" bank_id="121" acct_num="0068241848"' in
[15015]123            self.browser.contents)
[10765]124
[12979]125    def test_interswitch_form_ticket_expired(self):
126        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
127        acc_payment = createObject('waeup.StudentOnlinePayment')
128        acc_payment.p_state = 'unpaid'
129        acc_payment.p_category = 'clearance'
130        acc_payment.p_id = 'xyz'
131        acc_payment.pay_item_id = '123'
132        acc_payment.amount_auth = 876.0
133        self.student['payments']['xyz'] = acc_payment
134        self.browser.open(self.payments_path + '/xyz')
[15718]135        self.browser.getLink("Pay via Interswitch", index=0).click()
[15015]136        self.assertMatches('...<input type="hidden" name="pay_item_id" value="101" />...',
[12979]137                           self.browser.contents)
138        self.assertMatches('...Total Amount Authorized:...',
139                           self.browser.contents)
140        self.assertEqual(self.student.current_mode, 'ug_ft')
141        self.assertMatches(
142            '...<input type="hidden" name="amount" value="87600" />...',
143            self.browser.contents)
[13020]144        delta = timedelta(days=8)
[12979]145        acc_payment.creation_date -= delta
146        self.browser.open(self.payments_path + '/xyz')
[15718]147        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]148        self.assertMatches(
149            '...This payment ticket is too old. Please create a new ticket...',
150            self.browser.contents)
[13020]151        delta = timedelta(days=2)
[12979]152        acc_payment.creation_date += delta
153        self.browser.open(self.payments_path + '/xyz')
[15718]154        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]155        self.assertMatches('...Total Amount Authorized:...',
156                           self.browser.contents)
157
[10765]158    @external_test
159    def test_webservice(self):
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']
166        self.browser.getControl("Create ticket").click()
167        self.assertMatches('...ticket created...',
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.payment_url = self.browser.url
173        # First we have open InterswitchPageStudent to set provider_amt
174        # and gateway_amt
175        self.browser.open(self.payment_url + '/goto_interswitch')
176        # Now we can call the webservice
177        self.browser.open(self.payment_url + '/request_webservice')
178        self.assertMatches('...Unsuccessful callback...',
179                          self.browser.contents)
180        # The payment is now in state failed ...
181        self.assertMatches('...<span>Failed</span>...',
182                          self.browser.contents)
183        # ... and the catalog has been updated
184        cat = getUtility(ICatalog, name='payments_catalog')
185        results = list(
186            cat.searchResults(p_state=('failed', 'failed')))
187        self.assertEqual(len(results), 1)
188        self.assertEqual(results[0].p_state, 'failed')
189
190        # Let's replace the p_id with a valid p_id of the Uniben
191        # live system. This is definitely not an appropriate
192        # solution for testing, but we have no choice since
193        # Interswitch doesn't provide any interface
194        # for testing.
195        payment = self.student['payments'][self.value]
196        payment.p_id = 'p3547789850240'
197        self.browser.open(self.payment_url + '/request_webservice')
198        self.assertMatches('...Callback amount does not match...',
199                          self.browser.contents)
200        # The payment is now in state failed ...
201        self.assertMatches('...<span>Failed</span>...',
202                          self.browser.contents)
203        # Let's replace the amount autorized with the amount of the
204        # live system payment
205        payment.amount_auth = payment.r_amount_approved
206        self.browser.open(self.payment_url + '/request_webservice')
207        self.assertMatches('...Successful payment...',
208                          self.browser.contents)
209        # The payment is now in state paid ...
210        self.assertMatches('...<span>Paid</span>...',
211                          self.browser.contents)
212        # ... and the catalog has been updated
213        cat = getUtility(ICatalog, name='payments_catalog')
214        results = list(
215            cat.searchResults(p_state=('paid', 'paid')))
216        self.assertEqual(len(results), 1)
217        self.assertEqual(results[0].p_state, 'paid')
218        # Approval is logged in students.log ...
219        logfile = os.path.join(
220            self.app['datacenter'].storage, 'logs', 'students.log')
221        logcontent = open(logfile).read()
222        self.assertTrue(
223            'zope.mgr - '
[15000]224            'kofacustom.edopoly.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
[10765]225            'X1000000 - successful schoolfee payment: p3547789850240\n'
226            in logcontent)
227        # ... and in payments.log
228        logfile = os.path.join(
229            self.app['datacenter'].storage, 'logs', 'payments.log')
230        logcontent = open(logfile).read()
231        self.assertTrue(
232            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
233            '12000.0,00,0.0,150.0,0.0,,,\n'
234            in logcontent)
235
236
237class InterswitchTestsApplicants(ApplicantsFullSetup):
238    """Tests for the Interswitch payment gateway.
239    """
240
241    layer = FunctionalLayer
242
243    def setUp(self):
244        super(InterswitchTestsApplicants, self).setUp()
[11271]245        configuration = SessionConfiguration()
246        configuration.academic_session = datetime.now().year - 2
247        self.app['configuration'].addSessionConfiguration(configuration)
248        self.configuration = configuration
[10765]249        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
250        self.browser.open(self.manage_path)
251        #IWorkflowState(self.student).setState('started')
252        super(InterswitchTestsApplicants, self).fill_correct_values()
253        self.applicantscontainer.application_fee = 1000.0
254        self.browser.getControl(name="form.nationality").value = ['NG']
255        self.browser.getControl(name="transition").value = ['start']
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
[15718]273        self.browser.getLink("Pay via Interswitch", index=0).click()
[10765]274        self.assertMatches('...Total Amount Authorized:...',
275                           self.browser.contents)
276        self.assertMatches(
[11813]277            '...<input type="hidden" name="amount" value="100000" />...',
[10765]278            self.browser.contents)
[13020]279        delta = timedelta(days=8)
[12979]280        self.applicant.values()[0].creation_date -= delta
281        self.browser.open(self.payment_url)
[15718]282        self.browser.getLink("Pay via Interswitch", index=0).click()
[12979]283        self.assertMatches(
284            '...This payment ticket is too old. Please create a new ticket...',
285            self.browser.contents)
[10765]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.