author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 30 Nov 2010 15:05:07 +0100 | |
changeset 6712 | 3e6cd6048be8 |
parent 6595 | 00cd0b273cf5 |
child 6851 | 824d5b6eae7f |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Fake objects to ease testing of cubicweb without a fully working environment |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5467
diff
changeset
|
19 |
""" |
0 | 20 |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
23 |
from logilab.database import get_db_helper |
0 | 24 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
25 |
from cubicweb.req import RequestSessionBase |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2703
diff
changeset
|
26 |
from cubicweb.cwvreg import CubicWebVRegistry |
0 | 27 |
from cubicweb.web.request import CubicWebRequestBase |
28 |
from cubicweb.devtools import BASE_URL, BaseApptestConfiguration |
|
29 |
||
30 |
||
31 |
class FakeConfig(dict, BaseApptestConfiguration): |
|
32 |
translations = {} |
|
5467
57372dbfd114
[https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5444
diff
changeset
|
33 |
uiprops = {} |
0 | 34 |
apphome = None |
35 |
def __init__(self, appid='data', apphome=None, cubes=()): |
|
36 |
self.appid = appid |
|
37 |
self.apphome = apphome |
|
38 |
self._cubes = cubes |
|
39 |
self['auth-mode'] = 'cookie' |
|
1482 | 40 |
self['uid'] = None |
0 | 41 |
self['base-url'] = BASE_URL |
42 |
self['rql-cache-size'] = 100 |
|
5444
f7fdb5dd82f6
[webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5443
diff
changeset
|
43 |
self.datadir_url = BASE_URL + 'data/' |
1482 | 44 |
|
0 | 45 |
def cubes(self, expand=False): |
46 |
return self._cubes |
|
1482 | 47 |
|
0 | 48 |
def sources(self): |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5467
diff
changeset
|
49 |
return {'system': {'db-driver': 'sqlite'}} |
0 | 50 |
|
51 |
||
52 |
class FakeRequest(CubicWebRequestBase): |
|
53 |
"""test implementation of an cubicweb request object""" |
|
54 |
||
55 |
def __init__(self, *args, **kwargs): |
|
56 |
if not (args or 'vreg' in kwargs): |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2703
diff
changeset
|
57 |
kwargs['vreg'] = CubicWebVRegistry(FakeConfig(), initlog=False) |
0 | 58 |
kwargs['https'] = False |
59 |
self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') |
|
60 |
super(FakeRequest, self).__init__(*args, **kwargs) |
|
61 |
self._session_data = {} |
|
62 |
self._headers = {} |
|
63 |
||
64 |
def header_accept_language(self): |
|
65 |
"""returns an ordered list of preferred languages""" |
|
66 |
return ('en',) |
|
67 |
||
68 |
def header_if_modified_since(self): |
|
69 |
return None |
|
70 |
||
71 |
def relative_path(self, includeparams=True): |
|
72 |
"""return the normalized path of the request (ie at least relative |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
73 |
to the instance's root, but some other normalization may be needed |
0 | 74 |
so that the returned path may be used to compare to generated urls |
75 |
""" |
|
76 |
if self._url.startswith(BASE_URL): |
|
77 |
url = self._url[len(BASE_URL):] |
|
78 |
else: |
|
79 |
url = self._url |
|
80 |
if includeparams: |
|
81 |
return url |
|
82 |
return url.split('?', 1)[0] |
|
83 |
||
84 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
85 |
"""set output content type for this request. An optional filename |
|
86 |
may be given |
|
87 |
""" |
|
88 |
pass |
|
89 |
||
4457
297a63704761
mimick actual request set_header method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
90 |
def set_header(self, header, value, raw=True): |
0 | 91 |
"""set an output HTTP header""" |
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
92 |
self._headers[header] = value |
1482 | 93 |
|
0 | 94 |
def add_header(self, header, value): |
95 |
"""set an output HTTP header""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
96 |
self._headers[header] = value # XXX |
1482 | 97 |
|
0 | 98 |
def remove_header(self, header): |
99 |
"""remove an output HTTP header""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
100 |
self._headers.pop(header, None) |
1482 | 101 |
|
0 | 102 |
def get_header(self, header, default=None): |
103 |
"""return the value associated with the given input header, |
|
104 |
raise KeyError if the header is not set |
|
105 |
""" |
|
106 |
return self._headers.get(header, default) |
|
107 |
||
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3652
diff
changeset
|
108 |
def set_cookie(self, cookie, key, maxage=300, expires=None): |
0 | 109 |
"""set / update a cookie key |
110 |
||
111 |
by default, cookie will be available for the next 5 minutes |
|
112 |
""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
113 |
morsel = cookie[key] |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
114 |
if maxage is not None: |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
115 |
morsel['Max-Age'] = maxage |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
116 |
if expires: |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
117 |
morsel['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S %z') |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
118 |
# make sure cookie is set on the correct path |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
119 |
morsel['path'] = self.base_url_path() |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
120 |
self.add_header('Set-Cookie', morsel.OutputString()) |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
121 |
self.add_header('Cookie', morsel.OutputString()) |
0 | 122 |
|
123 |
def remove_cookie(self, cookie, key): |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
124 |
self.remove_header('Set-Cookie') |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
125 |
self.remove_header('Cookie') |
0 | 126 |
|
127 |
def validate_cache(self): |
|
128 |
pass |
|
129 |
||
6595
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
130 |
def build_url_params(self, **kwargs): |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
131 |
# overriden to get predictable resultts |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
132 |
args = [] |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
133 |
for param, values in sorted(kwargs.iteritems()): |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
134 |
if not isinstance(values, (list, tuple)): |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
135 |
values = (values,) |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
136 |
for value in values: |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
137 |
assert value is not None |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
138 |
args.append(u'%s=%s' % (param, self.url_quote(value))) |
00cd0b273cf5
[test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
139 |
return '&'.join(args) |
0 | 140 |
|
141 |
class FakeUser(object): |
|
142 |
login = 'toto' |
|
143 |
eid = 0 |
|
144 |
def in_groups(self, groups): |
|
145 |
return True |
|
146 |
||
147 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
148 |
class FakeSession(RequestSessionBase): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
149 |
read_security = write_security = True |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
150 |
set_read_security = set_write_security = lambda *args, **kwargs: None |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
151 |
|
0 | 152 |
def __init__(self, repo=None, user=None): |
153 |
self.repo = repo |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2703
diff
changeset
|
154 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 155 |
self.pool = FakePool() |
156 |
self.user = user or FakeUser() |
|
157 |
self.is_internal_session = False |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
158 |
self.transaction_data = {} |
1482 | 159 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
160 |
def execute(self, *args, **kwargs): |
0 | 161 |
pass |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
162 |
|
0 | 163 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
164 |
self.transaction_data.clear() |
0 | 165 |
def close(self, *args): |
166 |
pass |
|
167 |
def system_sql(self, sql, args=None): |
|
168 |
pass |
|
169 |
||
170 |
def set_entity_cache(self, entity): |
|
171 |
pass |
|
1482 | 172 |
|
0 | 173 |
class FakeRepo(object): |
174 |
querier = None |
|
175 |
def __init__(self, schema, vreg=None, config=None): |
|
176 |
self.extids = {} |
|
177 |
self.eids = {} |
|
178 |
self._count = 0 |
|
179 |
self.schema = schema |
|
180 |
self.config = config or FakeConfig() |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2703
diff
changeset
|
181 |
self.vreg = vreg or CubicWebVRegistry(self.config, initlog=False) |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2703
diff
changeset
|
182 |
self.vreg.schema = schema |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
183 |
self.sources = [] |
0 | 184 |
|
185 |
def internal_session(self): |
|
186 |
return FakeSession(self) |
|
1481 | 187 |
|
188 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
189 |
recreate=False): |
|
0 | 190 |
try: |
191 |
return self.extids[extid] |
|
192 |
except KeyError: |
|
193 |
if not insert: |
|
194 |
return None |
|
195 |
self._count += 1 |
|
196 |
eid = self._count |
|
197 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
198 |
self.extids[extid] = eid |
|
199 |
self.eids[eid] = extid |
|
200 |
source.after_entity_insertion(session, extid, entity) |
|
201 |
return eid |
|
1482 | 202 |
|
0 | 203 |
def eid2extid(self, source, eid, session=None): |
204 |
return self.eids[eid] |
|
205 |
||
206 |
||
207 |
class FakeSource(object): |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4459
diff
changeset
|
208 |
dbhelper = get_db_helper('sqlite') |
0 | 209 |
def __init__(self, uri): |
210 |
self.uri = uri |
|
211 |
||
1482 | 212 |
|
0 | 213 |
class FakePool(object): |
214 |
def source(self, uri): |
|
215 |
return FakeSource(uri) |