author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 12:15:52 +0200 | |
branch | oldstable |
changeset 5424 | 8ecbcbff9777 |
parent 5421 | 8167de96c523 |
child 5443 | f299ee54d7e0 |
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 |
19 |
||
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 = {} |
|
33 |
apphome = None |
|
34 |
def __init__(self, appid='data', apphome=None, cubes=()): |
|
35 |
self.appid = appid |
|
36 |
self.apphome = apphome |
|
37 |
self._cubes = cubes |
|
38 |
self['auth-mode'] = 'cookie' |
|
1482 | 39 |
self['uid'] = None |
0 | 40 |
self['base-url'] = BASE_URL |
41 |
self['rql-cache-size'] = 100 |
|
1482 | 42 |
|
0 | 43 |
def cubes(self, expand=False): |
44 |
return self._cubes |
|
1482 | 45 |
|
0 | 46 |
def sources(self): |
47 |
return {} |
|
48 |
||
49 |
||
50 |
class FakeRequest(CubicWebRequestBase): |
|
51 |
"""test implementation of an cubicweb request object""" |
|
52 |
||
53 |
def __init__(self, *args, **kwargs): |
|
54 |
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
|
55 |
kwargs['vreg'] = CubicWebVRegistry(FakeConfig(), initlog=False) |
0 | 56 |
kwargs['https'] = False |
57 |
self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') |
|
58 |
super(FakeRequest, self).__init__(*args, **kwargs) |
|
59 |
self._session_data = {} |
|
60 |
self._headers = {} |
|
61 |
||
62 |
def header_accept_language(self): |
|
63 |
"""returns an ordered list of preferred languages""" |
|
64 |
return ('en',) |
|
65 |
||
66 |
def header_if_modified_since(self): |
|
67 |
return None |
|
68 |
||
69 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
70 |
"""return the root url of the instance""" |
0 | 71 |
return BASE_URL |
72 |
||
73 |
def relative_path(self, includeparams=True): |
|
74 |
"""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
|
75 |
to the instance's root, but some other normalization may be needed |
0 | 76 |
so that the returned path may be used to compare to generated urls |
77 |
""" |
|
78 |
if self._url.startswith(BASE_URL): |
|
79 |
url = self._url[len(BASE_URL):] |
|
80 |
else: |
|
81 |
url = self._url |
|
82 |
if includeparams: |
|
83 |
return url |
|
84 |
return url.split('?', 1)[0] |
|
85 |
||
86 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
87 |
"""set output content type for this request. An optional filename |
|
88 |
may be given |
|
89 |
""" |
|
90 |
pass |
|
91 |
||
4457
297a63704761
mimick actual request set_header method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
92 |
def set_header(self, header, value, raw=True): |
0 | 93 |
"""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
|
94 |
self._headers[header] = value |
1482 | 95 |
|
0 | 96 |
def add_header(self, header, value): |
97 |
"""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
|
98 |
self._headers[header] = value # XXX |
1482 | 99 |
|
0 | 100 |
def remove_header(self, header): |
101 |
"""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
|
102 |
self._headers.pop(header, None) |
1482 | 103 |
|
0 | 104 |
def get_header(self, header, default=None): |
105 |
"""return the value associated with the given input header, |
|
106 |
raise KeyError if the header is not set |
|
107 |
""" |
|
108 |
return self._headers.get(header, default) |
|
109 |
||
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3652
diff
changeset
|
110 |
def set_cookie(self, cookie, key, maxage=300, expires=None): |
0 | 111 |
"""set / update a cookie key |
112 |
||
113 |
by default, cookie will be available for the next 5 minutes |
|
114 |
""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
if expires: |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
119 |
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
|
120 |
# 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
|
121 |
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
|
122 |
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
|
123 |
self.add_header('Cookie', morsel.OutputString()) |
0 | 124 |
|
125 |
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
|
126 |
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
|
127 |
self.remove_header('Cookie') |
0 | 128 |
|
129 |
def validate_cache(self): |
|
130 |
pass |
|
131 |
||
132 |
||
133 |
class FakeUser(object): |
|
134 |
login = 'toto' |
|
135 |
eid = 0 |
|
136 |
def in_groups(self, groups): |
|
137 |
return True |
|
138 |
||
139 |
||
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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
|
0 | 144 |
def __init__(self, repo=None, user=None): |
145 |
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
|
146 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 147 |
self.pool = FakePool() |
148 |
self.user = user or FakeUser() |
|
149 |
self.is_internal_session = False |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
150 |
self.transaction_data = {} |
1482 | 151 |
|
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
|
152 |
def execute(self, *args, **kwargs): |
0 | 153 |
pass |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
154 |
|
0 | 155 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
156 |
self.transaction_data.clear() |
0 | 157 |
def close(self, *args): |
158 |
pass |
|
159 |
def system_sql(self, sql, args=None): |
|
160 |
pass |
|
161 |
||
162 |
def set_entity_cache(self, entity): |
|
163 |
pass |
|
1482 | 164 |
|
0 | 165 |
class FakeRepo(object): |
166 |
querier = None |
|
167 |
def __init__(self, schema, vreg=None, config=None): |
|
168 |
self.extids = {} |
|
169 |
self.eids = {} |
|
170 |
self._count = 0 |
|
171 |
self.schema = schema |
|
172 |
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
|
173 |
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
|
174 |
self.vreg.schema = schema |
0 | 175 |
|
176 |
def internal_session(self): |
|
177 |
return FakeSession(self) |
|
1481 | 178 |
|
179 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
180 |
recreate=False): |
|
0 | 181 |
try: |
182 |
return self.extids[extid] |
|
183 |
except KeyError: |
|
184 |
if not insert: |
|
185 |
return None |
|
186 |
self._count += 1 |
|
187 |
eid = self._count |
|
188 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
189 |
self.extids[extid] = eid |
|
190 |
self.eids[eid] = extid |
|
191 |
source.after_entity_insertion(session, extid, entity) |
|
192 |
return eid |
|
1482 | 193 |
|
0 | 194 |
def eid2extid(self, source, eid, session=None): |
195 |
return self.eids[eid] |
|
196 |
||
197 |
||
198 |
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
|
199 |
dbhelper = get_db_helper('sqlite') |
0 | 200 |
def __init__(self, uri): |
201 |
self.uri = uri |
|
202 |
||
1482 | 203 |
|
0 | 204 |
class FakePool(object): |
205 |
def source(self, uri): |
|
206 |
return FakeSource(uri) |