author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Dec 2009 13:05:53 +0100 | |
branch | stable |
changeset 4205 | 4458c7cc193b |
parent 3801 | 561d7ade5719 |
child 3890 | d7a270f50f54 |
child 4212 | ab6573088b4a |
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 |
||
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
17 |
from cubicweb.devtools.apptest import EnvBasedTC |
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 |
||
140 |
class ApplicationTC(EnvBasedTC): |
|
3523
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
141 |
def setUp(self): |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
142 |
super(ApplicationTC, self).setUp() |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
143 |
def raise_hdlr(*args, **kwargs): |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
144 |
raise |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
145 |
self.app.error_handler = raise_hdlr |
0 | 146 |
|
147 |
def publish(self, req, path='view'): |
|
148 |
return self.app.publish(path, req) |
|
149 |
||
150 |
def expect_redirect(self, callback, req): |
|
151 |
try: |
|
152 |
res = callback(req) |
|
153 |
print res |
|
154 |
except Redirect, ex: |
|
155 |
try: |
|
156 |
path, params = ex.location.split('?', 1) |
|
157 |
except ValueError: |
|
158 |
path = ex.location |
|
159 |
params = {} |
|
160 |
else: |
|
161 |
cleanup = lambda p: (p[0], unquote(p[1])) |
|
162 |
params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p) |
|
163 |
path = path[len(req.base_url()):] |
|
164 |
return path, params |
|
165 |
else: |
|
166 |
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
|
167 |
|
0 | 168 |
def expect_redirect_publish(self, req, path='view'): |
169 |
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
|
170 |
|
0 | 171 |
def test_cnx_user_groups_sync(self): |
172 |
user = self.user() |
|
173 |
self.assertEquals(user.groups, set(('managers',))) |
|
174 |
self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
175 |
user = self.user() |
|
176 |
self.assertEquals(user.groups, set(('managers',))) |
|
177 |
self.commit() |
|
178 |
user = self.user() |
|
179 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
180 |
# cleanup |
|
181 |
self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
182 |
self.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
183 |
|
0 | 184 |
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
|
185 |
req = self.request(u'CWEType X WHERE X final FALSE, X meta FALSE') |
0 | 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_nonregr_publish2(self): |
189 |
req = self.request(u'Any count(N) WHERE N todo_by U, N is Note, U eid %s' |
|
190 |
% self.user().eid) |
|
191 |
self.app.publish('view', req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
192 |
|
0 | 193 |
def test_publish_validation_error(self): |
194 |
req = self.request() |
|
195 |
user = self.user() |
|
196 |
req.form = { |
|
197 |
'eid': `user.eid`, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
198 |
'__type:'+`user.eid`: 'CWUser', |
0 | 199 |
'login:'+`user.eid`: '', # ERROR: no login specified |
200 |
'edits-login:'+`user.eid`: unicode(user.login), |
|
201 |
# just a sample, missing some necessary information for real life |
|
202 |
'__errorurl': 'view?vid=edition...' |
|
203 |
} |
|
204 |
path, params = self.expect_redirect_publish(req, 'edit') |
|
205 |
forminfo = req.get_session_data('view?vid=edition...') |
|
206 |
eidmap = forminfo['eidmap'] |
|
207 |
self.assertEquals(eidmap, {}) |
|
208 |
values = forminfo['values'] |
|
209 |
self.assertEquals(values['login:'+`user.eid`], '') |
|
210 |
self.assertEquals(values['edits-login:'+`user.eid`], user.login) |
|
211 |
self.assertEquals(values['eid'], `user.eid`) |
|
212 |
errors = forminfo['errors'] |
|
213 |
self.assertEquals(errors.entity, user.eid) |
|
214 |
self.assertEquals(errors.errors['login'], 'required attribute') |
|
215 |
||
216 |
||
217 |
def test_validation_error_dont_loose_subentity_data(self): |
|
218 |
"""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
|
219 |
""" |
0 | 220 |
req = self.request() |
221 |
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
|
222 |
'__type:X': 'CWUser', |
0 | 223 |
# missing required field |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
224 |
'login:X': u'', 'edits-login:X': '', |
0 | 225 |
'surname:X': u'Mr Ouaoua', 'edits-surname:X': '', |
226 |
'__type:Y': 'EmailAddress', |
|
227 |
# but email address is set |
|
228 |
'address:Y': u'bougloup@logilab.fr', 'edits-address:Y': '', |
|
229 |
'alias:Y': u'', 'edits-alias:Y': '', |
|
230 |
'use_email:X': 'Y', 'edits-use_email:X': INTERNAL_FIELD_VALUE, |
|
231 |
# necessary to get validation error handling |
|
232 |
'__errorurl': 'view?vid=edition...', |
|
233 |
} |
|
234 |
req.form = form |
|
235 |
# monkey patch edited_eid to ensure both entities are edited, not only X |
|
236 |
req.edited_eids = lambda : ('Y', 'X') |
|
237 |
path, params = self.expect_redirect_publish(req, 'edit') |
|
238 |
forminfo = req.get_session_data('view?vid=edition...') |
|
239 |
self.assertUnorderedIterableEquals(forminfo['eidmap'].keys(), ['X', 'Y']) |
|
240 |
self.assertEquals(forminfo['errors'].entity, forminfo['eidmap']['X']) |
|
241 |
self.assertEquals(forminfo['errors'].errors, {'login': 'required attribute', |
|
242 |
'upassword': 'required attribute'}) |
|
243 |
self.assertEquals(forminfo['values'], form) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
244 |
|
0 | 245 |
def _test_cleaned(self, kwargs, injected, cleaned): |
246 |
req = self.request(**kwargs) |
|
247 |
page = self.app.publish('view', req) |
|
248 |
self.failIf(injected in page, (kwargs, injected)) |
|
249 |
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
|
250 |
|
0 | 251 |
def test_nonregr_script_kiddies(self): |
252 |
"""test against current script injection""" |
|
253 |
injected = '<i>toto</i>' |
|
254 |
cleaned = 'toto' |
|
255 |
for kwargs in ({'__message': injected}, |
|
256 |
{'vid': injected}, |
|
257 |
{'vtitle': injected}, |
|
258 |
): |
|
259 |
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
|
260 |
|
0 | 261 |
def test_site_wide_eproperties_sync(self): |
262 |
# XXX work in all-in-one configuration but not in twisted for instance |
|
263 |
# in which case we need a kindof repo -> http server notification |
|
264 |
# protocol |
|
265 |
vreg = self.app.vreg |
|
266 |
# default value |
|
267 |
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
|
268 |
self.execute('INSERT CWProperty X: X value "fr", X pkey "ui.language"') |
0 | 269 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
270 |
self.commit() |
|
271 |
self.assertEquals(vreg.property_value('ui.language'), 'fr') |
|
272 |
self.execute('SET X value "de" WHERE X pkey "ui.language"') |
|
273 |
self.assertEquals(vreg.property_value('ui.language'), 'fr') |
|
274 |
self.commit() |
|
275 |
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
|
276 |
self.execute('DELETE CWProperty X WHERE X pkey "ui.language"') |
0 | 277 |
self.assertEquals(vreg.property_value('ui.language'), 'de') |
278 |
self.commit() |
|
279 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
|
280 |
||
281 |
def test_fb_login_concept(self): |
|
282 |
"""see data/views.py""" |
|
283 |
self.set_option('auth-mode', 'cookie') |
|
284 |
self.set_option('anonymous-user', 'anon') |
|
285 |
self.login('anon') |
|
286 |
req = self.request() |
|
287 |
origcnx = req.cnx |
|
288 |
req.form['__fblogin'] = u'turlututu' |
|
289 |
page = self.publish(req) |
|
290 |
self.failIf(req.cnx is origcnx) |
|
291 |
self.assertEquals(req.user.login, 'turlututu') |
|
292 |
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
|
293 |
|
0 | 294 |
# authentication tests #################################################### |
295 |
||
296 |
def _init_auth(self, authmode, anonuser=None): |
|
297 |
self.set_option('auth-mode', authmode) |
|
298 |
self.set_option('anonymous-user', anonuser) |
|
299 |
req = self.request() |
|
300 |
origcnx = req.cnx |
|
301 |
req.cnx = None |
|
302 |
sh = self.app.session_handler |
|
303 |
# not properly cleaned between tests |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
304 |
self.open_sessions = sh.session_manager._sessions = {} |
0 | 305 |
return req, origcnx |
306 |
||
307 |
def _test_auth_succeed(self, req, origcnx): |
|
308 |
sh = self.app.session_handler |
|
309 |
path, params = self.expect_redirect(lambda x: self.app.connect(x), req) |
|
310 |
cnx = req.cnx |
|
311 |
self.assertEquals(len(self.open_sessions), 1, self.open_sessions) |
|
312 |
self.assertEquals(cnx.login, origcnx.login) |
|
313 |
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
|
314 |
self.assertEquals(cnx.anonymous_connection, False) |
0 | 315 |
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
|
316 |
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
|
317 |
|
0 | 318 |
def _test_auth_fail(self, req): |
319 |
self.assertRaises(AuthenticationError, self.app.connect, req) |
|
320 |
self.assertEquals(req.cnx, None) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
321 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 322 |
clear_cache(req, 'get_authorization') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
323 |
|
0 | 324 |
def test_http_auth_no_anon(self): |
325 |
req, origcnx = self._init_auth('http') |
|
326 |
self._test_auth_fail(req) |
|
327 |
self.assertRaises(ExplicitLogin, self.publish, req, 'login') |
|
328 |
self.assertEquals(req.cnx, None) |
|
329 |
authstr = base64.encodestring('%s:%s' % (origcnx.login, origcnx.password)) |
|
330 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
331 |
self._test_auth_succeed(req, origcnx) |
|
332 |
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
|
333 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 334 |
|
335 |
def test_cookie_auth_no_anon(self): |
|
336 |
req, origcnx = self._init_auth('cookie') |
|
337 |
self._test_auth_fail(req) |
|
338 |
form = self.publish(req, 'login') |
|
339 |
self.failUnless('__login' in form) |
|
340 |
self.failUnless('__password' in form) |
|
341 |
self.assertEquals(req.cnx, None) |
|
342 |
req.form['__login'] = origcnx.login |
|
343 |
req.form['__password'] = origcnx.password |
|
344 |
self._test_auth_succeed(req, origcnx) |
|
345 |
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
|
346 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 347 |
|
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
348 |
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
|
349 |
login = self.request().user.login |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
350 |
address = login + u'@localhost' |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
351 |
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
|
352 |
'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
|
353 |
self.commit() |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
354 |
# option allow-email-login not set |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
355 |
req, origcnx = self._init_auth('cookie') |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
356 |
req.form['__login'] = address |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
357 |
req.form['__password'] = origcnx.password |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
358 |
self._test_auth_fail(req) |
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
359 |
# 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
|
360 |
origcnx.login = address |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
361 |
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
|
362 |
req.form['__login'] = address |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
363 |
req.form['__password'] = origcnx.password |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
364 |
self._test_auth_succeed(req, origcnx) |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
365 |
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
|
366 |
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
|
367 |
|
0 | 368 |
def _test_auth_anon(self, req): |
369 |
self.app.connect(req) |
|
370 |
acnx = req.cnx |
|
371 |
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
|
372 |
self.assertEquals(acnx.login, 'anon') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
373 |
self.assertEquals(acnx.password, 'anon') |
0 | 374 |
self.failUnless(acnx.anonymous_connection) |
375 |
self._reset_cookie(req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
376 |
|
0 | 377 |
def _reset_cookie(self, req): |
378 |
# preparing the suite of the test |
|
379 |
# set session id in cookie |
|
380 |
cookie = Cookie.SimpleCookie() |
|
381 |
cookie['__session'] = req.cnx.sessionid |
|
382 |
req._headers['Cookie'] = cookie['__session'].OutputString() |
|
383 |
clear_cache(req, 'get_authorization') |
|
384 |
# reset cnx as if it was a new incoming request |
|
385 |
req.cnx = None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
386 |
|
0 | 387 |
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
|
388 |
self.assertEquals(len(self.open_sessions), 1) |
0 | 389 |
self.app.connect(req) |
390 |
self.assertEquals(req.message, 'authentication failure') |
|
391 |
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
|
392 |
self.assertEquals(len(self.open_sessions), 1) |
0 | 393 |
self._reset_cookie(req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
394 |
|
0 | 395 |
def test_http_auth_anon_allowed(self): |
396 |
req, origcnx = self._init_auth('http', 'anon') |
|
397 |
self._test_auth_anon(req) |
|
398 |
authstr = base64.encodestring('toto:pouet') |
|
399 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
400 |
self._test_anon_auth_fail(req) |
|
401 |
authstr = base64.encodestring('%s:%s' % (origcnx.login, origcnx.password)) |
|
402 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
403 |
self._test_auth_succeed(req, origcnx) |
|
404 |
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
|
405 |
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
|
406 |
|
0 | 407 |
def test_cookie_auth_anon_allowed(self): |
408 |
req, origcnx = self._init_auth('cookie', 'anon') |
|
409 |
self._test_auth_anon(req) |
|
410 |
req.form['__login'] = 'toto' |
|
411 |
req.form['__password'] = 'pouet' |
|
412 |
self._test_anon_auth_fail(req) |
|
413 |
req.form['__login'] = origcnx.login |
|
414 |
req.form['__password'] = origcnx.password |
|
415 |
self._test_auth_succeed(req, origcnx) |
|
416 |
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
|
417 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 418 |
|
3523
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
419 |
def test_non_regr_optional_first_var(self): |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
420 |
req = self.request() |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
421 |
# expect a rset with None in [0][0] |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
422 |
req.form['rql'] = 'rql:Any OV1, X WHERE X custom_workflow OV1?' |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
423 |
self.publish(req) |
0 | 424 |
|
425 |
if __name__ == '__main__': |
|
426 |
unittest_main() |