[10765] | 1 | ## $Id: tests.py 15105 2018-08-13 05:55:02Z henrik $ |
---|
| 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 |
---|
[11271] | 19 | from datetime import datetime, date, timedelta |
---|
[10765] | 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 |
---|
[15000] | 26 | from kofacustom.edopoly.testing import FunctionalLayer |
---|
[10765] | 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() |
---|
| 52 | |
---|
| 53 | def test_interswitch_form(self): |
---|
| 54 | # Manager can access InterswitchForm |
---|
[15015] | 55 | # School Fee |
---|
[10765] | 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() |
---|
[15056] | 63 | self.assertTrue( |
---|
[15072] | 64 | 'Pay ICT, Logbook and SIWESS fee(s) first.' in self.browser.contents) |
---|
[15056] | 65 | |
---|
| 66 | # Add required payments first. |
---|
| 67 | payment = createObject('waeup.StudentOnlinePayment') |
---|
| 68 | payment.p_state = 'paid' |
---|
| 69 | payment.p_category = 'ict_entre' |
---|
| 70 | payment.p_id = '123' |
---|
| 71 | payment.p_session = 2004 |
---|
| 72 | self.student['payments']['123'] = payment |
---|
| 73 | payment = createObject('waeup.StudentOnlinePayment') |
---|
| 74 | payment.p_state = 'paid' |
---|
| 75 | payment.p_category = 'logbook_combo' |
---|
| 76 | payment.p_id = '123' |
---|
| 77 | payment.p_session = 2004 |
---|
| 78 | self.student['payments']['234'] = payment |
---|
| 79 | payment = createObject('waeup.StudentOnlinePayment') |
---|
| 80 | payment.p_state = 'paid' |
---|
| 81 | payment.p_category = 'siwess_combo' |
---|
| 82 | payment.p_id = '123' |
---|
| 83 | payment.p_session = 2004 |
---|
| 84 | self.student['payments']['345'] = payment |
---|
| 85 | |
---|
| 86 | self.browser.open(self.payments_path + '/addop') |
---|
| 87 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 88 | self.browser.getControl("Create ticket").click() |
---|
[10765] | 89 | self.assertMatches('...ticket created...', |
---|
| 90 | self.browser.contents) |
---|
| 91 | ctrl = self.browser.getControl(name='val_id') |
---|
[15056] | 92 | self.value = ctrl.options[3] |
---|
[10765] | 93 | self.browser.getLink(self.value).click() |
---|
| 94 | self.assertMatches('...Amount Authorized...', |
---|
| 95 | self.browser.contents) |
---|
[14874] | 96 | self.assertTrue( |
---|
| 97 | '<span>40000.0</span>' in self.browser.contents) |
---|
[10765] | 98 | self.payment_url = self.browser.url |
---|
| 99 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[15015] | 100 | self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in |
---|
[10765] | 101 | self.browser.contents) |
---|
| 102 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
[14874] | 103 | self.assertTrue( |
---|
| 104 | '<input type="hidden" name="amount" value="4000000" />' in |
---|
[10765] | 105 | self.browser.contents) |
---|
[14874] | 106 | self.assertTrue( |
---|
[15105] | 107 | 'item_name="Tuition/Exams Fee" item_amt="3975000" bank_id="121" acct_num="0068241848"' in |
---|
[10765] | 108 | self.browser.contents) |
---|
[15015] | 109 | # Clearance Fee |
---|
| 110 | self.browser.open(self.payments_path + '/addop') |
---|
| 111 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
| 112 | self.browser.getControl("Create ticket").click() |
---|
| 113 | self.assertMatches('...ticket created...', |
---|
| 114 | self.browser.contents) |
---|
| 115 | ctrl = self.browser.getControl(name='val_id') |
---|
[15056] | 116 | self.value = ctrl.options[4] |
---|
[15015] | 117 | self.browser.getLink(self.value).click() |
---|
| 118 | self.assertTrue( |
---|
| 119 | '<span>3456.0</span>' in self.browser.contents) |
---|
| 120 | self.payment_url = self.browser.url |
---|
| 121 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 122 | self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in |
---|
| 123 | self.browser.contents) |
---|
| 124 | self.assertTrue( |
---|
| 125 | '<input type="hidden" name="amount" value="345600" />' in |
---|
| 126 | self.browser.contents) |
---|
| 127 | self.assertTrue( |
---|
[15030] | 128 | 'item_name="Acceptance Fee" item_amt="170600" bank_id="121" acct_num="0068241848"' in |
---|
[15015] | 129 | self.browser.contents) |
---|
[10765] | 130 | |
---|
[12979] | 131 | def test_interswitch_form_ticket_expired(self): |
---|
| 132 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 133 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
| 134 | acc_payment.p_state = 'unpaid' |
---|
| 135 | acc_payment.p_category = 'clearance' |
---|
| 136 | acc_payment.p_id = 'xyz' |
---|
| 137 | acc_payment.pay_item_id = '123' |
---|
| 138 | acc_payment.amount_auth = 876.0 |
---|
| 139 | self.student['payments']['xyz'] = acc_payment |
---|
| 140 | self.browser.open(self.payments_path + '/xyz') |
---|
| 141 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[15015] | 142 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="101" />...', |
---|
[12979] | 143 | self.browser.contents) |
---|
| 144 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 145 | self.browser.contents) |
---|
| 146 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 147 | self.assertMatches( |
---|
| 148 | '...<input type="hidden" name="amount" value="87600" />...', |
---|
| 149 | self.browser.contents) |
---|
[13020] | 150 | delta = timedelta(days=8) |
---|
[12979] | 151 | acc_payment.creation_date -= delta |
---|
| 152 | self.browser.open(self.payments_path + '/xyz') |
---|
| 153 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 154 | self.assertMatches( |
---|
| 155 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 156 | self.browser.contents) |
---|
[13020] | 157 | delta = timedelta(days=2) |
---|
[12979] | 158 | acc_payment.creation_date += delta |
---|
| 159 | self.browser.open(self.payments_path + '/xyz') |
---|
| 160 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 161 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 162 | self.browser.contents) |
---|
| 163 | |
---|
[10765] | 164 | @external_test |
---|
| 165 | def test_webservice(self): |
---|
| 166 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 167 | self.browser.open(self.payments_path) |
---|
| 168 | IWorkflowState(self.student).setState('cleared') |
---|
| 169 | self.student.nationality = u'NG' |
---|
| 170 | self.browser.open(self.payments_path + '/addop') |
---|
| 171 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 172 | self.browser.getControl("Create ticket").click() |
---|
| 173 | self.assertMatches('...ticket created...', |
---|
| 174 | self.browser.contents) |
---|
| 175 | ctrl = self.browser.getControl(name='val_id') |
---|
| 176 | self.value = ctrl.options[0] |
---|
| 177 | self.browser.getLink(self.value).click() |
---|
| 178 | self.payment_url = self.browser.url |
---|
| 179 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 180 | # and gateway_amt |
---|
| 181 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 182 | # Now we can call the webservice |
---|
| 183 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 184 | self.assertMatches('...Unsuccessful callback...', |
---|
| 185 | self.browser.contents) |
---|
| 186 | # The payment is now in state failed ... |
---|
| 187 | self.assertMatches('...<span>Failed</span>...', |
---|
| 188 | self.browser.contents) |
---|
| 189 | # ... and the catalog has been updated |
---|
| 190 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 191 | results = list( |
---|
| 192 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 193 | self.assertEqual(len(results), 1) |
---|
| 194 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 195 | |
---|
| 196 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 197 | # live system. This is definitely not an appropriate |
---|
| 198 | # solution for testing, but we have no choice since |
---|
| 199 | # Interswitch doesn't provide any interface |
---|
| 200 | # for testing. |
---|
| 201 | payment = self.student['payments'][self.value] |
---|
| 202 | payment.p_id = 'p3547789850240' |
---|
| 203 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 204 | self.assertMatches('...Callback amount does not match...', |
---|
| 205 | self.browser.contents) |
---|
| 206 | # The payment is now in state failed ... |
---|
| 207 | self.assertMatches('...<span>Failed</span>...', |
---|
| 208 | self.browser.contents) |
---|
| 209 | # Let's replace the amount autorized with the amount of the |
---|
| 210 | # live system payment |
---|
| 211 | payment.amount_auth = payment.r_amount_approved |
---|
| 212 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 213 | self.assertMatches('...Successful payment...', |
---|
| 214 | self.browser.contents) |
---|
| 215 | # The payment is now in state paid ... |
---|
| 216 | self.assertMatches('...<span>Paid</span>...', |
---|
| 217 | self.browser.contents) |
---|
| 218 | # ... and the catalog has been updated |
---|
| 219 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 220 | results = list( |
---|
| 221 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 222 | self.assertEqual(len(results), 1) |
---|
| 223 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 224 | # Approval is logged in students.log ... |
---|
| 225 | logfile = os.path.join( |
---|
| 226 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 227 | logcontent = open(logfile).read() |
---|
| 228 | self.assertTrue( |
---|
| 229 | 'zope.mgr - ' |
---|
[15000] | 230 | 'kofacustom.edopoly.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
[10765] | 231 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
| 232 | in logcontent) |
---|
| 233 | # ... and in payments.log |
---|
| 234 | logfile = os.path.join( |
---|
| 235 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 236 | logcontent = open(logfile).read() |
---|
| 237 | self.assertTrue( |
---|
| 238 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
| 239 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
| 240 | in logcontent) |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 244 | """Tests for the Interswitch payment gateway. |
---|
| 245 | """ |
---|
| 246 | |
---|
| 247 | layer = FunctionalLayer |
---|
| 248 | |
---|
| 249 | def setUp(self): |
---|
| 250 | super(InterswitchTestsApplicants, self).setUp() |
---|
[11271] | 251 | configuration = SessionConfiguration() |
---|
| 252 | configuration.academic_session = datetime.now().year - 2 |
---|
| 253 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
| 254 | self.configuration = configuration |
---|
[10765] | 255 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 256 | self.browser.open(self.manage_path) |
---|
| 257 | #IWorkflowState(self.student).setState('started') |
---|
| 258 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
| 259 | self.applicantscontainer.application_fee = 1000.0 |
---|
| 260 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 261 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 262 | self.browser.getControl("Save").click() |
---|
| 263 | self.browser.getControl("Add online").click() |
---|
| 264 | self.assertMatches('...ticket created...', |
---|
| 265 | self.browser.contents) |
---|
| 266 | #ctrl = self.browser.getControl(name='val_id') |
---|
| 267 | #value = ctrl.options[0] |
---|
| 268 | #self.browser.getLink(value).click() |
---|
| 269 | self.assertMatches('...Amount Authorized...', |
---|
| 270 | self.browser.contents) |
---|
| 271 | self.assertMatches( |
---|
| 272 | '...<span>1000.0</span>...', |
---|
| 273 | self.browser.contents) |
---|
| 274 | self.payment_url = self.browser.url |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | def test_interswitch_form(self): |
---|
| 278 | # Manager can access InterswitchForm |
---|
| 279 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 280 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 281 | self.browser.contents) |
---|
| 282 | self.assertMatches( |
---|
[11813] | 283 | '...<input type="hidden" name="amount" value="100000" />...', |
---|
[10765] | 284 | self.browser.contents) |
---|
[13020] | 285 | delta = timedelta(days=8) |
---|
[12979] | 286 | self.applicant.values()[0].creation_date -= delta |
---|
| 287 | self.browser.open(self.payment_url) |
---|
| 288 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 289 | self.assertMatches( |
---|
| 290 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 291 | self.browser.contents) |
---|
[10765] | 292 | |
---|
| 293 | @external_test |
---|
| 294 | def test_webservice(self): |
---|
| 295 | |
---|
| 296 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 297 | self.assertMatches('...Unsuccessful callback...', |
---|
| 298 | self.browser.contents) |
---|
| 299 | # The payment is now in state failed |
---|
| 300 | self.assertMatches('...<span>Failed</span>...', |
---|
| 301 | self.browser.contents) |
---|