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