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