[10765] | 1 | ## $Id: tests.py 16434 2021-03-25 11:24:25Z 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 |
---|
[15563] | 26 | from kofacustom.iuokada.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() |
---|
[15656] | 52 | self.app['configuration']['2004'].interswitch_enabled = True |
---|
[10765] | 53 | |
---|
| 54 | def test_interswitch_form(self): |
---|
[16270] | 55 | self.app['configuration']['2004'].registration_fee = 7000.0 |
---|
[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') |
---|
[16137] | 61 | self.browser.getControl(name="form.p_category").value = ['registration'] |
---|
[16434] | 62 | self.browser.getControl(name="form.p_option").value = ['first'] |
---|
[15883] | 63 | self.browser.getControl("Create payment").click() |
---|
[16137] | 64 | self.assertTrue('Payment created' in self.browser.contents) |
---|
[16270] | 65 | self.browser.open(self.payments_path) |
---|
[10765] | 66 | ctrl = self.browser.getControl(name='val_id') |
---|
| 67 | self.value = ctrl.options[0] |
---|
| 68 | self.browser.getLink(self.value).click() |
---|
[14874] | 69 | self.assertTrue( |
---|
[16270] | 70 | '<span>7000.0</span>' in self.browser.contents) |
---|
[10765] | 71 | self.payment_url = self.browser.url |
---|
[15704] | 72 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[16137] | 73 | self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in |
---|
[10765] | 74 | self.browser.contents) |
---|
| 75 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
[15773] | 76 | # 250 gateway charge have been added |
---|
| 77 | payment = self.student['payments'][self.value] |
---|
[16137] | 78 | self.assertEqual(payment.amount_auth, 7250) |
---|
[16270] | 79 | self.assertEqual(payment.net_amt, 7000) |
---|
[14874] | 80 | self.assertTrue( |
---|
[16137] | 81 | '<input type="hidden" name="amount" value="725000" />' in |
---|
[10765] | 82 | self.browser.contents) |
---|
[16270] | 83 | # WAeAC charge have been substracted |
---|
[14874] | 84 | self.assertTrue( |
---|
[16434] | 85 | 'item_name="Registration Fee" item_amt="200000" bank_id="8" acct_num="2021420049"' in |
---|
[10765] | 86 | self.browser.contents) |
---|
[16270] | 87 | self.assertTrue( |
---|
| 88 | 'item_name="WAeAC" item_amt="500000" bank_id="31" acct_num="0773411069"' in |
---|
| 89 | self.browser.contents) |
---|
[10765] | 90 | |
---|
[12979] | 91 | def test_interswitch_form_ticket_expired(self): |
---|
| 92 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 93 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
| 94 | acc_payment.p_state = 'unpaid' |
---|
[15773] | 95 | acc_payment.p_category = 'registration' |
---|
[12979] | 96 | acc_payment.p_id = 'xyz' |
---|
| 97 | acc_payment.pay_item_id = '123' |
---|
[15773] | 98 | acc_payment.amount_auth = 1000.0 |
---|
[12979] | 99 | self.student['payments']['xyz'] = acc_payment |
---|
| 100 | self.browser.open(self.payments_path + '/xyz') |
---|
[15704] | 101 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15773] | 102 | self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' in |
---|
[12979] | 103 | self.browser.contents) |
---|
| 104 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 105 | self.browser.contents) |
---|
| 106 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
[16270] | 107 | # 250 gateway charge have been added |
---|
[15773] | 108 | self.assertTrue( |
---|
[16270] | 109 | '<input type="hidden" name="amount" value="125000" />' in |
---|
[12979] | 110 | self.browser.contents) |
---|
[16270] | 111 | self.assertEqual(acc_payment.amount_auth, 1250) |
---|
[15773] | 112 | self.assertEqual(acc_payment.net_amt, 1000) |
---|
[13020] | 113 | delta = timedelta(days=8) |
---|
[12979] | 114 | acc_payment.creation_date -= delta |
---|
| 115 | self.browser.open(self.payments_path + '/xyz') |
---|
[15704] | 116 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[12979] | 117 | self.assertMatches( |
---|
| 118 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 119 | self.browser.contents) |
---|
[13020] | 120 | delta = timedelta(days=2) |
---|
[12979] | 121 | acc_payment.creation_date += delta |
---|
| 122 | self.browser.open(self.payments_path + '/xyz') |
---|
[15704] | 123 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[12979] | 124 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 125 | self.browser.contents) |
---|
| 126 | |
---|
[10765] | 127 | @external_test |
---|
| 128 | def test_webservice(self): |
---|
| 129 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 130 | self.browser.open(self.payments_path) |
---|
| 131 | IWorkflowState(self.student).setState('cleared') |
---|
| 132 | self.student.nationality = u'NG' |
---|
| 133 | self.browser.open(self.payments_path + '/addop') |
---|
| 134 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
[15883] | 135 | self.browser.getControl("Create payment").click() |
---|
[10765] | 136 | self.assertMatches('...ticket created...', |
---|
| 137 | self.browser.contents) |
---|
| 138 | ctrl = self.browser.getControl(name='val_id') |
---|
| 139 | self.value = ctrl.options[0] |
---|
| 140 | self.browser.getLink(self.value).click() |
---|
| 141 | self.payment_url = self.browser.url |
---|
| 142 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 143 | # and gateway_amt |
---|
| 144 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 145 | # Now we can call the webservice |
---|
| 146 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 147 | self.assertMatches('...Unsuccessful callback...', |
---|
| 148 | self.browser.contents) |
---|
| 149 | # The payment is now in state failed ... |
---|
| 150 | self.assertMatches('...<span>Failed</span>...', |
---|
| 151 | self.browser.contents) |
---|
| 152 | # ... and the catalog has been updated |
---|
| 153 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 154 | results = list( |
---|
| 155 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 156 | self.assertEqual(len(results), 1) |
---|
| 157 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 158 | |
---|
| 159 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 160 | # live system. This is definitely not an appropriate |
---|
| 161 | # solution for testing, but we have no choice since |
---|
| 162 | # Interswitch doesn't provide any interface |
---|
| 163 | # for testing. |
---|
| 164 | payment = self.student['payments'][self.value] |
---|
| 165 | payment.p_id = 'p3547789850240' |
---|
| 166 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 167 | self.assertMatches('...Callback amount does not match...', |
---|
| 168 | self.browser.contents) |
---|
| 169 | # The payment is now in state failed ... |
---|
| 170 | self.assertMatches('...<span>Failed</span>...', |
---|
| 171 | self.browser.contents) |
---|
| 172 | # Let's replace the amount autorized with the amount of the |
---|
| 173 | # live system payment |
---|
| 174 | payment.amount_auth = payment.r_amount_approved |
---|
| 175 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 176 | self.assertMatches('...Successful payment...', |
---|
| 177 | self.browser.contents) |
---|
| 178 | # The payment is now in state paid ... |
---|
| 179 | self.assertMatches('...<span>Paid</span>...', |
---|
| 180 | self.browser.contents) |
---|
| 181 | # ... and the catalog has been updated |
---|
| 182 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 183 | results = list( |
---|
| 184 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 185 | self.assertEqual(len(results), 1) |
---|
| 186 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 187 | # Approval is logged in students.log ... |
---|
| 188 | logfile = os.path.join( |
---|
| 189 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 190 | logcontent = open(logfile).read() |
---|
| 191 | self.assertTrue( |
---|
| 192 | 'zope.mgr - ' |
---|
[15563] | 193 | 'kofacustom.iuokada.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
[10765] | 194 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
| 195 | in logcontent) |
---|
| 196 | # ... and in payments.log |
---|
| 197 | logfile = os.path.join( |
---|
| 198 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 199 | logcontent = open(logfile).read() |
---|
| 200 | self.assertTrue( |
---|
| 201 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
| 202 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
| 203 | in logcontent) |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 207 | """Tests for the Interswitch payment gateway. |
---|
| 208 | """ |
---|
| 209 | |
---|
| 210 | layer = FunctionalLayer |
---|
| 211 | |
---|
[16082] | 212 | def fill_correct_values(self): |
---|
| 213 | # Fill the edit form with suitable values |
---|
| 214 | self.browser.getControl(name="form.firstname").value = 'John' |
---|
| 215 | self.browser.getControl(name="form.middlename").value = 'Anthony' |
---|
| 216 | self.browser.getControl(name="form.lastname").value = 'Tester' |
---|
| 217 | self.browser.getControl(name="custom.course1").value = ['CERT1'] |
---|
| 218 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
| 219 | self.browser.getControl(name="form.sex").value = ['m'] |
---|
| 220 | self.browser.getControl(name="form.email").value = 'xx@yy.zz' |
---|
[16292] | 221 | self.browser.getControl(name="form.perm_address").value = 'Adresse' |
---|
[16082] | 222 | |
---|
[10765] | 223 | def setUp(self): |
---|
| 224 | super(InterswitchTestsApplicants, self).setUp() |
---|
[11271] | 225 | configuration = SessionConfiguration() |
---|
| 226 | configuration.academic_session = datetime.now().year - 2 |
---|
| 227 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
| 228 | self.configuration = configuration |
---|
[10765] | 229 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 230 | self.browser.open(self.manage_path) |
---|
| 231 | #IWorkflowState(self.student).setState('started') |
---|
[16082] | 232 | self.fill_correct_values() |
---|
[10765] | 233 | self.applicantscontainer.application_fee = 1000.0 |
---|
| 234 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 235 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 236 | self.browser.getControl("Save").click() |
---|
| 237 | self.browser.getControl("Add online").click() |
---|
[15883] | 238 | self.assertMatches('...Payment created...', |
---|
[10765] | 239 | self.browser.contents) |
---|
| 240 | #ctrl = self.browser.getControl(name='val_id') |
---|
| 241 | #value = ctrl.options[0] |
---|
| 242 | #self.browser.getLink(value).click() |
---|
| 243 | self.assertMatches('...Amount Authorized...', |
---|
| 244 | self.browser.contents) |
---|
| 245 | self.assertMatches( |
---|
| 246 | '...<span>1000.0</span>...', |
---|
| 247 | self.browser.contents) |
---|
| 248 | self.payment_url = self.browser.url |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | def test_interswitch_form(self): |
---|
| 252 | # Manager can access InterswitchForm |
---|
[15704] | 253 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[10765] | 254 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 255 | self.browser.contents) |
---|
| 256 | self.assertMatches( |
---|
[11813] | 257 | '...<input type="hidden" name="amount" value="100000" />...', |
---|
[10765] | 258 | self.browser.contents) |
---|
[13020] | 259 | delta = timedelta(days=8) |
---|
[12979] | 260 | self.applicant.values()[0].creation_date -= delta |
---|
| 261 | self.browser.open(self.payment_url) |
---|
[15704] | 262 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[12979] | 263 | self.assertMatches( |
---|
| 264 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 265 | self.browser.contents) |
---|
[10765] | 266 | |
---|
| 267 | @external_test |
---|
| 268 | def test_webservice(self): |
---|
| 269 | |
---|
| 270 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 271 | self.assertMatches('...Unsuccessful callback...', |
---|
| 272 | self.browser.contents) |
---|
| 273 | # The payment is now in state failed |
---|
| 274 | self.assertMatches('...<span>Failed</span>...', |
---|
| 275 | self.browser.contents) |
---|