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