1 | ## $Id: tests.py 14743 2017-08-02 08:28:31Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2017 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 | import unittest |
---|
20 | from datetime import datetime, timedelta, date |
---|
21 | from zope.component import createObject, getUtility |
---|
22 | from zope.catalog.interfaces import ICatalog |
---|
23 | from hurry.workflow.interfaces import IWorkflowState |
---|
24 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
25 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
26 | from waeup.kofa.configuration import SessionConfiguration |
---|
27 | from kofacustom.nigeria.students.payments import NigeriaStudentOnlinePayment |
---|
28 | from kofacustom.nigeria.testing import FunctionalLayer |
---|
29 | |
---|
30 | from kofacustom.nigeria.remita.helpers import ( |
---|
31 | get_JSON_POST_response, get_payment_status_via_rrr, query_remita) |
---|
32 | |
---|
33 | # Also run tests that send requests to external servers? |
---|
34 | # If you enable this, please make sure the external services |
---|
35 | # do exist really and are not bothered by being spammed by a test programme. |
---|
36 | EXTERNAL_TESTS = True |
---|
37 | |
---|
38 | def external_test(func): |
---|
39 | if not EXTERNAL_TESTS: |
---|
40 | myself = __file__ |
---|
41 | if myself.endswith('.pyc'): |
---|
42 | myself = myself[:-1] |
---|
43 | print "WARNING: external tests are skipped!" |
---|
44 | print "WARNING: edit %s to enable them." % myself |
---|
45 | return |
---|
46 | return func |
---|
47 | |
---|
48 | class HelperTests(unittest.TestCase): |
---|
49 | |
---|
50 | merchantId = '2547916' |
---|
51 | serviceTypeId = '4430731' |
---|
52 | api_key = '1946' |
---|
53 | orderId = '987698769876' |
---|
54 | amount = '1000' |
---|
55 | responseurl = 'http://xxxx' |
---|
56 | host = 'www.remitademo.net' |
---|
57 | lineitems = ( |
---|
58 | {"lineItemsId":"itemid1","beneficiaryName":"Oshadami Mike", |
---|
59 | "beneficiaryAccount":"6020067886","bankCode":"011", |
---|
60 | "beneficiaryAmount":"500","deductFeeFrom":"1"}, |
---|
61 | {"lineItemsId":"itemid2","beneficiaryName":"Ogunseye Mujib", |
---|
62 | "beneficiaryAccount":"0360883515","bankCode":"050", |
---|
63 | "beneficiaryAmount":"500","deductFeeFrom":"0"} |
---|
64 | ) |
---|
65 | |
---|
66 | @external_test |
---|
67 | def test_get_JSON_POST_response(self): |
---|
68 | |
---|
69 | url = '/remita/ecomm/split/init.reg' # /remita/ecomm/v2/init.reg |
---|
70 | |
---|
71 | resp = get_JSON_POST_response( |
---|
72 | merchantId=self.merchantId, serviceTypeId=self.serviceTypeId, |
---|
73 | api_key=self.api_key, orderId=self.orderId, |
---|
74 | amount=self.amount, responseurl=self.responseurl, |
---|
75 | host=self.host, url=url, https=False, |
---|
76 | fullname='Anton Meier', email='am@xxx.de', |
---|
77 | lineitems=self.lineitems) |
---|
78 | assert resp == { |
---|
79 | u'status': u'RRR Already Exist for the orderId', |
---|
80 | u'orderID': u'987698769876', |
---|
81 | u'RRR': u'320007640976 ', # strange trailing whitespace which |
---|
82 | # obviously does not belong to the RRR |
---|
83 | u'statuscode': u'055'} |
---|
84 | |
---|
85 | @external_test |
---|
86 | def test_payment_status_via_rrr(self): |
---|
87 | |
---|
88 | resp = get_payment_status_via_rrr( |
---|
89 | merchantId=self.merchantId, |
---|
90 | api_key=self.api_key, |
---|
91 | RRR='320007640976', |
---|
92 | host=self.host, |
---|
93 | https=False, |
---|
94 | ) |
---|
95 | assert resp == { |
---|
96 | u'orderId': u'987698769876', |
---|
97 | u'status': u'021', |
---|
98 | u'amount': 1000.0, |
---|
99 | u'transactiontime': u'2017-08-01 12:10:32 PM', |
---|
100 | u'message': u'Transaction Pending', |
---|
101 | u'lineitems': [{u'status': u'021', u'lineItemsId': u'itemid1'}, |
---|
102 | {u'status': u'021', u'lineItemsId': u'itemid2'} |
---|
103 | ], |
---|
104 | u'RRR': u'320007640976'} |
---|
105 | |
---|
106 | class RemitaTestsStudents(StudentsFullSetup): |
---|
107 | """Tests for the Remita payment gateway. |
---|
108 | """ |
---|
109 | |
---|
110 | layer = FunctionalLayer |
---|
111 | |
---|
112 | merchantId = '2547916' |
---|
113 | serviceTypeId = '4430731' |
---|
114 | api_key = '1946' |
---|
115 | orderId = '3456346346' |
---|
116 | amount = '1000' |
---|
117 | responseurl = 'http://xxxx' |
---|
118 | host = 'www.remitademo.net' |
---|
119 | |
---|
120 | def setUp(self): |
---|
121 | super(RemitaTestsStudents, self).setUp() |
---|
122 | self.app['configuration']['2004'].remita_enabled = True |
---|
123 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
124 | self.browser.open(self.payments_path) |
---|
125 | IWorkflowState(self.student).setState('cleared') |
---|
126 | self.student.nationality = u'NG' |
---|
127 | self.browser.open(self.payments_path + '/addop') |
---|
128 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
129 | self.browser.getControl("Create ticket").click() |
---|
130 | self.assertMatches('...ticket created...', |
---|
131 | self.browser.contents) |
---|
132 | ctrl = self.browser.getControl(name='val_id') |
---|
133 | self.value = ctrl.options[0] |
---|
134 | self.browser.getLink(self.value).click() |
---|
135 | self.assertMatches('...Amount Authorized...', |
---|
136 | self.browser.contents) |
---|
137 | self.assertTrue('<span>40000.0</span>', self.browser.contents) |
---|
138 | self.payment_url = self.browser.url |
---|
139 | self.payment = self.student['payments'][self.value] |
---|
140 | |
---|
141 | @external_test |
---|
142 | def test_query_remita(self): |
---|
143 | # We can only test the first part of query_interswitch since |
---|
144 | # we have no succesful payment. |
---|
145 | qr = query_remita( |
---|
146 | self.payment, |
---|
147 | merchantId=self.merchantId, |
---|
148 | api_key=self.api_key, |
---|
149 | RRR='320007640976', |
---|
150 | host=self.host, |
---|
151 | https=False, |
---|
152 | verify=False) |
---|
153 | assert qr == ( |
---|
154 | False, |
---|
155 | u'Unsuccessful callback: ${a}', |
---|
156 | u'unsuccessful callback for schoolfee payment %s: Transaction Pending' |
---|
157 | % self.payment.p_id) |
---|
158 | |
---|
159 | @external_test |
---|
160 | def test_remita_form(self): |
---|
161 | # Manager can access InterswitchForm |
---|
162 | self.browser.getLink("Pay via Remita").click() |
---|
163 | # The RRR has been retrieved |
---|
164 | self.assertTrue('<input name="rrr" type="hidden" value="280007640804">' |
---|
165 | in self.browser.contents) |
---|
166 | self.assertEqual(self.payment.r_pay_reference, '280007640804') |
---|
167 | # Means of testing end here. |
---|
168 | return |
---|