source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/etranzact/tests.py @ 15727

Last change on this file since 15727 was 15702, checked in by Henrik Bettermann, 5 years ago

Add first Payoutlet components. Not yet tested.
Rename eTranzact.

  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1## $Id: tests.py 15702 2019-10-25 09:59:20Z 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
21import json
22import hashlib
23import httplib
24from urllib import urlencode
25from datetime import datetime, timedelta, date
26from zope.component import createObject, getUtility
27from zope.catalog.interfaces import ICatalog
28from hurry.workflow.interfaces import IWorkflowState
29from waeup.kofa.students.tests.test_browser import StudentsFullSetup
30from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
31from waeup.kofa.configuration import SessionConfiguration
32from kofacustom.nigeria.students.payments import NigeriaStudentOnlinePayment
33from kofacustom.nigeria.testing import FunctionalLayer
34from kofacustom.nigeria.etranzact.helpers import (
35    query_history,)
36
37#from kofacustom.nigeria.etranzact.helpers import (query_etranzact)
38
39# Also run tests that send requests to external servers?
40#   If you enable this, please make sure the external services
41#   do exist really and are not bothered by being spammed by a test programme.
42
43EXTERNAL_TESTS = True
44
45TERMINAL_ID = '0000000001'
46HOST = 'demo.etranzact.com'
47HTTPS = True
48SECRET_KEY = 'DEMO_KEY'
49LOGO_URL = 'https://iuokada.waeup.org/static_custom/iou_logo.png'
50PAYOUTLET_QUERY_URL = 'http://demo.etranzact.com/WebConnectPlus/query.jsp'
51
52# Valid transaction id in Etranzact system
53TID = 'p5689785145198'
54
55def external_test(func):
56    if not EXTERNAL_TESTS:
57        myself = __file__
58        if myself.endswith('.pyc'):
59            myself = myself[:-1]
60        print "WARNING: external tests are skipped!"
61        print "WARNING: edit %s to enable them." % myself
62        return
63    return func
64
65def post_caller(host, https, terminal_id, transaction_id, responseurl,
66                        amount_auth, email, phone, display_fullname, hashvalue,
67                        logo_url):
68    headers={"Content-type": "application/x-www-form-urlencoded",
69             "Accept": "text/plain"}
70    url = "/webconnect/v3/caller.jsp"
71    if https:
72        h = httplib.HTTPSConnection(host)
73    else:
74        h = httplib.HTTPConnection(host)
75    args = {'TERMINAL_ID': terminal_id,
76            'TRANSACTION_ID': transaction_id,
77            'RESPONSE_URL': responseurl,
78            'AMOUNT': amount_auth,
79            'EMAIL': email,
80            'PHONENO': phone,
81            'FULL_NAME': display_fullname,
82            'CURRENCY_CODE': 'NGN',
83            'CHECKSUM': hashvalue,
84            'LOGO_URL': logo_url,
85            }
86    h.request('POST', url, urlencode(args), headers)
87    return
88    response = h.getresponse()
89    if response.status!=200:
90        return 'Connection error (%s, %s)' % (response.status, response.reason)
91    resp = response.read()
92    return resp
93
94def create_transaction(transaction_id):
95    responseurl = 'http://xxxx'
96    amount = '4444.0'
97    email = 'aa@aa.ng'
98    phone = '12324'
99    display_fullname = 'Tester'
100    logo_url = 'http://xxxx'
101    hashargs =  amount + TERMINAL_ID + transaction_id \
102        + responseurl + 'DEMO_KEY'
103    hashvalue = hashlib.md5(hashargs).hexdigest()
104    response = post_caller(HOST, HTTPS, TERMINAL_ID,
105                         transaction_id, responseurl,
106                         amount, email, phone,
107                         display_fullname, hashvalue,
108                         logo_url)
109    return response
110
111
112class HelperTests(unittest.TestCase):
113
114    terminal_id = TERMINAL_ID
115
116    @external_test
117    def test_query_history(self):
118        transaction_id = str(random.randint(100000000, 999999999))
119        raw, formvars = query_history(HOST, self.terminal_id,
120                                transaction_id, HTTPS)
121        self.assertTrue(
122            'Transaction with the given transaction_id (%s) not found'
123            % transaction_id in raw)
124        # Okay, let's create a transaction
125        caller_response = create_transaction(transaction_id)
126        self.assertEqual(caller_response, None)
127        # It seems that the transaction has been created but we don't get a
128        # useful response
129        raw, formvars = query_history(HOST, self.terminal_id,
130                                transaction_id, HTTPS)
131        self.assertTrue(
132            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\r\n    '
133            '"http://www.w3.org/TR/html4/loose.dtd">' in raw)
134        # The same, an 'empty' response obviously means that the transaction
135        # was found but no result was  sent to the response_url because the
136        # payment was cancelled.
137
138        # Peter: No response would be returned because payment status information
139        # was not saved. Whenever you get a null response, means payment was
140        # not received only payments with error code 0 is successful.
141        # So in this case transaction fails.
142
143        # We manually created and tried to pay a transaction
144        # which seems to be valid till next restart of the demo portal.
145        raw, formvars = query_history(HOST, self.terminal_id, TID, HTTPS)
146        # Now Etranzact is redirecting but the response is still useless
147        self.assertTrue('Redirecting...' in raw)
148        self.assertEqual(formvars['TRANSACTION_ID'], TID)
149        return
150
151class EtranzactTestsApplicants(ApplicantsFullSetup):
152    """Tests for the Etranzact payment gateway.
153    """
154
155    layer = FunctionalLayer
156
157    def setUp(self):
158        super(EtranzactTestsApplicants, self).setUp()
159        configuration = SessionConfiguration()
160        configuration.academic_session = datetime.now().year - 2
161        configuration.etranzact_webconnect_enabled = True
162        configuration.etranzact_payoutlet_enabled = True
163        self.app['configuration'].addSessionConfiguration(configuration)
164        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
165        self.browser.open(self.manage_path)
166        #IWorkflowState(self.student).setState('started')
167        super(EtranzactTestsApplicants, self).fill_correct_values()
168        self.applicantscontainer.application_fee = 3333.0
169        self.browser.getControl(name="form.nationality").value = ['NG']
170        self.browser.getControl(name="transition").value = ['start']
171        self.browser.getControl("Save").click()
172        self.browser.getControl("Add online").click()
173        self.assertMatches('...ticket created...',
174                           self.browser.contents)
175        self.payment = self.applicant.values()[0]
176        self.payment_url = self.browser.url
177
178    @external_test
179    def test_applicant_views(self):
180        # Manager can access Etranzact form
181        self.browser.getLink("Pay via Etranzact").click()
182        self.assertTrue("Pay now" in self.browser.contents)
183        # Means of testing end here.
184        # We requery an existing paiment now.
185        self.payment.p_id = TID
186        self.browser.open(self.payment_url)
187        self.browser.getLink("Requery Etranzact History").click()
188        self.assertTrue('Wrong checksum.' in self.browser.contents)
189        # ... probably because responseurl of the transaction stored in the
190        # system and the responseurl generated in process_response are
191        # different
192        # Means of testing end here again.
193        return
194
195class EtranzactTestsStudents(StudentsFullSetup):
196    """Tests for the Etranzact payment gateway.
197    """
198
199    layer = FunctionalLayer
200
201    def setUp(self):
202        super(EtranzactTestsStudents, self).setUp()
203        self.app['configuration']['2004'].etranzact_webconnect_enabled = True
204        self.app['configuration']['2004'].etranzact_payoutlet_enabled = True
205        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
206        self.browser.open(self.payments_path)
207        IWorkflowState(self.student).setState('cleared')
208        self.student.nationality = u'NG'
209        self.browser.open(self.payments_path + '/addop')
210        self.browser.getControl(name="form.p_category").value = ['schoolfee']
211        self.browser.getControl("Create ticket").click()
212        self.assertMatches('...ticket created...',
213                           self.browser.contents)
214        ctrl = self.browser.getControl(name='val_id')
215        self.value = ctrl.options[0]
216        self.browser.getLink(self.value).click()
217        self.assertMatches('...Amount Authorized...',
218                           self.browser.contents)
219        self.assertTrue('<span>40000.0</span>', self.browser.contents)
220        self.payment_url = self.browser.url
221        self.payment = self.student['payments'][self.value]
222
223    @external_test
224    def test_student_views(self):
225        # Manager can access Etranzact form
226        self.browser.getLink("Pay via Etranzact").click()
227        self.assertTrue("Pay now" in self.browser.contents)
228        # Means of testing end here.
229        # We requery an existing paiment now.
230        self.payment.p_id = TID
231        self.browser.open(self.payment_url)
232        self.browser.getLink("Requery Etranzact History").click()
233        self.assertTrue('Wrong checksum.' in self.browser.contents)
234        # ... probably because responseurl and amount stored in the
235        # system and the responseurl generated in process_response are
236        # different
237        # Means of testing end here again.
238        return
239
240    @external_test
241    def test_student_payoutlet_views(self):
242        self.browser.getLink("Enter Etranzact PIN").click()
243        self.browser.getControl(name="confirmation_number").value = '1234'
244        self.browser.getControl("Submit to Etranzact").click()
Note: See TracBrowser for help on using the repository browser.