author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 23 Sep 2010 22:37:46 +0200 | |
branch | stable |
changeset 6329 | 918dd7448630 |
parent 5768 | 1e73a466aa69 |
child 6427 | c8a5ac2d1eaa |
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 |
||
130 |
||
131 |
class FakeUser(object): |
|
132 |
login = 'toto' |
|
133 |
eid = 0 |
|
134 |
def in_groups(self, groups): |
|
135 |
return True |
|
136 |
||
137 |
||
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
|
0 | 142 |
def __init__(self, repo=None, user=None): |
143 |
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
|
144 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 145 |
self.pool = FakePool() |
146 |
self.user = user or FakeUser() |
|
147 |
self.is_internal_session = False |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
148 |
self.transaction_data = {} |
1482 | 149 |
|
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
|
150 |
def execute(self, *args, **kwargs): |
0 | 151 |
pass |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
152 |
|
0 | 153 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
154 |
self.transaction_data.clear() |
0 | 155 |
def close(self, *args): |
156 |
pass |
|
157 |
def system_sql(self, sql, args=None): |
|
158 |
pass |
|
159 |
||
160 |
def set_entity_cache(self, entity): |
|
161 |
pass |
|
1482 | 162 |
|
0 | 163 |
class FakeRepo(object): |
164 |
querier = None |
|
165 |
def __init__(self, schema, vreg=None, config=None): |
|
166 |
self.extids = {} |
|
167 |
self.eids = {} |
|
168 |
self._count = 0 |
|
169 |
self.schema = schema |
|
170 |
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
|
171 |
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
|
172 |
self.vreg.schema = schema |
0 | 173 |
|
174 |
def internal_session(self): |
|
175 |
return FakeSession(self) |
|
1481 | 176 |
|
177 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
178 |
recreate=False): |
|
0 | 179 |
try: |
180 |
return self.extids[extid] |
|
181 |
except KeyError: |
|
182 |
if not insert: |
|
183 |
return None |
|
184 |
self._count += 1 |
|
185 |
eid = self._count |
|
186 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
187 |
self.extids[extid] = eid |
|
188 |
self.eids[eid] = extid |
|
189 |
source.after_entity_insertion(session, extid, entity) |
|
190 |
return eid |
|
1482 | 191 |
|
0 | 192 |
def eid2extid(self, source, eid, session=None): |
193 |
return self.eids[eid] |
|
194 |
||
195 |
||
196 |
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
|
197 |
dbhelper = get_db_helper('sqlite') |
0 | 198 |
def __init__(self, uri): |
199 |
self.uri = uri |
|
200 |
||
1482 | 201 |
|
0 | 202 |
class FakePool(object): |
203 |
def source(self, uri): |
|
204 |
return FakeSource(uri) |