1 | ## $Id: test_captcha.py 7294 2011-12-06 22:33:31Z 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 | ICaptchaResponse, ICaptchaRequest, ICaptcha, ICaptchaChooser) |
---|
26 | from waeup.sirp.browser.captcha import ( |
---|
27 | CaptchaResponse, CaptchaRequest, NullCaptcha, CaptchaChooser) |
---|
28 | |
---|
29 | class CaptchaResponseTests(FunctionalTestCase): |
---|
30 | |
---|
31 | layer = FunctionalLayer |
---|
32 | |
---|
33 | def test_ifaces(self): |
---|
34 | # make sure we implement the promised interfaces |
---|
35 | obj = CaptchaResponse(True) |
---|
36 | verify.verifyClass(ICaptchaResponse, CaptchaResponse) |
---|
37 | verify.verifyObject(ICaptchaResponse, obj) |
---|
38 | return |
---|
39 | |
---|
40 | def test_init_vals(self): |
---|
41 | # make sure the initial values are stored |
---|
42 | resp = CaptchaResponse(False, 'some-msg') |
---|
43 | self.assertEqual(resp.is_valid, False) |
---|
44 | self.assertEqual(resp.error_code, 'some-msg') |
---|
45 | return |
---|
46 | |
---|
47 | class CaptchaRequestTests(FunctionalTestCase): |
---|
48 | |
---|
49 | layer = FunctionalLayer |
---|
50 | |
---|
51 | def test_ifaces(self): |
---|
52 | # make sure we implement the promised interfces |
---|
53 | obj = CaptchaRequest(None) |
---|
54 | verify.verifyClass(ICaptchaRequest, CaptchaRequest) |
---|
55 | verify.verifyObject(ICaptchaRequest, obj) |
---|
56 | return |
---|
57 | |
---|
58 | def test_init_vals(self): |
---|
59 | # make sure initial values are stored |
---|
60 | req = CaptchaRequest('my-solution', 'my-challenge') |
---|
61 | self.assertEqual(req.solution, 'my-solution') |
---|
62 | self.assertEqual(req.challenge, 'my-challenge') |
---|
63 | return |
---|
64 | |
---|
65 | class CaptchaTestBase(object): |
---|
66 | |
---|
67 | def test_ifaces(self): |
---|
68 | # make sure we implement the promised interfaces |
---|
69 | obj = self.factory() |
---|
70 | verify.verifyClass(ICaptcha, self.factory) |
---|
71 | verify.verifyObject(ICaptcha, obj) |
---|
72 | return |
---|
73 | |
---|
74 | def test_utility(self): |
---|
75 | # the null captcha is also registered as a utility for ICaptcha |
---|
76 | captcha = getUtility(ICaptcha, name=self.name) |
---|
77 | self.assertTrue(isinstance(captcha, self.factory)) |
---|
78 | return |
---|
79 | |
---|
80 | class NullCaptchaTests(FunctionalTestCase, CaptchaTestBase): |
---|
81 | |
---|
82 | layer = FunctionalLayer |
---|
83 | |
---|
84 | factory = NullCaptcha |
---|
85 | name = 'No captcha' |
---|
86 | |
---|
87 | def test_verify(self): |
---|
88 | # null captchas accept any input request |
---|
89 | captcha = self.factory() |
---|
90 | result1 = captcha.verify(CaptchaRequest('my-sol', 'my-challenge')) |
---|
91 | result2 = captcha.verify(CaptchaRequest(None, None)) |
---|
92 | self.assertEqual(result1.is_valid, True) |
---|
93 | self.assertEqual(result2.is_valid, True) |
---|
94 | return |
---|
95 | |
---|
96 | def test_display(self): |
---|
97 | # null captchas do not generate additional HTML code |
---|
98 | captcha = self.factory() |
---|
99 | result = captcha.display() |
---|
100 | self.assertEqual(result, u'') |
---|
101 | return |
---|
102 | |
---|
103 | class TestCaptchaTests(FunctionalTestCase): |
---|
104 | |
---|
105 | layer = FunctionalLayer |
---|
106 | |
---|
107 | class CaptchaChooserTests(FunctionalTestCase): |
---|
108 | # These tests do not require a site setup |
---|
109 | # See testsuite below for insite tests. |
---|
110 | |
---|
111 | layer = FunctionalLayer |
---|
112 | |
---|
113 | def test_ifaces(self): |
---|
114 | # make sure we implement the promised interfaces |
---|
115 | obj = CaptchaChooser() |
---|
116 | verify.verifyClass(ICaptchaChooser, CaptchaChooser) |
---|
117 | verify.verifyObject(ICaptchaChooser, obj) |
---|
118 | return |
---|
119 | |
---|
120 | def test_utility(self): |
---|
121 | # the global captcha chooser is registered as a global utility |
---|
122 | result = getUtility(ICaptchaChooser) |
---|
123 | self.assertTrue(isinstance(result, CaptchaChooser)) |
---|
124 | return |
---|
125 | |
---|
126 | def test_get_avail_captchas(self): |
---|
127 | # we can get a dict of available captchas. |
---|
128 | chooser = getUtility(ICaptchaChooser) |
---|
129 | result = dict(chooser.getAvailCaptchas()) |
---|
130 | no_captcha = result.get('No captcha', None) |
---|
131 | self.assertTrue(no_captcha is not None) |
---|
132 | default_captcha = result.get(u'', None) |
---|
133 | self.assertTrue(default_captcha is None) |
---|
134 | return |
---|
135 | |
---|
136 | def test_get_captcha_wo_site(self): |
---|
137 | # if there is no site set, we get the default captcha |
---|
138 | setSite(None) |
---|
139 | chooser = getUtility(ICaptchaChooser) |
---|
140 | result = chooser.getCaptcha() |
---|
141 | default = getUtility(ICaptcha) |
---|
142 | self.assertTrue(result is default) |
---|
143 | return |
---|
144 | |
---|
145 | class FakeSite(grok.Site, grok.Container): |
---|
146 | pass |
---|
147 | |
---|
148 | class CaptchaChooserTestsWithSite(FunctionalTestCase): |
---|
149 | |
---|
150 | layer = FunctionalLayer |
---|
151 | |
---|
152 | def setUp(self): |
---|
153 | super(CaptchaChooserTestsWithSite, self).setUp() |
---|
154 | self.getRootFolder()['app'] = FakeSite() |
---|
155 | self.site = self.getRootFolder()['app'] |
---|
156 | return |
---|
157 | |
---|
158 | def tearDown(self): |
---|
159 | super(CaptchaChooserTestsWithSite, self).tearDown() |
---|
160 | clearSite(self.site) |
---|
161 | return |
---|
162 | |
---|
163 | def test_get_captcha_no_captcha_in_config(self): |
---|
164 | # if a site has no configuration setting for captchas, we get |
---|
165 | # the default captcha |
---|
166 | setSite(self.site) |
---|
167 | chooser = getUtility(ICaptchaChooser) |
---|
168 | result = chooser.getCaptcha() |
---|
169 | default = getUtility(ICaptcha) |
---|
170 | self.assertTrue(result is default) |
---|
171 | return |
---|
172 | |
---|
173 | def test_get_captcha_empty_captcha_in_config(self): |
---|
174 | # if a site has None as configuration setting for captchas, we get |
---|
175 | # the default captcha |
---|
176 | setSite(self.site) |
---|
177 | self.site['configuration'] = dict() |
---|
178 | chooser = getUtility(ICaptchaChooser) |
---|
179 | result = chooser.getCaptcha() |
---|
180 | default = getUtility(ICaptcha) |
---|
181 | self.assertTrue(result is default) |
---|
182 | return |
---|
183 | |
---|
184 | def test_get_captcha_invalid_captcha_in_config(self): |
---|
185 | # if a site has None as configuration setting for captchas, we get |
---|
186 | # the default captcha |
---|
187 | setSite(self.site) |
---|
188 | self.site['configuration'] = dict(captcha='invalid name') |
---|
189 | chooser = getUtility(ICaptchaChooser) |
---|
190 | result = chooser.getCaptcha() |
---|
191 | default = getUtility(ICaptcha) |
---|
192 | self.assertTrue(result is default) |
---|
193 | return |
---|
194 | |
---|
195 | def test_get_captcha_invalid_captcha_in_config(self): |
---|
196 | # if a site has None as configuration setting for captchas, we get |
---|
197 | # the default captcha |
---|
198 | setSite(self.site) |
---|
199 | self.site['configuration'] = dict(captcha='No captcha') |
---|
200 | chooser = getUtility(ICaptchaChooser) |
---|
201 | result = chooser.getCaptcha() |
---|
202 | no_captcha = getUtility(ICaptcha, name='No captcha') |
---|
203 | default = getUtility(ICaptcha) |
---|
204 | self.assertTrue(result is not default) |
---|
205 | self.assertTrue(result is no_captcha) |
---|
206 | return |
---|