[15586] | 1 | ## $Id: tests.py 15596 2019-09-20 06:51:47Z 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 | ## |
---|
| 18 | import os |
---|
| 19 | import unittest |
---|
| 20 | import random |
---|
| 21 | import json |
---|
| 22 | import hashlib |
---|
[15589] | 23 | import httplib |
---|
| 24 | from urllib import urlencode |
---|
[15585] | 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 ( |
---|
[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 | |
---|
| 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 | |
---|
[15589] | 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 | |
---|
[15585] | 108 | class HelperTests(unittest.TestCase): |
---|
| 109 | |
---|
| 110 | terminal_id = TERMINAL_ID |
---|
| 111 | |
---|
| 112 | @external_test |
---|
[15589] | 113 | def test_query_history(self): |
---|
[15585] | 114 | transaction_id = str(random.randint(100000000, 999999999)) |
---|
[15596] | 115 | raw, formvars = query_history(HOST, self.terminal_id, |
---|
| 116 | transaction_id, HTTPS) |
---|
[15593] | 117 | self.assertTrue( |
---|
[15585] | 118 | 'Transaction with the given transaction_id (%s) not found' |
---|
[15596] | 119 | % transaction_id in raw) |
---|
[15585] | 120 | # Okay, let's create a transaction |
---|
[15589] | 121 | caller_response = create_transaction(transaction_id) |
---|
[15585] | 122 | self.assertEqual(caller_response, None) |
---|
| 123 | # It seems that the transaction has been created but we don't get a |
---|
| 124 | # useful response |
---|
[15596] | 125 | raw, formvars = query_history(HOST, self.terminal_id, |
---|
| 126 | transaction_id, HTTPS) |
---|
[15593] | 127 | self.assertTrue( |
---|
| 128 | '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\r\n ' |
---|
[15596] | 129 | '"http://www.w3.org/TR/html4/loose.dtd">' in raw) |
---|
[15585] | 130 | # The same, an 'empty' response obviously means that the transaction |
---|
[15593] | 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' |
---|
[15596] | 141 | raw, formvars = query_history(HOST, self.terminal_id, |
---|
| 142 | transaction_id, HTTPS) |
---|
[15593] | 143 | # Now eTranzact is redirecting but the response is still useless |
---|
[15596] | 144 | self.assertTrue('Redirecting...' in raw) |
---|
[15593] | 145 | print query_response |
---|
[15585] | 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 |
---|
[15589] | 175 | def test_views(self): |
---|
| 176 | # Manager can access eTranzact form |
---|
[15585] | 177 | self.browser.getLink("Pay via eTranzact").click() |
---|
| 178 | self.assertTrue("Pay now" in self.browser.contents) |
---|
[15593] | 179 | |
---|
[15585] | 180 | return |
---|
| 181 | |
---|
| 182 | |
---|