source: main/kofacustom.ekodisco/branches/0.1/src/kofacustom/ekodisco/interswitch/tests.py @ 17935

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

Fix test.

  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1## $Id: tests.py 10807 2013-11-28 18:35:10Z 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 createObject, 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 kofacustom.ekodisco.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 = False
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
52    def test_interswitch_form(self):
53        # Manager can access InterswitchForm
54        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
55        self.browser.open(self.payments_path)
56        IWorkflowState(self.student).setState('cleared')
57        self.student.nationality = u'NG'
58        self.browser.open(self.payments_path + '/addop')
59        self.browser.getControl(name="form.p_category").value = ['schoolfee']
60        self.browser.getControl("Create ticket").click()
61        self.assertMatches('...ticket created...',
62                           self.browser.contents)
63        ctrl = self.browser.getControl(name='val_id')
64        self.value = ctrl.options[0]
65        self.browser.getLink(self.value).click()
66        self.assertMatches('...Amount Authorized...',
67                           self.browser.contents)
68        self.assertMatches(
69            '...<span>40000.0</span>...',
70            self.browser.contents)
71        self.payment_url = self.browser.url
72        self.browser.getLink("CollegePAY", index=0).click()
73        # The total amount to be processed by Interswitch
74        # has been reduced by the Interswitch fee of 150 Nairas
75        self.assertMatches('...<input type="hidden" name="pay_item_id" value="0000" />...',
76                           self.browser.contents)
77        self.assertMatches('...Total Amount Authorized:...',
78                           self.browser.contents)
79        self.assertEqual(self.student.current_mode, 'ug_ft')
80        self.assertMatches(
81            '...<input type="hidden" name="amount" value="4000000.0" />...',
82            self.browser.contents)
83        self.assertMatches(
84            '...item_name="Meter Charge" item_amt="4000000" bank_id="00" acct_num="00000000"...',
85            self.browser.contents)
86
87    @external_test
88    def test_webservice(self):
89        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
90        self.browser.open(self.payments_path)
91        IWorkflowState(self.student).setState('cleared')
92        self.student.nationality = u'NG'
93        self.browser.open(self.payments_path + '/addop')
94        self.browser.getControl(name="form.p_category").value = ['schoolfee']
95        self.browser.getControl("Create ticket").click()
96        self.assertMatches('...ticket created...',
97                           self.browser.contents)
98        ctrl = self.browser.getControl(name='val_id')
99        self.value = ctrl.options[0]
100        self.browser.getLink(self.value).click()
101        self.payment_url = self.browser.url
102        # First we have open InterswitchPageStudent to set provider_amt
103        # and gateway_amt
104        self.browser.open(self.payment_url + '/goto_interswitch')
105        # Now we can call the webservice
106        self.browser.open(self.payment_url + '/request_webservice')
107        self.assertMatches('...Unsuccessful callback...',
108                          self.browser.contents)
109        # The payment is now in state failed ...
110        self.assertMatches('...<span>Failed</span>...',
111                          self.browser.contents)
112        # ... and the catalog has been updated
113        cat = getUtility(ICatalog, name='payments_catalog')
114        results = list(
115            cat.searchResults(p_state=('failed', 'failed')))
116        self.assertEqual(len(results), 1)
117        self.assertEqual(results[0].p_state, 'failed')
118
119        # Let's replace the p_id with a valid p_id of the Uniben
120        # live system. This is definitely not an appropriate
121        # solution for testing, but we have no choice since
122        # Interswitch doesn't provide any interface
123        # for testing.
124        payment = self.student['payments'][self.value]
125        payment.p_id = 'p3547789850240'
126        self.browser.open(self.payment_url + '/request_webservice')
127        self.assertMatches('...Callback amount does not match...',
128                          self.browser.contents)
129        # The payment is now in state failed ...
130        self.assertMatches('...<span>Failed</span>...',
131                          self.browser.contents)
132        # Let's replace the amount autorized with the amount of the
133        # live system payment
134        payment.amount_auth = payment.r_amount_approved
135        self.browser.open(self.payment_url + '/request_webservice')
136        self.assertMatches('...Successful payment...',
137                          self.browser.contents)
138        # The payment is now in state paid ...
139        self.assertMatches('...<span>Paid</span>...',
140                          self.browser.contents)
141        # ... and the catalog has been updated
142        cat = getUtility(ICatalog, name='payments_catalog')
143        results = list(
144            cat.searchResults(p_state=('paid', 'paid')))
145        self.assertEqual(len(results), 1)
146        self.assertEqual(results[0].p_state, 'paid')
147        # Approval is logged in students.log ...
148        logfile = os.path.join(
149            self.app['datacenter'].storage, 'logs', 'students.log')
150        logcontent = open(logfile).read()
151        self.assertTrue(
152            'zope.mgr - '
153            'kofacustom.ekodisco.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - '
154            'X1000000 - successful schoolfee payment: p3547789850240\n'
155            in logcontent)
156        # ... and in payments.log
157        logfile = os.path.join(
158            self.app['datacenter'].storage, 'logs', 'payments.log')
159        logcontent = open(logfile).read()
160        self.assertTrue(
161            '"zope.mgr",X1000000,p3547789850240,schoolfee,'
162            '12000.0,00,0.0,150.0,0.0,,,\n'
163            in logcontent)
Note: See TracBrowser for help on using the repository browser.