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

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

Fix tests.

  • Property svn:keywords set to Id
File size: 13.8 KB
Line 
1## $Id: tests.py 10369 2013-06-23 20:15:45Z 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, createObject
20from zope.catalog.interfaces import ICatalog
21from hurry.workflow.interfaces import IWorkflowState
22from waeup.kofa.university.faculty import Faculty
23from waeup.kofa.university.department import Department
24from waeup.kofa.students.tests.test_browser import StudentsFullSetup
25from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
26from waeup.kofa.configuration import SessionConfiguration
27from waeup.futminna.testing import FunctionalLayer
28
29# Also run tests that send requests to external servers?
30#   If you enable this, please make sure the external services
31#   do exist really and are not bothered by being spammed by a test programme.
32EXTERNAL_TESTS = False
33
34def external_test(func):
35    if not EXTERNAL_TESTS:
36        myself = __file__
37        if myself.endswith('.pyc'):
38            myself = myself[:-2]
39        print "WARNING: external tests are skipped!"
40        print "WARNING: edit %s to enable them." % myself
41        return
42    return func
43
44
45class InterswitchTestsStudents(StudentsFullSetup):
46    """Tests for the Interswitch payment gateway.
47    """
48
49    layer = FunctionalLayer
50
51    def setUp(self):
52        super(InterswitchTestsStudents, self).setUp()
53        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
54        self.browser.open(self.payments_path)
55        IWorkflowState(self.student).setState('cleared')
56        self.browser.open(self.payments_path + '/addop')
57        self.browser.getControl(name="form.p_category").value = ['schoolfee']
58        self.browser.getControl("Create ticket").click()
59        self.assertMatches('...Payment ticket created...',
60                           self.browser.contents)
61        ctrl = self.browser.getControl(name='val_id')
62        self.value = ctrl.options[0]
63        self.browser.getLink(self.value).click()
64        self.assertMatches('...Amount Authorized...',
65                           self.browser.contents)
66        self.assertMatches(
67            '...<span>131500.0</span>...',
68            self.browser.contents)
69        self.payment_url = self.browser.url
70
71    def test_interswitch_form(self):
72        # Manager can access InterswitchForm
73        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
74        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
75        self.browser.getLink("CollegePAY", index=0).click()
76        # Split amounts have been set.
77        self.assertEqual(self.student['payments'][self.value].provider_amt, 1500.0)
78        self.assertEqual(self.student['payments'][self.value].gateway_amt, 300.0)
79        # The total amount to be processed by Interswitch
80        # has been reduced by the Interswitch fee of 150 Nairas
81        self.assertMatches('...Total Amount Authorized:...',
82                           self.browser.contents)
83        self.assertMatches(
84            '...<input type="hidden" name="amount" value="13150000.0" />...',
85            self.browser.contents)
86        self.assertMatches(
87            '...item_name="School Fee" item_amt="12970000" bank_id="31" acct_num="0021030851"...',
88            self.browser.contents)
89        self.assertMatches(
90            '...item_name="BT Education" item_amt="150000" bank_id="117" acct_num="1010764827"...',
91            self.browser.contents)
92
93        # Create clearance fee ticket
94        self.browser.open(self.payments_path + '/addop')
95        self.browser.getControl(name="form.p_category").value = ['clearance']
96        self.browser.getControl("Create ticket").click()
97        self.assertMatches('...ticket created...',
98                           self.browser.contents)
99        ctrl = self.browser.getControl(name='val_id')
100        value = ctrl.options[1]
101        self.browser.getLink(value).click()
102        self.assertMatches('...Amount Authorized...',
103                           self.browser.contents)
104        self.assertMatches(
105            '...<span>3456.0</span>...',
106            self.browser.contents)
107        # Manager can access InterswitchForm
108        self.browser.getLink("CollegePAY", index=0).click()
109        # Split amounts have been set.
110        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
111        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
112        # The total amount to be processed by Interswitch
113        # has been reduced by the Interswitch fee of 150 Nairas
114        self.assertMatches('...Total Amount Authorized:...',
115                           self.browser.contents)
116        self.assertMatches(
117            '...<input type="hidden" name="amount" value="345600.0" />...',
118            self.browser.contents)
119        self.assertMatches(
120            '...item_name="Acceptance Fee" item_amt="315600" bank_id="120" acct_num="1750005063"...',
121            self.browser.contents)
122        # BT does nor charge a fee for clearance
123        self.assertFalse("BT Education" in self.browser.contents)
124
125        # Create hostel maintenance ticket
126        self.browser.open(self.payments_path + '/addop')
127        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
128        self.browser.getControl("Create ticket").click()
129        self.assertMatches('...You have not yet booked accommodation...',
130                           self.browser.contents)
131        # Students have to book bed first
132        self.browser.open(self.acco_path)
133        IWorkflowState(self.student).setState('admitted')
134        self.browser.getLink("Book accommodation").click()
135        self.assertFalse('Activation Code:' in self.browser.contents)
136        self.browser.getControl("Create bed ticket").click()
137        # Bed is randomly selected but, since there is only
138        # one bed for this student, we know that ...
139        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
140                           self.browser.contents)
141        self.browser.open(self.payments_path + '/addop')
142        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
143        self.browser.getControl("Create ticket").click()
144        self.assertMatches('...ticket created...',
145                           self.browser.contents)
146        ctrl = self.browser.getControl(name='val_id')
147        value = ctrl.options[2]
148        self.browser.getLink(value).click()
149        self.assertMatches('...Amount Authorized...',
150                           self.browser.contents)
151        self.assertMatches(
152            '...<span>10000.0</span>...',
153            self.browser.contents)
154        # Manager can access InterswitchForm
155        self.browser.getLink("CollegePAY", index=0).click()
156        # Split amounts have been set.
157        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
158        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
159        # The total amount to be processed by Interswitch
160        # has been reduced by the Interswitch fee of 150 Nairas
161        self.assertMatches('...Total Amount Authorized:...',
162                           self.browser.contents)
163        self.assertMatches(
164            '...<input type="hidden" name="amount" value="1000000.0" />...',
165            self.browser.contents)
166        self.assertMatches(
167            '...item_name="Hostel Maintenance Fee" item_amt="970000" bank_id="8" acct_num="2018856637"...',
168            self.browser.contents)
169        # BT does nor charge a fee for clearance
170        self.assertFalse("BT Education" in self.browser.contents)
171
172
173    @external_test
174    def test_webservice(self):
175        # First we have open InterswitchPageStudent to set provider_amt
176        # and gateway_amt
177        self.browser.open(self.payment_url + '/goto_interswitch')
178        # Now we can call the webservice
179        self.browser.open(self.payment_url + '/request_webservice')
180        self.assertMatches('...Unsuccessful callback...',
181                          self.browser.contents)
182        # The payment is now in state failed ...
183        self.assertMatches('...<span>Failed</span>...',
184                          self.browser.contents)
185        # ... and the catalog has been updated
186        cat = getUtility(ICatalog, name='payments_catalog')
187        results = list(
188            cat.searchResults(p_state=('failed', 'failed')))
189        self.assertEqual(len(results), 1)
190        self.assertEqual(results[0].p_state, 'failed')
191
192        # Let's replace the p_id with a valid p_id of the FUTMinna
193        # live system. This is definitely not an appropriate
194        # solution for testing, but we have no choice since
195        # Interswitch doesn't provide any interface
196        # for testing.
197        payment = self.student['payments'][self.value]
198        payment.p_id = 'p3522296094055'
199        self.browser.open(self.payment_url + '/request_webservice')
200        self.assertMatches('...Callback amount does not match...',
201                          self.browser.contents)
202        # The payment is now in state failed ...
203        self.assertMatches('...<span>Failed</span>...',
204                          self.browser.contents)
205        # Let's replace the amount autorized with the amount of the
206        # live system payment
207        payment.amount_auth = payment.r_amount_approved
208        self.browser.open(self.payment_url + '/request_webservice')
209        self.assertMatches('...Successful payment...',
210                          self.browser.contents)
211        # The payment is now in state paid ...
212        self.assertMatches('...<span>Paid</span>...',
213                          self.browser.contents)
214        # ... and the catalog has been updated
215        cat = getUtility(ICatalog, name='payments_catalog')
216        results = list(
217            cat.searchResults(p_state=('paid', 'paid')))
218        self.assertEqual(len(results), 1)
219        self.assertEqual(results[0].p_state, 'paid')
220        # Approval is logged in students.log ...
221        logfile = os.path.join(
222            self.app['datacenter'].storage, 'logs', 'students.log')
223        logcontent = open(logfile).read()
224        self.assertTrue(
225            'zope.mgr - '
226            'waeup.futminna.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - '
227            'M1000000 - successful schoolfee payment: p3522296094055\n'
228            in logcontent)
229        # ... and in payments.log
230        logfile = os.path.join(
231            self.app['datacenter'].storage, 'logs', 'payments.log')
232        logcontent = open(logfile).read()
233        self.assertTrue(
234            '"zope.mgr",M1000000,p3522296094055,schoolfee,'
235            '20000.0,00,1500.0,300.0,0.0,,,\n'
236            in logcontent)
237
238class InterswitchTestsApplicants(ApplicantsFullSetup):
239    """Tests for the Interswitch payment gateway.
240    """
241
242    layer = FunctionalLayer
243
244    def setUp(self):
245        super(InterswitchTestsApplicants, self).setUp()
246        # Create at least one FUTMinna faculty
247        self.app['faculties']['CPGS'] = Faculty(code='ABC')
248        self.app['faculties']['CPGS']['dep1'] = Department(code='dep1')
249        self.certificate2 = createObject('waeup.Certificate')
250        self.certificate2.code = u'CERT2'
251        self.certificate2.application_category = 'pg_ft'
252        self.certificate2.study_mode = 'pg_ft'
253        self.certificate2.start_level = 999
254        self.certificate2.end_level = 999
255        self.app['faculties']['CPGS']['dep1'].certificates.addCertificate(
256            self.certificate2)
257        self.applicantscontainer.application_category = 'pg_ft'
258        self.applicant.applicant_id = u'pg_anything'
259
260        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
261        self.browser.open(self.manage_path)
262        #IWorkflowState(self.student).setState('started')
263        super(InterswitchTestsApplicants, self).fill_correct_values()
264        self.browser.getControl(name="form.course1").value = ['CERT2']
265        self.applicantscontainer.application_fee = 3333.0
266        self.browser.getControl(name="form.nationality").value = ['NG']
267        self.browser.getControl(name="transition").value = ['start']
268        self.browser.getControl("Save").click()
269        self.browser.getControl("Add online").click()
270        self.assertMatches('...ticket created...',
271                           self.browser.contents)
272        #ctrl = self.browser.getControl(name='val_id')
273        #value = ctrl.options[0]
274        #self.browser.getLink(value).click()
275        self.assertMatches('...Amount Authorized...',
276                           self.browser.contents)
277        self.assertMatches(
278            '...<span>3333.0</span>...',
279            self.browser.contents)
280        self.payment_url = self.browser.url
281
282
283    def test_interswitch_form(self):
284
285        # Manager can access InterswitchForm
286        self.browser.getLink("CollegePAY", index=0).click()
287        self.assertMatches('...Total Amount Authorized:...',
288                           self.browser.contents)
289        self.assertTrue(
290            '<input type="hidden" name="amount" value="333300.0" />'
291            in self.browser.contents)
292        self.assertTrue(
293            '<item_detail item_id="1" item_name="application" '
294            'item_amt="253300" bank_id="8" acct_num="2005910931" />'
295            in self.browser.contents)
296        self.assertTrue(
297            '<item_detail item_id="2" item_name="BT Education" '
298            'item_amt="50000" bank_id="117" acct_num="1010764827" />'
299            in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.