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