source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py @ 16378

Last change on this file since 16378 was 16378, checked in by Henrik Bettermann, 4 years ago

Fix tests.

  • Property svn:keywords set to Id
File size: 12.0 KB
Line 
1## $Id: tests.py 16378 2021-01-20 10:12:42Z 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, timedelta, date
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 waeup.kofa.students.payments import StudentOnlinePayment
27from kofacustom.nigeria.interswitch.helpers import query_interswitch
28from kofacustom.nigeria.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[:-1]
40        print "WARNING: external tests are skipped!"
41        print "WARNING: edit %s to enable them." % myself
42        return
43    return func
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.student.nationality = u'NG'
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('...ticket created...',
61                           self.browser.contents)
62        self.browser.open(self.payments_path)
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.assertTrue('<span>40000.0</span>', self.browser.contents)
69        self.payment_url = self.browser.url
70        self.payment = self.student['payments'][self.value]
71
72    def test_interswitch_form(self):
73        # Manager can access InterswitchForm
74        self.browser.getLink("Pay via Interswitch", index=0).click()
75        # The total amount to be processed by Interswitch
76        # has been reduced by the Interswitch fee of 150 Nairas
77        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
78                           self.browser.contents)
79        self.assertMatches('...Total Amount Authorized:...',
80                           self.browser.contents)
81        self.assertEqual(self.student.current_mode, 'ug_ft')
82        self.assertMatches(
83            '...<input type="hidden" name="amount" value="4000000" />...',
84            self.browser.contents)
85
86        # Create school fee ticket for returning students. Payment is made
87        # for next session.
88        current_payment_key = self.student['payments'].keys()[0]
89        self.certificate.study_mode = u'ug_pt'
90        IWorkflowState(self.student).setState('returning')
91        configuration = createObject('waeup.SessionConfiguration')
92        configuration.academic_session = 2005
93        self.app['configuration'].addSessionConfiguration(configuration)
94        self.browser.open(self.payments_path + '/addop')
95        self.browser.getControl(name="form.p_category").value = ['schoolfee']
96        self.browser.getControl("Create ticket").click()
97        self.browser.open(self.payments_path)
98        ctrl = self.browser.getControl(name='val_id')
99        value = ctrl.options[1]
100        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
101        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
102        self.browser.getLink(value).click()
103        self.browser.getLink("Pay via Interswitch", index=0).click()
104        # Split amounts have been set.
105        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
106        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
107        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
108                           self.browser.contents)
109        self.assertTrue(
110            '<input type="hidden" name="amount" value="2030000" />' in
111            self.browser.contents)
112
113    def test_interswitch_form_ticket_expired(self):
114        # Manager can access InterswitchForm
115        self.browser.getLink("Pay via Interswitch", index=0).click()
116        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
117                           self.browser.contents)
118        self.assertMatches('...Total Amount Authorized:...',
119                           self.browser.contents)
120        self.assertEqual(self.student.current_mode, 'ug_ft')
121        self.assertTrue(
122            '<input type="hidden" name="amount" value="4030000" />' in
123            self.browser.contents)
124        delta = timedelta(days=8)
125        self.payment.creation_date -= delta
126        self.browser.open(self.payment_url)
127        self.browser.getLink("Pay via Interswitch", index=0).click()
128        self.assertMatches(
129            '...This payment ticket is too old. Please create a new ticket...',
130            self.browser.contents)
131        delta = timedelta(days=2)
132        self.payment.creation_date += delta
133        self.browser.open(self.payment_url)
134        self.browser.getLink("Pay via Interswitch", index=0).click()
135        self.assertMatches('...Total Amount Authorized:...',
136                           self.browser.contents)
137
138    def test_interswitch_form_ticket_expired_tz(self):
139        # The form copes with timezones when calculating expirements.
140        # We should not have TZ data in timestamps processed, but it looks
141        # like we get some with imports :-/
142        self.browser.getLink("Pay via Interswitch", index=0).click()
143        delta = timedelta(days=8)
144        self.payment.creation_date -= delta
145        from pytz import timezone
146        self.payment.creation_date = timezone("Europe/Berlin").localize(
147            self.payment.creation_date)
148        self.browser.open(self.payment_url)
149        self.browser.getLink("Pay via Interswitch", index=0).click()
150        self.assertMatches(
151            '...This payment ticket is too old. Please create a new ticket...',
152            self.browser.contents)
153        delta = timedelta(days=2)
154        self.payment.creation_date += delta
155        self.browser.open(self.payment_url)
156        self.browser.getLink("Pay via Interswitch", index=0).click()
157        self.assertMatches('...Total Amount Authorized:...',
158                           self.browser.contents)
159
160    @external_test
161    def test_query_interswitch_SOAP(self):
162        host = 'webpay.interswitchng.com'
163        url = '/paydirect/services/TransactionQueryWs.asmx'
164        https = True
165        mac = None
166        product_id = '5845' # AAUE regular
167        payment = StudentOnlinePayment()
168        payment.p_id ='p4465649308559'
169        payment.amount_auth = 60250.0
170        success, msg, log = query_interswitch(
171            payment, product_id, host, url, https, mac, False)
172        self.assertEqual('Successful callback received.', msg)
173        self.assertTrue(success)
174        self.assertTrue(
175            '00:Approved Successful:6025000:3154:p4465649308559:'
176            'ZIB|WEB|ABAL|3-11-2015|021336:000457580882' in log)
177
178    @external_test
179    def test_query_interswitch_JSON(self):
180        host = 'webpay.interswitchng.com'
181        url = '/paydirect/api/v1/gettransaction.json'
182        mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023'
183        https = True
184        product_id = '5845' # AAUE regular
185        payment = StudentOnlinePayment()
186        payment.p_id ='p4465649308559'
187        payment.amount_auth = 1.0
188        success, msg, log = query_interswitch(
189            payment, product_id, host, url, https, mac, False)
190        self.assertFalse(success)
191        self.assertTrue('Unsuccessful callback:' in msg)
192        self.assertTrue('Amount Inconsistency' in log)
193        payment.amount_auth = 60250.0
194        success, msg, log = query_interswitch(
195            payment, product_id, host, url, https, mac, False)
196        self.assertEqual('Successful callback received', msg)
197        self.assertTrue(success)
198        self.assertTrue(
199            "{u'SplitAccounts': [], "
200            "u'MerchantReference': u'p4465649308559', "
201            "u'PaymentReference': u'ZIB|WEB|ABAL|3-11-2015|021336', "
202            "u'TransactionDate': u'2015-11-03T16:40:54.487', "
203            "u'RetrievalReferenceNumber': u'000457580882', "
204            "u'ResponseDescription': u'Approved Successful', "
205            "u'Amount': 6025000, "
206            "u'CardNumber': u'3154', "
207            "u'ResponseCode': u'00', "
208            "u'LeadBankCbnCode': None, "
209            "u'LeadBankName': None}" in log)
210
211class InterswitchTestsApplicants(ApplicantsFullSetup):
212    """Tests for the Interswitch payment gateway.
213    """
214
215    layer = FunctionalLayer
216
217    def setUp(self):
218        super(InterswitchTestsApplicants, self).setUp()
219        configuration = SessionConfiguration()
220        configuration.academic_session = datetime.now().year - 2
221        self.app['configuration'].addSessionConfiguration(configuration)
222        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
223        self.browser.open(self.manage_path)
224        #IWorkflowState(self.student).setState('started')
225        super(InterswitchTestsApplicants, self).fill_correct_values()
226        self.applicantscontainer.application_fee = 1000.0
227        self.browser.getControl(name="form.nationality").value = ['NG']
228        self.browser.getControl(name="transition").value = ['start']
229        self.browser.getControl("Save").click()
230        self.browser.getControl("Add online").click()
231        self.assertMatches('...ticket created...',
232                           self.browser.contents)
233        self.payment = self.applicant.values()[0]
234        self.payment_url = self.browser.url
235
236    def test_interswitch_form(self):
237        self.assertMatches('...Amount Authorized...',
238                           self.browser.contents)
239        self.assertMatches(
240            '...<span>1000.0</span>...',
241            self.browser.contents)
242        # Manager can access InterswitchForm
243        self.browser.getLink("Pay via Interswitch", index=0).click()
244        self.assertMatches('...Total Amount Authorized:...',
245                           self.browser.contents)
246        self.assertMatches(
247            '...<input type="hidden" name="amount" value="100000" />...',
248            self.browser.contents)
249        delta = timedelta(days=8)
250        self.payment.creation_date -= delta
251        self.browser.open(self.payment_url)
252        self.browser.getLink("Pay via Interswitch", index=0).click()
253        self.assertMatches(
254            '...This payment ticket is too old. Please create a new ticket...',
255            self.browser.contents)
256        delta = timedelta(days=2)
257        self.payment.creation_date += delta
258        self.browser.getLink("Pay via Interswitch", index=0).click()
259        self.assertMatches('...Total Amount Authorized:...',
260                           self.browser.contents)
261
Note: See TracBrowser for help on using the repository browser.