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

Last change on this file since 11150 was 10834, checked in by Henrik Bettermann, 11 years ago

Adjust tests.

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