[10765] | 1 | ## $Id: tests.py 10765 2013-11-19 12:57:15Z 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 |
---|
| 19 | from zope.component import createObject, getUtility |
---|
| 20 | from zope.catalog.interfaces import ICatalog |
---|
| 21 | from hurry.workflow.interfaces import IWorkflowState |
---|
| 22 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
| 23 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
| 24 | from waeup.kofa.configuration import SessionConfiguration |
---|
| 25 | from kofacustom.skeleton.testing import FunctionalLayer |
---|
| 26 | |
---|
| 27 | # Also run tests that send requests to external servers? |
---|
| 28 | # If you enable this, please make sure the external services |
---|
| 29 | # do exist really and are not bothered by being spammed by a test programme. |
---|
| 30 | EXTERNAL_TESTS = False |
---|
| 31 | |
---|
| 32 | def external_test(func): |
---|
| 33 | if not EXTERNAL_TESTS: |
---|
| 34 | myself = __file__ |
---|
| 35 | if myself.endswith('.pyc'): |
---|
| 36 | myself = myself[:-2] |
---|
| 37 | print "WARNING: external tests are skipped!" |
---|
| 38 | print "WARNING: edit %s to enable them." % myself |
---|
| 39 | return |
---|
| 40 | return func |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
| 44 | """Tests for the Interswitch payment gateway. |
---|
| 45 | """ |
---|
| 46 | |
---|
| 47 | layer = FunctionalLayer |
---|
| 48 | |
---|
| 49 | def setUp(self): |
---|
| 50 | super(InterswitchTestsStudents, self).setUp() |
---|
| 51 | |
---|
| 52 | def test_interswitch_form(self): |
---|
| 53 | # Manager can access InterswitchForm |
---|
| 54 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 55 | self.browser.open(self.payments_path) |
---|
| 56 | IWorkflowState(self.student).setState('cleared') |
---|
| 57 | self.student.nationality = u'NG' |
---|
| 58 | self.browser.open(self.payments_path + '/addop') |
---|
| 59 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 60 | self.browser.getControl("Create ticket").click() |
---|
| 61 | self.assertMatches('...ticket created...', |
---|
| 62 | self.browser.contents) |
---|
| 63 | ctrl = self.browser.getControl(name='val_id') |
---|
| 64 | self.value = ctrl.options[0] |
---|
| 65 | self.browser.getLink(self.value).click() |
---|
| 66 | self.assertMatches('...Amount Authorized...', |
---|
| 67 | self.browser.contents) |
---|
| 68 | self.assertMatches( |
---|
| 69 | '...<span>40000.0</span>...', |
---|
| 70 | self.browser.contents) |
---|
| 71 | self.payment_url = self.browser.url |
---|
| 72 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 73 | # The total amount to be processed by Interswitch |
---|
| 74 | # has been reduced by the Interswitch fee of 150 Nairas |
---|
| 75 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="0000" />...', |
---|
| 76 | self.browser.contents) |
---|
| 77 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 78 | self.browser.contents) |
---|
| 79 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
| 80 | self.assertMatches( |
---|
| 81 | '...<input type="hidden" name="amount" value="4000000.0" />...', |
---|
| 82 | self.browser.contents) |
---|
| 83 | self.assertMatches( |
---|
| 84 | '...item_name="School Fee" item_amt="4000000" bank_id="00" acct_num="00000000"...', |
---|
| 85 | self.browser.contents) |
---|
| 86 | |
---|
| 87 | @external_test |
---|
| 88 | def test_webservice(self): |
---|
| 89 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 90 | self.browser.open(self.payments_path) |
---|
| 91 | IWorkflowState(self.student).setState('cleared') |
---|
| 92 | self.student.nationality = u'NG' |
---|
| 93 | self.browser.open(self.payments_path + '/addop') |
---|
| 94 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
| 95 | self.browser.getControl("Create ticket").click() |
---|
| 96 | self.assertMatches('...ticket created...', |
---|
| 97 | self.browser.contents) |
---|
| 98 | ctrl = self.browser.getControl(name='val_id') |
---|
| 99 | self.value = ctrl.options[0] |
---|
| 100 | self.browser.getLink(self.value).click() |
---|
| 101 | self.payment_url = self.browser.url |
---|
| 102 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 103 | # and gateway_amt |
---|
| 104 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 105 | # Now we can call the webservice |
---|
| 106 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 107 | self.assertMatches('...Unsuccessful callback...', |
---|
| 108 | self.browser.contents) |
---|
| 109 | # The payment is now in state failed ... |
---|
| 110 | self.assertMatches('...<span>Failed</span>...', |
---|
| 111 | self.browser.contents) |
---|
| 112 | # ... and the catalog has been updated |
---|
| 113 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 114 | results = list( |
---|
| 115 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 116 | self.assertEqual(len(results), 1) |
---|
| 117 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 118 | |
---|
| 119 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
| 120 | # live system. This is definitely not an appropriate |
---|
| 121 | # solution for testing, but we have no choice since |
---|
| 122 | # Interswitch doesn't provide any interface |
---|
| 123 | # for testing. |
---|
| 124 | payment = self.student['payments'][self.value] |
---|
| 125 | payment.p_id = 'p3547789850240' |
---|
| 126 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 127 | self.assertMatches('...Callback amount does not match...', |
---|
| 128 | self.browser.contents) |
---|
| 129 | # The payment is now in state failed ... |
---|
| 130 | self.assertMatches('...<span>Failed</span>...', |
---|
| 131 | self.browser.contents) |
---|
| 132 | # Let's replace the amount autorized with the amount of the |
---|
| 133 | # live system payment |
---|
| 134 | payment.amount_auth = payment.r_amount_approved |
---|
| 135 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 136 | self.assertMatches('...Successful payment...', |
---|
| 137 | self.browser.contents) |
---|
| 138 | # The payment is now in state paid ... |
---|
| 139 | self.assertMatches('...<span>Paid</span>...', |
---|
| 140 | self.browser.contents) |
---|
| 141 | # ... and the catalog has been updated |
---|
| 142 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 143 | results = list( |
---|
| 144 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 145 | self.assertEqual(len(results), 1) |
---|
| 146 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 147 | # Approval is logged in students.log ... |
---|
| 148 | logfile = os.path.join( |
---|
| 149 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 150 | logcontent = open(logfile).read() |
---|
| 151 | self.assertTrue( |
---|
| 152 | 'zope.mgr - ' |
---|
| 153 | 'kofacustom.skeleton.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - ' |
---|
| 154 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
| 155 | in logcontent) |
---|
| 156 | # ... and in payments.log |
---|
| 157 | logfile = os.path.join( |
---|
| 158 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 159 | logcontent = open(logfile).read() |
---|
| 160 | self.assertTrue( |
---|
| 161 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
| 162 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
| 163 | in logcontent) |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
| 167 | """Tests for the Interswitch payment gateway. |
---|
| 168 | """ |
---|
| 169 | |
---|
| 170 | layer = FunctionalLayer |
---|
| 171 | |
---|
| 172 | def setUp(self): |
---|
| 173 | super(InterswitchTestsApplicants, self).setUp() |
---|
| 174 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 175 | self.browser.open(self.manage_path) |
---|
| 176 | #IWorkflowState(self.student).setState('started') |
---|
| 177 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
| 178 | self.applicantscontainer.application_fee = 1000.0 |
---|
| 179 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 180 | self.browser.getControl(name="transition").value = ['start'] |
---|
| 181 | self.browser.getControl("Save").click() |
---|
| 182 | self.browser.getControl("Add online").click() |
---|
| 183 | self.assertMatches('...ticket created...', |
---|
| 184 | self.browser.contents) |
---|
| 185 | #ctrl = self.browser.getControl(name='val_id') |
---|
| 186 | #value = ctrl.options[0] |
---|
| 187 | #self.browser.getLink(value).click() |
---|
| 188 | self.assertMatches('...Amount Authorized...', |
---|
| 189 | self.browser.contents) |
---|
| 190 | self.assertMatches( |
---|
| 191 | '...<span>1000.0</span>...', |
---|
| 192 | self.browser.contents) |
---|
| 193 | self.payment_url = self.browser.url |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | def test_interswitch_form(self): |
---|
| 197 | |
---|
| 198 | # Manager can access InterswitchForm |
---|
| 199 | self.browser.getLink("CollegePAY", index=0).click() |
---|
| 200 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 201 | self.browser.contents) |
---|
| 202 | self.assertMatches( |
---|
| 203 | '...<input type="hidden" name="amount" value="100000.0" />...', |
---|
| 204 | self.browser.contents) |
---|
| 205 | |
---|
| 206 | @external_test |
---|
| 207 | def test_webservice(self): |
---|
| 208 | |
---|
| 209 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 210 | self.assertMatches('...Unsuccessful callback...', |
---|
| 211 | self.browser.contents) |
---|
| 212 | # The payment is now in state failed |
---|
| 213 | self.assertMatches('...<span>Failed</span>...', |
---|
| 214 | self.browser.contents) |
---|