[16639] | 1 | ## $Id: tests.py 16845 2022-02-28 17:30:07Z henrik $ |
---|
[15614] | 2 | ## |
---|
| 3 | ## Copyright (C) 2011 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 | from datetime import datetime, date, timedelta |
---|
| 20 | from zope.component import createObject, getUtility |
---|
| 21 | from zope.catalog.interfaces import ICatalog |
---|
| 22 | from hurry.workflow.interfaces import IWorkflowState |
---|
| 23 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
| 24 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
| 25 | from waeup.kofa.configuration import SessionConfiguration |
---|
[16615] | 26 | from kofacustom.edocons.testing import FunctionalLayer |
---|
[15614] | 27 | |
---|
| 28 | # Also run tests that send requests to external servers? |
---|
| 29 | # If you enable this, please make sure the external services |
---|
| 30 | # do exist really and are not bothered by being spammed by a test programme. |
---|
| 31 | EXTERNAL_TESTS = False |
---|
| 32 | |
---|
| 33 | def external_test(func): |
---|
| 34 | if not EXTERNAL_TESTS: |
---|
| 35 | myself = __file__ |
---|
| 36 | if myself.endswith('.pyc'): |
---|
| 37 | myself = myself[:-2] |
---|
| 38 | print "WARNING: external tests are skipped!" |
---|
| 39 | print "WARNING: edit %s to enable them." % myself |
---|
| 40 | return |
---|
| 41 | return func |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
| 45 | """Tests for the Interswitch payment gateway. |
---|
| 46 | """ |
---|
| 47 | |
---|
| 48 | layer = FunctionalLayer |
---|
| 49 | |
---|
| 50 | def setUp(self): |
---|
| 51 | super(InterswitchTestsStudents, self).setUp() |
---|
[16591] | 52 | self.app['configuration']['2004'].interswitch_enabled = True |
---|
[15614] | 53 | |
---|
| 54 | def test_interswitch_form(self): |
---|
| 55 | # Manager can access InterswitchForm |
---|
| 56 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 57 | self.browser.open(self.payments_path) |
---|
| 58 | IWorkflowState(self.student).setState('cleared') |
---|
| 59 | self.student.nationality = u'NG' |
---|
| 60 | self.browser.open(self.payments_path + '/addop') |
---|
| 61 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 62 | self.browser.getControl("Create ticket").click() |
---|
| 63 | self.assertMatches('...ticket created...', |
---|
| 64 | self.browser.contents) |
---|
[16591] | 65 | self.browser.open(self.payments_path) |
---|
[15614] | 66 | ctrl = self.browser.getControl(name='val_id') |
---|
| 67 | self.value = ctrl.options[0] |
---|
| 68 | self.browser.getLink(self.value).click() |
---|
| 69 | self.assertMatches('...Amount Authorized...', |
---|
| 70 | self.browser.contents) |
---|
| 71 | self.assertTrue( |
---|
| 72 | '<span>40000.0</span>' in self.browser.contents) |
---|
| 73 | self.payment_url = self.browser.url |
---|
[16698] | 74 | self.browser.getLink("Pay via Interswitch CollegePAY", index=0).click() |
---|
[16845] | 75 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="Default_Payable_MX26002" />...', |
---|
[15614] | 76 | self.browser.contents) |
---|
| 77 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 78 | self.assertTrue( |
---|
[16642] | 79 | '<input type="hidden" name="amount" value="4030000" />' in |
---|
[15614] | 80 | self.browser.contents) |
---|
| 81 | self.assertTrue( |
---|
[16709] | 82 | 'item_name="School Fee (total amount)" item_amt="4000000" bank_id="31" acct_num="1489560452"' in |
---|
[15614] | 83 | self.browser.contents) |
---|
[16704] | 84 | self.browser.open(self.payments_path + '/addop') |
---|
| 85 | self.browser.getControl(name="form.p_category").value = ['schoolfee_1'] |
---|
| 86 | self.browser.getControl("Create ticket").click() |
---|
| 87 | self.assertMatches('...ticket created...', |
---|
| 88 | self.browser.contents) |
---|
| 89 | self.browser.open(self.payments_path) |
---|
| 90 | ctrl = self.browser.getControl(name='val_id') |
---|
| 91 | self.value = ctrl.options[1] |
---|
| 92 | self.browser.getLink(self.value).click() |
---|
| 93 | self.assertMatches('...Amount Authorized...', |
---|
| 94 | self.browser.contents) |
---|
| 95 | self.assertTrue( |
---|
[16709] | 96 | '<span>26400.0</span>' in self.browser.contents) |
---|
[16704] | 97 | self.payment_url = self.browser.url |
---|
| 98 | self.browser.getLink("Pay via Interswitch CollegePAY", index=0).click() |
---|
[16845] | 99 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="Default_Payable_MX26002" />...', |
---|
[16704] | 100 | self.browser.contents) |
---|
| 101 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 102 | self.assertTrue( |
---|
[16709] | 103 | '<input type="hidden" name="amount" value="2670000" />' in |
---|
[16704] | 104 | self.browser.contents) |
---|
| 105 | self.assertTrue( |
---|
[16709] | 106 | 'item_name="School Fee (66% - 1st instalment)" item_amt="2640000" bank_id="31" acct_num="1489560452"' in |
---|
[16704] | 107 | self.browser.contents) |
---|
[15614] | 108 | |
---|
| 109 | def test_interswitch_form_ticket_expired(self): |
---|
| 110 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 111 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
| 112 | acc_payment.p_state = 'unpaid' |
---|
[16704] | 113 | acc_payment.p_category = 'schoolfee' |
---|
[15614] | 114 | acc_payment.p_id = 'xyz' |
---|
| 115 | acc_payment.pay_item_id = '123' |
---|
[15756] | 116 | acc_payment.amount_auth = 1000.0 |
---|
[15614] | 117 | self.student['payments']['xyz'] = acc_payment |
---|
| 118 | self.browser.open(self.payments_path + '/xyz') |
---|
[16698] | 119 | self.browser.getLink("Pay via Interswitch CollegePAY", index=0).click() |
---|
[16845] | 120 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="Default_Payable_MX26002" />...', |
---|
[15614] | 121 | self.browser.contents) |
---|
| 122 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 123 | self.browser.contents) |
---|
| 124 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 125 | self.assertMatches( |
---|
[15756] | 126 | '...<input type="hidden" name="amount" value="115000" />...', |
---|
[15614] | 127 | self.browser.contents) |
---|
| 128 | delta = timedelta(days=8) |
---|
| 129 | acc_payment.creation_date -= delta |
---|
| 130 | self.browser.open(self.payments_path + '/xyz') |
---|
[15756] | 131 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15614] | 132 | self.assertMatches( |
---|
| 133 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 134 | self.browser.contents) |
---|
| 135 | delta = timedelta(days=2) |
---|
| 136 | acc_payment.creation_date += delta |
---|
| 137 | self.browser.open(self.payments_path + '/xyz') |
---|
[15756] | 138 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15614] | 139 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 140 | self.browser.contents) |
---|
| 141 | |
---|
| 142 | @external_test |
---|
| 143 | def test_webservice(self): |
---|
| 144 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 145 | self.browser.open(self.payments_path) |
---|
| 146 | IWorkflowState(self.student).setState('cleared') |
---|
| 147 | self.student.nationality = u'NG' |
---|
| 148 | self.browser.open(self.payments_path + '/addop') |
---|
| 149 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 150 | self.browser.getControl("Create ticket").click() |
---|
| 151 | self.assertMatches('...ticket created...', |
---|
| 152 | self.browser.contents) |
---|
| 153 | ctrl = self.browser.getControl(name='val_id') |
---|
| 154 | self.value = ctrl.options[0] |
---|
| 155 | self.browser.getLink(self.value).click() |
---|
| 156 | self.payment_url = self.browser.url |
---|
| 157 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 158 | # and gateway_amt |
---|
| 159 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 160 | # Now we can call the webservice |
---|
| 161 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 162 | self.assertMatches('...Unsuccessful callback...', |
---|
| 163 | self.browser.contents) |
---|
| 164 | # The payment is now in state failed ... |
---|
| 165 | self.assertMatches('...<span>Failed</span>...', |
---|
| 166 | self.browser.contents) |
---|
| 167 | # ... and the catalog has been updated |
---|
| 168 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 169 | results = list( |
---|
| 170 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 171 | self.assertEqual(len(results), 1) |
---|
| 172 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 173 | |
---|
| 174 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 175 | # live system. This is definitely not an appropriate |
---|
| 176 | # solution for testing, but we have no choice since |
---|
| 177 | # Interswitch doesn't provide any interface |
---|
| 178 | # for testing. |
---|
| 179 | payment = self.student['payments'][self.value] |
---|
| 180 | payment.p_id = 'p3547789850240' |
---|
| 181 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 182 | self.assertMatches('...Callback amount does not match...', |
---|
| 183 | self.browser.contents) |
---|
| 184 | # The payment is now in state failed ... |
---|
| 185 | self.assertMatches('...<span>Failed</span>...', |
---|
| 186 | self.browser.contents) |
---|
| 187 | # Let's replace the amount autorized with the amount of the |
---|
| 188 | # live system payment |
---|
| 189 | payment.amount_auth = payment.r_amount_approved |
---|
| 190 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 191 | self.assertMatches('...Successful payment...', |
---|
| 192 | self.browser.contents) |
---|
| 193 | # The payment is now in state paid ... |
---|
| 194 | self.assertMatches('...<span>Paid</span>...', |
---|
| 195 | self.browser.contents) |
---|
| 196 | # ... and the catalog has been updated |
---|
| 197 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 198 | results = list( |
---|
| 199 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 200 | self.assertEqual(len(results), 1) |
---|
| 201 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 202 | # Approval is logged in students.log ... |
---|
| 203 | logfile = os.path.join( |
---|
| 204 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 205 | logcontent = open(logfile).read() |
---|
| 206 | self.assertTrue( |
---|
| 207 | 'zope.mgr - ' |
---|
[16615] | 208 | 'kofacustom.edocons.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
[15614] | 209 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
| 210 | in logcontent) |
---|
| 211 | # ... and in payments.log |
---|
| 212 | logfile = os.path.join( |
---|
| 213 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 214 | logcontent = open(logfile).read() |
---|
| 215 | self.assertTrue( |
---|
| 216 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
| 217 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
| 218 | in logcontent) |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 222 | """Tests for the Interswitch payment gateway. |
---|
| 223 | """ |
---|
| 224 | |
---|
| 225 | layer = FunctionalLayer |
---|
| 226 | |
---|
| 227 | def setUp(self): |
---|
| 228 | super(InterswitchTestsApplicants, self).setUp() |
---|
| 229 | configuration = SessionConfiguration() |
---|
| 230 | configuration.academic_session = datetime.now().year - 2 |
---|
| 231 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
| 232 | self.configuration = configuration |
---|
| 233 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 234 | self.browser.open(self.manage_path) |
---|
| 235 | #IWorkflowState(self.student).setState('started') |
---|
[16655] | 236 | self.browser.getControl(name="form.firstname").value = 'John' |
---|
| 237 | self.browser.getControl(name="form.middlename").value = 'Anthony' |
---|
| 238 | self.browser.getControl(name="form.lastname").value = 'Tester' |
---|
| 239 | #self.browser.getControl(name="form.course1").value = ['CERT1'] |
---|
| 240 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
| 241 | self.browser.getControl(name="form.sex").value = ['m'] |
---|
| 242 | self.browser.getControl(name="form.email").value = 'xx@yy.zz' |
---|
[15614] | 243 | self.applicantscontainer.application_fee = 1000.0 |
---|
| 244 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 245 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 246 | self.browser.getControl("Save").click() |
---|
| 247 | self.browser.getControl("Add online").click() |
---|
| 248 | self.assertMatches('...ticket created...', |
---|
| 249 | self.browser.contents) |
---|
| 250 | #ctrl = self.browser.getControl(name='val_id') |
---|
| 251 | #value = ctrl.options[0] |
---|
| 252 | #self.browser.getLink(value).click() |
---|
| 253 | self.assertMatches('...Amount Authorized...', |
---|
| 254 | self.browser.contents) |
---|
| 255 | self.assertMatches( |
---|
| 256 | '...<span>1000.0</span>...', |
---|
| 257 | self.browser.contents) |
---|
| 258 | self.payment_url = self.browser.url |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | def test_interswitch_form(self): |
---|
| 262 | # Manager can access InterswitchForm |
---|
[15756] | 263 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15614] | 264 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 265 | self.browser.contents) |
---|
| 266 | self.assertMatches( |
---|
| 267 | '...<input type="hidden" name="amount" value="100000" />...', |
---|
| 268 | self.browser.contents) |
---|
| 269 | delta = timedelta(days=8) |
---|
| 270 | self.applicant.values()[0].creation_date -= delta |
---|
| 271 | self.browser.open(self.payment_url) |
---|
[15756] | 272 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15614] | 273 | self.assertMatches( |
---|
| 274 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 275 | self.browser.contents) |
---|
| 276 | |
---|
| 277 | @external_test |
---|
| 278 | def test_webservice(self): |
---|
| 279 | |
---|
| 280 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 281 | self.assertMatches('...Unsuccessful callback...', |
---|
| 282 | self.browser.contents) |
---|
| 283 | # The payment is now in state failed |
---|
| 284 | self.assertMatches('...<span>Failed</span>...', |
---|
| 285 | self.browser.contents) |
---|