source: main/kofacustom.skeleton/trunk/src/kofacustom/skeleton/interswitch/tests.py @ 15959

Last change on this file since 15959 was 15756, checked in by Henrik Bettermann, 5 years ago

Adjust to changes in nigeria package.

File size: 11.3 KB
RevLine 
[15614]1## $Id: tests.py 14874 2017-10-24 08:25: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
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.skeleton.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 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
55        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
56        self.browser.open(self.payments_path)
57        IWorkflowState(self.student).setState('cleared')
58        self.student.nationality = u'NG'
59        self.browser.open(self.payments_path + '/addop')
60        self.browser.getControl(name="form.p_category").value = ['schoolfee']
61        self.browser.getControl("Create ticket").click()
62        self.assertMatches('...ticket created...',
63                           self.browser.contents)
64        ctrl = self.browser.getControl(name='val_id')
65        self.value = ctrl.options[0]
66        self.browser.getLink(self.value).click()
67        self.assertMatches('...Amount Authorized...',
68                           self.browser.contents)
69        self.assertTrue(
70            '<span>40000.0</span>' in self.browser.contents)
71        self.payment_url = self.browser.url
[15756]72        self.browser.getLink("Pay via Interswitch", index=0).click()
[15614]73        self.assertTrue('<input type="hidden" name="pay_item_id" value="0000" />' in
74                           self.browser.contents)
75        self.assertEqual(self.student.current_mode, 'ug_ft')
76        self.assertTrue(
[15756]77            '<input type="hidden" name="amount" value="4015000" />' in
[15614]78            self.browser.contents)
79        self.assertTrue(
[15756]80            'item_name="School Fee" item_amt="4000000" bank_id="00" acct_num="00000000"' in
[15614]81            self.browser.contents)
82
83    def test_interswitch_form_ticket_expired(self):
84        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
85        acc_payment = createObject('waeup.StudentOnlinePayment')
86        acc_payment.p_state = 'unpaid'
87        acc_payment.p_category = 'clearance'
88        acc_payment.p_id = 'xyz'
89        acc_payment.pay_item_id = '123'
[15756]90        acc_payment.amount_auth = 1000.0
[15614]91        self.student['payments']['xyz'] = acc_payment
92        self.browser.open(self.payments_path + '/xyz')
[15756]93        self.browser.getLink("Pay via Interswitch", index=0).click()
[15614]94        self.assertMatches('...<input type="hidden" name="pay_item_id" value="0000" />...',
95                           self.browser.contents)
96        self.assertMatches('...Total Amount Authorized:...',
97                           self.browser.contents)
98        self.assertEqual(self.student.current_mode, 'ug_ft')
99        self.assertMatches(
[15756]100            '...<input type="hidden" name="amount" value="115000" />...',
[15614]101            self.browser.contents)
102        delta = timedelta(days=8)
103        acc_payment.creation_date -= delta
104        self.browser.open(self.payments_path + '/xyz')
[15756]105        self.browser.getLink("Pay via Interswitch", index=0).click()
[15614]106        self.assertMatches(
107            '...This payment ticket is too old. Please create a new ticket...',
108            self.browser.contents)
109        delta = timedelta(days=2)
110        acc_payment.creation_date += delta
111        self.browser.open(self.payments_path + '/xyz')
[15756]112        self.browser.getLink("Pay via Interswitch", index=0).click()
[15614]113        self.assertMatches('...Total Amount Authorized:...',
114                           self.browser.contents)
115
116    @external_test
117    def test_webservice(self):
118        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
119        self.browser.open(self.payments_path)
120        IWorkflowState(self.student).setState('cleared')
121        self.student.nationality = u'NG'
122        self.browser.open(self.payments_path + '/addop')
123        self.browser.getControl(name="form.p_category").value = ['schoolfee']
124        self.browser.getControl("Create ticket").click()
125        self.assertMatches('...ticket created...',
126                           self.browser.contents)
127        ctrl = self.browser.getControl(name='val_id')
128        self.value = ctrl.options[0]
129        self.browser.getLink(self.value).click()
130        self.payment_url = self.browser.url
131        # First we have open InterswitchPageStudent to set provider_amt
132        # and gateway_amt
133        self.browser.open(self.payment_url + '/goto_interswitch')
134        # Now we can call the webservice
135        self.browser.open(self.payment_url + '/request_webservice')
136        self.assertMatches('...Unsuccessful callback...',
137                          self.browser.contents)
138        # The payment is now in state failed ...
139        self.assertMatches('...<span>Failed</span>...',
140                          self.browser.contents)
141        # ... and the catalog has been updated
142        cat = getUtility(ICatalog, name='payments_catalog')
143        results = list(
144            cat.searchResults(p_state=('failed', 'failed')))
145        self.assertEqual(len(results), 1)
146        self.assertEqual(results[0].p_state, 'failed')
147
148        # Let's replace the p_id with a valid p_id of the Uniben
149        # live system. This is definitely not an appropriate
150        # solution for testing, but we have no choice since
151        # Interswitch doesn't provide any interface
152        # for testing.
153        payment = self.student['payments'][self.value]
154        payment.p_id = 'p3547789850240'
155        self.browser.open(self.payment_url + '/request_webservice')
156        self.assertMatches('...Callback amount does not match...',
157                          self.browser.contents)
158        # The payment is now in state failed ...
159        self.assertMatches('...<span>Failed</span>...',
160                          self.browser.contents)
161        # Let's replace the amount autorized with the amount of the
162        # live system payment
163        payment.amount_auth = payment.r_amount_approved
164        self.browser.open(self.payment_url + '/request_webservice')
165        self.assertMatches('...Successful payment...',
166                          self.browser.contents)
167        # The payment is now in state paid ...
168        self.assertMatches('...<span>Paid</span>...',
169                          self.browser.contents)
170        # ... and the catalog has been updated
171        cat = getUtility(ICatalog, name='payments_catalog')
172        results = list(
173            cat.searchResults(p_state=('paid', 'paid')))
174        self.assertEqual(len(results), 1)
175        self.assertEqual(results[0].p_state, 'paid')
176        # Approval is logged in students.log ...
177        logfile = os.path.join(
178            self.app['datacenter'].storage, 'logs', 'students.log')
179        logcontent = open(logfile).read()
180        self.assertTrue(
181            'zope.mgr - '
182            'kofacustom.skeleton.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
183            'X1000000 - successful schoolfee payment: p3547789850240\n'
184            in logcontent)
185        # ... and in payments.log
186        logfile = os.path.join(
187            self.app['datacenter'].storage, 'logs', 'payments.log')
188        logcontent = open(logfile).read()
189        self.assertTrue(
190            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
191            '12000.0,00,0.0,150.0,0.0,,,\n'
192            in logcontent)
193
194
195class InterswitchTestsApplicants(ApplicantsFullSetup):
196    """Tests for the Interswitch payment gateway.
197    """
198
199    layer = FunctionalLayer
200
201    def setUp(self):
202        super(InterswitchTestsApplicants, self).setUp()
203        configuration = SessionConfiguration()
204        configuration.academic_session = datetime.now().year - 2
205        self.app['configuration'].addSessionConfiguration(configuration)
206        self.configuration = configuration
207        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
208        self.browser.open(self.manage_path)
209        #IWorkflowState(self.student).setState('started')
210        super(InterswitchTestsApplicants, self).fill_correct_values()
211        self.applicantscontainer.application_fee = 1000.0
212        self.browser.getControl(name="form.nationality").value = ['NG']
213        self.browser.getControl(name="transition").value = ['start']
214        self.browser.getControl("Save").click()
215        self.browser.getControl("Add online").click()
216        self.assertMatches('...ticket created...',
217                           self.browser.contents)
218        #ctrl = self.browser.getControl(name='val_id')
219        #value = ctrl.options[0]
220        #self.browser.getLink(value).click()
221        self.assertMatches('...Amount Authorized...',
222                           self.browser.contents)
223        self.assertMatches(
224            '...<span>1000.0</span>...',
225            self.browser.contents)
226        self.payment_url = self.browser.url
227
228
229    def test_interswitch_form(self):
230        # Manager can access InterswitchForm
[15756]231        self.browser.getLink("Pay via Interswitch", index=0).click()
[15614]232        self.assertMatches('...Total Amount Authorized:...',
233                           self.browser.contents)
234        self.assertMatches(
235            '...<input type="hidden" name="amount" value="100000" />...',
236            self.browser.contents)
237        delta = timedelta(days=8)
238        self.applicant.values()[0].creation_date -= delta
239        self.browser.open(self.payment_url)
[15756]240        self.browser.getLink("Pay via Interswitch", index=0).click()
[15614]241        self.assertMatches(
242            '...This payment ticket is too old. Please create a new ticket...',
243            self.browser.contents)
244
245    @external_test
246    def test_webservice(self):
247
248        self.browser.open(self.payment_url + '/request_webservice')
249        self.assertMatches('...Unsuccessful callback...',
250                          self.browser.contents)
251        # The payment is now in state failed
252        self.assertMatches('...<span>Failed</span>...',
253                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.