1 | ## $Id: tests.py 13388 2015-11-04 11:13:30Z 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 waeup.aaue.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 InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
45 | """Tests for the Interswitch payment gateway. |
---|
46 | """ |
---|
47 | |
---|
48 | layer = FunctionalLayer |
---|
49 | |
---|
50 | def setUp(self): |
---|
51 | super(InterswitchTestsApplicants, self).setUp() |
---|
52 | configuration = SessionConfiguration() |
---|
53 | configuration.academic_session = datetime.now().year - 2 |
---|
54 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
55 | self.configuration = configuration |
---|
56 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
57 | self.browser.open(self.manage_path) |
---|
58 | #IWorkflowState(self.student).setState('started') |
---|
59 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
60 | self.browser.getControl(name="form.programme_type").value = ['regular'] |
---|
61 | self.applicantscontainer.application_fee = 1000.0 |
---|
62 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
63 | self.browser.getControl(name="transition").value = ['start'] |
---|
64 | self.browser.getControl("Save").click() |
---|
65 | self.browser.getControl("Add online").click() |
---|
66 | self.assertTrue('ticket created' in self.browser.contents) |
---|
67 | #self.browser.getLink(value).click() |
---|
68 | self.assertTrue('Amount Authorized' in self.browser.contents) |
---|
69 | self.assertTrue('<span>1000.0</span>' in self.browser.contents) |
---|
70 | self.payment_url = self.browser.url |
---|
71 | |
---|
72 | def test_interswitch_form(self): |
---|
73 | # Manager can access InterswitchForm |
---|
74 | self.browser.getLink("CollegePAY", index=0).click() |
---|
75 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
76 | self.assertTrue( |
---|
77 | '<input type="hidden" name="amount" value="100000" />' |
---|
78 | in self.browser.contents) |
---|
79 | delta = timedelta(days=8) |
---|
80 | self.applicant.values()[0].creation_date -= delta |
---|
81 | self.browser.open(self.payment_url) |
---|
82 | self.browser.getLink("CollegePAY", index=0).click() |
---|
83 | self.assertTrue( |
---|
84 | 'This payment ticket is too old. Please create a new ticket' |
---|
85 | in self.browser.contents) |
---|
86 | |
---|
87 | @external_test |
---|
88 | def test_webservice(self): |
---|
89 | |
---|
90 | self.browser.open(self.payment_url + '/request_webservice') |
---|
91 | self.assertTrue('Unsuccessful callback' in self.browser.contents) |
---|
92 | # The payment is now in state failed |
---|
93 | self.assertTrue('<span>Failed</span>' in self.browser.contents) |
---|
94 | |
---|
95 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
96 | """Tests for the Interswitch payment gateway. |
---|
97 | """ |
---|
98 | |
---|
99 | layer = FunctionalLayer |
---|
100 | |
---|
101 | def setUp(self): |
---|
102 | super(InterswitchTestsStudents, self).setUp() |
---|
103 | |
---|
104 | def test_interswitch_form_pt_school_fees(self): |
---|
105 | self.certificate.study_mode = 'ug_pt' |
---|
106 | self.student['studycourse'].entry_session = 2013 |
---|
107 | self.student['studycourse'].certificate.school_fee_1 = 51750.0 |
---|
108 | # Manager can access InterswitchForm |
---|
109 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
110 | self.browser.open(self.payments_path) |
---|
111 | IWorkflowState(self.student).setState('cleared') |
---|
112 | self.student.nationality = u'NG' |
---|
113 | self.browser.open(self.payments_path + '/addop') |
---|
114 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
115 | self.browser.getControl("Create ticket").click() |
---|
116 | self.assertTrue('ticket created' in self.browser.contents) |
---|
117 | ctrl = self.browser.getControl(name='val_id') |
---|
118 | self.value = ctrl.options[0] |
---|
119 | self.browser.getLink(self.value).click() |
---|
120 | self.assertTrue('Amount Authorized' in self.browser.contents) |
---|
121 | self.assertTrue('<span>51750.0</span>' in self.browser.contents) |
---|
122 | self.payment_url = self.browser.url |
---|
123 | self.browser.getLink("CollegePAY", index=0).click() |
---|
124 | self.assertTrue( |
---|
125 | 'Please pay acceptance fee first' in self.browser.contents) |
---|
126 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
127 | acc_payment.p_state = 'paid' |
---|
128 | acc_payment.p_category = 'clearance' |
---|
129 | self.student['payments']['xyz'] = acc_payment |
---|
130 | self.browser.getLink("CollegePAY", index=0).click() |
---|
131 | self.assertTrue('<input type="hidden" name="pay_item_id" value="105" />' |
---|
132 | in self.browser.contents) |
---|
133 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
134 | self.assertEqual(self.student.current_mode, 'ug_pt') |
---|
135 | self.assertTrue( |
---|
136 | '<input type="hidden" name="amount" value="5175000" />' |
---|
137 | in self.browser.contents) |
---|
138 | self.assertTrue( |
---|
139 | 'item_name="School Fee" item_amt="4600000" bank_id="7" ' |
---|
140 | 'acct_num="1014847058"' in self.browser.contents) |
---|
141 | |
---|
142 | def test_interswitch_form_regular_school_fees(self): |
---|
143 | self.certificate.study_mode = 'ug_ft' |
---|
144 | self.student['studycourse'].entry_session = 2013 |
---|
145 | self.student['studycourse'].certificate.school_fee_1 = 51750.0 |
---|
146 | # Manager can access InterswitchForm |
---|
147 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
148 | self.browser.open(self.payments_path) |
---|
149 | IWorkflowState(self.student).setState('cleared') |
---|
150 | self.student.nationality = u'NG' |
---|
151 | self.browser.open(self.payments_path + '/addop') |
---|
152 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
153 | self.browser.getControl("Create ticket").click() |
---|
154 | self.assertTrue('ticket created' in |
---|
155 | self.browser.contents) |
---|
156 | ctrl = self.browser.getControl(name='val_id') |
---|
157 | self.value = ctrl.options[0] |
---|
158 | self.browser.getLink(self.value).click() |
---|
159 | self.assertTrue('Amount Authorized' in |
---|
160 | self.browser.contents) |
---|
161 | self.assertTrue('<span>51750.0</span>' in self.browser.contents) |
---|
162 | self.payment_url = self.browser.url |
---|
163 | self.browser.getLink("CollegePAY", index=0).click() |
---|
164 | self.assertTrue( |
---|
165 | 'Please pay acceptance fee first' in self.browser.contents) |
---|
166 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
167 | acc_payment.p_state = 'paid' |
---|
168 | acc_payment.p_category = 'clearance' |
---|
169 | self.student['payments']['xyz'] = acc_payment |
---|
170 | self.browser.getLink("CollegePAY", index=0).click() |
---|
171 | self.assertTrue('<input type="hidden" name="pay_item_id" value="101" />' |
---|
172 | in self.browser.contents) |
---|
173 | self.assertTrue('Total Amount Authorized:' |
---|
174 | in self.browser.contents) |
---|
175 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
176 | self.assertTrue( |
---|
177 | '<input type="hidden" name="amount" value="5175000" />' |
---|
178 | in self.browser.contents) |
---|
179 | self.assertTrue( |
---|
180 | 'item_name="School Fee" item_amt="4750000" bank_id="117" ' |
---|
181 | 'acct_num="1010827641"' in self.browser.contents) |
---|
182 | |
---|
183 | |
---|
184 | def test_interswitch_form_pt_acceptance_fees(self): |
---|
185 | self.certificate.study_mode = 'ug_pt' |
---|
186 | self.app['configuration']['2004'].clearance_fee_pt = 43050.0 |
---|
187 | # Manager can access InterswitchForm |
---|
188 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
189 | self.browser.open(self.payments_path) |
---|
190 | IWorkflowState(self.student).setState('admitted') |
---|
191 | self.student.nationality = u'NG' |
---|
192 | self.browser.open(self.payments_path + '/addop') |
---|
193 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
194 | self.browser.getControl("Create ticket").click() |
---|
195 | self.assertTrue('ticket created' in self.browser.contents) |
---|
196 | ctrl = self.browser.getControl(name='val_id') |
---|
197 | self.value = ctrl.options[0] |
---|
198 | self.browser.getLink(self.value).click() |
---|
199 | self.assertTrue('Amount Authorized' in self.browser.contents) |
---|
200 | self.assertTrue('<span>43050.0</span>' in self.browser.contents) |
---|
201 | self.payment_url = self.browser.url |
---|
202 | self.browser.getLink("CollegePAY", index=0).click() |
---|
203 | self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />' in |
---|
204 | self.browser.contents) |
---|
205 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
206 | self.assertTrue( |
---|
207 | '<input type="hidden" name="amount" value="4305000" />' |
---|
208 | in self.browser.contents) |
---|
209 | self.assertTrue( |
---|
210 | 'item_name="Acceptance Fee" item_amt="4000000" bank_id="117" ' |
---|
211 | 'acct_num="1014066976"' in self.browser.contents) |
---|
212 | |
---|
213 | def test_interswitch_form_regular_acceptance_fees(self): |
---|
214 | self.certificate.study_mode = 'ug_ft' |
---|
215 | self.app['configuration']['2004'].clearance_fee = 43050.0 |
---|
216 | # Manager can access InterswitchForm |
---|
217 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
218 | self.browser.open(self.payments_path) |
---|
219 | IWorkflowState(self.student).setState('admitted') |
---|
220 | self.student.nationality = u'NG' |
---|
221 | self.browser.open(self.payments_path + '/addop') |
---|
222 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
223 | self.browser.getControl("Create ticket").click() |
---|
224 | self.assertTrue('ticket created' in self.browser.contents) |
---|
225 | ctrl = self.browser.getControl(name='val_id') |
---|
226 | self.value = ctrl.options[0] |
---|
227 | self.browser.getLink(self.value).click() |
---|
228 | self.assertTrue('Amount Authorized' in self.browser.contents) |
---|
229 | self.assertTrue('<span>43050.0</span>' in self.browser.contents) |
---|
230 | self.payment_url = self.browser.url |
---|
231 | self.browser.getLink("CollegePAY", index=0).click() |
---|
232 | self.assertTrue('<input type="hidden" name="pay_item_id" value="102" />' |
---|
233 | in self.browser.contents) |
---|
234 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
235 | self.assertTrue( |
---|
236 | '<input type="hidden" name="amount" value="4305000" />' |
---|
237 | in self.browser.contents) |
---|
238 | self.assertTrue( |
---|
239 | 'item_name="Acceptance Fee" item_amt="4280000" bank_id="117" ' |
---|
240 | 'acct_num="1010827641"' in self.browser.contents) |
---|
241 | |
---|
242 | def test_interswitch_form_welfare_fees(self): |
---|
243 | self.certificate.study_mode = 'ug_pt' |
---|
244 | self.app['configuration']['2004'].welfare_fee = 1567.0 |
---|
245 | # Manager can access InterswitchForm |
---|
246 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
247 | self.browser.open(self.payments_path) |
---|
248 | IWorkflowState(self.student).setState('admitted') |
---|
249 | self.student.nationality = u'NG' |
---|
250 | self.browser.open(self.payments_path + '/addop') |
---|
251 | self.browser.getControl(name="form.p_category").value = ['welfare'] |
---|
252 | self.browser.getControl("Create ticket").click() |
---|
253 | self.assertTrue('ticket created' in self.browser.contents) |
---|
254 | ctrl = self.browser.getControl(name='val_id') |
---|
255 | self.value = ctrl.options[0] |
---|
256 | self.browser.getLink(self.value).click() |
---|
257 | self.assertTrue('Amount Authorized' in self.browser.contents) |
---|
258 | self.assertTrue( |
---|
259 | '<span>1567.0</span>' in self.browser.contents) |
---|
260 | self.payment_url = self.browser.url |
---|
261 | self.browser.getLink("CollegePAY", index=0).click() |
---|
262 | self.assertTrue('<div class="alert alert-danger">Not applicable</div>' |
---|
263 | in self.browser.contents) |
---|
264 | self.certificate.study_mode = 'ug_ft' |
---|
265 | self.browser.getLink("CollegePAY", index=0).click() |
---|
266 | self.assertTrue('<input type="hidden" name="pay_item_id" value="105" />' |
---|
267 | in self.browser.contents) |
---|
268 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
269 | self.assertTrue( |
---|
270 | '<input type="hidden" name="amount" value="156700" />' |
---|
271 | in self.browser.contents) |
---|
272 | self.assertTrue( |
---|
273 | 'item_name="AAU Student Welfare Assurance Fee" item_amt="131700" ' |
---|
274 | 'bank_id="123" acct_num="1006407792"' |
---|
275 | in self.browser.contents) |
---|
276 | |
---|
277 | def test_interswitch_form_ticket_expired(self): |
---|
278 | self.certificate.study_mode = 'ug_pt' |
---|
279 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
280 | acc_payment = createObject('waeup.StudentOnlinePayment') |
---|
281 | acc_payment.p_state = 'unpaid' |
---|
282 | acc_payment.p_category = 'clearance' |
---|
283 | acc_payment.p_id = 'xyz' |
---|
284 | acc_payment.pay_item_id = '123' |
---|
285 | acc_payment.amount_auth = 876.0 |
---|
286 | self.student['payments']['xyz'] = acc_payment |
---|
287 | self.browser.open(self.payments_path + '/xyz') |
---|
288 | self.browser.getLink("CollegePAY", index=0).click() |
---|
289 | self.assertTrue('<input type="hidden" name="pay_item_id" value="104" />' |
---|
290 | in self.browser.contents) |
---|
291 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
292 | self.assertEqual(self.student.current_mode, 'ug_pt') |
---|
293 | self.assertTrue( |
---|
294 | '<input type="hidden" name="amount" value="87600" />' |
---|
295 | in self.browser.contents) |
---|
296 | delta = timedelta(days=8) |
---|
297 | acc_payment.creation_date -= delta |
---|
298 | self.browser.open(self.payments_path + '/xyz') |
---|
299 | self.browser.getLink("CollegePAY", index=0).click() |
---|
300 | self.assertTrue( |
---|
301 | 'This payment ticket is too old. Please create a new ticket' |
---|
302 | in self.browser.contents) |
---|
303 | delta = timedelta(days=2) |
---|
304 | acc_payment.creation_date += delta |
---|
305 | self.browser.open(self.payments_path + '/xyz') |
---|
306 | self.browser.getLink("CollegePAY", index=0).click() |
---|
307 | self.assertTrue('Total Amount Authorized:' in self.browser.contents) |
---|
308 | |
---|
309 | @external_test |
---|
310 | def test_webservice(self): |
---|
311 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
312 | self.browser.open(self.payments_path) |
---|
313 | IWorkflowState(self.student).setState('cleared') |
---|
314 | self.student.nationality = u'NG' |
---|
315 | self.browser.open(self.payments_path + '/addop') |
---|
316 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
317 | self.browser.getControl("Create ticket").click() |
---|
318 | self.assertTrue('ticket created' in self.browser.contents) |
---|
319 | ctrl = self.browser.getControl(name='val_id') |
---|
320 | self.value = ctrl.options[0] |
---|
321 | self.browser.getLink(self.value).click() |
---|
322 | self.payment_url = self.browser.url |
---|
323 | # First we have to open InterswitchPageStudent to set provider_amt |
---|
324 | # and gateway_amt |
---|
325 | self.browser.open(self.payment_url + '/goto_interswitch') |
---|
326 | # Now we can call the webservice |
---|
327 | self.browser.open(self.payment_url + '/request_webservice') |
---|
328 | self.assertTrue('Unsuccessful callback' in self.browser.contents) |
---|
329 | # The payment is now in state failed ... |
---|
330 | self.assertTrue('<span>Failed</span>' in self.browser.contents) |
---|
331 | # ... and the catalog has been updated |
---|
332 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
333 | results = list( |
---|
334 | cat.searchResults(p_state=('failed', 'failed'))) |
---|
335 | self.assertEqual(len(results), 1) |
---|
336 | self.assertEqual(results[0].p_state, 'failed') |
---|
337 | |
---|
338 | # Let's replace the p_id with a valid p_id of the AAUE |
---|
339 | # live system. This is definitely not an appropriate |
---|
340 | # solution for testing, but we have no choice since |
---|
341 | # Interswitch doesn't provide any interface |
---|
342 | # for testing. |
---|
343 | payment = self.student['payments'][self.value] |
---|
344 | payment.p_id = 'p4465649308559' |
---|
345 | self.browser.open(self.payment_url + '/request_webservice') |
---|
346 | self.assertTrue('Unsuccessful callback: Amount Inconsistency' in |
---|
347 | self.browser.contents) |
---|
348 | # The payment is now in state failed ... |
---|
349 | self.assertTrue('<span>Failed</span>' in self.browser.contents) |
---|
350 | # Let's replace the amount autorized with the amount of the |
---|
351 | # live system payment |
---|
352 | payment.amount_auth = 60250.0 |
---|
353 | self.browser.open(self.payment_url + '/request_webservice') |
---|
354 | self.assertTrue('Successful payment' in |
---|
355 | self.browser.contents) |
---|
356 | # The payment is now in state paid ... |
---|
357 | self.assertTrue('<span>Paid</span>' in self.browser.contents) |
---|
358 | # ... and the catalog has been updated |
---|
359 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
360 | results = list( |
---|
361 | cat.searchResults(p_state=('paid', 'paid'))) |
---|
362 | self.assertEqual(len(results), 1) |
---|
363 | self.assertEqual(results[0].p_state, 'paid') |
---|
364 | # Approval is logged in students.log ... |
---|
365 | logfile = os.path.join( |
---|
366 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
367 | logcontent = open(logfile).read() |
---|
368 | self.assertTrue( |
---|
369 | 'zope.mgr - ' |
---|
370 | 'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - ' |
---|
371 | 'E1000000 - successful schoolfee payment: p4465649308559\n' |
---|
372 | in logcontent) |
---|
373 | # ... and in payments.log |
---|
374 | logfile = os.path.join( |
---|
375 | self.app['datacenter'].storage, 'logs', 'payments.log') |
---|
376 | logcontent = open(logfile).read() |
---|
377 | self.assertTrue( |
---|
378 | '"zope.mgr",E1000000,p4465649308559,schoolfee,' |
---|
379 | '60250.0,00,1500.0,250.0,0.0,,,\n' |
---|
380 | in logcontent) |
---|