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

Last change on this file since 15020 was 15020, checked in by Henrik Bettermann, 6 years ago

Adjust test.

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