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