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