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
RevLine 
[11640]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
[12973]19from datetime import datetime, timedelta, date
[11640]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
[13386]26from waeup.kofa.students.payments import StudentOnlinePayment
27from kofacustom.nigeria.interswitch.helpers import query_interswitch
[11640]28from kofacustom.nigeria.testing import FunctionalLayer
29
[13386]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.
[13387]33EXTERNAL_TESTS = False
[13386]34
35def external_test(func):
36    if not EXTERNAL_TESTS:
37        myself = __file__
38        if myself.endswith('.pyc'):
[14737]39            myself = myself[:-1]
[13386]40        print "WARNING: external tests are skipped!"
41        print "WARNING: edit %s to enable them." % myself
42        return
43    return func
44
[11640]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)
[16378]62        self.browser.open(self.payments_path)
[11640]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)
[13386]68        self.assertTrue('<span>40000.0</span>', self.browser.contents)
[11640]69        self.payment_url = self.browser.url
[12973]70        self.payment = self.student['payments'][self.value]
[11640]71
72    def test_interswitch_form(self):
73        # Manager can access InterswitchForm
[15702]74        self.browser.getLink("Pay via Interswitch", index=0).click()
[11640]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(
[11767]83            '...<input type="hidden" name="amount" value="4000000" />...',
[11640]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()
[16378]97        self.browser.open(self.payments_path)
[11640]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()
[15702]103        self.browser.getLink("Pay via Interswitch", index=0).click()
[11640]104        # Split amounts have been set.
105        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
[15755]106        self.assertEqual(self.student['payments'][value].gateway_amt, 300.0)
[11640]107        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
108                           self.browser.contents)
[15755]109        self.assertTrue(
110            '<input type="hidden" name="amount" value="2030000" />' in
[11640]111            self.browser.contents)
112
[12973]113    def test_interswitch_form_ticket_expired(self):
114        # Manager can access InterswitchForm
[15702]115        self.browser.getLink("Pay via Interswitch", index=0).click()
[12973]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')
[15755]121        self.assertTrue(
122            '<input type="hidden" name="amount" value="4030000" />' in
[12973]123            self.browser.contents)
[13015]124        delta = timedelta(days=8)
[12973]125        self.payment.creation_date -= delta
126        self.browser.open(self.payment_url)
[15702]127        self.browser.getLink("Pay via Interswitch", index=0).click()
[12973]128        self.assertMatches(
129            '...This payment ticket is too old. Please create a new ticket...',
130            self.browser.contents)
[13015]131        delta = timedelta(days=2)
[12973]132        self.payment.creation_date += delta
133        self.browser.open(self.payment_url)
[15702]134        self.browser.getLink("Pay via Interswitch", index=0).click()
[12973]135        self.assertMatches('...Total Amount Authorized:...',
136                           self.browser.contents)
137
[13477]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 :-/
[15702]142        self.browser.getLink("Pay via Interswitch", index=0).click()
[13477]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)
[15702]149        self.browser.getLink("Pay via Interswitch", index=0).click()
[13477]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)
[15702]156        self.browser.getLink("Pay via Interswitch", index=0).click()
[13477]157        self.assertMatches('...Total Amount Authorized:...',
158                           self.browser.contents)
159
[13386]160    @external_test
[13387]161    def test_query_interswitch_SOAP(self):
[13386]162        host = 'webpay.interswitchng.com'
163        url = '/paydirect/services/TransactionQueryWs.asmx'
164        https = True
[13387]165        mac = None
[13386]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(
[16115]171            payment, product_id, host, url, https, mac, False)
172        self.assertEqual('Successful callback received.', msg)
[13386]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
[13387]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(
[16115]189            payment, product_id, host, url, https, mac, False)
[13387]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(
[16115]195            payment, product_id, host, url, https, mac, False)
[13387]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
[11640]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)
[12973]233        self.payment = self.applicant.values()[0]
[11640]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
[15702]243        self.browser.getLink("Pay via Interswitch", index=0).click()
[11640]244        self.assertMatches('...Total Amount Authorized:...',
245                           self.browser.contents)
246        self.assertMatches(
[11767]247            '...<input type="hidden" name="amount" value="100000" />...',
[11640]248            self.browser.contents)
[13015]249        delta = timedelta(days=8)
[12973]250        self.payment.creation_date -= delta
251        self.browser.open(self.payment_url)
[15702]252        self.browser.getLink("Pay via Interswitch", index=0).click()
[12973]253        self.assertMatches(
254            '...This payment ticket is too old. Please create a new ticket...',
255            self.browser.contents)
[13015]256        delta = timedelta(days=2)
[12973]257        self.payment.creation_date += delta
[15702]258        self.browser.getLink("Pay via Interswitch", index=0).click()
[12973]259        self.assertMatches('...Total Amount Authorized:...',
260                           self.browser.contents)
[11640]261
Note: See TracBrowser for help on using the repository browser.