source: main/waeup.futminna/trunk/src/waeup/futminna/interswitch/tests.py @ 9791

Last change on this file since 9791 was 9791, checked in by Henrik Bettermann, 12 years ago

p_id is from the FUTMinna portal.

  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1## $Id: tests.py 9791 2012-12-07 16:46:16Z 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 zope.component import getUtility
20from zope.catalog.interfaces import ICatalog
21from hurry.workflow.interfaces import IWorkflowState
22from waeup.kofa.students.tests.test_browser import StudentsFullSetup
23from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
24from waeup.kofa.configuration import SessionConfiguration
25from waeup.futminna.testing import FunctionalLayer
26
27# Also run tests that send requests to external servers?
28#   If you enable this, please make sure the external services
29#   do exist really and are not bothered by being spammed by a test programme.
30EXTERNAL_TESTS = True
31
32def external_test(func):
33    if not EXTERNAL_TESTS:
34        myself = __file__
35        if myself.endswith('.pyc'):
36            myself = myself[:-2]
37        print "WARNING: external tests are skipped!"
38        print "WARNING: edit %s to enable them." % myself
39        return
40    return func
41
42
43class InterswitchTestsStudents(StudentsFullSetup):
44    """Tests for the Interswitch payment gateway.
45    """
46
47    layer = FunctionalLayer
48
49    def setUp(self):
50        super(InterswitchTestsStudents, self).setUp()
51        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
52        self.browser.open(self.payments_path)
53        IWorkflowState(self.student).setState('cleared')
54        self.browser.open(self.payments_path + '/addop')
55        self.browser.getControl(name="form.p_category").value = ['schoolfee']
56        self.browser.getControl("Create ticket").click()
57        self.assertMatches('...Payment ticket created...',
58                           self.browser.contents)
59        ctrl = self.browser.getControl(name='val_id')
60        self.value = ctrl.options[0]
61        self.browser.getLink(self.value).click()
62        self.assertMatches('...Amount Authorized...',
63                           self.browser.contents)
64        self.assertMatches(
65            '...<span>131500.0</span>...',
66            self.browser.contents)
67        self.payment_url = self.browser.url
68
69    def test_interswitch_form(self):
70        # Manager can access InterswitchForm
71        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
72        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
73        self.browser.getLink("CollegePAY", index=0).click()
74        # Split amounts have been set.
75        self.assertEqual(self.student['payments'][self.value].provider_amt, 1500.0)
76        self.assertEqual(self.student['payments'][self.value].gateway_amt, 300.0)
77        # The total amount to be processed by Interswitch
78        # has been reduced by the Interswitch fee of 150 Nairas
79        self.assertMatches('...Total Amount Authorized:...',
80                           self.browser.contents)
81        self.assertMatches(
82            '...<input type="hidden" name="amount" value="13150000.0" />...',
83            self.browser.contents)
84        self.assertMatches(
85            '...item_name="School Fee" item_amt="12970000" bank_id="31" acct_num="0021030851"...',
86            self.browser.contents)
87        self.assertMatches(
88            '...item_name="BT Education" item_amt="150000" bank_id="31" acct_num="0026781725"...',
89            self.browser.contents)
90
91        # Create clearance fee ticket
92        self.browser.open(self.payments_path + '/addop')
93        self.browser.getControl(name="form.p_category").value = ['clearance']
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        value = ctrl.options[1]
99        self.browser.getLink(value).click()
100        self.assertMatches('...Amount Authorized...',
101                           self.browser.contents)
102        self.assertMatches(
103            '...<span>3456.0</span>...',
104            self.browser.contents)
105        # Manager can access InterswitchForm
106        self.browser.getLink("CollegePAY", index=0).click()
107        # Split amounts have been set.
108        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
109        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
110        # The total amount to be processed by Interswitch
111        # has been reduced by the Interswitch fee of 150 Nairas
112        self.assertMatches('...Total Amount Authorized:...',
113                           self.browser.contents)
114        self.assertMatches(
115            '...<input type="hidden" name="amount" value="345600.0" />...',
116            self.browser.contents)
117        self.assertMatches(
118            '...item_name="Acceptance Fee" item_amt="315600" bank_id="120" acct_num="1750005063"...',
119            self.browser.contents)
120        # BT does nor charge a fee for clearance
121        self.assertFalse("BT Education" in self.browser.contents)
122
123        # Create hostel maintenance ticket
124        self.browser.open(self.payments_path + '/addop')
125        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
126        self.browser.getControl("Create ticket").click()
127        self.assertMatches('...You have not yet booked accommodation...',
128                           self.browser.contents)
129        # Students have to book bed first
130        self.browser.open(self.acco_path)
131        IWorkflowState(self.student).setState('admitted')
132        self.browser.getLink("Book accommodation").click()
133        self.assertFalse('Activation Code:' in self.browser.contents)
134        self.browser.getControl("Create bed ticket").click()
135        # Bed is randomly selected but, since there is only
136        # one bed for this student, we know that ...
137        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
138                           self.browser.contents)
139        self.browser.open(self.payments_path + '/addop')
140        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
141        self.browser.getControl("Create ticket").click()
142        self.assertMatches('...ticket created...',
143                           self.browser.contents)
144        ctrl = self.browser.getControl(name='val_id')
145        value = ctrl.options[2]
146        self.browser.getLink(value).click()
147        self.assertMatches('...Amount Authorized...',
148                           self.browser.contents)
149        self.assertMatches(
150            '...<span>10000.0</span>...',
151            self.browser.contents)
152        # Manager can access InterswitchForm
153        self.browser.getLink("CollegePAY", index=0).click()
154        # Split amounts have been set.
155        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
156        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
157        # The total amount to be processed by Interswitch
158        # has been reduced by the Interswitch fee of 150 Nairas
159        self.assertMatches('...Total Amount Authorized:...',
160                           self.browser.contents)
161        self.assertMatches(
162            '...<input type="hidden" name="amount" value="1000000.0" />...',
163            self.browser.contents)
164        self.assertMatches(
165            '...item_name="Hostel Maintenance Fee" item_amt="970000" bank_id="8" acct_num="2018856637"...',
166            self.browser.contents)
167        # BT does nor charge a fee for clearance
168        self.assertFalse("BT Education" in self.browser.contents)
169
170
171    @external_test
172    def test_webservice(self):
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 FUTMinna
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 = 'p3522296094055'
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 - '
224            'waeup.futminna.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - '
225            'M1000000 - successful schoolfee payment: p3522296094055\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",M1000000,p3522296094055,schoolfee,'
233            '20000.0,00,1500.0,300.0,0.0,,,\n'
234            in logcontent)
Note: See TracBrowser for help on using the repository browser.