1 | ## $Id: tests.py 15087 2018-07-12 07:33:18Z 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 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 |
---|
26 | from kofacustom.edopoly.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 | # School Fee |
---|
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.assertTrue( |
---|
64 | 'Pay ICT, Logbook and SIWESS fee(s) first.' in self.browser.contents) |
---|
65 | |
---|
66 | # Add required payments first. |
---|
67 | payment = createObject('waeup.StudentOnlinePayment') |
---|
68 | payment.p_state = 'paid' |
---|
69 | payment.p_category = 'ict_entre' |
---|
70 | payment.p_id = '123' |
---|
71 | payment.p_session = 2004 |
---|
72 | self.student['payments']['123'] = payment |
---|
73 | payment = createObject('waeup.StudentOnlinePayment') |
---|
74 | payment.p_state = 'paid' |
---|
75 | payment.p_category = 'logbook_combo' |
---|
76 | payment.p_id = '123' |
---|
77 | payment.p_session = 2004 |
---|
78 | self.student['payments']['234'] = payment |
---|
79 | payment = createObject('waeup.StudentOnlinePayment') |
---|
80 | payment.p_state = 'paid' |
---|
81 | payment.p_category = 'siwess_combo' |
---|
82 | payment.p_id = '123' |
---|
83 | payment.p_session = 2004 |
---|
84 | self.student['payments']['345'] = payment |
---|
85 | |
---|
86 | self.browser.open(self.payments_path + '/addop') |
---|
87 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
88 | self.browser.getControl("Create ticket").click() |
---|
89 | self.assertMatches('...ticket created...', |
---|
90 | self.browser.contents) |
---|
91 | ctrl = self.browser.getControl(name='val_id') |
---|
92 | self.value = ctrl.options[3] |
---|
93 | self.browser.getLink(self.value).click() |
---|
94 | self.assertMatches('...Amount Authorized...', |
---|
95 | self.browser.contents) |
---|
96 | self.assertTrue( |
---|
97 | '<span>40000.0</span>' in self.browser.contents) |
---|
98 | self.payment_url = self.browser.url |
---|
99 | self.browser.getLink("CollegePAY", index=0).click() |
---|
100 | self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in |
---|
101 | self.browser.contents) |
---|
102 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
103 | self.assertTrue( |
---|
104 | '<input type="hidden" name="amount" value="4000000" />' in |
---|
105 | self.browser.contents) |
---|
106 | self.assertTrue( |
---|
107 | 'item_name="Tuition/Exams Fee and Student Union Dues" item_amt="3775000" bank_id="121" acct_num="0068241848"' in |
---|
108 | self.browser.contents) |
---|
109 | self.assertTrue( |
---|
110 | 'item_name="Student Union Dues" item_amt="200000" bank_id="121" acct_num="0066437412"' in |
---|
111 | self.browser.contents) |
---|
112 | # Clearance Fee |
---|
113 | self.browser.open(self.payments_path + '/addop') |
---|
114 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
115 | self.browser.getControl("Create ticket").click() |
---|
116 | self.assertMatches('...ticket created...', |
---|
117 | self.browser.contents) |
---|
118 | ctrl = self.browser.getControl(name='val_id') |
---|
119 | self.value = ctrl.options[4] |
---|
120 | self.browser.getLink(self.value).click() |
---|
121 | self.assertTrue( |
---|
122 | '<span>3456.0</span>' in self.browser.contents) |
---|
123 | self.payment_url = self.browser.url |
---|
124 | self.browser.getLink("CollegePAY", index=0).click() |
---|
125 | self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' in |
---|
126 | self.browser.contents) |
---|
127 | self.assertTrue( |
---|
128 | '<input type="hidden" name="amount" value="345600" />' in |
---|
129 | self.browser.contents) |
---|
130 | self.assertTrue( |
---|
131 | 'item_name="Acceptance Fee" item_amt="170600" bank_id="121" acct_num="0068241848"' in |
---|
132 | self.browser.contents) |
---|
133 | |
---|
134 | def test_interswitch_form_ticket_expired(self): |
---|
135 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
136 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
137 | acc_payment.p_state = 'unpaid' |
---|
138 | acc_payment.p_category = 'clearance' |
---|
139 | acc_payment.p_id = 'xyz' |
---|
140 | acc_payment.pay_item_id = '123' |
---|
141 | acc_payment.amount_auth = 876.0 |
---|
142 | self.student['payments']['xyz'] = acc_payment |
---|
143 | self.browser.open(self.payments_path + '/xyz') |
---|
144 | self.browser.getLink("CollegePAY", index=0).click() |
---|
145 | self.assertMatches('...<input type="hidden" name="pay_item_id" value="101" />...', |
---|
146 | self.browser.contents) |
---|
147 | self.assertMatches('...Total Amount Authorized:...', |
---|
148 | self.browser.contents) |
---|
149 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
150 | self.assertMatches( |
---|
151 | '...<input type="hidden" name="amount" value="87600" />...', |
---|
152 | self.browser.contents) |
---|
153 | delta = timedelta(days=8) |
---|
154 | acc_payment.creation_date -= delta |
---|
155 | self.browser.open(self.payments_path + '/xyz') |
---|
156 | self.browser.getLink("CollegePAY", index=0).click() |
---|
157 | self.assertMatches( |
---|
158 | '...This payment ticket is too old. Please create a new ticket...', |
---|
159 | self.browser.contents) |
---|
160 | delta = timedelta(days=2) |
---|
161 | acc_payment.creation_date += delta |
---|
162 | self.browser.open(self.payments_path + '/xyz') |
---|
163 | self.browser.getLink("CollegePAY", index=0).click() |
---|
164 | self.assertMatches('...Total Amount Authorized:...', |
---|
165 | self.browser.contents) |
---|
166 | |
---|
167 | @external_test |
---|
168 | def test_webservice(self): |
---|
169 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
170 | self.browser.open(self.payments_path) |
---|
171 | IWorkflowState(self.student).setState('cleared') |
---|
172 | self.student.nationality = u'NG' |
---|
173 | self.browser.open(self.payments_path + '/addop') |
---|
174 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
175 | self.browser.getControl("Create ticket").click() |
---|
176 | self.assertMatches('...ticket created...', |
---|
177 | self.browser.contents) |
---|
178 | ctrl = self.browser.getControl(name='val_id') |
---|
179 | self.value = ctrl.options[0] |
---|
180 | self.browser.getLink(self.value).click() |
---|
181 | self.payment_url = self.browser.url |
---|
182 | # First we have open InterswitchPageStudent to set provider_amt |
---|
183 | # and gateway_amt |
---|
184 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
185 | # Now we can call the webservice |
---|
186 | self.browser.open(self.payment_url + '/request_webservice') |
---|
187 | self.assertMatches('...Unsuccessful callback...', |
---|
188 | self.browser.contents) |
---|
189 | # The payment is now in state failed ... |
---|
190 | self.assertMatches('...<span>Failed</span>...', |
---|
191 | self.browser.contents) |
---|
192 | # ... and the catalog has been updated |
---|
193 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
194 | results = list( |
---|
195 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
196 | self.assertEqual(len(results), 1) |
---|
197 | self.assertEqual(results[0].p_state, 'failed') |
---|
198 | |
---|
199 | # Let's replace the p_id with a valid p_id of the Uniben |
---|
200 | # live system. This is definitely not an appropriate |
---|
201 | # solution for testing, but we have no choice since |
---|
202 | # Interswitch doesn't provide any interface |
---|
203 | # for testing. |
---|
204 | payment = self.student['payments'][self.value] |
---|
205 | payment.p_id = 'p3547789850240' |
---|
206 | self.browser.open(self.payment_url + '/request_webservice') |
---|
207 | self.assertMatches('...Callback amount does not match...', |
---|
208 | self.browser.contents) |
---|
209 | # The payment is now in state failed ... |
---|
210 | self.assertMatches('...<span>Failed</span>...', |
---|
211 | self.browser.contents) |
---|
212 | # Let's replace the amount autorized with the amount of the |
---|
213 | # live system payment |
---|
214 | payment.amount_auth = payment.r_amount_approved |
---|
215 | self.browser.open(self.payment_url + '/request_webservice') |
---|
216 | self.assertMatches('...Successful payment...', |
---|
217 | self.browser.contents) |
---|
218 | # The payment is now in state paid ... |
---|
219 | self.assertMatches('...<span>Paid</span>...', |
---|
220 | self.browser.contents) |
---|
221 | # ... and the catalog has been updated |
---|
222 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
223 | results = list( |
---|
224 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
225 | self.assertEqual(len(results), 1) |
---|
226 | self.assertEqual(results[0].p_state, 'paid') |
---|
227 | # Approval is logged in students.log ... |
---|
228 | logfile = os.path.join( |
---|
229 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
230 | logcontent = open(logfile).read() |
---|
231 | self.assertTrue( |
---|
232 | 'zope.mgr - ' |
---|
233 | 'kofacustom.edopoly.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
234 | 'X1000000 - successful schoolfee payment: p3547789850240\n' |
---|
235 | in logcontent) |
---|
236 | # ... and in payments.log |
---|
237 | logfile = os.path.join( |
---|
238 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
239 | logcontent = open(logfile).read() |
---|
240 | self.assertTrue( |
---|
241 | '"zope.mgr",X1000000,p3547789850240,schoolfee,' |
---|
242 | '12000.0,00,0.0,150.0,0.0,,,\n' |
---|
243 | in logcontent) |
---|
244 | |
---|
245 | |
---|
246 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
247 | """Tests for the Interswitch payment gateway. |
---|
248 | """ |
---|
249 | |
---|
250 | layer = FunctionalLayer |
---|
251 | |
---|
252 | def setUp(self): |
---|
253 | super(InterswitchTestsApplicants, self).setUp() |
---|
254 | configuration = SessionConfiguration() |
---|
255 | configuration.academic_session = datetime.now().year - 2 |
---|
256 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
257 | self.configuration = configuration |
---|
258 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
259 | self.browser.open(self.manage_path) |
---|
260 | #IWorkflowState(self.student).setState('started') |
---|
261 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
262 | self.applicantscontainer.application_fee = 1000.0 |
---|
263 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
264 | self.browser.getControl(name="transition").value = ['start'] |
---|
265 | self.browser.getControl("Save").click() |
---|
266 | self.browser.getControl("Add online").click() |
---|
267 | self.assertMatches('...ticket created...', |
---|
268 | self.browser.contents) |
---|
269 | #ctrl = self.browser.getControl(name='val_id') |
---|
270 | #value = ctrl.options[0] |
---|
271 | #self.browser.getLink(value).click() |
---|
272 | self.assertMatches('...Amount Authorized...', |
---|
273 | self.browser.contents) |
---|
274 | self.assertMatches( |
---|
275 | '...<span>1000.0</span>...', |
---|
276 | self.browser.contents) |
---|
277 | self.payment_url = self.browser.url |
---|
278 | |
---|
279 | |
---|
280 | def test_interswitch_form(self): |
---|
281 | # Manager can access InterswitchForm |
---|
282 | self.browser.getLink("CollegePAY", index=0).click() |
---|
283 | self.assertMatches('...Total Amount Authorized:...', |
---|
284 | self.browser.contents) |
---|
285 | self.assertMatches( |
---|
286 | '...<input type="hidden" name="amount" value="100000" />...', |
---|
287 | self.browser.contents) |
---|
288 | delta = timedelta(days=8) |
---|
289 | self.applicant.values()[0].creation_date -= delta |
---|
290 | self.browser.open(self.payment_url) |
---|
291 | self.browser.getLink("CollegePAY", index=0).click() |
---|
292 | self.assertMatches( |
---|
293 | '...This payment ticket is too old. Please create a new ticket...', |
---|
294 | self.browser.contents) |
---|
295 | |
---|
296 | @external_test |
---|
297 | def test_webservice(self): |
---|
298 | |
---|
299 | self.browser.open(self.payment_url + '/request_webservice') |
---|
300 | self.assertMatches('...Unsuccessful callback...', |
---|
301 | self.browser.contents) |
---|
302 | # The payment is now in state failed |
---|
303 | self.assertMatches('...<span>Failed</span>...', |
---|
304 | self.browser.contents) |
---|