[10765] | 1 | ## $Id: tests.py 11271 2014-02-22 19:22:03Z uli $ |
---|
| 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 |
---|
| 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() |
---|
| 52 | |
---|
| 53 | def test_interswitch_form(self): |
---|
| 54 | # Manager can access InterswitchForm |
---|
| 55 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 56 | self.browser.open(self.payments_path) |
---|
| 57 | IWorkflowState(self.student).setState('cleared') |
---|
| 58 | self.student.nationality = u'NG' |
---|
| 59 | self.browser.open(self.payments_path + '/addop') |
---|
| 60 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 61 | self.browser.getControl("Create ticket").click() |
---|
| 62 | self.assertMatches('...ticket created...', |
---|
| 63 | self.browser.contents) |
---|
| 64 | ctrl = self.browser.getControl(name='val_id') |
---|
| 65 | self.value = ctrl.options[0] |
---|
| 66 | self.browser.getLink(self.value).click() |
---|
| 67 | self.assertMatches('...Amount Authorized...', |
---|
| 68 | self.browser.contents) |
---|
| 69 | self.assertMatches( |
---|
| 70 | '...<span>40000.0</span>...', |
---|
| 71 | self.browser.contents) |
---|
| 72 | self.payment_url = self.browser.url |
---|
| 73 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 74 | # The total amount to be processed by Interswitch |
---|
| 75 | # has been reduced by the Interswitch fee of 150 Nairas |
---|
| 76 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="0000" />...', |
---|
| 77 | self.browser.contents) |
---|
| 78 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 79 | self.browser.contents) |
---|
| 80 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 81 | self.assertMatches( |
---|
| 82 | '...<input type="hidden" name="amount" value="4000000.0" />...', |
---|
| 83 | self.browser.contents) |
---|
| 84 | self.assertMatches( |
---|
| 85 | '...item_name="School Fee" item_amt="4000000" bank_id="00" acct_num="00000000"...', |
---|
| 86 | self.browser.contents) |
---|
| 87 | |
---|
| 88 | @external_test |
---|
| 89 | def test_webservice(self): |
---|
| 90 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 91 | self.browser.open(self.payments_path) |
---|
| 92 | IWorkflowState(self.student).setState('cleared') |
---|
| 93 | self.student.nationality = u'NG' |
---|
| 94 | self.browser.open(self.payments_path + '/addop') |
---|
| 95 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 96 | self.browser.getControl("Create ticket").click() |
---|
| 97 | self.assertMatches('...ticket created...', |
---|
| 98 | self.browser.contents) |
---|
| 99 | ctrl = self.browser.getControl(name='val_id') |
---|
| 100 | self.value = ctrl.options[0] |
---|
| 101 | self.browser.getLink(self.value).click() |
---|
| 102 | self.payment_url = self.browser.url |
---|
| 103 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 104 | # and gateway_amt |
---|
| 105 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 106 | # Now we can call the webservice |
---|
| 107 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 108 | self.assertMatches('...Unsuccessful callback...', |
---|
| 109 | self.browser.contents) |
---|
| 110 | # The payment is now in state failed ... |
---|
| 111 | self.assertMatches('...<span>Failed</span>...', |
---|
| 112 | self.browser.contents) |
---|
| 113 | # ... and the catalog has been updated |
---|
| 114 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 115 | results = list( |
---|
| 116 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 117 | self.assertEqual(len(results), 1) |
---|
| 118 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 119 | |
---|
| 120 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 121 | # live system. This is definitely not an appropriate |
---|
| 122 | # solution for testing, but we have no choice since |
---|
| 123 | # Interswitch doesn't provide any interface |
---|
| 124 | # for testing. |
---|
| 125 | payment = self.student['payments'][self.value] |
---|
| 126 | payment.p_id = 'p3547789850240' |
---|
| 127 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 128 | self.assertMatches('...Callback amount does not match...', |
---|
| 129 | self.browser.contents) |
---|
| 130 | # The payment is now in state failed ... |
---|
| 131 | self.assertMatches('...<span>Failed</span>...', |
---|
| 132 | self.browser.contents) |
---|
| 133 | # Let's replace the amount autorized with the amount of the |
---|
| 134 | # live system payment |
---|
| 135 | payment.amount_auth = payment.r_amount_approved |
---|
| 136 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 137 | self.assertMatches('...Successful payment...', |
---|
| 138 | self.browser.contents) |
---|
| 139 | # The payment is now in state paid ... |
---|
| 140 | self.assertMatches('...<span>Paid</span>...', |
---|
| 141 | self.browser.contents) |
---|
| 142 | # ... and the catalog has been updated |
---|
| 143 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 144 | results = list( |
---|
| 145 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 146 | self.assertEqual(len(results), 1) |
---|
| 147 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 148 | # Approval is logged in students.log ... |
---|
| 149 | logfile = os.path.join( |
---|
| 150 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 151 | logcontent = open(logfile).read() |
---|
| 152 | self.assertTrue( |
---|
| 153 | 'zope.mgr - ' |
---|
| 154 | 'kofacustom.skeleton.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - ' |
---|
| 155 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
| 156 | in logcontent) |
---|
| 157 | # ... and in payments.log |
---|
| 158 | logfile = os.path.join( |
---|
| 159 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 160 | logcontent = open(logfile).read() |
---|
| 161 | self.assertTrue( |
---|
| 162 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
| 163 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
| 164 | in logcontent) |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 168 | """Tests for the Interswitch payment gateway. |
---|
| 169 | """ |
---|
| 170 | |
---|
| 171 | layer = FunctionalLayer |
---|
| 172 | |
---|
| 173 | def setUp(self): |
---|
| 174 | super(InterswitchTestsApplicants, self).setUp() |
---|
[11271] | 175 | configuration = SessionConfiguration() |
---|
| 176 | configuration.academic_session = datetime.now().year - 2 |
---|
| 177 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
| 178 | self.configuration = configuration |
---|
[10765] | 179 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 180 | self.browser.open(self.manage_path) |
---|
| 181 | #IWorkflowState(self.student).setState('started') |
---|
| 182 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
| 183 | self.applicantscontainer.application_fee = 1000.0 |
---|
| 184 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 185 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 186 | self.browser.getControl("Save").click() |
---|
| 187 | self.browser.getControl("Add online").click() |
---|
| 188 | self.assertMatches('...ticket created...', |
---|
| 189 | self.browser.contents) |
---|
| 190 | #ctrl = self.browser.getControl(name='val_id') |
---|
| 191 | #value = ctrl.options[0] |
---|
| 192 | #self.browser.getLink(value).click() |
---|
| 193 | self.assertMatches('...Amount Authorized...', |
---|
| 194 | self.browser.contents) |
---|
| 195 | self.assertMatches( |
---|
| 196 | '...<span>1000.0</span>...', |
---|
| 197 | self.browser.contents) |
---|
| 198 | self.payment_url = self.browser.url |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | def test_interswitch_form(self): |
---|
| 202 | |
---|
| 203 | # Manager can access InterswitchForm |
---|
| 204 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 205 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 206 | self.browser.contents) |
---|
| 207 | self.assertMatches( |
---|
| 208 | '...<input type="hidden" name="amount" value="100000.0" />...', |
---|
| 209 | self.browser.contents) |
---|
| 210 | |
---|
| 211 | @external_test |
---|
| 212 | def test_webservice(self): |
---|
| 213 | |
---|
| 214 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 215 | self.assertMatches('...Unsuccessful callback...', |
---|
| 216 | self.browser.contents) |
---|
| 217 | # The payment is now in state failed |
---|
| 218 | self.assertMatches('...<span>Failed</span>...', |
---|
| 219 | self.browser.contents) |
---|