1 | ## $Id: tests.py 15593 2019-09-19 10:12:53Z 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 | ## |
---|
18 | import os |
---|
19 | import unittest |
---|
20 | import random |
---|
21 | import json |
---|
22 | import hashlib |
---|
23 | import httplib |
---|
24 | from urllib import urlencode |
---|
25 | from datetime import datetime, timedelta, date |
---|
26 | from zope.component import createObject, getUtility |
---|
27 | from zope.catalog.interfaces import ICatalog |
---|
28 | from hurry.workflow.interfaces import IWorkflowState |
---|
29 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
30 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
31 | from waeup.kofa.configuration import SessionConfiguration |
---|
32 | from kofacustom.nigeria.students.payments import NigeriaStudentOnlinePayment |
---|
33 | from kofacustom.nigeria.testing import FunctionalLayer |
---|
34 | from 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 | |
---|
43 | EXTERNAL_TESTS = True |
---|
44 | |
---|
45 | TERMINAL_ID = '0000000001' |
---|
46 | HOST = 'demo.etranzact.com' |
---|
47 | HTTPS = True |
---|
48 | SECRET_KEY = 'DEMO_KEY' |
---|
49 | LOGO_URL = 'https://iuokada.waeup.org/static_custom/iou_logo.png' |
---|
50 | |
---|
51 | def 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 | |
---|
61 | def 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 | |
---|
90 | def 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 | |
---|
108 | class 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 | response = query_history(HOST, self.terminal_id, |
---|
116 | transaction_id, HTTPS, 'http://xxxxx') |
---|
117 | self.assertTrue( |
---|
118 | 'Transaction with the given transaction_id (%s) not found' |
---|
119 | % transaction_id in response) |
---|
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 | query_response = query_history(HOST, self.terminal_id, |
---|
126 | transaction_id, HTTPS, 'http://xxxxx') |
---|
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 query_response) |
---|
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 | query_response = query_history(HOST, self.terminal_id, |
---|
142 | transaction_id, HTTPS, 'http://xxxxx') |
---|
143 | # Now eTranzact is redirecting but the response is still useless |
---|
144 | print query_response |
---|
145 | self.assertTrue('Redirecting...' in query_response) |
---|
146 | return |
---|
147 | |
---|
148 | class 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 | # create an eTranzact transaction. |
---|
180 | logo_url = 'http://xxxx' |
---|
181 | responseurl = self.payment_url + '/receive_etranzact' |
---|
182 | amount = "%.1f" % self.payment.amount_auth |
---|
183 | customer = self.applicant |
---|
184 | hashargs = amount + TERMINAL_ID + self.payment.p_id \ |
---|
185 | + responseurl + 'DEMO_KEY' |
---|
186 | hashvalue = hashlib.md5(hashargs).hexdigest() |
---|
187 | post_caller(HOST, HTTPS, TERMINAL_ID, |
---|
188 | self.payment.p_id, responseurl, |
---|
189 | self.payment.amount_auth, customer.email, |
---|
190 | customer.phone, |
---|
191 | customer.display_fullname, hashvalue, |
---|
192 | logo_url) |
---|
193 | self.browser.open(self.payment_url + '/requery_history') |
---|
194 | # Means of testing end here. |
---|
195 | |
---|
196 | #hashargs = amount + TERMINAL_ID + 'etz1568638104web' \ |
---|
197 | # + responseurl + 'DEMO_KEY' |
---|
198 | #hashvalue = hashlib.md5(hashargs).hexdigest() |
---|
199 | #self.browser.open(self.payment_url + '/requery_history') |
---|
200 | #self.browser.getControl("Requery now").click() |
---|
201 | #import pdb; pdb.set_trace() |
---|
202 | |
---|
203 | |
---|
204 | return |
---|
205 | |
---|
206 | |
---|