[10765] | 1 | ## $Id: tests.py 14889 2017-11-06 08:11:13Z 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 |
---|
[14889] | 26 | from waeup.kofa.university.faculty import Faculty |
---|
| 27 | from waeup.kofa.university.department import Department |
---|
[14716] | 28 | from kofacustom.dspg.testing import FunctionalLayer |
---|
[10765] | 29 | |
---|
| 30 | # Also run tests that send requests to external servers? |
---|
| 31 | # If you enable this, please make sure the external services |
---|
| 32 | # do exist really and are not bothered by being spammed by a test programme. |
---|
| 33 | EXTERNAL_TESTS = False |
---|
| 34 | |
---|
[14732] | 35 | SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg') |
---|
| 36 | |
---|
[10765] | 37 | def external_test(func): |
---|
| 38 | if not EXTERNAL_TESTS: |
---|
| 39 | myself = __file__ |
---|
| 40 | if myself.endswith('.pyc'): |
---|
| 41 | myself = myself[:-2] |
---|
| 42 | print "WARNING: external tests are skipped!" |
---|
| 43 | print "WARNING: edit %s to enable them." % myself |
---|
| 44 | return |
---|
| 45 | return func |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
| 49 | """Tests for the Interswitch payment gateway. |
---|
| 50 | """ |
---|
| 51 | |
---|
| 52 | layer = FunctionalLayer |
---|
| 53 | |
---|
| 54 | def setUp(self): |
---|
| 55 | super(InterswitchTestsStudents, self).setUp() |
---|
| 56 | |
---|
| 57 | def test_interswitch_form(self): |
---|
| 58 | # Manager can access InterswitchForm |
---|
| 59 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 60 | self.browser.open(self.payments_path) |
---|
| 61 | IWorkflowState(self.student).setState('cleared') |
---|
| 62 | self.student.nationality = u'NG' |
---|
[14875] | 63 | # ND FT Non-Deltan Fresh Student School Fee |
---|
| 64 | self.certificate.study_mode = 'nd_ft' |
---|
| 65 | self.certificate.school_fee_3 = 30000.0 |
---|
[10765] | 66 | self.browser.open(self.payments_path + '/addop') |
---|
| 67 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 68 | self.browser.getControl("Create ticket").click() |
---|
| 69 | self.assertMatches('...ticket created...', |
---|
| 70 | self.browser.contents) |
---|
| 71 | ctrl = self.browser.getControl(name='val_id') |
---|
| 72 | self.value = ctrl.options[0] |
---|
| 73 | self.browser.getLink(self.value).click() |
---|
[14889] | 74 | self.assertTrue('<span>30000.0</span>' in self.browser.contents) |
---|
[10765] | 75 | self.payment_url = self.browser.url |
---|
| 76 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[14875] | 77 | self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' |
---|
| 78 | in self.browser.contents) |
---|
| 79 | self.assertTrue( |
---|
| 80 | '<input type="hidden" name="amount" value="3000000" />' in |
---|
| 81 | self.browser.contents) |
---|
| 82 | self.assertTrue( |
---|
| 83 | 'item_name="School Fee" item_amt="2695000" bank_id="31" acct_num="0026235493"' in |
---|
| 84 | self.browser.contents) |
---|
| 85 | # ND FT Non-Deltan Fresh Student Acceptance Fee |
---|
| 86 | self.app['configuration']['2004'].clearance_fee = 12345.0 |
---|
| 87 | self.browser.open(self.payments_path + '/addop') |
---|
| 88 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
| 89 | self.browser.getControl("Create ticket").click() |
---|
| 90 | self.assertMatches('...ticket created...', |
---|
[10765] | 91 | self.browser.contents) |
---|
[14875] | 92 | ctrl = self.browser.getControl(name='val_id') |
---|
| 93 | self.value = ctrl.options[1] |
---|
| 94 | self.browser.getLink(self.value).click() |
---|
| 95 | self.assertMatches('...Amount Authorized...', |
---|
[10765] | 96 | self.browser.contents) |
---|
| 97 | self.assertMatches( |
---|
[14875] | 98 | '...<span>12345.0</span>...', |
---|
[10765] | 99 | self.browser.contents) |
---|
[14875] | 100 | self.payment_url = self.browser.url |
---|
| 101 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 102 | self.assertTrue('<input type="hidden" name="pay_item_id" value="103" />' in |
---|
| 103 | self.browser.contents) |
---|
| 104 | self.assertTrue( |
---|
| 105 | '<input type="hidden" name="amount" value="1234500" />' in |
---|
[10765] | 106 | self.browser.contents) |
---|
[14875] | 107 | self.assertTrue( |
---|
| 108 | 'item_name="Acceptance" item_amt="1159500" bank_id="8" acct_num="2004402644"' in |
---|
| 109 | self.browser.contents) |
---|
[10765] | 110 | |
---|
[12979] | 111 | def test_interswitch_form_ticket_expired(self): |
---|
| 112 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 113 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
| 114 | acc_payment.p_state = 'unpaid' |
---|
| 115 | acc_payment.p_category = 'clearance' |
---|
| 116 | acc_payment.p_id = 'xyz' |
---|
| 117 | acc_payment.pay_item_id = '123' |
---|
| 118 | acc_payment.amount_auth = 876.0 |
---|
| 119 | self.student['payments']['xyz'] = acc_payment |
---|
| 120 | self.browser.open(self.payments_path + '/xyz') |
---|
| 121 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[14862] | 122 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="103" />...', |
---|
[12979] | 123 | self.browser.contents) |
---|
| 124 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 125 | self.browser.contents) |
---|
| 126 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 127 | self.assertMatches( |
---|
| 128 | '...<input type="hidden" name="amount" value="87600" />...', |
---|
| 129 | self.browser.contents) |
---|
[13020] | 130 | delta = timedelta(days=8) |
---|
[12979] | 131 | acc_payment.creation_date -= delta |
---|
| 132 | self.browser.open(self.payments_path + '/xyz') |
---|
| 133 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 134 | self.assertMatches( |
---|
| 135 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 136 | self.browser.contents) |
---|
[13020] | 137 | delta = timedelta(days=2) |
---|
[12979] | 138 | acc_payment.creation_date += delta |
---|
| 139 | self.browser.open(self.payments_path + '/xyz') |
---|
| 140 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 141 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 142 | self.browser.contents) |
---|
| 143 | |
---|
[14889] | 144 | def test_interswitch_form_pt_payments(self): |
---|
| 145 | # Add SPAT certificate |
---|
| 146 | certificate = createObject('waeup.Certificate') |
---|
| 147 | certificate.code = u'CERT2' |
---|
| 148 | certificate.application_category = 'basic' # can be anything |
---|
| 149 | certificate.start_level = 100 |
---|
| 150 | certificate.end_level = 500 |
---|
| 151 | certificate.study_mode = u'ug_ft' # can be anything |
---|
| 152 | self.app['faculties']['SPAT'] = Faculty(code=u'SPAT') |
---|
| 153 | self.app['faculties']['SPAT']['dep2'] = Department(code=u'dep2') |
---|
| 154 | department = self.app['faculties']['SPAT']['dep2'] |
---|
| 155 | self.app['faculties']['SPAT']['dep2'].certificates.addCertificate( |
---|
| 156 | certificate) |
---|
| 157 | certificate.school_fee_3 = 30000.0 |
---|
| 158 | self.student['studycourse'].certificate = certificate |
---|
| 159 | # Manager can access InterswitchForm |
---|
| 160 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 161 | self.browser.open(self.payments_path) |
---|
| 162 | IWorkflowState(self.student).setState('cleared') |
---|
| 163 | self.student.nationality = u'NG' |
---|
| 164 | # ND PT Non-Deltan Fresh Student School Fee |
---|
| 165 | self.certificate.study_mode = 'nd_ft' |
---|
| 166 | self.certificate.school_fee_3 = 30000.0 |
---|
| 167 | self.browser.open(self.payments_path + '/addop') |
---|
| 168 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 169 | self.browser.getControl("Create ticket").click() |
---|
| 170 | self.assertMatches('...ticket created...', self.browser.contents) |
---|
| 171 | ctrl = self.browser.getControl(name='val_id') |
---|
| 172 | self.value = ctrl.options[0] |
---|
| 173 | self.browser.getLink(self.value).click() |
---|
| 174 | self.assertTrue('<span>30000.0</span>' in self.browser.contents) |
---|
| 175 | self.payment_url = self.browser.url |
---|
| 176 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 177 | self.assertTrue('<item_detail item_id="1" item_name="School Fee 1" item_amt="1617000" bank_id="117" acct_num="1012808851" />' |
---|
| 178 | in self.browser.contents) |
---|
| 179 | self.assertTrue('<item_detail item_id="2" item_name="School Fee 2" item_amt="1078000" bank_id="123" acct_num="1002883141" />' |
---|
| 180 | in self.browser.contents) |
---|
| 181 | self.assertTrue('<item_detail item_id="3" item_name="BT Education" item_amt="280000" bank_id="8" acct_num="2028964403" />' |
---|
| 182 | in self.browser.contents) |
---|
| 183 | |
---|
[10765] | 184 | @external_test |
---|
| 185 | def test_webservice(self): |
---|
| 186 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 187 | self.browser.open(self.payments_path) |
---|
| 188 | IWorkflowState(self.student).setState('cleared') |
---|
| 189 | self.student.nationality = u'NG' |
---|
| 190 | self.browser.open(self.payments_path + '/addop') |
---|
| 191 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 192 | self.browser.getControl("Create ticket").click() |
---|
| 193 | self.assertMatches('...ticket created...', |
---|
| 194 | self.browser.contents) |
---|
| 195 | ctrl = self.browser.getControl(name='val_id') |
---|
| 196 | self.value = ctrl.options[0] |
---|
| 197 | self.browser.getLink(self.value).click() |
---|
| 198 | self.payment_url = self.browser.url |
---|
| 199 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 200 | # and gateway_amt |
---|
| 201 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 202 | # Now we can call the webservice |
---|
| 203 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 204 | self.assertMatches('...Unsuccessful callback...', |
---|
| 205 | self.browser.contents) |
---|
| 206 | # The payment is now in state failed ... |
---|
| 207 | self.assertMatches('...<span>Failed</span>...', |
---|
| 208 | self.browser.contents) |
---|
| 209 | # ... and the catalog has been updated |
---|
| 210 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 211 | results = list( |
---|
| 212 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 213 | self.assertEqual(len(results), 1) |
---|
| 214 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 215 | |
---|
| 216 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 217 | # live system. This is definitely not an appropriate |
---|
| 218 | # solution for testing, but we have no choice since |
---|
| 219 | # Interswitch doesn't provide any interface |
---|
| 220 | # for testing. |
---|
| 221 | payment = self.student['payments'][self.value] |
---|
| 222 | payment.p_id = 'p3547789850240' |
---|
| 223 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 224 | self.assertMatches('...Callback amount does not match...', |
---|
| 225 | self.browser.contents) |
---|
| 226 | # The payment is now in state failed ... |
---|
| 227 | self.assertMatches('...<span>Failed</span>...', |
---|
| 228 | self.browser.contents) |
---|
| 229 | # Let's replace the amount autorized with the amount of the |
---|
| 230 | # live system payment |
---|
| 231 | payment.amount_auth = payment.r_amount_approved |
---|
| 232 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 233 | self.assertMatches('...Successful payment...', |
---|
| 234 | self.browser.contents) |
---|
| 235 | # The payment is now in state paid ... |
---|
| 236 | self.assertMatches('...<span>Paid</span>...', |
---|
| 237 | self.browser.contents) |
---|
| 238 | # ... and the catalog has been updated |
---|
| 239 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 240 | results = list( |
---|
| 241 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 242 | self.assertEqual(len(results), 1) |
---|
| 243 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 244 | # Approval is logged in students.log ... |
---|
| 245 | logfile = os.path.join( |
---|
| 246 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 247 | logcontent = open(logfile).read() |
---|
| 248 | self.assertTrue( |
---|
| 249 | 'zope.mgr - ' |
---|
[14716] | 250 | 'kofacustom.dspg.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
[10765] | 251 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
| 252 | in logcontent) |
---|
| 253 | # ... and in payments.log |
---|
| 254 | logfile = os.path.join( |
---|
| 255 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 256 | logcontent = open(logfile).read() |
---|
| 257 | self.assertTrue( |
---|
| 258 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
| 259 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
| 260 | in logcontent) |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 264 | """Tests for the Interswitch payment gateway. |
---|
| 265 | """ |
---|
| 266 | |
---|
| 267 | layer = FunctionalLayer |
---|
| 268 | |
---|
| 269 | def setUp(self): |
---|
| 270 | super(InterswitchTestsApplicants, self).setUp() |
---|
[11271] | 271 | configuration = SessionConfiguration() |
---|
| 272 | configuration.academic_session = datetime.now().year - 2 |
---|
| 273 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
| 274 | self.configuration = configuration |
---|
[10765] | 275 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 276 | self.browser.open(self.manage_path) |
---|
| 277 | #IWorkflowState(self.student).setState('started') |
---|
| 278 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
| 279 | self.applicantscontainer.application_fee = 1000.0 |
---|
| 280 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 281 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 282 | self.browser.getControl("Save").click() |
---|
[14889] | 283 | |
---|
| 284 | |
---|
| 285 | def test_interswitch_form(self): |
---|
[10765] | 286 | self.browser.getControl("Add online").click() |
---|
[14732] | 287 | self.assertMatches('...passport photo before making payment...', |
---|
| 288 | self.browser.contents) |
---|
| 289 | self.browser.open(self.manage_path) |
---|
| 290 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
| 291 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 292 | #self.browser.getControl(name="transition").value = ['start'] |
---|
| 293 | image = open(SAMPLE_IMAGE, 'rb') |
---|
| 294 | ctrl = self.browser.getControl(name='form.passport') |
---|
| 295 | file_ctrl = ctrl.mech_control |
---|
| 296 | file_ctrl.add_file(image, filename='myphoto.jpg') |
---|
| 297 | self.browser.getControl("Save").click() |
---|
| 298 | self.browser.getControl("Add online").click() |
---|
[10765] | 299 | self.assertMatches('...ticket created...', |
---|
| 300 | self.browser.contents) |
---|
| 301 | #ctrl = self.browser.getControl(name='val_id') |
---|
| 302 | #value = ctrl.options[0] |
---|
| 303 | #self.browser.getLink(value).click() |
---|
| 304 | self.assertMatches('...Amount Authorized...', |
---|
| 305 | self.browser.contents) |
---|
[14889] | 306 | self.assertTrue('span>1000.0</span>' in self.browser.contents) |
---|
[10765] | 307 | self.payment_url = self.browser.url |
---|
| 308 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[14889] | 309 | self.assertTrue('<input type="hidden" name="amount" value="100000" />' |
---|
| 310 | in self.browser.contents) |
---|
[13020] | 311 | delta = timedelta(days=8) |
---|
[12979] | 312 | self.applicant.values()[0].creation_date -= delta |
---|
| 313 | self.browser.open(self.payment_url) |
---|
| 314 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 315 | self.assertMatches( |
---|
| 316 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 317 | self.browser.contents) |
---|
[14889] | 318 | # ND PT Application Fee |
---|
| 319 | self.applicantscontainer.application_category = 'ndpt' |
---|
| 320 | self.certificate.application_category = 'ndpt' |
---|
| 321 | self.browser.open(self.manage_path) |
---|
| 322 | self.browser.getControl("Add online").click() |
---|
| 323 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 324 | self.assertTrue('<item_detail item_id="1" item_name="Application 1" item_amt="15000" bank_id="117" acct_num="1015220292" />' |
---|
| 325 | in self.browser.contents) |
---|
| 326 | self.assertTrue('<item_detail item_id="2" item_name="Application 2" item_amt="10000" bank_id="123" acct_num="1002883141" />' |
---|
| 327 | in self.browser.contents) |
---|
| 328 | self.assertTrue('<item_detail item_id="3" item_name="BT Education" item_amt="50000" bank_id="8" acct_num="2028964403" />' |
---|
| 329 | in self.browser.contents) |
---|
[10765] | 330 | |
---|
| 331 | @external_test |
---|
| 332 | def test_webservice(self): |
---|
| 333 | |
---|
| 334 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 335 | self.assertMatches('...Unsuccessful callback...', |
---|
| 336 | self.browser.contents) |
---|
| 337 | # The payment is now in state failed |
---|
| 338 | self.assertMatches('...<span>Failed</span>...', |
---|
| 339 | self.browser.contents) |
---|