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

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

Add etranzact components for students.

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