1 | ## $Id: test_captcha.py 7312 2011-12-09 10:44:38Z uli $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet |
---|
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 grok |
---|
19 | from zope.component import getAdapter, getUtility |
---|
20 | from zope.component.hooks import setSite, clearSite |
---|
21 | from zope.interface import verify |
---|
22 | from zope.publisher.browser import TestRequest |
---|
23 | from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase |
---|
24 | from waeup.sirp.browser.captcha import ( |
---|
25 | CaptchaResponse, CaptchaRequest, NullCaptcha, StaticCaptcha, ReCaptcha, |
---|
26 | CaptchaManager) |
---|
27 | from waeup.sirp.browser.interfaces import ( |
---|
28 | ICaptchaRequest, ICaptchaResponse, ICaptcha, ICaptchaConfig, |
---|
29 | ICaptchaManager) |
---|
30 | |
---|
31 | |
---|
32 | class CaptchaResponseTests(FunctionalTestCase): |
---|
33 | |
---|
34 | layer = FunctionalLayer |
---|
35 | |
---|
36 | def test_ifaces(self): |
---|
37 | # make sure we implement the promised interfaces |
---|
38 | obj = CaptchaResponse(True) |
---|
39 | verify.verifyClass(ICaptchaResponse, CaptchaResponse) |
---|
40 | verify.verifyObject(ICaptchaResponse, obj) |
---|
41 | return |
---|
42 | |
---|
43 | def test_init_vals(self): |
---|
44 | # make sure the initial values are stored |
---|
45 | resp = CaptchaResponse(False, 'some-msg') |
---|
46 | self.assertEqual(resp.is_valid, False) |
---|
47 | self.assertEqual(resp.error_code, 'some-msg') |
---|
48 | return |
---|
49 | |
---|
50 | class CaptchaRequestTests(FunctionalTestCase): |
---|
51 | |
---|
52 | layer = FunctionalLayer |
---|
53 | |
---|
54 | def test_ifaces(self): |
---|
55 | # make sure we implement the promised interfces |
---|
56 | obj = CaptchaRequest(None) |
---|
57 | verify.verifyClass(ICaptchaRequest, CaptchaRequest) |
---|
58 | verify.verifyObject(ICaptchaRequest, obj) |
---|
59 | return |
---|
60 | |
---|
61 | def test_init_vals(self): |
---|
62 | # make sure initial values are stored |
---|
63 | req = CaptchaRequest('my-solution', 'my-challenge') |
---|
64 | self.assertEqual(req.solution, 'my-solution') |
---|
65 | self.assertEqual(req.challenge, 'my-challenge') |
---|
66 | return |
---|
67 | |
---|
68 | class CaptchaTestBase(object): |
---|
69 | |
---|
70 | def test_ifaces(self): |
---|
71 | # make sure we implement the promised interfaces |
---|
72 | obj = self.factory() |
---|
73 | verify.verifyClass(ICaptcha, self.factory) |
---|
74 | verify.verifyObject(ICaptcha, obj) |
---|
75 | return |
---|
76 | |
---|
77 | def test_utility(self): |
---|
78 | # the null captcha is also registered as a utility for ICaptcha |
---|
79 | captcha = getUtility(ICaptcha, name=self.name) |
---|
80 | self.assertTrue(isinstance(captcha, self.factory)) |
---|
81 | return |
---|
82 | |
---|
83 | class NullCaptchaTests(FunctionalTestCase, CaptchaTestBase): |
---|
84 | |
---|
85 | layer = FunctionalLayer |
---|
86 | |
---|
87 | factory = NullCaptcha |
---|
88 | name = 'No captcha' |
---|
89 | |
---|
90 | def test_verify(self): |
---|
91 | # null captchas accept any input request |
---|
92 | captcha = self.factory() |
---|
93 | result1 = captcha.verify(TestRequest()) |
---|
94 | result2 = captcha.verify( |
---|
95 | TestRequest(form={'solution': 'a', 'challenge': 'b'})) |
---|
96 | self.assertEqual(result1.is_valid, True) |
---|
97 | self.assertEqual(result2.is_valid, True) |
---|
98 | return |
---|
99 | |
---|
100 | def test_display(self): |
---|
101 | # null captchas do not generate additional HTML code |
---|
102 | captcha = self.factory() |
---|
103 | result = captcha.display() |
---|
104 | self.assertEqual(result, u'') |
---|
105 | return |
---|
106 | |
---|
107 | class StaticCaptchaTests(FunctionalTestCase, CaptchaTestBase): |
---|
108 | |
---|
109 | layer = FunctionalLayer |
---|
110 | |
---|
111 | factory = StaticCaptcha |
---|
112 | name = 'Testing captcha' |
---|
113 | |
---|
114 | def test_verify(self): |
---|
115 | # id captchas accept any solution that matches challenge and |
---|
116 | # is not empty or None |
---|
117 | captcha = self.factory() |
---|
118 | request1 = TestRequest(form={'solution': 'my-sol', |
---|
119 | 'challenge': 'my-challenge'}) |
---|
120 | request2 = TestRequest() |
---|
121 | request3 = TestRequest(form={'solution': '', 'challenge': ''}) |
---|
122 | request4 = TestRequest(form={'solution': 'my-sol', |
---|
123 | 'challenge': 'my-sol'}) |
---|
124 | result1 = captcha.verify(request1) |
---|
125 | result2 = captcha.verify(request2) |
---|
126 | result3 = captcha.verify(request3) |
---|
127 | result4 = captcha.verify(request4) |
---|
128 | self.assertEqual(result1.is_valid, False) |
---|
129 | self.assertEqual(result2.is_valid, False) |
---|
130 | self.assertEqual(result3.is_valid, False) |
---|
131 | self.assertEqual(result4.is_valid, True) |
---|
132 | return |
---|
133 | |
---|
134 | def test_display(self): |
---|
135 | # id captchas provide a simple HTML input |
---|
136 | captcha = self.factory() |
---|
137 | result = captcha.display() |
---|
138 | self.assertMatches( |
---|
139 | '<input type="hidden" name="challenge"' |
---|
140 | ' value="..." /><br />Type: ...<br />' |
---|
141 | '<input type="text" name="solution" value="the-solution" /><br />', |
---|
142 | result) |
---|
143 | return |
---|
144 | |
---|
145 | class ReCaptchaTests(FunctionalTestCase, CaptchaTestBase): |
---|
146 | |
---|
147 | layer = FunctionalLayer |
---|
148 | |
---|
149 | factory = ReCaptcha |
---|
150 | name = 'ReCaptcha' |
---|
151 | |
---|
152 | def DISABLEDtest_verify(self): |
---|
153 | # recaptcha verification cannot be tested easily. As the |
---|
154 | # solution and other environment parameters is only known to |
---|
155 | # the remote server, we cannot guess on server side what the |
---|
156 | # solution is. Further more this test contacts a remote |
---|
157 | # server so that we might want to disable this test by |
---|
158 | # default. |
---|
159 | captcha = self.factory() |
---|
160 | request1 = TestRequest( |
---|
161 | form={'recaptcha_challenge_field': 'my-challenge', |
---|
162 | 'recaptcha_response_field': 'my-solution'}) |
---|
163 | result1 = captcha.verify(request1) |
---|
164 | self.assertEqual(result1.is_valid, False) |
---|
165 | self.assertEqual(result1.error_code, 'invalid-request-cookie') |
---|
166 | return |
---|
167 | |
---|
168 | def test_display(self): |
---|
169 | # recaptchas provide the pieces to trigger the remote API |
---|
170 | captcha = self.factory() |
---|
171 | result = captcha.display() |
---|
172 | self.assertMatches( |
---|
173 | '<script type="text/javascript" src="..."></script>' |
---|
174 | '<noscript>' |
---|
175 | '<iframe src="..." height="300" width="500" ' |
---|
176 | ' frameborder="0"></iframe><br />' |
---|
177 | '<textarea name="recaptcha_challenge_field" rows="3" ' |
---|
178 | ' cols="40"></textarea>' |
---|
179 | '<input type="hidden" name="recaptcha_response_field" ' |
---|
180 | ' value="manual_challenge" /></noscript>', |
---|
181 | result) |
---|
182 | return |
---|
183 | |
---|
184 | class CaptchaManagerTests(FunctionalTestCase): |
---|
185 | # These tests do not require a site setup |
---|
186 | # See testsuite below for insite tests. |
---|
187 | |
---|
188 | layer = FunctionalLayer |
---|
189 | |
---|
190 | def test_ifaces(self): |
---|
191 | # make sure we implement the promised interfaces |
---|
192 | obj = CaptchaManager() |
---|
193 | verify.verifyClass(ICaptchaManager, CaptchaManager) |
---|
194 | verify.verifyObject(ICaptchaManager, obj) |
---|
195 | return |
---|
196 | |
---|
197 | def test_utility(self): |
---|
198 | # the global captcha chooser is registered as a global utility |
---|
199 | result = getUtility(ICaptchaManager) |
---|
200 | self.assertTrue(isinstance(result, CaptchaManager)) |
---|
201 | return |
---|
202 | |
---|
203 | def test_get_avail_captchas(self): |
---|
204 | # we can get a dict of available captchas. |
---|
205 | chooser = getUtility(ICaptchaManager) |
---|
206 | result = dict(chooser.getAvailCaptchas()) |
---|
207 | no_captcha = result.get('No captcha', None) |
---|
208 | self.assertTrue(no_captcha is not None) |
---|
209 | default_captcha = result.get(u'', None) |
---|
210 | self.assertTrue(default_captcha is None) |
---|
211 | return |
---|
212 | |
---|
213 | def test_get_captcha_wo_site(self): |
---|
214 | # if there is no site set, we get the default captcha |
---|
215 | setSite(None) |
---|
216 | chooser = getUtility(ICaptchaManager) |
---|
217 | result = chooser.getCaptcha() |
---|
218 | default = getUtility(ICaptcha) |
---|
219 | self.assertTrue(result is default) |
---|
220 | return |
---|
221 | |
---|
222 | class FakeSite(grok.Site, grok.Container): |
---|
223 | pass |
---|
224 | |
---|
225 | class FakeConfiguration(object): |
---|
226 | captcha = None |
---|
227 | |
---|
228 | def __init__(self, captcha=None): |
---|
229 | self.captcha = captcha |
---|
230 | |
---|
231 | class CaptchaManagerTestsWithSite(FunctionalTestCase): |
---|
232 | |
---|
233 | layer = FunctionalLayer |
---|
234 | |
---|
235 | def setUp(self): |
---|
236 | super(CaptchaManagerTestsWithSite, self).setUp() |
---|
237 | self.getRootFolder()['app'] = FakeSite() |
---|
238 | self.site = self.getRootFolder()['app'] |
---|
239 | return |
---|
240 | |
---|
241 | def tearDown(self): |
---|
242 | super(CaptchaManagerTestsWithSite, self).tearDown() |
---|
243 | clearSite(self.site) |
---|
244 | return |
---|
245 | |
---|
246 | def test_get_captcha_no_captcha_in_config(self): |
---|
247 | # if a site has no configuration setting for captchas, we get |
---|
248 | # the default captcha |
---|
249 | setSite(self.site) |
---|
250 | chooser = getUtility(ICaptchaManager) |
---|
251 | result = chooser.getCaptcha() |
---|
252 | default = getUtility(ICaptcha) |
---|
253 | self.assertTrue(result is default) |
---|
254 | return |
---|
255 | |
---|
256 | def test_get_captcha_empty_captcha_in_config(self): |
---|
257 | # if a site has None as configuration setting for captchas, we get |
---|
258 | # the default captcha |
---|
259 | setSite(self.site) |
---|
260 | self.site['configuration'] = dict() |
---|
261 | chooser = getUtility(ICaptchaManager) |
---|
262 | result = chooser.getCaptcha() |
---|
263 | default = getUtility(ICaptcha) |
---|
264 | self.assertTrue(result is default) |
---|
265 | return |
---|
266 | |
---|
267 | def test_get_captcha_invalid_captcha_in_config(self): |
---|
268 | # if a site has None as configuration setting for captchas, we get |
---|
269 | # the default captcha |
---|
270 | setSite(self.site) |
---|
271 | self.site['configuration'] = FakeConfiguration(captcha='invalid name') |
---|
272 | chooser = getUtility(ICaptchaManager) |
---|
273 | result = chooser.getCaptcha() |
---|
274 | default = getUtility(ICaptcha) |
---|
275 | self.assertTrue(result is default) |
---|
276 | return |
---|
277 | |
---|
278 | def test_get_captcha_invalid_captcha_in_config(self): |
---|
279 | # if a site has None as configuration setting for captchas, we get |
---|
280 | # the default captcha |
---|
281 | setSite(self.site) |
---|
282 | self.site['configuration'] = FakeConfiguration(captcha='No captcha') |
---|
283 | chooser = getUtility(ICaptchaManager) |
---|
284 | result = chooser.getCaptcha() |
---|
285 | no_captcha = getUtility(ICaptcha, name='No captcha') |
---|
286 | default = getUtility(ICaptcha) |
---|
287 | self.assertTrue(result is not default) |
---|
288 | self.assertTrue(result is no_captcha) |
---|
289 | return |
---|