source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/remita/tests.py @ 14755

Last change on this file since 14755 was 14754, checked in by Henrik Bettermann, 7 years ago

Add and improve tests.

  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1## $Id: tests.py 14754 2017-08-03 05:41:45Z henrik $
2##
3## Copyright (C) 2017 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
19import unittest
20import random
21from datetime import datetime, timedelta, date
22from zope.component import createObject, getUtility
23from zope.catalog.interfaces import ICatalog
24from hurry.workflow.interfaces import IWorkflowState
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 kofacustom.nigeria.students.payments import NigeriaStudentOnlinePayment
29from kofacustom.nigeria.testing import FunctionalLayer
30
31from kofacustom.nigeria.remita.helpers import (
32    get_JSON_POST_response, get_payment_status_via_rrr, query_remita)
33
34# Also run tests that send requests to external servers?
35#   If you enable this, please make sure the external services
36#   do exist really and are not bothered by being spammed by a test programme.
37EXTERNAL_TESTS = True
38
39def external_test(func):
40    if not EXTERNAL_TESTS:
41        myself = __file__
42        if myself.endswith('.pyc'):
43            myself = myself[:-1]
44        print "WARNING: external tests are skipped!"
45        print "WARNING: edit %s to enable them." % myself
46        return
47    return func
48
49class HelperTests(unittest.TestCase):
50
51    merchantId = '2547916'
52    serviceTypeId = '4430731'
53    api_key = '1946'
54    responseurl = 'http://xxxx'
55    host = 'www.remitademo.net'
56    url = '/remita/ecomm/split/init.reg'  # /remita/ecomm/v2/init.reg
57    lineitems = (
58                  {"lineItemsId":"itemid1","beneficiaryName":"Oshadami Mike",
59                  "beneficiaryAccount":"6020067886","bankCode":"011",
60                  "beneficiaryAmount":"500","deductFeeFrom":"1"},
61                  {"lineItemsId":"itemid2","beneficiaryName":"Ogunseye Mujib",
62                  "beneficiaryAccount":"0360883515","bankCode":"050",
63                  "beneficiaryAmount":"500","deductFeeFrom":"0"}
64                )
65    amount = 1000.0
66
67    def _get_transaction_data(self):
68        self.orderId = str(random.randint(100000000, 999999999))
69        resp = get_JSON_POST_response(
70                merchantId=self.merchantId, serviceTypeId=self.serviceTypeId,
71                api_key=self.api_key, orderId=self.orderId,
72                amount=self.amount, responseurl=self.responseurl,
73                host=self.host, url=self.url, https=False,
74                fullname='Anton Meier', email='am@xxx.de',
75                lineitems=self.lineitems)
76        self.rrr = resp['RRR'].rstrip()
77
78    @external_test
79    def test_get_JSON_POST_response(self):
80        self._get_transaction_data()
81        resp = get_JSON_POST_response(
82                merchantId=self.merchantId, serviceTypeId=self.serviceTypeId,
83                api_key=self.api_key, orderId=self.orderId,
84                amount=self.amount, responseurl=self.responseurl,
85                host=self.host, url=self.url, https=False,
86                fullname='Anton Meier', email='am@xxx.de',
87                lineitems=self.lineitems)
88        assert resp == {
89            u'status': u'RRR Already Exist for the orderId',
90            u'orderID': self.orderId,
91            u'RRR': self.rrr + ' ',  # strange trailing whitespace which
92                                     # obviously does not belong to the RRR
93            u'statuscode': u'055'}
94
95        resp = get_payment_status_via_rrr(
96                merchantId=self.merchantId,
97                api_key=self.api_key,
98                RRR=self.rrr,
99                host=self.host,
100                https=False,
101                )
102        assert resp['orderId'] == self.orderId
103        assert resp['status'] == '021'
104        assert resp['amount'] == self.amount
105        assert resp['lineitems'] == [
106            {u'status': u'021', u'lineItemsId': u'itemid1'},
107            {u'status': u'021', u'lineItemsId': u'itemid2'}]
108        assert resp['RRR'] == self.rrr
109        assert resp['message'] == u'Transaction Pending'
110
111class RemitaTestsStudents(StudentsFullSetup):
112    """Tests for the Remita payment gateway.
113    """
114
115    layer = FunctionalLayer
116
117    merchantId = '2547916'
118    serviceTypeId = '4430731'
119    api_key = '1946'
120    responseurl = 'http://xxxx'
121    host = 'www.remitademo.net'
122
123    # successful transaction
124    # (hopefully this transaction remain in the Remita database)
125    orderId = '3456346346'
126    rrr = u'280007640804'
127    amount = 1000.0
128
129    # pending transaction
130    #orderId_p = '987698769876'
131    rrr_p = u'320007640976'
132
133    def setUp(self):
134        super(RemitaTestsStudents, self).setUp()
135        self.app['configuration']['2004'].remita_enabled = True
136        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
137        self.browser.open(self.payments_path)
138        IWorkflowState(self.student).setState('cleared')
139        self.student.nationality = u'NG'
140        self.browser.open(self.payments_path + '/addop')
141        self.browser.getControl(name="form.p_category").value = ['schoolfee']
142        self.browser.getControl("Create ticket").click()
143        self.assertMatches('...ticket created...',
144                           self.browser.contents)
145        ctrl = self.browser.getControl(name='val_id')
146        self.value = ctrl.options[0]
147        self.browser.getLink(self.value).click()
148        self.assertMatches('...Amount Authorized...',
149                           self.browser.contents)
150        self.assertTrue('<span>40000.0</span>', self.browser.contents)
151        self.payment_url = self.browser.url
152        self.payment = self.student['payments'][self.value]
153
154    @external_test
155    def test_query_remita(self):
156        #requery pending transaction
157        qr = query_remita(
158            self.payment,
159            merchantId=self.merchantId,
160            api_key=self.api_key,
161            RRR=self.rrr_p,
162            host=self.host,
163            https=False,
164            verify=False)
165        assert qr == (
166            False,
167            u'Unsuccessful callback: ${a}',
168            u'unsuccessful callback for schoolfee payment %s: Transaction Pending'
169            % self.payment.p_id)
170
171        #requery successful transaction
172        self.payment.amount_auth = 1000.0
173        self.payment.p_id = self.orderId
174        qr = query_remita(
175            self.payment,
176            merchantId=self.merchantId,
177            api_key=self.api_key,
178            RRR=self.rrr,
179            host=self.host,
180            https=False,
181            verify=False)
182        assert qr[0] == True
183        assert qr[1] == u'Successful callback received'
184
185    @external_test
186    def test_remita_form(self):
187        # Manager can access InterswitchForm
188        self.browser.getLink("Pay via Remita").click()
189        # The RRR has been retrieved
190        self.assertTrue('<input name="rrr" type="hidden" value="%s">' % self.rrr
191            in self.browser.contents)
192        self.assertEqual(self.payment.r_pay_reference, self.rrr)
193        # Means of testing end here.
194        return
195
196    @external_test
197    def test_requery_verify_payment_status(self):
198        # Manager can access InterswitchForm
199        self.browser.getLink("Requery Remita Payment Status").click()
200        self.assertTrue('Remita Retrieval Reference not found.'
201            in self.browser.contents)
202        self.payment.r_pay_reference = self.rrr
203        self.browser.getLink("Requery Remita Payment Status").click()
204        self.assertTrue('Callback amount does not match.'
205            in self.browser.contents)
206        self.payment.amount_auth = 1000.0
207        self.browser.getLink("Requery Remita Payment Status").click()
208        self.assertTrue('Callback order id does not match.'
209            in self.browser.contents)
210        self.payment.p_id = self.orderId
211        self.browser.getLink("Requery Remita Payment Status").click()
212        self.assertTrue('Successful payment' in self.browser.contents)
213        self.assertEqual(self.payment.r_desc, 'Approved')
214        self.assertEqual(self.payment.r_amount_approved , 1000.0)
215        self.assertEqual(self.payment.r_code, '01')
216        self.browser.getLink("Verify Remita Payment Status").click()
217        self.assertTrue('Successful payment' in self.browser.contents)
218        return
Note: See TracBrowser for help on using the repository browser.