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