1 | ## $Id: tests.py 15702 2019-10-25 09:59:20Z 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, timedelta, date |
---|
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.kofa.students.payments import StudentOnlinePayment |
---|
27 | from kofacustom.nigeria.interswitch.helpers import query_interswitch |
---|
28 | from kofacustom.nigeria.testing import FunctionalLayer |
---|
29 | |
---|
30 | # Also run tests that send requests to external servers? |
---|
31 | # If you enable this, please make sure the external services |
---|
32 | # do exist really and are not bothered by being spammed by a test programme. |
---|
33 | EXTERNAL_TESTS = False |
---|
34 | |
---|
35 | def external_test(func): |
---|
36 | if not EXTERNAL_TESTS: |
---|
37 | myself = __file__ |
---|
38 | if myself.endswith('.pyc'): |
---|
39 | myself = myself[:-1] |
---|
40 | print "WARNING: external tests are skipped!" |
---|
41 | print "WARNING: edit %s to enable them." % myself |
---|
42 | return |
---|
43 | return func |
---|
44 | |
---|
45 | class InterswitchTestsStudents(StudentsFullSetup): |
---|
46 | """Tests for the Interswitch payment gateway. |
---|
47 | """ |
---|
48 | |
---|
49 | layer = FunctionalLayer |
---|
50 | |
---|
51 | def setUp(self): |
---|
52 | super(InterswitchTestsStudents, self).setUp() |
---|
53 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
54 | self.browser.open(self.payments_path) |
---|
55 | IWorkflowState(self.student).setState('cleared') |
---|
56 | self.student.nationality = u'NG' |
---|
57 | self.browser.open(self.payments_path + '/addop') |
---|
58 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
59 | self.browser.getControl("Create ticket").click() |
---|
60 | self.assertMatches('...ticket created...', |
---|
61 | self.browser.contents) |
---|
62 | ctrl = self.browser.getControl(name='val_id') |
---|
63 | self.value = ctrl.options[0] |
---|
64 | self.browser.getLink(self.value).click() |
---|
65 | self.assertMatches('...Amount Authorized...', |
---|
66 | self.browser.contents) |
---|
67 | self.assertTrue('<span>40000.0</span>', self.browser.contents) |
---|
68 | self.payment_url = self.browser.url |
---|
69 | self.payment = self.student['payments'][self.value] |
---|
70 | |
---|
71 | def test_interswitch_form(self): |
---|
72 | # Manager can access InterswitchForm |
---|
73 | self.browser.getLink("Pay via Interswitch", 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" />...', |
---|
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" />...', |
---|
83 | self.browser.contents) |
---|
84 | |
---|
85 | # Create school fee ticket for returning students. Payment is made |
---|
86 | # for next session. |
---|
87 | current_payment_key = self.student['payments'].keys()[0] |
---|
88 | self.certificate.study_mode = u'ug_pt' |
---|
89 | IWorkflowState(self.student).setState('returning') |
---|
90 | configuration = createObject('waeup.SessionConfiguration') |
---|
91 | configuration.academic_session = 2005 |
---|
92 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
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 | |
---|
97 | ctrl = self.browser.getControl(name='val_id') |
---|
98 | value = ctrl.options[1] |
---|
99 | self.assertEqual(self.student['payments'][value].provider_amt, 0.0) |
---|
100 | self.assertEqual(self.student['payments'][value].gateway_amt, 0.0) |
---|
101 | self.browser.getLink(value).click() |
---|
102 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
103 | # Split amounts have been set. |
---|
104 | self.assertEqual(self.student['payments'][value].provider_amt, 0.0) |
---|
105 | self.assertEqual(self.student['payments'][value].gateway_amt, 0.0) |
---|
106 | self.assertMatches('...<input type="hidden" name="pay_item_id" />...', |
---|
107 | self.browser.contents) |
---|
108 | self.assertMatches( |
---|
109 | '...<input type="hidden" name="amount" value="2000000" />...', |
---|
110 | self.browser.contents) |
---|
111 | |
---|
112 | def test_interswitch_form_ticket_expired(self): |
---|
113 | # Manager can access InterswitchForm |
---|
114 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
115 | self.assertMatches('...<input type="hidden" name="pay_item_id" />...', |
---|
116 | self.browser.contents) |
---|
117 | self.assertMatches('...Total Amount Authorized:...', |
---|
118 | self.browser.contents) |
---|
119 | self.assertEqual(self.student.current_mode, 'ug_ft') |
---|
120 | self.assertMatches( |
---|
121 | '...<input type="hidden" name="amount" value="4000000" />...', |
---|
122 | self.browser.contents) |
---|
123 | delta = timedelta(days=8) |
---|
124 | self.payment.creation_date -= delta |
---|
125 | self.browser.open(self.payment_url) |
---|
126 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
127 | self.assertMatches( |
---|
128 | '...This payment ticket is too old. Please create a new ticket...', |
---|
129 | self.browser.contents) |
---|
130 | delta = timedelta(days=2) |
---|
131 | self.payment.creation_date += delta |
---|
132 | self.browser.open(self.payment_url) |
---|
133 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
134 | self.assertMatches('...Total Amount Authorized:...', |
---|
135 | self.browser.contents) |
---|
136 | |
---|
137 | def test_interswitch_form_ticket_expired_tz(self): |
---|
138 | # The form copes with timezones when calculating expirements. |
---|
139 | # We should not have TZ data in timestamps processed, but it looks |
---|
140 | # like we get some with imports :-/ |
---|
141 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
142 | delta = timedelta(days=8) |
---|
143 | self.payment.creation_date -= delta |
---|
144 | from pytz import timezone |
---|
145 | self.payment.creation_date = timezone("Europe/Berlin").localize( |
---|
146 | self.payment.creation_date) |
---|
147 | self.browser.open(self.payment_url) |
---|
148 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
149 | self.assertMatches( |
---|
150 | '...This payment ticket is too old. Please create a new ticket...', |
---|
151 | self.browser.contents) |
---|
152 | delta = timedelta(days=2) |
---|
153 | self.payment.creation_date += delta |
---|
154 | self.browser.open(self.payment_url) |
---|
155 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
156 | self.assertMatches('...Total Amount Authorized:...', |
---|
157 | self.browser.contents) |
---|
158 | |
---|
159 | @external_test |
---|
160 | def test_query_interswitch_SOAP(self): |
---|
161 | host = 'webpay.interswitchng.com' |
---|
162 | url = '/paydirect/services/TransactionQueryWs.asmx' |
---|
163 | https = True |
---|
164 | mac = None |
---|
165 | product_id = '5845' # AAUE regular |
---|
166 | payment = StudentOnlinePayment() |
---|
167 | payment.p_id ='p4465649308559' |
---|
168 | payment.amount_auth = 60250.0 |
---|
169 | success, msg, log = query_interswitch( |
---|
170 | payment, product_id, host, url, https, mac) |
---|
171 | self.assertEqual('Successful callback received', msg) |
---|
172 | self.assertTrue(success) |
---|
173 | self.assertTrue( |
---|
174 | '00:Approved Successful:6025000:3154:p4465649308559:' |
---|
175 | 'ZIB|WEB|ABAL|3-11-2015|021336:000457580882' in log) |
---|
176 | |
---|
177 | @external_test |
---|
178 | def test_query_interswitch_JSON(self): |
---|
179 | host = 'webpay.interswitchng.com' |
---|
180 | url = '/paydirect/api/v1/gettransaction.json' |
---|
181 | mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023' |
---|
182 | https = True |
---|
183 | product_id = '5845' # AAUE regular |
---|
184 | payment = StudentOnlinePayment() |
---|
185 | payment.p_id ='p4465649308559' |
---|
186 | payment.amount_auth = 1.0 |
---|
187 | success, msg, log = query_interswitch( |
---|
188 | payment, product_id, host, url, https, mac) |
---|
189 | self.assertFalse(success) |
---|
190 | self.assertTrue('Unsuccessful callback:' in msg) |
---|
191 | self.assertTrue('Amount Inconsistency' in log) |
---|
192 | payment.amount_auth = 60250.0 |
---|
193 | success, msg, log = query_interswitch( |
---|
194 | payment, product_id, host, url, https, mac) |
---|
195 | self.assertEqual('Successful callback received', msg) |
---|
196 | self.assertTrue(success) |
---|
197 | self.assertTrue( |
---|
198 | "{u'SplitAccounts': [], " |
---|
199 | "u'MerchantReference': u'p4465649308559', " |
---|
200 | "u'PaymentReference': u'ZIB|WEB|ABAL|3-11-2015|021336', " |
---|
201 | "u'TransactionDate': u'2015-11-03T16:40:54.487', " |
---|
202 | "u'RetrievalReferenceNumber': u'000457580882', " |
---|
203 | "u'ResponseDescription': u'Approved Successful', " |
---|
204 | "u'Amount': 6025000, " |
---|
205 | "u'CardNumber': u'3154', " |
---|
206 | "u'ResponseCode': u'00', " |
---|
207 | "u'LeadBankCbnCode': None, " |
---|
208 | "u'LeadBankName': None}" in log) |
---|
209 | |
---|
210 | class InterswitchTestsApplicants(ApplicantsFullSetup): |
---|
211 | """Tests for the Interswitch payment gateway. |
---|
212 | """ |
---|
213 | |
---|
214 | layer = FunctionalLayer |
---|
215 | |
---|
216 | def setUp(self): |
---|
217 | super(InterswitchTestsApplicants, self).setUp() |
---|
218 | configuration = SessionConfiguration() |
---|
219 | configuration.academic_session = datetime.now().year - 2 |
---|
220 | self.app['configuration'].addSessionConfiguration(configuration) |
---|
221 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
222 | self.browser.open(self.manage_path) |
---|
223 | #IWorkflowState(self.student).setState('started') |
---|
224 | super(InterswitchTestsApplicants, self).fill_correct_values() |
---|
225 | self.applicantscontainer.application_fee = 1000.0 |
---|
226 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
227 | self.browser.getControl(name="transition").value = ['start'] |
---|
228 | self.browser.getControl("Save").click() |
---|
229 | self.browser.getControl("Add online").click() |
---|
230 | self.assertMatches('...ticket created...', |
---|
231 | self.browser.contents) |
---|
232 | self.payment = self.applicant.values()[0] |
---|
233 | self.payment_url = self.browser.url |
---|
234 | |
---|
235 | def test_interswitch_form(self): |
---|
236 | self.assertMatches('...Amount Authorized...', |
---|
237 | self.browser.contents) |
---|
238 | self.assertMatches( |
---|
239 | '...<span>1000.0</span>...', |
---|
240 | self.browser.contents) |
---|
241 | # Manager can access InterswitchForm |
---|
242 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
243 | self.assertMatches('...Total Amount Authorized:...', |
---|
244 | self.browser.contents) |
---|
245 | self.assertMatches( |
---|
246 | '...<input type="hidden" name="amount" value="100000" />...', |
---|
247 | self.browser.contents) |
---|
248 | delta = timedelta(days=8) |
---|
249 | self.payment.creation_date -= delta |
---|
250 | self.browser.open(self.payment_url) |
---|
251 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
252 | self.assertMatches( |
---|
253 | '...This payment ticket is too old. Please create a new ticket...', |
---|
254 | self.browser.contents) |
---|
255 | delta = timedelta(days=2) |
---|
256 | self.payment.creation_date += delta |
---|
257 | self.browser.getLink("Pay via Interswitch", index=0).click() |
---|
258 | self.assertMatches('...Total Amount Authorized:...', |
---|
259 | self.browser.contents) |
---|
260 | |
---|