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

Last change on this file since 12479 was 12479, checked in by Henrik Bettermann, 10 years ago

Forward hash value to Interswitch payment gateway.

  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1## $Id: tests.py 12479 2015-01-16 05:52:04Z 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.assertMatches(
70            '...<span>40000.0</span>...',
71            self.browser.contents)
72        self.payment_url = self.browser.url
73        self.browser.getLink("CollegePAY", index=0).click()
74        self.assertMatches('...<input type="hidden" name="pay_item_id" value="0000" />...',
75                           self.browser.contents)
76        self.assertMatches('...Total Amount Authorized:...',
77                           self.browser.contents)
78        self.assertEqual(self.student.current_mode, 'ug_ft')
79        self.assertMatches(
80            '...<input type="hidden" name="amount" value="4000000" />...',
81            self.browser.contents)
82        self.assertMatches(
83            '...item_name="School Fee" item_amt="4000000" bank_id="00" acct_num="00000000"...',
84            self.browser.contents)
85
86    @external_test
87    def test_webservice(self):
88        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
89        self.browser.open(self.payments_path)
90        IWorkflowState(self.student).setState('cleared')
91        self.student.nationality = u'NG'
92        self.browser.open(self.payments_path + '/addop')
93        self.browser.getControl(name="form.p_category").value = ['schoolfee']
94        self.browser.getControl("Create ticket").click()
95        self.assertMatches('...ticket created...',
96                           self.browser.contents)
97        ctrl = self.browser.getControl(name='val_id')
98        self.value = ctrl.options[0]
99        self.browser.getLink(self.value).click()
100        self.payment_url = self.browser.url
101        # First we have open InterswitchPageStudent to set provider_amt
102        # and gateway_amt
103        self.browser.open(self.payment_url + '/goto_interswitch')
104        # Now we can call the webservice
105        self.browser.open(self.payment_url + '/request_webservice')
106        self.assertMatches('...Unsuccessful callback...',
107                          self.browser.contents)
108        # The payment is now in state failed ...
109        self.assertMatches('...<span>Failed</span>...',
110                          self.browser.contents)
111        # ... and the catalog has been updated
112        cat = getUtility(ICatalog, name='payments_catalog')
113        results = list(
114            cat.searchResults(p_state=('failed', 'failed')))
115        self.assertEqual(len(results), 1)
116        self.assertEqual(results[0].p_state, 'failed')
117
118        # Let's replace the p_id with a valid p_id of the Uniben
119        # live system. This is definitely not an appropriate
120        # solution for testing, but we have no choice since
121        # Interswitch doesn't provide any interface
122        # for testing.
123        payment = self.student['payments'][self.value]
124        payment.p_id = 'p3547789850240'
125        self.browser.open(self.payment_url + '/request_webservice')
126        self.assertMatches('...Callback amount does not match...',
127                          self.browser.contents)
128        # The payment is now in state failed ...
129        self.assertMatches('...<span>Failed</span>...',
130                          self.browser.contents)
131        # Let's replace the amount autorized with the amount of the
132        # live system payment
133        payment.amount_auth = payment.r_amount_approved
134        self.browser.open(self.payment_url + '/request_webservice')
135        self.assertMatches('...Successful payment...',
136                          self.browser.contents)
137        # The payment is now in state paid ...
138        self.assertMatches('...<span>Paid</span>...',
139                          self.browser.contents)
140        # ... and the catalog has been updated
141        cat = getUtility(ICatalog, name='payments_catalog')
142        results = list(
143            cat.searchResults(p_state=('paid', 'paid')))
144        self.assertEqual(len(results), 1)
145        self.assertEqual(results[0].p_state, 'paid')
146        # Approval is logged in students.log ...
147        logfile = os.path.join(
148            self.app['datacenter'].storage, 'logs', 'students.log')
149        logcontent = open(logfile).read()
150        self.assertTrue(
151            'zope.mgr - '
152            'kofacustom.skeleton.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
153            'X1000000 - successful schoolfee payment: p3547789850240\n'
154            in logcontent)
155        # ... and in payments.log
156        logfile = os.path.join(
157            self.app['datacenter'].storage, 'logs', 'payments.log')
158        logcontent = open(logfile).read()
159        self.assertTrue(
160            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
161            '12000.0,00,0.0,150.0,0.0,,,\n'
162            in logcontent)
163
164
165class InterswitchTestsApplicants(ApplicantsFullSetup):
166    """Tests for the Interswitch payment gateway.
167    """
168
169    layer = FunctionalLayer
170
171    def setUp(self):
172        super(InterswitchTestsApplicants, self).setUp()
173        configuration = SessionConfiguration()
174        configuration.academic_session = datetime.now().year - 2
175        self.app['configuration'].addSessionConfiguration(configuration)
176        self.configuration = configuration
177        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
178        self.browser.open(self.manage_path)
179        #IWorkflowState(self.student).setState('started')
180        super(InterswitchTestsApplicants, self).fill_correct_values()
181        self.applicantscontainer.application_fee = 1000.0
182        self.browser.getControl(name="form.nationality").value = ['NG']
183        self.browser.getControl(name="transition").value = ['start']
184        self.browser.getControl("Save").click()
185        self.browser.getControl("Add online").click()
186        self.assertMatches('...ticket created...',
187                           self.browser.contents)
188        #ctrl = self.browser.getControl(name='val_id')
189        #value = ctrl.options[0]
190        #self.browser.getLink(value).click()
191        self.assertMatches('...Amount Authorized...',
192                           self.browser.contents)
193        self.assertMatches(
194            '...<span>1000.0</span>...',
195            self.browser.contents)
196        self.payment_url = self.browser.url
197
198
199    def test_interswitch_form(self):
200
201        # Manager can access InterswitchForm
202        self.browser.getLink("CollegePAY", index=0).click()
203        self.assertMatches('...Total Amount Authorized:...',
204                           self.browser.contents)
205        self.assertMatches(
206            '...<input type="hidden" name="amount" value="100000" />...',
207            self.browser.contents)
208
209    @external_test
210    def test_webservice(self):
211
212        self.browser.open(self.payment_url + '/request_webservice')
213        self.assertMatches('...Unsuccessful callback...',
214                          self.browser.contents)
215        # The payment is now in state failed
216        self.assertMatches('...<span>Failed</span>...',
217                          self.browser.contents)
Note: See TracBrowser for help on using the repository browser.