source: main/kofacustom.wdu/trunk/src/kofacustom/wdu/interswitch/tests.py @ 14292

Last change on this file since 14292 was 13593, checked in by Henrik Bettermann, 9 years ago

Adjust tests.

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