[7894] | 1 | ## $Id: tests.py 9943 2013-02-13 10:17:01Z 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 | ## |
---|
[9780] | 18 | import os |
---|
[9711] | 19 | from zope.component import getUtility |
---|
| 20 | from zope.catalog.interfaces import ICatalog |
---|
[7894] | 21 | from hurry.workflow.interfaces import IWorkflowState |
---|
| 22 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
[8266] | 23 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
| 24 | from waeup.kofa.configuration import SessionConfiguration |
---|
[8460] | 25 | from waeup.fceokene.testing import FunctionalLayer |
---|
[7894] | 26 | |
---|
[7970] | 27 | # Also run tests that send requests to external servers? |
---|
| 28 | # If you enable this, please make sure the external services |
---|
| 29 | # do exist really and are not bothered by being spammed by a test programme. |
---|
[9903] | 30 | EXTERNAL_TESTS = False |
---|
[7970] | 31 | |
---|
| 32 | def external_test(func): |
---|
| 33 | if not EXTERNAL_TESTS: |
---|
| 34 | myself = __file__ |
---|
| 35 | if myself.endswith('.pyc'): |
---|
| 36 | myself = myself[:-2] |
---|
| 37 | print "WARNING: external tests are skipped!" |
---|
| 38 | print "WARNING: edit %s to enable them." % myself |
---|
| 39 | return |
---|
| 40 | return func |
---|
| 41 | |
---|
| 42 | |
---|
[8266] | 43 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
[7929] | 44 | """Tests for the Interswitch payment gateway. |
---|
[7894] | 45 | """ |
---|
| 46 | |
---|
| 47 | layer = FunctionalLayer |
---|
| 48 | |
---|
[7970] | 49 | def setUp(self): |
---|
[8266] | 50 | super(InterswitchTestsStudents, self).setUp() |
---|
[9943] | 51 | self.certificate.study_mode = 'nce_ft' |
---|
[7970] | 52 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
[7995] | 53 | self.browser.open(self.payments_path) |
---|
[7970] | 54 | IWorkflowState(self.student).setState('cleared') |
---|
[7995] | 55 | self.browser.open(self.payments_path + '/addop') |
---|
[9613] | 56 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
[7970] | 57 | self.browser.getControl("Create ticket").click() |
---|
| 58 | self.assertMatches('...ticket created...', |
---|
| 59 | self.browser.contents) |
---|
| 60 | ctrl = self.browser.getControl(name='val_id') |
---|
[9780] | 61 | self.value = ctrl.options[0] |
---|
| 62 | self.browser.getLink(self.value).click() |
---|
[7970] | 63 | self.assertMatches('...Amount Authorized...', |
---|
| 64 | self.browser.contents) |
---|
[8266] | 65 | self.assertMatches( |
---|
[9143] | 66 | '...<span>12495.0</span>...', |
---|
[8266] | 67 | self.browser.contents) |
---|
[7970] | 68 | self.payment_url = self.browser.url |
---|
| 69 | |
---|
| 70 | |
---|
[8263] | 71 | # def callback_url(self, payment_url, resp, apprAmt): |
---|
| 72 | # return payment_url + ( |
---|
| 73 | # '/isw_callback?echo=' + |
---|
| 74 | # '&resp=%s' + |
---|
| 75 | # '&desc=Something went wrong' + |
---|
| 76 | # '&txnRef=p1331792385335' + |
---|
| 77 | # '&payRef=' + '&retRef=' + |
---|
| 78 | # '&cardNum=0' + |
---|
| 79 | # '&apprAmt=%s' + |
---|
| 80 | # '&url=http://xyz') % (resp, apprAmt) |
---|
[7894] | 81 | |
---|
[7970] | 82 | def test_interswitch_form(self): |
---|
[7894] | 83 | |
---|
[9780] | 84 | self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0) |
---|
| 85 | self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0) |
---|
| 86 | self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0) |
---|
[9613] | 87 | # Manager can access InterswitchForm for the created school fee ticket |
---|
[7894] | 88 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[9780] | 89 | # Split amounts have been set |
---|
| 90 | self.assertEqual(self.student['payments'][self.value].provider_amt, 1600.0) |
---|
| 91 | self.assertEqual(self.student['payments'][self.value].gateway_amt, 150.0) |
---|
| 92 | self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 1400.0) |
---|
[7894] | 93 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 94 | self.browser.contents) |
---|
| 95 | self.assertMatches( |
---|
[9143] | 96 | '...<input type="hidden" name="amount" value="1249500.0" />...', |
---|
[7894] | 97 | self.browser.contents) |
---|
[9129] | 98 | self.assertMatches( |
---|
[9682] | 99 | '...item_name="School Fee" item_amt="934800" bank_id="117" acct_num="6216801033"...', |
---|
[9129] | 100 | self.browser.contents) |
---|
| 101 | self.assertMatches( |
---|
| 102 | '...item_name="FCEOkene Split" item_amt="140000" bank_id="117" acct_num="6216801058"...', |
---|
| 103 | self.browser.contents) |
---|
| 104 | self.assertMatches( |
---|
[9682] | 105 | '...item_name="BT Education" item_amt="160000" bank_id="31" acct_num="0026781725"...', |
---|
[9129] | 106 | self.browser.contents) |
---|
[7894] | 107 | |
---|
[9613] | 108 | # Let's do the same for maintenance fee payment |
---|
| 109 | |
---|
| 110 | self.browser.open(self.payments_path) |
---|
| 111 | self.browser.open(self.payments_path + '/addop') |
---|
| 112 | self.browser.getControl( |
---|
| 113 | name="form.p_category").value = ['hostel_maintenance'] |
---|
| 114 | self.browser.getControl("Create ticket").click() |
---|
| 115 | self.assertMatches('...You have not yet booked accommodation...', |
---|
| 116 | self.browser.contents) |
---|
| 117 | # Students have to book bed first |
---|
| 118 | self.browser.open(self.acco_path) |
---|
| 119 | IWorkflowState(self.student).setState('admitted') |
---|
| 120 | self.browser.getLink("Book accommodation").click() |
---|
| 121 | self.assertFalse('Activation Code:' in self.browser.contents) |
---|
| 122 | self.browser.getControl("Create bed ticket").click() |
---|
| 123 | # Bed is randomly selected but, since there is only |
---|
| 124 | # one bed for this student, we know that ... |
---|
| 125 | self.assertMatches('...Hall 1, Block A, Room 101, Bed A...', |
---|
| 126 | self.browser.contents) |
---|
| 127 | self.assertMatches('...ticket created...', |
---|
| 128 | self.browser.contents) |
---|
| 129 | self.browser.open(self.payments_path + '/addop') |
---|
| 130 | self.browser.getControl( |
---|
| 131 | name="form.p_category").value = ['hostel_maintenance'] |
---|
| 132 | self.browser.getControl("Create ticket").click() |
---|
| 133 | self.assertMatches('...ticket created...', |
---|
| 134 | self.browser.contents) |
---|
| 135 | ctrl = self.browser.getControl(name='val_id') |
---|
| 136 | value = ctrl.options[1] |
---|
| 137 | self.browser.getLink(value).click() |
---|
| 138 | self.assertMatches('...Amount Authorized...', |
---|
| 139 | self.browser.contents) |
---|
| 140 | self.assertMatches( |
---|
[9628] | 141 | '...<span>4150.0</span>...', |
---|
[9613] | 142 | self.browser.contents) |
---|
| 143 | self.payment_url = self.browser.url |
---|
[9780] | 144 | self.assertEqual(self.student['payments'][value].provider_amt, 0.0) |
---|
| 145 | self.assertEqual(self.student['payments'][value].gateway_amt, 0.0) |
---|
| 146 | self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0) |
---|
[9613] | 147 | # Manager can access InterswitchForm |
---|
| 148 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[9780] | 149 | # Split amounts have been set |
---|
| 150 | self.assertEqual(self.student['payments'][value].provider_amt, 0.0) |
---|
| 151 | self.assertEqual(self.student['payments'][value].gateway_amt, 150.0) |
---|
| 152 | self.assertEqual(self.student['payments'][value].thirdparty_amt, 0.0) |
---|
[9613] | 153 | # The total amount to be processed by Interswitch |
---|
| 154 | # has been reduced by the Interswitch fee of 150 Nairas |
---|
| 155 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 156 | self.browser.contents) |
---|
| 157 | self.assertMatches( |
---|
| 158 | '...<input type="hidden" name="amount" value="400000.0" />...', |
---|
| 159 | self.browser.contents) |
---|
| 160 | self.assertMatches( |
---|
| 161 | '...item_name="Hostel Maintenance Fee" item_amt="385000" bank_id="117" acct_num="1012044132"...', |
---|
| 162 | self.browser.contents) |
---|
| 163 | # BT does nor charge a fee for maintenance fee |
---|
| 164 | self.assertFalse("BT Education" in self.browser.contents) |
---|
| 165 | |
---|
[8263] | 166 | # @external_test |
---|
| 167 | # def test_callback(self): |
---|
[7970] | 168 | |
---|
[7894] | 169 | # Manager can call callback manually |
---|
[8263] | 170 | # self.browser.open(self.callback_url(self.payment_url, 'XX', '300')) |
---|
| 171 | # self.assertMatches('...Unsuccessful callback: Something went wrong...', |
---|
| 172 | # self.browser.contents) |
---|
| 173 | # self.assertMatches('...Failed...', |
---|
| 174 | # self.browser.contents) |
---|
| 175 | # self.browser.open(self.payment_url + '/isw_callback') |
---|
| 176 | # self.assertMatches('...Unsuccessful callback: Incomplete query string...', |
---|
| 177 | # self.browser.contents) |
---|
| 178 | # self.assertMatches('...Failed...', |
---|
| 179 | # self.browser.contents) |
---|
| 180 | # self.browser.open(self.callback_url(self.payment_url, '00', '300000')) |
---|
| 181 | # self.assertMatches('...Wrong amount...', |
---|
| 182 | # self.browser.contents) |
---|
| 183 | # self.browser.open(self.callback_url(self.payment_url, '00', '4000000')) |
---|
| 184 | # self.assertMatches('...Valid callback received...', |
---|
| 185 | # self.browser.contents) |
---|
[7930] | 186 | |
---|
[7970] | 187 | @external_test |
---|
[7930] | 188 | def test_webservice(self): |
---|
[9780] | 189 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 190 | # and gateway_amt |
---|
| 191 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 192 | # Now we can call the webservice |
---|
[7970] | 193 | self.browser.open(self.payment_url + '/request_webservice') |
---|
[9711] | 194 | #self.assertMatches('...Unsuccessful callback...', |
---|
| 195 | # self.browser.contents) |
---|
[9780] | 196 | self.assertMatches('...Unsuccessful callback...', |
---|
[7930] | 197 | self.browser.contents) |
---|
[9711] | 198 | # The payment is now in state failed ... |
---|
[8266] | 199 | self.assertMatches('...<span>Failed</span>...', |
---|
| 200 | self.browser.contents) |
---|
[9711] | 201 | # ... and the catalog has been updated |
---|
| 202 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 203 | results = list( |
---|
| 204 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 205 | self.assertEqual(len(results), 1) |
---|
| 206 | self.assertEqual(results[0].p_state, 'failed') |
---|
[8266] | 207 | |
---|
[9790] | 208 | # Let's replace the p_id with a valid p_id of the FCEOkene |
---|
[9780] | 209 | # live system. This is definitely not an appropriate |
---|
| 210 | # solution for testing, but we have no choice since |
---|
| 211 | # Interswitch doesn't provide any interface |
---|
| 212 | # for testing. |
---|
| 213 | payment = self.student['payments'][self.value] |
---|
| 214 | payment.p_id = 'p3536651296379' |
---|
| 215 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 216 | self.assertMatches('...Callback amount does not match...', |
---|
| 217 | self.browser.contents) |
---|
| 218 | # The payment is now in state failed ... |
---|
| 219 | self.assertMatches('...<span>Failed</span>...', |
---|
| 220 | self.browser.contents) |
---|
| 221 | # Let's replace the amount autorized with the amount of the |
---|
| 222 | # live system payment |
---|
| 223 | payment.amount_auth = payment.r_amount_approved |
---|
| 224 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 225 | self.assertMatches('...Successful payment...', |
---|
| 226 | self.browser.contents) |
---|
| 227 | # The payment is now in state paid ... |
---|
| 228 | self.assertMatches('...<span>Paid</span>...', |
---|
| 229 | self.browser.contents) |
---|
| 230 | # ... and the catalog has been updated |
---|
| 231 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 232 | results = list( |
---|
| 233 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 234 | self.assertEqual(len(results), 1) |
---|
| 235 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 236 | # Approval is logged in students.log ... |
---|
| 237 | logfile = os.path.join( |
---|
| 238 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 239 | logcontent = open(logfile).read() |
---|
| 240 | self.assertTrue( |
---|
| 241 | 'zope.mgr - ' |
---|
| 242 | 'waeup.fceokene.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - ' |
---|
| 243 | 'K1000000 - successful schoolfee payment: p3536651296379\n' |
---|
| 244 | in logcontent) |
---|
| 245 | # ... and in payments.log |
---|
| 246 | logfile = os.path.join( |
---|
| 247 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 248 | logcontent = open(logfile).read() |
---|
| 249 | self.assertTrue( |
---|
| 250 | '"zope.mgr",K1000000,p3536651296379,schoolfee,' |
---|
| 251 | '3150.0,00,1600.0,150.0,1400.0,,,\n' |
---|
| 252 | in logcontent) |
---|
| 253 | |
---|
[8266] | 254 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 255 | """Tests for the Interswitch payment gateway. |
---|
| 256 | """ |
---|
| 257 | |
---|
| 258 | layer = FunctionalLayer |
---|
| 259 | |
---|
| 260 | def setUp(self): |
---|
| 261 | super(InterswitchTestsApplicants, self).setUp() |
---|
| 262 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 263 | self.browser.open(self.manage_path) |
---|
| 264 | #IWorkflowState(self.student).setState('started') |
---|
| 265 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
[8528] | 266 | self.applicantscontainer.application_fee = 1000.0 |
---|
[9455] | 267 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
[8266] | 268 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 269 | self.browser.getControl("Save").click() |
---|
| 270 | self.browser.getControl("Add online").click() |
---|
| 271 | self.assertMatches('...ticket created...', |
---|
| 272 | self.browser.contents) |
---|
| 273 | self.assertMatches('...Amount Authorized...', |
---|
| 274 | self.browser.contents) |
---|
| 275 | self.assertMatches( |
---|
| 276 | '...<span>1000.0</span>...', |
---|
| 277 | self.browser.contents) |
---|
| 278 | self.payment_url = self.browser.url |
---|
[9780] | 279 | self.browser.open(self.manage_path) |
---|
| 280 | ctrl = self.browser.getControl(name='val_id') |
---|
| 281 | self.value = ctrl.options[0] |
---|
[8266] | 282 | |
---|
| 283 | |
---|
| 284 | def test_interswitch_form(self): |
---|
| 285 | |
---|
| 286 | # Manager can access InterswitchForm |
---|
[9780] | 287 | self.browser.open(self.payment_url) |
---|
[8266] | 288 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 289 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 290 | self.browser.contents) |
---|
| 291 | self.assertMatches( |
---|
[8276] | 292 | '...<input type="hidden" name="amount" value="100000.0" />...', |
---|
[8266] | 293 | self.browser.contents) |
---|
| 294 | |
---|
| 295 | @external_test |
---|
| 296 | def test_webservice(self): |
---|
[9780] | 297 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 298 | # and gateway_amt |
---|
| 299 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 300 | # Now we can call the webservice |
---|
[8266] | 301 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 302 | self.assertMatches('...Unsuccessful callback...', |
---|
| 303 | self.browser.contents) |
---|
[9780] | 304 | # The payment is now in state failed ... |
---|
[8266] | 305 | self.assertMatches('...<span>Failed</span>...', |
---|
| 306 | self.browser.contents) |
---|
[9780] | 307 | # ... and the catalog has been updated |
---|
| 308 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 309 | results = list( |
---|
| 310 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 311 | self.assertEqual(len(results), 1) |
---|
| 312 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 313 | |
---|
| 314 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 315 | # live system. This is definitely not an appropriate |
---|
| 316 | # solution for testing, but we have no choice since |
---|
| 317 | # Interswitch doesn't provide any interface |
---|
| 318 | # for testing. |
---|
| 319 | payment = self.applicant[self.value] |
---|
| 320 | payment.p_id = 'p3536651296379' |
---|
| 321 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 322 | self.assertMatches('...Callback amount does not match...', |
---|
| 323 | self.browser.contents) |
---|
| 324 | # The payment is now in state failed ... |
---|
| 325 | self.assertMatches('...<span>Failed</span>...', |
---|
| 326 | self.browser.contents) |
---|
| 327 | # Let's replace the amount autorized with the amount of the |
---|
| 328 | # live system payment |
---|
| 329 | payment.amount_auth = payment.r_amount_approved |
---|
| 330 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 331 | self.assertMatches('...Successful payment...', |
---|
| 332 | self.browser.contents) |
---|
| 333 | # The payment is now in state paid ... |
---|
| 334 | self.assertMatches('...<span>Paid</span>...', |
---|
| 335 | self.browser.contents) |
---|
| 336 | # ... and the catalog has been updated |
---|
| 337 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 338 | results = list( |
---|
| 339 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 340 | self.assertEqual(len(results), 1) |
---|
| 341 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 342 | # Approval is logged in students.log ... |
---|
| 343 | logfile = os.path.join( |
---|
| 344 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
| 345 | logcontent = open(logfile).read() |
---|
| 346 | self.assertTrue( |
---|
| 347 | 'zope.mgr - ' |
---|
| 348 | 'waeup.fceokene.interswitch.browser.InterswitchPaymentRequestWebservicePageApplicant - ' |
---|
| 349 | '%s - successful payment: p3536651296379\n' |
---|
| 350 | % self.applicant.applicant_id in logcontent) |
---|
| 351 | # ... and in payments.log |
---|
| 352 | logfile = os.path.join( |
---|
| 353 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 354 | logcontent = open(logfile).read() |
---|
| 355 | self.assertTrue( |
---|
| 356 | '"zope.mgr",%s,p3536651296379,application,' |
---|
| 357 | '3150.0,00,500.0,150.0,0.0,,,\n' % self.applicant.applicant_id |
---|
| 358 | in logcontent) |
---|