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

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

Save modifications. They are completely untested because demo server is offline.

  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1## $Id: tests.py 15596 2019-09-20 06:51:47Z 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'
50
51def external_test(func):
52    if not EXTERNAL_TESTS:
53        myself = __file__
54        if myself.endswith('.pyc'):
55            myself = myself[:-1]
56        print "WARNING: external tests are skipped!"
57        print "WARNING: edit %s to enable them." % myself
58        return
59    return func
60
61def post_caller(host, https, terminal_id, transaction_id, responseurl,
62                        amount_auth, email, phone, display_fullname, hashvalue,
63                        logo_url):
64    headers={"Content-type": "application/x-www-form-urlencoded",
65             "Accept": "text/plain"}
66    url = "/webconnect/v3/caller.jsp"
67    if https:
68        h = httplib.HTTPSConnection(host)
69    else:
70        h = httplib.HTTPConnection(host)
71    args = {'TERMINAL_ID': terminal_id,
72            'TRANSACTION_ID': transaction_id,
73            'RESPONSE_URL': responseurl,
74            'AMOUNT': amount_auth,
75            'EMAIL': email,
76            'PHONENO': phone,
77            'FULL_NAME': display_fullname,
78            'CURRENCY_CODE': 'NGN',
79            'CHECKSUM': hashvalue,
80            'LOGO_URL': logo_url,
81            }
82    h.request('POST', url, urlencode(args), headers)
83    return
84    response = h.getresponse()
85    if response.status!=200:
86        return 'Connection error (%s, %s)' % (response.status, response.reason)
87    resp = response.read()
88    return resp
89
90def create_transaction(transaction_id):
91    responseurl = 'http://xxxx'
92    amount = '4444.0'
93    email = 'aa@aa.ng'
94    phone = '12324'
95    display_fullname = 'Tester'
96    logo_url = 'http://xxxx'
97    hashargs =  amount + TERMINAL_ID + transaction_id \
98        + responseurl + 'DEMO_KEY'
99    hashvalue = hashlib.md5(hashargs).hexdigest()
100    response = post_caller(HOST, HTTPS, TERMINAL_ID,
101                         transaction_id, responseurl,
102                         amount, email, phone,
103                         display_fullname, hashvalue,
104                         logo_url)
105    return response
106
107
108class HelperTests(unittest.TestCase):
109
110    terminal_id = TERMINAL_ID
111
112    @external_test
113    def test_query_history(self):
114        transaction_id = str(random.randint(100000000, 999999999))
115        raw, formvars = query_history(HOST, self.terminal_id,
116                                transaction_id, HTTPS)
117        self.assertTrue(
118            'Transaction with the given transaction_id (%s) not found'
119            % transaction_id in raw)
120        # Okay, let's create a transaction
121        caller_response = create_transaction(transaction_id)
122        self.assertEqual(caller_response, None)
123        # It seems that the transaction has been created but we don't get a
124        # useful response
125        raw, formvars = query_history(HOST, self.terminal_id,
126                                transaction_id, HTTPS)
127        self.assertTrue(
128            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\r\n    '
129            '"http://www.w3.org/TR/html4/loose.dtd">' in raw)
130        # The same, an 'empty' response obviously means that the transaction
131        # was found but no result was  sent to the response_url because the
132        # payment was cancelled.
133
134        # Peter: No response would be returned because payment status information
135        # was not saved. Whenever you get a null response, means payment was
136        # not received only payments with error code 0 is successful.
137        # So in this case transaction fails.
138
139        # Peter has manually created a successful payment. Let's use this one.
140        transaction_id = 'etz1568638104web'
141        raw, formvars = query_history(HOST, self.terminal_id,
142                                transaction_id, HTTPS)
143        # Now eTranzact is redirecting but the response is still useless
144        self.assertTrue('Redirecting...' in raw)
145        print query_response
146        return
147
148class EtranzactTestsApplicants(ApplicantsFullSetup):
149    """Tests for the Etranzact payment gateway.
150    """
151
152    layer = FunctionalLayer
153
154    def setUp(self):
155        super(EtranzactTestsApplicants, self).setUp()
156        configuration = SessionConfiguration()
157        configuration.academic_session = datetime.now().year - 2
158        configuration.etranzact_enabled = True
159        self.app['configuration'].addSessionConfiguration(configuration)
160        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
161        self.browser.open(self.manage_path)
162        #IWorkflowState(self.student).setState('started')
163        super(EtranzactTestsApplicants, self).fill_correct_values()
164        self.applicantscontainer.application_fee = 1000.0
165        self.browser.getControl(name="form.nationality").value = ['NG']
166        self.browser.getControl(name="transition").value = ['start']
167        self.browser.getControl("Save").click()
168        self.browser.getControl("Add online").click()
169        self.assertMatches('...ticket created...',
170                           self.browser.contents)
171        self.payment = self.applicant.values()[0]
172        self.payment_url = self.browser.url
173
174    @external_test
175    def test_views(self):
176        # Manager can access eTranzact form
177        self.browser.getLink("Pay via eTranzact").click()
178        self.assertTrue("Pay now" in self.browser.contents)
179
180        return
181
182
Note: See TracBrowser for help on using the repository browser.