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