[7894] | 1 | ## $Id: tests.py 9791 2012-12-07 16:46:16Z 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 | ## |
---|
[9785] | 18 | import os |
---|
[9712] | 19 | from zope.component import getUtility |
---|
| 20 | from zope.catalog.interfaces import ICatalog |
---|
[7894] | 21 | from hurry.workflow.interfaces import IWorkflowState |
---|
| 22 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
[8266] | 23 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
| 24 | from waeup.kofa.configuration import SessionConfiguration |
---|
[8619] | 25 | from waeup.futminna.testing import FunctionalLayer |
---|
[7894] | 26 | |
---|
[7970] | 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. |
---|
[9785] | 30 | EXTERNAL_TESTS = True |
---|
[7970] | 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 | |
---|
[8266] | 43 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
[7929] | 44 | """Tests for the Interswitch payment gateway. |
---|
[7894] | 45 | """ |
---|
| 46 | |
---|
| 47 | layer = FunctionalLayer |
---|
| 48 | |
---|
[7970] | 49 | def setUp(self): |
---|
[8266] | 50 | super(InterswitchTestsStudents, self).setUp() |
---|
[7970] | 51 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
[7995] | 52 | self.browser.open(self.payments_path) |
---|
[7970] | 53 | IWorkflowState(self.student).setState('cleared') |
---|
[7995] | 54 | self.browser.open(self.payments_path + '/addop') |
---|
[9386] | 55 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
[7970] | 56 | self.browser.getControl("Create ticket").click() |
---|
[9285] | 57 | self.assertMatches('...Payment ticket created...', |
---|
[7970] | 58 | self.browser.contents) |
---|
[9285] | 59 | ctrl = self.browser.getControl(name='val_id') |
---|
[9785] | 60 | self.value = ctrl.options[0] |
---|
| 61 | self.browser.getLink(self.value).click() |
---|
[8266] | 62 | self.assertMatches('...Amount Authorized...', |
---|
| 63 | self.browser.contents) |
---|
| 64 | self.assertMatches( |
---|
[9285] | 65 | '...<span>131500.0</span>...', |
---|
[8266] | 66 | self.browser.contents) |
---|
| 67 | self.payment_url = self.browser.url |
---|
| 68 | |
---|
[9432] | 69 | def test_interswitch_form(self): |
---|
[8266] | 70 | # Manager can access InterswitchForm |
---|
[9785] | 71 | self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0) |
---|
| 72 | self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0) |
---|
[8266] | 73 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[9785] | 74 | # Split amounts have been set. |
---|
| 75 | self.assertEqual(self.student['payments'][self.value].provider_amt, 1500.0) |
---|
| 76 | self.assertEqual(self.student['payments'][self.value].gateway_amt, 300.0) |
---|
[9386] | 77 | # The total amount to be processed by Interswitch |
---|
| 78 | # has been reduced by the Interswitch fee of 150 Nairas |
---|
[8266] | 79 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 80 | self.browser.contents) |
---|
| 81 | self.assertMatches( |
---|
[9285] | 82 | '...<input type="hidden" name="amount" value="13150000.0" />...', |
---|
[8266] | 83 | self.browser.contents) |
---|
[9386] | 84 | self.assertMatches( |
---|
[9630] | 85 | '...item_name="School Fee" item_amt="12970000" bank_id="31" acct_num="0021030851"...', |
---|
[9386] | 86 | self.browser.contents) |
---|
| 87 | self.assertMatches( |
---|
[9630] | 88 | '...item_name="BT Education" item_amt="150000" bank_id="31" acct_num="0026781725"...', |
---|
[9386] | 89 | self.browser.contents) |
---|
[8266] | 90 | |
---|
[9386] | 91 | # Create clearance fee ticket |
---|
| 92 | self.browser.open(self.payments_path + '/addop') |
---|
| 93 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
| 94 | self.browser.getControl("Create ticket").click() |
---|
| 95 | self.assertMatches('...ticket created...', |
---|
| 96 | self.browser.contents) |
---|
| 97 | ctrl = self.browser.getControl(name='val_id') |
---|
| 98 | value = ctrl.options[1] |
---|
| 99 | self.browser.getLink(value).click() |
---|
| 100 | self.assertMatches('...Amount Authorized...', |
---|
| 101 | self.browser.contents) |
---|
| 102 | self.assertMatches( |
---|
[9714] | 103 | '...<span>3456.0</span>...', |
---|
[9386] | 104 | self.browser.contents) |
---|
| 105 | # Manager can access InterswitchForm |
---|
| 106 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[9785] | 107 | # Split amounts have been set. |
---|
| 108 | self.assertEqual(self.student['payments'][value].provider_amt, 0.0) |
---|
| 109 | self.assertEqual(self.student['payments'][value].gateway_amt, 300.0) |
---|
[9386] | 110 | # The total amount to be processed by Interswitch |
---|
| 111 | # has been reduced by the Interswitch fee of 150 Nairas |
---|
| 112 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 113 | self.browser.contents) |
---|
| 114 | self.assertMatches( |
---|
[9714] | 115 | '...<input type="hidden" name="amount" value="345600.0" />...', |
---|
[9386] | 116 | self.browser.contents) |
---|
| 117 | self.assertMatches( |
---|
[9714] | 118 | '...item_name="Acceptance Fee" item_amt="315600" bank_id="120" acct_num="1750005063"...', |
---|
[9386] | 119 | self.browser.contents) |
---|
| 120 | # BT does nor charge a fee for clearance |
---|
| 121 | self.assertFalse("BT Education" in self.browser.contents) |
---|
| 122 | |
---|
[9410] | 123 | # Create hostel maintenance ticket |
---|
| 124 | self.browser.open(self.payments_path + '/addop') |
---|
| 125 | self.browser.getControl(name="form.p_category").value = ['hostel_maintenance'] |
---|
| 126 | self.browser.getControl("Create ticket").click() |
---|
[9431] | 127 | self.assertMatches('...You have not yet booked accommodation...', |
---|
| 128 | self.browser.contents) |
---|
| 129 | # Students have to book bed first |
---|
| 130 | self.browser.open(self.acco_path) |
---|
| 131 | IWorkflowState(self.student).setState('admitted') |
---|
| 132 | self.browser.getLink("Book accommodation").click() |
---|
| 133 | self.assertFalse('Activation Code:' in self.browser.contents) |
---|
| 134 | self.browser.getControl("Create bed ticket").click() |
---|
| 135 | # Bed is randomly selected but, since there is only |
---|
| 136 | # one bed for this student, we know that ... |
---|
| 137 | self.assertMatches('...Hall 1, Block A, Room 101, Bed A...', |
---|
| 138 | self.browser.contents) |
---|
| 139 | self.browser.open(self.payments_path + '/addop') |
---|
| 140 | self.browser.getControl(name="form.p_category").value = ['hostel_maintenance'] |
---|
| 141 | self.browser.getControl("Create ticket").click() |
---|
[9410] | 142 | self.assertMatches('...ticket created...', |
---|
| 143 | self.browser.contents) |
---|
| 144 | ctrl = self.browser.getControl(name='val_id') |
---|
| 145 | value = ctrl.options[2] |
---|
| 146 | self.browser.getLink(value).click() |
---|
| 147 | self.assertMatches('...Amount Authorized...', |
---|
| 148 | self.browser.contents) |
---|
| 149 | self.assertMatches( |
---|
| 150 | '...<span>10000.0</span>...', |
---|
| 151 | self.browser.contents) |
---|
| 152 | # Manager can access InterswitchForm |
---|
| 153 | self.browser.getLink("CollegePAY", index=0).click() |
---|
[9785] | 154 | # Split amounts have been set. |
---|
| 155 | self.assertEqual(self.student['payments'][value].provider_amt, 0.0) |
---|
| 156 | self.assertEqual(self.student['payments'][value].gateway_amt, 300.0) |
---|
[9410] | 157 | # The total amount to be processed by Interswitch |
---|
| 158 | # has been reduced by the Interswitch fee of 150 Nairas |
---|
| 159 | self.assertMatches('...Total Amount Authorized:...', |
---|
| 160 | self.browser.contents) |
---|
| 161 | self.assertMatches( |
---|
| 162 | '...<input type="hidden" name="amount" value="1000000.0" />...', |
---|
| 163 | self.browser.contents) |
---|
| 164 | self.assertMatches( |
---|
| 165 | '...item_name="Hostel Maintenance Fee" item_amt="970000" bank_id="8" acct_num="2018856637"...', |
---|
| 166 | self.browser.contents) |
---|
| 167 | # BT does nor charge a fee for clearance |
---|
| 168 | self.assertFalse("BT Education" in self.browser.contents) |
---|
[9386] | 169 | |
---|
[9410] | 170 | |
---|
[8266] | 171 | @external_test |
---|
| 172 | def test_webservice(self): |
---|
[9785] | 173 | # First we have open InterswitchPageStudent to set provider_amt |
---|
| 174 | # and gateway_amt |
---|
| 175 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
| 176 | # Now we can call the webservice |
---|
[8266] | 177 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 178 | self.assertMatches('...Unsuccessful callback...', |
---|
| 179 | self.browser.contents) |
---|
[9712] | 180 | # The payment is now in state failed ... |
---|
[8266] | 181 | self.assertMatches('...<span>Failed</span>...', |
---|
[9712] | 182 | self.browser.contents) |
---|
| 183 | # ... and the catalog has been updated |
---|
| 184 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 185 | results = list( |
---|
| 186 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
| 187 | self.assertEqual(len(results), 1) |
---|
[9785] | 188 | self.assertEqual(results[0].p_state, 'failed') |
---|
| 189 | |
---|
[9791] | 190 | # Let's replace the p_id with a valid p_id of the FUTMinna |
---|
[9785] | 191 | # live system. This is definitely not an appropriate |
---|
| 192 | # solution for testing, but we have no choice since |
---|
| 193 | # Interswitch doesn't provide any interface |
---|
| 194 | # for testing. |
---|
| 195 | payment = self.student['payments'][self.value] |
---|
| 196 | payment.p_id = 'p3522296094055' |
---|
| 197 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 198 | self.assertMatches('...Callback amount does not match...', |
---|
| 199 | self.browser.contents) |
---|
| 200 | # The payment is now in state failed ... |
---|
| 201 | self.assertMatches('...<span>Failed</span>...', |
---|
| 202 | self.browser.contents) |
---|
| 203 | # Let's replace the amount autorized with the amount of the |
---|
| 204 | # live system payment |
---|
| 205 | payment.amount_auth = payment.r_amount_approved |
---|
| 206 | self.browser.open(self.payment_url + '/request_webservice') |
---|
| 207 | self.assertMatches('...Successful payment...', |
---|
| 208 | self.browser.contents) |
---|
| 209 | # The payment is now in state paid ... |
---|
| 210 | self.assertMatches('...<span>Paid</span>...', |
---|
| 211 | self.browser.contents) |
---|
| 212 | # ... and the catalog has been updated |
---|
| 213 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 214 | results = list( |
---|
| 215 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
| 216 | self.assertEqual(len(results), 1) |
---|
| 217 | self.assertEqual(results[0].p_state, 'paid') |
---|
| 218 | # Approval is logged in students.log ... |
---|
| 219 | logfile = os.path.join( |
---|
| 220 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
| 221 | logcontent = open(logfile).read() |
---|
| 222 | self.assertTrue( |
---|
| 223 | 'zope.mgr - ' |
---|
| 224 | 'waeup.futminna.interswitch.browser.InterswitchPaymentRequestWebservicePageStudent - ' |
---|
| 225 | 'M1000000 - successful schoolfee payment: p3522296094055\n' |
---|
| 226 | in logcontent) |
---|
| 227 | # ... and in payments.log |
---|
| 228 | logfile = os.path.join( |
---|
| 229 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
| 230 | logcontent = open(logfile).read() |
---|
| 231 | self.assertTrue( |
---|
| 232 | '"zope.mgr",M1000000,p3522296094055,schoolfee,' |
---|
| 233 | '20000.0,00,1500.0,300.0,0.0,,,\n' |
---|
| 234 | in logcontent) |
---|