1 | ## $Id: tests.py 14793 2017-08-12 06:29:57Z 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 | from datetime import datetime, timedelta, date |
---|
23 | from zope.component import createObject, getUtility |
---|
24 | from zope.catalog.interfaces import ICatalog |
---|
25 | from hurry.workflow.interfaces import IWorkflowState |
---|
26 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
27 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
28 | from waeup.kofa.configuration import SessionConfiguration |
---|
29 | from kofacustom.nigeria.students.payments import NigeriaStudentOnlinePayment |
---|
30 | from kofacustom.nigeria.testing import FunctionalLayer |
---|
31 | |
---|
32 | from kofacustom.nigeria.remita.helpers import ( |
---|
33 | get_JSON_POST_response, get_payment_status_via_rrr, query_remita) |
---|
34 | |
---|
35 | # Also run tests that send requests to external servers? |
---|
36 | # If you enable this, please make sure the external services |
---|
37 | # do exist really and are not bothered by being spammed by a test programme. |
---|
38 | EXTERNAL_TESTS = True |
---|
39 | |
---|
40 | MERCHANTID = '2547916' |
---|
41 | HOST = 'www.remitademo.net' |
---|
42 | HTTPS = False |
---|
43 | API_KEY = '1946' |
---|
44 | SERVICETYPEID = '4430731' |
---|
45 | |
---|
46 | def external_test(func): |
---|
47 | if not EXTERNAL_TESTS: |
---|
48 | myself = __file__ |
---|
49 | if myself.endswith('.pyc'): |
---|
50 | myself = myself[:-1] |
---|
51 | print "WARNING: external tests are skipped!" |
---|
52 | print "WARNING: edit %s to enable them." % myself |
---|
53 | return |
---|
54 | return func |
---|
55 | |
---|
56 | class HelperTests(unittest.TestCase): |
---|
57 | |
---|
58 | merchantId = MERCHANTID |
---|
59 | host = HOST |
---|
60 | https = HTTPS |
---|
61 | api_key = API_KEY |
---|
62 | serviceTypeId = SERVICETYPEID |
---|
63 | |
---|
64 | responseurl = 'http://xxxx' |
---|
65 | |
---|
66 | url = '/remita/ecomm/split/init.reg' # /remita/ecomm/v2/init.reg |
---|
67 | lineitems = ( |
---|
68 | {"lineItemsId":"itemid1","beneficiaryName":"Oshadami Mike", |
---|
69 | "beneficiaryAccount":"6020067886","bankCode":"011", |
---|
70 | "beneficiaryAmount":"500","deductFeeFrom":"1"}, |
---|
71 | {"lineItemsId":"itemid2","beneficiaryName":"Ogunseye Mujib", |
---|
72 | "beneficiaryAccount":"0360883515","bankCode":"050", |
---|
73 | "beneficiaryAmount":"500","deductFeeFrom":"0"} |
---|
74 | ) |
---|
75 | amount = 1000.0 |
---|
76 | |
---|
77 | def _get_transaction_data(self): |
---|
78 | self.orderId = str(random.randint(100000000, 999999999)) |
---|
79 | resp = get_JSON_POST_response( |
---|
80 | merchantId=self.merchantId, serviceTypeId=self.serviceTypeId, |
---|
81 | api_key=self.api_key, orderId=self.orderId, |
---|
82 | amount=self.amount, responseurl=self.responseurl, |
---|
83 | host=self.host, url=self.url, https=self.https, |
---|
84 | fullname='Anton Meier', email='am@xxx.de', |
---|
85 | lineitems=self.lineitems) |
---|
86 | self.rrr = resp['RRR'].rstrip() |
---|
87 | |
---|
88 | @external_test |
---|
89 | def test_get_JSON_POST_response(self): |
---|
90 | self._get_transaction_data() |
---|
91 | resp = get_JSON_POST_response( |
---|
92 | merchantId=self.merchantId, serviceTypeId=self.serviceTypeId, |
---|
93 | api_key=self.api_key, orderId=self.orderId, |
---|
94 | amount=self.amount, responseurl=self.responseurl, |
---|
95 | host=self.host, url=self.url, https=self.https, |
---|
96 | fullname='Anton Meier', email='am@xxx.de', |
---|
97 | lineitems=self.lineitems) |
---|
98 | assert resp == { |
---|
99 | u'status': u'RRR Already Exist for the orderId', |
---|
100 | u'orderID': self.orderId, |
---|
101 | u'RRR': self.rrr + ' ', # strange trailing whitespace which |
---|
102 | # obviously does not belong to the RRR |
---|
103 | u'statuscode': u'055'} |
---|
104 | |
---|
105 | resp = get_payment_status_via_rrr( |
---|
106 | merchantId=self.merchantId, |
---|
107 | api_key=self.api_key, |
---|
108 | RRR=self.rrr, |
---|
109 | host=self.host, |
---|
110 | https=self.https, |
---|
111 | ) |
---|
112 | assert resp['orderId'] == self.orderId |
---|
113 | assert resp['status'] == '021' |
---|
114 | assert resp['amount'] == self.amount |
---|
115 | assert resp['lineitems'] == [ |
---|
116 | {u'status': u'021', u'lineItemsId': u'itemid1'}, |
---|
117 | {u'status': u'021', u'lineItemsId': u'itemid2'}] |
---|
118 | assert resp['RRR'] == self.rrr |
---|
119 | assert resp['message'] == u'Transaction Pending' |
---|
120 | |
---|
121 | class RemitaTestsStudents(StudentsFullSetup): |
---|
122 | """Tests for the Remita payment gateway. |
---|
123 | """ |
---|
124 | |
---|
125 | layer = FunctionalLayer |
---|
126 | |
---|
127 | merchantId = MERCHANTID |
---|
128 | host = HOST |
---|
129 | https = HTTPS |
---|
130 | api_key = API_KEY |
---|
131 | serviceTypeId = SERVICETYPEID |
---|
132 | |
---|
133 | responseurl = 'http://xxxx' |
---|
134 | |
---|
135 | # successful transaction |
---|
136 | # (hopefully this transaction remains in the Remita database) |
---|
137 | orderId = '3456346346' |
---|
138 | rrr = u'280007640804' |
---|
139 | amount = 1000.0 |
---|
140 | |
---|
141 | # pending transaction |
---|
142 | #orderId_p = '987698769876' |
---|
143 | rrr_p = u'320007640976' |
---|
144 | |
---|
145 | def setUp(self): |
---|
146 | super(RemitaTestsStudents, self).setUp() |
---|
147 | self.app['configuration']['2004'].remita_enabled = True |
---|
148 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
149 | self.browser.open(self.payments_path) |
---|
150 | IWorkflowState(self.student).setState('cleared') |
---|
151 | self.student.nationality = u'NG' |
---|
152 | self.browser.open(self.payments_path + '/addop') |
---|
153 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
154 | self.browser.getControl("Create ticket").click() |
---|
155 | self.assertMatches('...ticket created...', |
---|
156 | self.browser.contents) |
---|
157 | ctrl = self.browser.getControl(name='val_id') |
---|
158 | self.value = ctrl.options[0] |
---|
159 | self.browser.getLink(self.value).click() |
---|
160 | self.assertMatches('...Amount Authorized...', |
---|
161 | self.browser.contents) |
---|
162 | self.assertTrue('<span>40000.0</span>', self.browser.contents) |
---|
163 | self.payment_url = self.browser.url |
---|
164 | self.payment = self.student['payments'][self.value] |
---|
165 | |
---|
166 | @external_test |
---|
167 | def test_query_remita(self): |
---|
168 | #requery pending transaction |
---|
169 | qr = query_remita( |
---|
170 | self.payment, |
---|
171 | merchantId=self.merchantId, |
---|
172 | api_key=self.api_key, |
---|
173 | RRR=self.rrr_p, |
---|
174 | host=self.host, |
---|
175 | https=self.https, |
---|
176 | verify=False) |
---|
177 | assert qr == ( |
---|
178 | False, |
---|
179 | u'Unsuccessful response: ${a}', |
---|
180 | u'unsuccessful response for schoolfee payment %s: Transaction Pending' |
---|
181 | % self.payment.p_id) |
---|
182 | |
---|
183 | #requery successful transaction |
---|
184 | self.payment.amount_auth = 1000.0 |
---|
185 | self.payment.p_id = self.orderId |
---|
186 | qr = query_remita( |
---|
187 | self.payment, |
---|
188 | merchantId=self.merchantId, |
---|
189 | api_key=self.api_key, |
---|
190 | RRR=self.rrr, |
---|
191 | host=self.host, |
---|
192 | https=self.https, |
---|
193 | verify=False) |
---|
194 | assert qr[0] == True |
---|
195 | assert qr[1] == u'Successful response received' |
---|
196 | |
---|
197 | @external_test |
---|
198 | def test_student_remita_form(self): |
---|
199 | # Manager can access Remita form |
---|
200 | self.browser.getLink("Pay via Remita").click() |
---|
201 | # The RRR has been retrieved |
---|
202 | self.assertTrue('<input name="rrr" type="hidden" value="%s">' % self.rrr |
---|
203 | in self.browser.contents) |
---|
204 | self.assertEqual(self.payment.r_pay_reference, self.rrr) |
---|
205 | self.assertEqual(self.payment.r_company, 'remita') |
---|
206 | # Retrieval has been logged |
---|
207 | logfile = os.path.join( |
---|
208 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
209 | logcontent = open(logfile).read() |
---|
210 | self.assertTrue( |
---|
211 | 'zope.mgr - kofacustom.nigeria.remita.studentsbrowser.RemitaPageStudent' |
---|
212 | ' - K1000000 - RRR retrieved: %s, ServiceTypeId: %s' |
---|
213 | % (self.rrr, self.serviceTypeId) in logcontent) |
---|
214 | # Means of testing end here. |
---|
215 | return |
---|
216 | |
---|
217 | @external_test |
---|
218 | def test_requery_verify_payment_status(self): |
---|
219 | # Manager can access Remita form |
---|
220 | self.browser.getLink("Requery Remita Payment Status").click() |
---|
221 | self.assertTrue('Remita Retrieval Reference not found.' |
---|
222 | in self.browser.contents) |
---|
223 | self.payment.r_pay_reference = self.rrr |
---|
224 | self.browser.getLink("Requery Remita Payment Status").click() |
---|
225 | self.assertTrue('Response amount does not match.' |
---|
226 | in self.browser.contents) |
---|
227 | self.payment.amount_auth = self.amount |
---|
228 | self.browser.getLink("Requery Remita Payment Status").click() |
---|
229 | self.assertTrue('Response order id does not match.' |
---|
230 | in self.browser.contents) |
---|
231 | self.payment.p_id = self.orderId |
---|
232 | self.browser.getLink("Requery Remita Payment Status").click() |
---|
233 | self.assertTrue('Successful payment' in self.browser.contents) |
---|
234 | self.assertEqual(self.payment.r_desc, 'Approved') |
---|
235 | self.assertEqual(self.payment.r_amount_approved , 1000.0) |
---|
236 | self.assertEqual(self.payment.r_code, '01') |
---|
237 | self.browser.getLink("Verify Remita Payment Status").click() |
---|
238 | self.assertTrue('Successful response received' in self.browser.contents) |
---|
239 | return |
---|
240 | |
---|
241 | class RemitaTestsWebserviceStudent(StudentsFullSetup): |
---|
242 | """Tests for the Remita payment gateway. |
---|
243 | """ |
---|
244 | layer = FunctionalLayer |
---|
245 | |
---|
246 | merchantId = MERCHANTID |
---|
247 | host = HOST |
---|
248 | https = HTTPS |
---|
249 | api_key = API_KEY |
---|
250 | |
---|
251 | responseurl = 'http://xxxx' |
---|
252 | |
---|
253 | # successful transaction |
---|
254 | # (hopefully this transaction remains in the Remita database) |
---|
255 | orderId = '3456346346' |
---|
256 | rrr = u'280007640804' |
---|
257 | amount = 1000.0 |
---|
258 | |
---|
259 | @external_test |
---|
260 | def test_payment_notification_listener_student(self): |
---|
261 | payment = createObject('waeup.StudentOnlinePayment') |
---|
262 | payment.p_category = u'schoolfee' |
---|
263 | payment.p_session = self.student.current_session |
---|
264 | payment.p_item = u'My Certificate' |
---|
265 | payment.p_id = self.orderId |
---|
266 | self.student['payments']['anykey'] = payment |
---|
267 | data = [{'orderRef': self.orderId, 'rrr': self.rrr},] |
---|
268 | # Send POST request with wrong payment amount |
---|
269 | payment.amount_auth = 2000.0 |
---|
270 | self.browser.post('http://localhost/app/paymentnotificationlistener', |
---|
271 | json.dumps(data), 'application/json; charset=utf-8') |
---|
272 | self.assertEqual('0 (1)', self.browser.contents) |
---|
273 | # Send POST request with correct payment amount |
---|
274 | payment.amount_auth = self.amount |
---|
275 | self.browser.post('http://localhost/app/paymentnotificationlistener', |
---|
276 | json.dumps(data), 'application/json; charset=utf-8') |
---|
277 | self.assertEqual('1 (1)', self.browser.contents) |
---|
278 | logfile = os.path.join( |
---|
279 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
280 | logcontent = open(logfile).read() |
---|
281 | self.assertTrue( |
---|
282 | 'zope.anybody - kofacustom.nigeria.remita.webservices.PaymentNotificationListenerWebservice' |
---|
283 | ' - K1000000 - valid response for schoolfee payment %s: ' |
---|
284 | % self.orderId in logcontent) |
---|
285 | self.assertTrue( |
---|
286 | 'zope.anybody - kofacustom.nigeria.remita.webservices.PaymentNotificationListenerWebservice' |
---|
287 | ' - K1000000 - successful schoolfee payment: %s' |
---|
288 | % self.orderId in logcontent) |
---|
289 | logfile = os.path.join( |
---|
290 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
291 | logcontent = open(logfile).read() |
---|
292 | self.assertTrue( |
---|
293 | 'zope.anybody - PaymentNotificationListenerWebservice response: ' |
---|
294 | '[{"rrr": "%s", "orderRef": "%s"}]' |
---|
295 | % (self.rrr, self.orderId) in logcontent) |
---|
296 | return |
---|
297 | |
---|
298 | class RemitaTestsWebserviceApplicant(ApplicantsFullSetup): |
---|
299 | """Tests for the Remita payment gateway. |
---|
300 | """ |
---|
301 | layer = FunctionalLayer |
---|
302 | |
---|
303 | merchantId = MERCHANTID |
---|
304 | host = HOST |
---|
305 | https = HTTPS |
---|
306 | api_key = API_KEY |
---|
307 | |
---|
308 | responseurl = 'http://xxxx' |
---|
309 | |
---|
310 | # successful transaction |
---|
311 | # (hopefully this transaction remains in the Remita database) |
---|
312 | orderId = '3456346346' |
---|
313 | rrr = u'280007640804' |
---|
314 | amount = 1000.0 |
---|
315 | |
---|
316 | @external_test |
---|
317 | def test_payment_notification_listener_applicant(self): |
---|
318 | self.applicantscontainer.application_fee = self.amount |
---|
319 | payment = createObject('waeup.ApplicantOnlinePayment') |
---|
320 | payment.p_category = u'application' |
---|
321 | payment.p_session = self.applicantscontainer.year |
---|
322 | payment.p_item = u'My Certificate' |
---|
323 | payment.p_id = self.orderId |
---|
324 | payment.amount_auth = self.amount |
---|
325 | self.applicant['anykey'] = payment |
---|
326 | IWorkflowState(self.applicant).setState('started') |
---|
327 | # Send POST request |
---|
328 | data = [{'orderRef': self.orderId, 'rrr': self.rrr},] |
---|
329 | self.browser.post('http://localhost/app/paymentnotificationlistener', |
---|
330 | json.dumps(data), 'application/json; charset=utf-8') |
---|
331 | self.assertEqual('1 (1)', self.browser.contents) |
---|
332 | logfile = os.path.join( |
---|
333 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
334 | logcontent = open(logfile).read() |
---|
335 | self.assertTrue( |
---|
336 | 'zope.anybody - kofacustom.nigeria.remita.webservices.PaymentNotificationListenerWebservice' |
---|
337 | ' - %s - valid response for application payment %s: ' |
---|
338 | % (self.applicant.applicant_id, self.orderId) in logcontent) |
---|
339 | self.assertTrue( |
---|
340 | 'zope.anybody - kofacustom.nigeria.remita.webservices.PaymentNotificationListenerWebservice' |
---|
341 | ' - %s - successful payment: %s' |
---|
342 | % (self.applicant.applicant_id, self.orderId) in logcontent) |
---|
343 | return |
---|
344 | |
---|
345 | class RemitaTestsApplicants(ApplicantsFullSetup): |
---|
346 | """Tests for the Remita payment gateway. |
---|
347 | """ |
---|
348 | |
---|
349 | layer = FunctionalLayer |
---|
350 | |
---|
351 | # successful transaction |
---|
352 | # (hopefully this transaction remains in the Remita database) |
---|
353 | #orderId = '3456346346' |
---|
354 | rrr = u'280007640804' |
---|
355 | serviceTypeId = SERVICETYPEID |
---|
356 | #amount = 1000.0 |
---|
357 | |
---|
358 | def setUp(self): |
---|
359 | super(RemitaTestsApplicants, self).setUp() |
---|
360 | configuration = SessionConfiguration() |
---|
361 | configuration.academic_session = datetime.now().year - 2 |
---|
362 | configuration.remita_enabled = True |
---|
363 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
364 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
365 | self.browser.open(self.manage_path) |
---|
366 | #IWorkflowState(self.student).setState('started') |
---|
367 | super(RemitaTestsApplicants, self).fill_correct_values() |
---|
368 | self.applicantscontainer.application_fee = 1000.0 |
---|
369 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
370 | self.browser.getControl(name="transition").value = ['start'] |
---|
371 | self.browser.getControl("Save").click() |
---|
372 | self.browser.getControl("Add online").click() |
---|
373 | self.assertMatches('...ticket created...', |
---|
374 | self.browser.contents) |
---|
375 | self.payment = self.applicant.values()[0] |
---|
376 | self.payment_url = self.browser.url |
---|
377 | |
---|
378 | @external_test |
---|
379 | def test_applicant_remita_form(self): |
---|
380 | # Manager can access Remita form |
---|
381 | self.browser.getLink("Pay via Remita").click() |
---|
382 | # The RRR has been retrieved |
---|
383 | self.assertTrue('<input name="rrr" type="hidden" value="%s">' % self.rrr |
---|
384 | in self.browser.contents) |
---|
385 | self.assertEqual(self.payment.r_pay_reference, self.rrr) |
---|
386 | self.assertEqual(self.payment.r_company, 'remita') |
---|
387 | # Retrieval has been logged |
---|
388 | logfile = os.path.join( |
---|
389 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
390 | logcontent = open(logfile).read() |
---|
391 | self.assertTrue( |
---|
392 | 'zope.mgr - kofacustom.nigeria.remita.applicantsbrowser.RemitaPageApplicant' |
---|
393 | ' - %s - RRR retrieved: %s, ServiceTypeId: %s' |
---|
394 | % (self.applicant.applicant_id, self.rrr, self.serviceTypeId) |
---|
395 | in logcontent) |
---|
396 | # Means of testing end here. |
---|
397 | return |
---|