[16639] | 1 | ## $Id: tests.py 16655 2021-09-30 05:31:28Z 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 |
---|
[16615] | 26 | from kofacustom.edocons.testing import FunctionalLayer |
---|
[15614] | 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() |
---|
[16591] | 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) |
---|
[16591] | 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( |
---|
[16642] | 79 | '<input type="hidden" name="amount" value="4030000" />' 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 - ' |
---|
[16615] | 184 | 'kofacustom.edocons.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
[15614] | 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') |
---|
[16655] | 212 | self.browser.getControl(name="form.firstname").value = 'John' |
---|
| 213 | self.browser.getControl(name="form.middlename").value = 'Anthony' |
---|
| 214 | self.browser.getControl(name="form.lastname").value = 'Tester' |
---|
| 215 | #self.browser.getControl(name="form.course1").value = ['CERT1'] |
---|
| 216 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
| 217 | self.browser.getControl(name="form.sex").value = ['m'] |
---|
| 218 | self.browser.getControl(name="form.email").value = 'xx@yy.zz' |
---|
[15614] | 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 |
---|
[15756] | 239 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15614] | 240 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 241 | self.browser.contents) |
---|
| 242 | self.assertMatches( |
---|
| 243 | '...<input type="hidden" name="amount" value="100000" />...', |
---|
| 244 | self.browser.contents) |
---|
| 245 | delta = timedelta(days=8) |
---|
| 246 | self.applicant.values()[0].creation_date -= delta |
---|
| 247 | self.browser.open(self.payment_url) |
---|
[15756] | 248 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
[15614] | 249 | self.assertMatches( |
---|
| 250 | '...This payment ticket is too old. Please create a new ticket...', |
---|
| 251 | self.browser.contents) |
---|
| 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) |
---|