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