author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 17 Aug 2009 18:37:16 +0200 | |
changeset 2883 | 57ff19806026 |
parent 2773 | b2530e3e0afb |
child 3293 | 69c0ba095536 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
"""unit tests for cubicweb.web.application |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
import base64, Cookie |
|
11 |
import sys |
|
12 |
from urllib import unquote |
|
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
13 |
|
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
14 |
from logilab.common.testlib import TestCase, unittest_main |
0 | 15 |
from logilab.common.decorators import clear_cache |
16 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
17 |
from cubicweb.devtools.testlib import CubicWebTC |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
18 |
from cubicweb.devtools.fake import FakeRequest |
0 | 19 |
from cubicweb.web import Redirect, AuthenticationError, ExplicitLogin, INTERNAL_FIELD_VALUE |
20 |
from cubicweb.web.views.basecontrollers import ViewController |
|
21 |
||
22 |
class FakeMapping: |
|
23 |
"""emulates a mapping module""" |
|
24 |
def __init__(self): |
|
25 |
self.ENTITIES_MAP = {} |
|
26 |
self.ATTRIBUTES_MAP = {} |
|
27 |
self.RELATIONS_MAP = {} |
|
28 |
||
29 |
class MockCursor: |
|
30 |
def __init__(self): |
|
31 |
self.executed = [] |
|
32 |
def execute(self, rql, args=None, cachekey=None): |
|
33 |
args = args or {} |
|
34 |
self.executed.append(rql % args) |
|
35 |
||
36 |
||
37 |
class FakeController(ViewController): |
|
38 |
||
39 |
def __init__(self, form=None): |
|
40 |
self.req = FakeRequest() |
|
41 |
self.req.form = form or {} |
|
42 |
self._cursor = self.req.cursor = MockCursor() |
|
43 |
||
44 |
def new_cursor(self): |
|
45 |
self._cursor = self.req.cursor = MockCursor() |
|
46 |
||
47 |
def set_form(self, form): |
|
48 |
self.req.form = form |
|
49 |
||
50 |
||
51 |
class RequestBaseTC(TestCase): |
|
52 |
def setUp(self): |
|
53 |
self.req = FakeRequest() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
54 |
|
0 | 55 |
|
56 |
def test_list_arg(self): |
|
57 |
"""tests the list_arg() function""" |
|
58 |
list_arg = self.req.list_form_param |
|
59 |
self.assertEquals(list_arg('arg3', {}), []) |
|
60 |
d = {'arg1' : "value1", |
|
61 |
'arg2' : ('foo', INTERNAL_FIELD_VALUE,), |
|
62 |
'arg3' : ['bar']} |
|
63 |
self.assertEquals(list_arg('arg1', d, True), ['value1']) |
|
64 |
self.assertEquals(d, {'arg2' : ('foo', INTERNAL_FIELD_VALUE), 'arg3' : ['bar'],}) |
|
65 |
self.assertEquals(list_arg('arg2', d, True), ['foo']) |
|
66 |
self.assertEquals({'arg3' : ['bar'],}, d) |
|
67 |
self.assertEquals(list_arg('arg3', d), ['bar',]) |
|
68 |
self.assertEquals({'arg3' : ['bar'],}, d) |
|
69 |
||
70 |
||
71 |
def test_from_controller(self): |
|
72 |
self.assertEquals(self.req.from_controller(), 'view') |
|
73 |
req = FakeRequest(url='project?vid=list') |
|
74 |
# this assertion is just to make sure that relative_path can be |
|
75 |
# correctly computed as it is used in from_controller() |
|
76 |
self.assertEquals(req.relative_path(False), 'project') |
|
77 |
self.assertEquals(req.from_controller(), 'view') |
|
78 |
# test on a valid non-view controller |
|
79 |
req = FakeRequest(url='login?x=1&y=2') |
|
80 |
self.assertEquals(req.relative_path(False), 'login') |
|
81 |
self.assertEquals(req.from_controller(), 'login') |
|
82 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
83 |
|
0 | 84 |
class UtilsTC(TestCase): |
85 |
"""test suite for misc application utilities""" |
|
86 |
||
87 |
def setUp(self): |
|
88 |
self.ctrl = FakeController() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
89 |
|
0 | 90 |
#def test_which_mapping(self): |
91 |
# """tests which mapping is used (application or core)""" |
|
92 |
# init_mapping() |
|
93 |
# from cubicweb.common import mapping |
|
94 |
# self.assertEquals(mapping.MAPPING_USED, 'core') |
|
95 |
# sys.modules['mapping'] = FakeMapping() |
|
96 |
# init_mapping() |
|
97 |
# self.assertEquals(mapping.MAPPING_USED, 'application') |
|
98 |
# del sys.modules['mapping'] |
|
99 |
||
100 |
def test_execute_linkto(self): |
|
101 |
"""tests the execute_linkto() function""" |
|
102 |
self.assertEquals(self.ctrl.execute_linkto(), None) |
|
103 |
self.assertEquals(self.ctrl._cursor.executed, |
|
104 |
[]) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
105 |
|
0 | 106 |
self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:object', |
107 |
'eid': 8}) |
|
108 |
self.ctrl.execute_linkto() |
|
109 |
self.assertEquals(self.ctrl._cursor.executed, |
|
110 |
['SET Y works_for X WHERE X eid 8, Y eid %s' % i |
|
111 |
for i in (12, 13, 14)]) |
|
112 |
||
113 |
self.ctrl.new_cursor() |
|
114 |
self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:subject', |
|
115 |
'eid': 8}) |
|
116 |
self.ctrl.execute_linkto() |
|
117 |
self.assertEquals(self.ctrl._cursor.executed, |
|
118 |
['SET X works_for Y WHERE X eid 8, Y eid %s' % i |
|
119 |
for i in (12, 13, 14)]) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
120 |
|
0 | 121 |
|
122 |
self.ctrl.new_cursor() |
|
123 |
self.ctrl.req.form = {'__linkto' : 'works_for:12_13_14:object'} |
|
124 |
self.ctrl.execute_linkto(eid=8) |
|
125 |
self.assertEquals(self.ctrl._cursor.executed, |
|
126 |
['SET Y works_for X WHERE X eid 8, Y eid %s' % i |
|
127 |
for i in (12, 13, 14)]) |
|
128 |
||
129 |
self.ctrl.new_cursor() |
|
130 |
self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:subject'}) |
|
131 |
self.ctrl.execute_linkto(eid=8) |
|
132 |
self.assertEquals(self.ctrl._cursor.executed, |
|
133 |
['SET X works_for Y WHERE X eid 8, Y eid %s' % i |
|
134 |
for i in (12, 13, 14)]) |
|
135 |
||
136 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
137 |
class ApplicationTC(CubicWebTC): |
0 | 138 |
|
139 |
def publish(self, req, path='view'): |
|
140 |
return self.app.publish(path, req) |
|
141 |
||
142 |
def expect_redirect(self, callback, req): |
|
143 |
try: |
|
144 |
res = callback(req) |
|
145 |
print res |
|
146 |
except Redirect, ex: |
|
147 |
try: |
|
148 |
path, params = ex.location.split('?', 1) |
|
149 |
except ValueError: |
|
150 |
path = ex.location |
|
151 |
params = {} |
|
152 |
else: |
|
153 |
cleanup = lambda p: (p[0], unquote(p[1])) |
|
154 |
params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p) |
|
155 |
path = path[len(req.base_url()):] |
|
156 |
return path, params |
|
157 |
else: |
|
158 |
self.fail('expected a Redirect exception') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
159 |
|
0 | 160 |
def expect_redirect_publish(self, req, path='view'): |
161 |
return self.expect_redirect(lambda x: self.publish(x, path), req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
162 |
|
0 | 163 |
def test_cnx_user_groups_sync(self): |
164 |
user = self.user() |
|
165 |
self.assertEquals(user.groups, set(('managers',))) |
|
166 |
self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
167 |
user = self.user() |
|
168 |
self.assertEquals(user.groups, set(('managers',))) |
|
169 |
self.commit() |
|
170 |
user = self.user() |
|
171 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
172 |
# cleanup |
|
173 |
self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
174 |
self.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
175 |
|
0 | 176 |
def test_nonregr_publish1(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
177 |
req = self.request(u'CWEType X WHERE X final FALSE, X meta FALSE') |
0 | 178 |
self.app.publish('view', req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
179 |
|
0 | 180 |
def test_nonregr_publish2(self): |
181 |
req = self.request(u'Any count(N) WHERE N todo_by U, N is Note, U eid %s' |
|
182 |
% self.user().eid) |
|
183 |
self.app.publish('view', req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
184 |
|
0 | 185 |
def test_publish_validation_error(self): |
186 |
req = self.request() |
|
187 |
user = self.user() |
|
188 |
req.form = { |
|
189 |
'eid': `user.eid`, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
190 |
'__type:'+`user.eid`: 'CWUser', |
0 | 191 |
'login:'+`user.eid`: '', # ERROR: no login specified |
192 |
'edits-login:'+`user.eid`: unicode(user.login), |
|
193 |
# just a sample, missing some necessary information for real life |
|
194 |
'__errorurl': 'view?vid=edition...' |
|
195 |
} |
|
196 |
path, params = self.expect_redirect_publish(req, 'edit') |
|
197 |
forminfo = req.get_session_data('view?vid=edition...') |
|
198 |
eidmap = forminfo['eidmap'] |
|
199 |
self.assertEquals(eidmap, {}) |
|
200 |
values = forminfo['values'] |
|
201 |
self.assertEquals(values['login:'+`user.eid`], '') |
|
202 |
self.assertEquals(values['edits-login:'+`user.eid`], user.login) |
|
203 |
self.assertEquals(values['eid'], `user.eid`) |
|
204 |
errors = forminfo['errors'] |
|
205 |
self.assertEquals(errors.entity, user.eid) |
|
206 |
self.assertEquals(errors.errors['login'], 'required attribute') |
|
207 |
||
208 |
||
209 |
def test_validation_error_dont_loose_subentity_data(self): |
|
210 |
"""test creation of two linked entities |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
211 |
""" |
0 | 212 |
req = self.request() |
213 |
form = {'eid': ['X', 'Y'], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
214 |
'__type:X': 'CWUser', |
0 | 215 |
# missing required field |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
216 |
'login:X': u'', 'edits-login:X': '', |
0 | 217 |
'surname:X': u'Mr Ouaoua', 'edits-surname:X': '', |
218 |
'__type:Y': 'EmailAddress', |
|
219 |
# but email address is set |
|
220 |
'address:Y': u'bougloup@logilab.fr', 'edits-address:Y': '', |
|
221 |
'alias:Y': u'', 'edits-alias:Y': '', |
|
222 |
'use_email:X': 'Y', 'edits-use_email:X': INTERNAL_FIELD_VALUE, |
|
223 |
# necessary to get validation error handling |
|
224 |
'__errorurl': 'view?vid=edition...', |
|
225 |
} |
|
226 |
req.form = form |
|
227 |
# monkey patch edited_eid to ensure both entities are edited, not only X |
|
228 |
req.edited_eids = lambda : ('Y', 'X') |
|
229 |
path, params = self.expect_redirect_publish(req, 'edit') |
|
230 |
forminfo = req.get_session_data('view?vid=edition...') |
|
231 |
self.assertUnorderedIterableEquals(forminfo['eidmap'].keys(), ['X', 'Y']) |
|
232 |
self.assertEquals(forminfo['errors'].entity, forminfo['eidmap']['X']) |
|
233 |
self.assertEquals(forminfo['errors'].errors, {'login': 'required attribute', |
|
234 |
'upassword': 'required attribute'}) |
|
235 |
self.assertEquals(forminfo['values'], form) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
236 |
|
0 | 237 |
def _test_cleaned(self, kwargs, injected, cleaned): |
238 |
req = self.request(**kwargs) |
|
239 |
page = self.app.publish('view', req) |
|
240 |
self.failIf(injected in page, (kwargs, injected)) |
|
241 |
self.failUnless(cleaned in page, (kwargs, cleaned)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
242 |
|
0 | 243 |
def test_nonregr_script_kiddies(self): |
244 |
"""test against current script injection""" |
|
245 |
injected = '<i>toto</i>' |
|
246 |
cleaned = 'toto' |
|
247 |
for kwargs in ({'__message': injected}, |
|
248 |
{'vid': injected}, |
|
249 |
{'vtitle': injected}, |
|
250 |
): |
|
251 |
yield self._test_cleaned, kwargs, injected, cleaned |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
252 |
|
0 | 253 |
def test_site_wide_eproperties_sync(self): |
254 |
# XXX work in all-in-one configuration but not in twisted for instance |
|
255 |
# in which case we need a kindof repo -> http server notification |
|
256 |
# protocol |
|
257 |
vreg = self.app.vreg |
|
258 |
# default value |
|
259 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
260 |
self.execute('INSERT CWProperty X: X value "fr", X pkey "ui.language"') |
0 | 261 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
262 |
self.commit() |
|
263 |
self.assertEquals(vreg.property_value('ui.language'), 'fr') |
|
264 |
self.execute('SET X value "de" WHERE X pkey "ui.language"') |
|
265 |
self.assertEquals(vreg.property_value('ui.language'), 'fr') |
|
266 |
self.commit() |
|
267 |
self.assertEquals(vreg.property_value('ui.language'), 'de') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
268 |
self.execute('DELETE CWProperty X WHERE X pkey "ui.language"') |
0 | 269 |
self.assertEquals(vreg.property_value('ui.language'), 'de') |
270 |
self.commit() |
|
271 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
|
272 |
||
273 |
def test_fb_login_concept(self): |
|
274 |
"""see data/views.py""" |
|
275 |
self.set_option('auth-mode', 'cookie') |
|
276 |
self.set_option('anonymous-user', 'anon') |
|
277 |
self.login('anon') |
|
278 |
req = self.request() |
|
279 |
origcnx = req.cnx |
|
280 |
req.form['__fblogin'] = u'turlututu' |
|
281 |
page = self.publish(req) |
|
282 |
self.failIf(req.cnx is origcnx) |
|
283 |
self.assertEquals(req.user.login, 'turlututu') |
|
284 |
self.failUnless('turlututu' in page, page) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
285 |
|
0 | 286 |
# authentication tests #################################################### |
287 |
||
288 |
def _init_auth(self, authmode, anonuser=None): |
|
289 |
self.set_option('auth-mode', authmode) |
|
290 |
self.set_option('anonymous-user', anonuser) |
|
291 |
req = self.request() |
|
292 |
origcnx = req.cnx |
|
293 |
req.cnx = None |
|
294 |
sh = self.app.session_handler |
|
295 |
# not properly cleaned between tests |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
296 |
self.open_sessions = sh.session_manager._sessions = {} |
0 | 297 |
return req, origcnx |
298 |
||
299 |
def _test_auth_succeed(self, req, origcnx): |
|
300 |
sh = self.app.session_handler |
|
301 |
path, params = self.expect_redirect(lambda x: self.app.connect(x), req) |
|
302 |
cnx = req.cnx |
|
303 |
self.assertEquals(len(self.open_sessions), 1, self.open_sessions) |
|
304 |
self.assertEquals(cnx.login, origcnx.login) |
|
305 |
self.assertEquals(cnx.password, origcnx.password) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
306 |
self.assertEquals(cnx.anonymous_connection, False) |
0 | 307 |
self.assertEquals(path, 'view') |
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
308 |
self.assertEquals(params, {'__message': 'welcome %s !' % cnx.user().login}) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
309 |
|
0 | 310 |
def _test_auth_fail(self, req): |
311 |
self.assertRaises(AuthenticationError, self.app.connect, req) |
|
312 |
self.assertEquals(req.cnx, None) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
313 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 314 |
clear_cache(req, 'get_authorization') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
315 |
|
0 | 316 |
def test_http_auth_no_anon(self): |
317 |
req, origcnx = self._init_auth('http') |
|
318 |
self._test_auth_fail(req) |
|
319 |
self.assertRaises(ExplicitLogin, self.publish, req, 'login') |
|
320 |
self.assertEquals(req.cnx, None) |
|
321 |
authstr = base64.encodestring('%s:%s' % (origcnx.login, origcnx.password)) |
|
322 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
323 |
self._test_auth_succeed(req, origcnx) |
|
324 |
self.assertRaises(AuthenticationError, self.publish, req, 'logout') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
325 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 326 |
|
327 |
def test_cookie_auth_no_anon(self): |
|
328 |
req, origcnx = self._init_auth('cookie') |
|
329 |
self._test_auth_fail(req) |
|
330 |
form = self.publish(req, 'login') |
|
331 |
self.failUnless('__login' in form) |
|
332 |
self.failUnless('__password' in form) |
|
333 |
self.assertEquals(req.cnx, None) |
|
334 |
req.form['__login'] = origcnx.login |
|
335 |
req.form['__password'] = origcnx.password |
|
336 |
self._test_auth_succeed(req, origcnx) |
|
337 |
self.assertRaises(AuthenticationError, self.publish, req, 'logout') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
338 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 339 |
|
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
340 |
def test_login_by_email(self): |
1489
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
341 |
login = self.request().user.login |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
342 |
address = login + u'@localhost' |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
343 |
self.execute('INSERT EmailAddress X: X address %(address)s, U primary_email X ' |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
344 |
'WHERE U login %(login)s', {'address': address, 'login': login}) |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
345 |
self.commit() |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
346 |
# option allow-email-login not set |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
347 |
req, origcnx = self._init_auth('cookie') |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
348 |
req.form['__login'] = address |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
349 |
req.form['__password'] = origcnx.password |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
350 |
self._test_auth_fail(req) |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
351 |
# option allow-email-login set |
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
352 |
origcnx.login = address |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
353 |
self.set_option('allow-email-login', True) |
1489
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
354 |
req.form['__login'] = address |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
355 |
req.form['__password'] = origcnx.password |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
356 |
self._test_auth_succeed(req, origcnx) |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
357 |
self.assertRaises(AuthenticationError, self.publish, req, 'logout') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
358 |
self.assertEquals(len(self.open_sessions), 0) |
1489
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
359 |
|
0 | 360 |
def _test_auth_anon(self, req): |
361 |
self.app.connect(req) |
|
362 |
acnx = req.cnx |
|
363 |
self.assertEquals(len(self.open_sessions), 1) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
364 |
self.assertEquals(acnx.login, 'anon') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
365 |
self.assertEquals(acnx.password, 'anon') |
0 | 366 |
self.failUnless(acnx.anonymous_connection) |
367 |
self._reset_cookie(req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
368 |
|
0 | 369 |
def _reset_cookie(self, req): |
370 |
# preparing the suite of the test |
|
371 |
# set session id in cookie |
|
372 |
cookie = Cookie.SimpleCookie() |
|
373 |
cookie['__session'] = req.cnx.sessionid |
|
374 |
req._headers['Cookie'] = cookie['__session'].OutputString() |
|
375 |
clear_cache(req, 'get_authorization') |
|
376 |
# reset cnx as if it was a new incoming request |
|
377 |
req.cnx = None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
378 |
|
0 | 379 |
def _test_anon_auth_fail(self, req): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
380 |
self.assertEquals(len(self.open_sessions), 1) |
0 | 381 |
self.app.connect(req) |
382 |
self.assertEquals(req.message, 'authentication failure') |
|
383 |
self.assertEquals(req.cnx.anonymous_connection, True) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
384 |
self.assertEquals(len(self.open_sessions), 1) |
0 | 385 |
self._reset_cookie(req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
386 |
|
0 | 387 |
def test_http_auth_anon_allowed(self): |
388 |
req, origcnx = self._init_auth('http', 'anon') |
|
389 |
self._test_auth_anon(req) |
|
390 |
authstr = base64.encodestring('toto:pouet') |
|
391 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
392 |
self._test_anon_auth_fail(req) |
|
393 |
authstr = base64.encodestring('%s:%s' % (origcnx.login, origcnx.password)) |
|
394 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
395 |
self._test_auth_succeed(req, origcnx) |
|
396 |
self.assertRaises(AuthenticationError, self.publish, req, 'logout') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
397 |
self.assertEquals(len(self.open_sessions), 0) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
398 |
|
0 | 399 |
def test_cookie_auth_anon_allowed(self): |
400 |
req, origcnx = self._init_auth('cookie', 'anon') |
|
401 |
self._test_auth_anon(req) |
|
402 |
req.form['__login'] = 'toto' |
|
403 |
req.form['__password'] = 'pouet' |
|
404 |
self._test_anon_auth_fail(req) |
|
405 |
req.form['__login'] = origcnx.login |
|
406 |
req.form['__password'] = origcnx.password |
|
407 |
self._test_auth_succeed(req, origcnx) |
|
408 |
self.assertRaises(AuthenticationError, self.publish, req, 'logout') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
409 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 410 |
|
411 |
||
412 |
if __name__ == '__main__': |
|
413 |
unittest_main() |