author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 18 Mar 2010 14:06:53 +0100 | |
branch | stable |
changeset 4941 | ec27aea9632b |
parent 4276 | e2ed81c20e74 |
child 5030 | 5238d9a8dfee |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3801
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
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): |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
40 |
self._cw = FakeRequest() |
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
41 |
self._cw.form = form or {} |
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
42 |
self._cursor = self._cw.cursor = MockCursor() |
0 | 43 |
|
44 |
def new_cursor(self): |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
45 |
self._cursor = self._cw.cursor = MockCursor() |
0 | 46 |
|
47 |
def set_form(self, form): |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
48 |
self._cw.form = form |
0 | 49 |
|
50 |
||
51 |
class RequestBaseTC(TestCase): |
|
52 |
def setUp(self): |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
53 |
self._cw = 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""" |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
58 |
list_arg = self._cw.list_form_param |
0 | 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): |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
72 |
self._cw.vreg['controllers'] = {'view': 1, 'login': 1} |
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
73 |
self.assertEquals(self._cw.from_controller(), 'view') |
0 | 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() |
|
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3293
diff
changeset
|
126 |
self.ctrl._cw.form = {'__linkto' : 'works_for:12_13_14:object'} |
0 | 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): |
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 test_cnx_user_groups_sync(self): |
|
148 |
user = self.user() |
|
149 |
self.assertEquals(user.groups, set(('managers',))) |
|
150 |
self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
151 |
user = self.user() |
|
152 |
self.assertEquals(user.groups, set(('managers',))) |
|
153 |
self.commit() |
|
154 |
user = self.user() |
|
155 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
156 |
# cleanup |
|
157 |
self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
158 |
self.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
159 |
|
0 | 160 |
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
|
161 |
req = self.request(u'CWEType X WHERE X final FALSE, X meta FALSE') |
0 | 162 |
self.app.publish('view', req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
163 |
|
0 | 164 |
def test_nonregr_publish2(self): |
165 |
req = self.request(u'Any count(N) WHERE N todo_by U, N is Note, U eid %s' |
|
166 |
% self.user().eid) |
|
167 |
self.app.publish('view', req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
168 |
|
0 | 169 |
def test_publish_validation_error(self): |
170 |
req = self.request() |
|
171 |
user = self.user() |
|
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
172 |
eid = unicode(user.eid) |
0 | 173 |
req.form = { |
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
174 |
'eid': eid, |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
175 |
'__type:'+eid: 'CWUser', '_cw_edited_fields:'+eid: 'login-subject', |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
176 |
'login-subject:'+eid: '', # ERROR: no login specified |
0 | 177 |
# just a sample, missing some necessary information for real life |
178 |
'__errorurl': 'view?vid=edition...' |
|
179 |
} |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
180 |
path, params = self.expect_redirect(lambda x: self.app_publish(x, 'edit'), req) |
0 | 181 |
forminfo = req.get_session_data('view?vid=edition...') |
182 |
eidmap = forminfo['eidmap'] |
|
183 |
self.assertEquals(eidmap, {}) |
|
184 |
values = forminfo['values'] |
|
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
185 |
self.assertEquals(values['login-subject:'+eid], '') |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
186 |
self.assertEquals(values['eid'], eid) |
4276 | 187 |
error = forminfo['error'] |
188 |
self.assertEquals(error.entity, user.eid) |
|
189 |
self.assertEquals(error.errors['login'], 'required attribute') |
|
0 | 190 |
|
191 |
||
192 |
def test_validation_error_dont_loose_subentity_data(self): |
|
193 |
"""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
|
194 |
""" |
0 | 195 |
req = self.request() |
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
196 |
form = {'eid': ['X', 'Y'], '__maineid': 'X', |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
197 |
'__type:X': 'CWUser', '_cw_edited_fields:X': 'login-subject,surname-subject', |
0 | 198 |
# missing required field |
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
199 |
'login-subject:X': u'', |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
200 |
'surname-subject:X': u'Mr Ouaoua', |
0 | 201 |
# but email address is set |
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
202 |
'__type:Y': 'EmailAddress', '_cw_edited_fields:Y': 'address-subject,alias-subject,use_email-object', |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
203 |
'address-subject:Y': u'bougloup@logilab.fr', |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
204 |
'alias-subject:Y': u'', |
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
205 |
'use_email-object:Y': 'X', |
0 | 206 |
# necessary to get validation error handling |
207 |
'__errorurl': 'view?vid=edition...', |
|
208 |
} |
|
209 |
req.form = form |
|
210 |
# monkey patch edited_eid to ensure both entities are edited, not only X |
|
211 |
req.edited_eids = lambda : ('Y', 'X') |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
212 |
path, params = self.expect_redirect(lambda x: self.app_publish(x, 'edit'), req) |
0 | 213 |
forminfo = req.get_session_data('view?vid=edition...') |
4172
4d4cef034eec
all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
214 |
self.assertEquals(set(forminfo['eidmap']), set('XY')) |
4276 | 215 |
self.assertEquals(forminfo['error'].entity, forminfo['eidmap']['X']) |
216 |
self.assertEquals(forminfo['error'].errors, {'login': 'required attribute', |
|
217 |
'upassword': 'required attribute'}) |
|
0 | 218 |
self.assertEquals(forminfo['values'], form) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
219 |
|
0 | 220 |
def _test_cleaned(self, kwargs, injected, cleaned): |
221 |
req = self.request(**kwargs) |
|
222 |
page = self.app.publish('view', req) |
|
223 |
self.failIf(injected in page, (kwargs, injected)) |
|
224 |
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
|
225 |
|
0 | 226 |
def test_nonregr_script_kiddies(self): |
227 |
"""test against current script injection""" |
|
228 |
injected = '<i>toto</i>' |
|
229 |
cleaned = 'toto' |
|
230 |
for kwargs in ({'__message': injected}, |
|
231 |
{'vid': injected}, |
|
232 |
{'vtitle': injected}, |
|
233 |
): |
|
234 |
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
|
235 |
|
0 | 236 |
def test_site_wide_eproperties_sync(self): |
237 |
# XXX work in all-in-one configuration but not in twisted for instance |
|
238 |
# in which case we need a kindof repo -> http server notification |
|
239 |
# protocol |
|
240 |
vreg = self.app.vreg |
|
241 |
# default value |
|
242 |
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
|
243 |
self.execute('INSERT CWProperty X: X value "fr", X pkey "ui.language"') |
0 | 244 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
245 |
self.commit() |
|
246 |
self.assertEquals(vreg.property_value('ui.language'), 'fr') |
|
247 |
self.execute('SET X value "de" WHERE X pkey "ui.language"') |
|
248 |
self.assertEquals(vreg.property_value('ui.language'), 'fr') |
|
249 |
self.commit() |
|
250 |
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
|
251 |
self.execute('DELETE CWProperty X WHERE X pkey "ui.language"') |
0 | 252 |
self.assertEquals(vreg.property_value('ui.language'), 'de') |
253 |
self.commit() |
|
254 |
self.assertEquals(vreg.property_value('ui.language'), 'en') |
|
255 |
||
256 |
def test_fb_login_concept(self): |
|
257 |
"""see data/views.py""" |
|
258 |
self.set_option('auth-mode', 'cookie') |
|
259 |
self.set_option('anonymous-user', 'anon') |
|
260 |
self.login('anon') |
|
261 |
req = self.request() |
|
262 |
origcnx = req.cnx |
|
263 |
req.form['__fblogin'] = u'turlututu' |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
264 |
page = self.app_publish(req) |
0 | 265 |
self.failIf(req.cnx is origcnx) |
266 |
self.assertEquals(req.user.login, 'turlututu') |
|
267 |
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
|
268 |
|
0 | 269 |
# authentication tests #################################################### |
270 |
||
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
271 |
def test_http_auth_no_anon(self): |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
272 |
req, origcnx = self.init_authentication('http') |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
273 |
self.assertAuthFailure(req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
274 |
self.assertRaises(ExplicitLogin, self.app_publish, req, 'login') |
0 | 275 |
self.assertEquals(req.cnx, None) |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
276 |
authstr = base64.encodestring('%s:%s' % (origcnx.login, origcnx.authinfo['password'])) |
0 | 277 |
req._headers['Authorization'] = 'basic %s' % authstr |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
278 |
self.assertAuthSuccess(req, origcnx) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
279 |
self.assertEquals(req.cnx.authinfo, {'password': origcnx.authinfo['password']}) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
280 |
self.assertRaises(AuthenticationError, self.app_publish, req, 'logout') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
281 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 282 |
|
283 |
def test_cookie_auth_no_anon(self): |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
284 |
req, origcnx = self.init_authentication('cookie') |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
285 |
self.assertAuthFailure(req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
286 |
form = self.app_publish(req, 'login') |
0 | 287 |
self.failUnless('__login' in form) |
288 |
self.failUnless('__password' in form) |
|
289 |
self.assertEquals(req.cnx, None) |
|
290 |
req.form['__login'] = origcnx.login |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
291 |
req.form['__password'] = origcnx.authinfo['password'] |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
292 |
self.assertAuthSuccess(req, origcnx) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
293 |
self.assertEquals(req.cnx.authinfo, {'password': origcnx.authinfo['password']}) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
294 |
self.assertRaises(AuthenticationError, self.app_publish, req, 'logout') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
295 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 296 |
|
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
297 |
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
|
298 |
login = self.request().user.login |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
299 |
address = login + u'@localhost' |
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
300 |
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
|
301 |
'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
|
302 |
self.commit() |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
303 |
# option allow-email-login not set |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
304 |
req, origcnx = self.init_authentication('cookie') |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
305 |
req.form['__login'] = address |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
306 |
req.form['__password'] = origcnx.authinfo['password'] |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
307 |
self.assertAuthFailure(req) |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
308 |
# 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
|
309 |
origcnx.login = address |
1490
6b024694d493
add allow-email-login option
Florent <florent@secondweb.fr>
parents:
1489
diff
changeset
|
310 |
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
|
311 |
req.form['__login'] = address |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
312 |
req.form['__password'] = origcnx.authinfo['password'] |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
313 |
self.assertAuthSuccess(req, origcnx) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
314 |
self.assertEquals(req.cnx.authinfo, {'password': origcnx.authinfo['password']}) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
315 |
self.assertRaises(AuthenticationError, self.app_publish, req, 'logout') |
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) |
1489
08acef58ad08
add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents:
1398
diff
changeset
|
317 |
|
0 | 318 |
def _reset_cookie(self, req): |
319 |
# preparing the suite of the test |
|
320 |
# set session id in cookie |
|
321 |
cookie = Cookie.SimpleCookie() |
|
322 |
cookie['__session'] = req.cnx.sessionid |
|
323 |
req._headers['Cookie'] = cookie['__session'].OutputString() |
|
324 |
clear_cache(req, 'get_authorization') |
|
325 |
# reset cnx as if it was a new incoming request |
|
326 |
req.cnx = None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
327 |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
328 |
def _test_auth_anon(self, req): |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
329 |
self.app.connect(req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
330 |
acnx = req.cnx |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
331 |
self.assertEquals(len(self.open_sessions), 1) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
332 |
self.assertEquals(acnx.login, 'anon') |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
333 |
self.assertEquals(acnx.authinfo['password'], 'anon') |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
334 |
self.failUnless(acnx.anonymous_connection) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
335 |
self._reset_cookie(req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
336 |
|
0 | 337 |
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
|
338 |
self.assertEquals(len(self.open_sessions), 1) |
0 | 339 |
self.app.connect(req) |
340 |
self.assertEquals(req.message, 'authentication failure') |
|
341 |
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
|
342 |
self.assertEquals(len(self.open_sessions), 1) |
0 | 343 |
self._reset_cookie(req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
344 |
|
0 | 345 |
def test_http_auth_anon_allowed(self): |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
346 |
req, origcnx = self.init_authentication('http', 'anon') |
0 | 347 |
self._test_auth_anon(req) |
348 |
authstr = base64.encodestring('toto:pouet') |
|
349 |
req._headers['Authorization'] = 'basic %s' % authstr |
|
350 |
self._test_anon_auth_fail(req) |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
351 |
authstr = base64.encodestring('%s:%s' % (origcnx.login, origcnx.authinfo['password'])) |
0 | 352 |
req._headers['Authorization'] = 'basic %s' % authstr |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
353 |
self.assertAuthSuccess(req, origcnx) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
354 |
self.assertEquals(req.cnx.authinfo, {'password': origcnx.authinfo['password']}) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
355 |
self.assertRaises(AuthenticationError, self.app_publish, req, 'logout') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
356 |
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
|
357 |
|
0 | 358 |
def test_cookie_auth_anon_allowed(self): |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
359 |
req, origcnx = self.init_authentication('cookie', 'anon') |
0 | 360 |
self._test_auth_anon(req) |
361 |
req.form['__login'] = 'toto' |
|
362 |
req.form['__password'] = 'pouet' |
|
363 |
self._test_anon_auth_fail(req) |
|
364 |
req.form['__login'] = origcnx.login |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
365 |
req.form['__password'] = origcnx.authinfo['password'] |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
366 |
self.assertAuthSuccess(req, origcnx) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
367 |
self.assertEquals(req.cnx.authinfo, {'password': origcnx.authinfo['password']}) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
368 |
self.assertRaises(AuthenticationError, self.app_publish, req, 'logout') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1490
diff
changeset
|
369 |
self.assertEquals(len(self.open_sessions), 0) |
0 | 370 |
|
3523
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
371 |
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
|
372 |
req = self.request() |
16880e7ee3fa
don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3265
diff
changeset
|
373 |
# 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
|
374 |
req.form['rql'] = 'rql:Any OV1, X WHERE X custom_workflow OV1?' |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3524
diff
changeset
|
375 |
self.app_publish(req) |
0 | 376 |
|
377 |
if __name__ == '__main__': |
|
378 |
unittest_main() |