author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 06 Jul 2010 17:15:25 +0200 | |
changeset 5909 | 9af8ac182f53 |
parent 5424 | 8ecbcbff9777 |
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
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:
4491
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
18 |
""" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
19 |
""" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
20 |
__docformat__ = "restructuredtext en" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
21 |
|
2856
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
22 |
from logilab.common.testlib import TestCase, TestSkipped |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
23 |
try: |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
24 |
import google.appengine |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
25 |
except ImportError: |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
26 |
raise TestSkipped('Can not import google.appengine. Skip this module') |
0 | 27 |
|
28 |
import os, os.path as osp |
|
29 |
import time |
|
30 |
from shutil import copy |
|
31 |
||
32 |
# additional monkey patches necessary in regular cubicweb environment |
|
33 |
from cubicweb.server import rqlannotation |
|
34 |
from cubicweb.goa.overrides import rqlannotation as goarqlannotation |
|
444 | 35 |
rqlannotation.SQLGenAnnotator = goarqlannotation.SQLGenAnnotator |
0 | 36 |
rqlannotation.set_qdata = goarqlannotation.set_qdata |
37 |
||
2856
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
38 |
from google.appengine.api import apiproxy_stub_map |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
39 |
from google.appengine.api import datastore_file_stub |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
40 |
from google.appengine.ext import db as gdb |
0 | 41 |
|
42 |
from cubicweb.devtools.fake import FakeRequest |
|
2856
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
43 |
|
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
44 |
from cubicweb.goa import db, do_monkey_patch |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
45 |
from cubicweb.goa.goavreg import GAEVRegistry |
0 | 46 |
from cubicweb.goa.goaconfig import GAEConfiguration |
47 |
from cubicweb.goa.dbinit import (create_user, create_groups, fix_entities, |
|
2856
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
48 |
init_persistent_schema, insert_versions) |
0 | 49 |
|
50 |
import logging |
|
51 |
logger = logging.getLogger() |
|
52 |
logger.setLevel(logging.CRITICAL) |
|
53 |
||
54 |
do_monkey_patch() |
|
55 |
||
56 |
class GAEBasedTC(TestCase): |
|
57 |
APP_ID = u'test_app' |
|
58 |
AUTH_DOMAIN = 'gmail.com' |
|
59 |
LOGGED_IN_USER = u't...@example.com' # set to '' for no logged in user |
|
60 |
MODEL_CLASSES = None |
|
61 |
LOAD_APP_MODULES = None |
|
62 |
config = None |
|
63 |
_DS_TEMPL_FILE = 'tmpdb-template' |
|
64 |
||
65 |
def load_schema_hook(self, loader): |
|
518 | 66 |
loader.import_yams_cube_schema('data') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
67 |
|
0 | 68 |
@property |
69 |
def DS_FILE(self): |
|
70 |
return self.DS_TEMPL_FILE.replace('-template', '') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
71 |
|
0 | 72 |
@property |
73 |
def DS_TEMPL_FILE(self): |
|
74 |
return self._DS_TEMPL_FILE + '_'.join(sorted(cls.__name__ for cls in self.MODEL_CLASSES)) |
|
75 |
||
76 |
def _set_ds_file(self, dsfile): |
|
77 |
# Start with a fresh api proxy. |
|
78 |
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
|
79 |
# Use a fresh stub datastore. |
|
80 |
stub = datastore_file_stub.DatastoreFileStub(self.APP_ID, dsfile, |
|
81 |
dsfile+'.history') |
|
82 |
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
83 |
|
0 | 84 |
def setUp(self): |
85 |
# Ensure we're in UTC. |
|
86 |
os.environ['TZ'] = 'UTC' |
|
87 |
time.tzset() |
|
88 |
if osp.exists(self.DS_TEMPL_FILE): |
|
89 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
90 |
need_ds_init = False |
|
91 |
self._set_ds_file(self.DS_FILE) |
|
92 |
else: |
|
93 |
need_ds_init = True |
|
94 |
self._set_ds_file(self.DS_TEMPL_FILE) |
|
95 |
# from google.appengine.api import mail_stub |
|
96 |
# from google3.apphosting.api import urlfetch_stub |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
97 |
# from google3.apphosting.api import user_service_stub |
0 | 98 |
# # Use a fresh stub UserService. |
99 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
100 |
# 'user', user_service_stub.UserServiceStub()) |
|
101 |
os.environ['AUTH_DOMAIN'] = self.AUTH_DOMAIN |
|
102 |
os.environ['USER_EMAIL'] = self.LOGGED_IN_USER |
|
103 |
# # Use a fresh urlfetch stub. |
|
104 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
105 |
# 'urlfetch', urlfetch_stub.URLFetchServiceStub()) |
|
106 |
# # Use a fresh mail stub. |
|
107 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
108 |
# 'mail', mail_stub.MailServiceStub()) |
|
109 |
if self.MODEL_CLASSES is None: |
|
110 |
raise Exception('GAEBasedTC should set MODEL_CLASSES class attribute') |
|
111 |
gdb._kind_map = {} |
|
112 |
self.config = self.config or GAEConfiguration('toto') |
|
113 |
self.config.init_log(logging.CRITICAL) |
|
114 |
self.schema = self.config.load_schema(self.MODEL_CLASSES, |
|
115 |
self.load_schema_hook) |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
116 |
self.vreg = GAEVregistry(self.config) |
0 | 117 |
self.vreg.schema = self.schema |
118 |
self.vreg.load_module(db) |
|
119 |
from cubicweb.goa.appobjects import sessions |
|
120 |
self.vreg.load_module(sessions) |
|
121 |
from cubicweb.entities import authobjs, schemaobjs |
|
122 |
self.vreg.load_module(authobjs) |
|
123 |
self.vreg.load_module(schemaobjs) |
|
124 |
if self.config['use-google-auth']: |
|
125 |
from cubicweb.goa.appobjects import gauthservice |
|
126 |
self.vreg.load_module(gauthservice) |
|
127 |
if self.LOAD_APP_MODULES is not None: |
|
128 |
for module in self.LOAD_APP_MODULES: |
|
129 |
self.vreg.load_module(module) |
|
130 |
for cls in self.MODEL_CLASSES: |
|
4491
a0f48c31b58a
kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
131 |
self.vreg.register(cls) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
132 |
self.session_manager = self.vreg.select('components', 'sessionmanager') |
0 | 133 |
if need_ds_init: |
134 |
# create default groups and create entities according to the schema |
|
135 |
create_groups() |
|
136 |
if not self.config['use-google-auth']: |
|
137 |
create_user(self.LOGGED_IN_USER, 'toto', ('users', 'managers')) |
|
138 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
139 |
else: |
|
140 |
req = FakeRequest(vreg=self.vreg) |
|
141 |
self.session = self.session_manager.open_session(req) |
|
142 |
self.user = self.session.user() |
|
143 |
ssession = self.config.repo_session(self.session.sessionid) |
|
144 |
ssession.set_pool() |
|
145 |
init_persistent_schema(ssession, self.schema) |
|
146 |
insert_versions(ssession, self.config) |
|
147 |
ssession.commit() |
|
148 |
fix_entities(self.schema) |
|
149 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
150 |
self._set_ds_file(self.DS_FILE) |
|
151 |
else: |
|
152 |
if not self.config['use-google-auth']: |
|
153 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
154 |
else: |
|
155 |
req = FakeRequest(vreg=self.vreg) |
|
156 |
self.session = self.session_manager.open_session(req) |
|
157 |
self.user = self.session.user() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
158 |
|
0 | 159 |
def tearDown(self): |
160 |
self.session.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
161 |
|
0 | 162 |
def request(self): |
163 |
req = FakeRequest(vreg=self.vreg) |
|
164 |
req.set_connection(self.session, self.user) |
|
165 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
166 |
|
0 | 167 |
def add_entity(self, etype, **kwargs): |
168 |
cu = self.session.cursor() |
|
169 |
rql = 'INSERT %s X' % etype |
|
170 |
if kwargs: |
|
171 |
rql += ': %s' % ', '.join('X %s %%(%s)s' % (key, key) for key in kwargs) |
|
172 |
rset = cu.execute(rql, kwargs) |
|
173 |
return rset.get_entity(0, 0) |
|
174 |
||
175 |
def execute(self, *args): |
|
176 |
return self.session.cursor().execute(*args) |
|
177 |
||
178 |
def commit(self): |
|
179 |
self.session.commit() |
|
180 |
||
181 |
def rollback(self): |
|
182 |
self.session.rollback() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
183 |
|
0 | 184 |
def create_user(self, login, groups=('users',), req=None): |
185 |
assert not self.config['use-google-auth'] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
186 |
user = self.add_entity('CWUser', upassword=str(login), login=unicode(login)) |
0 | 187 |
cu = self.session.cursor() |
188 |
cu.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
|
189 |
% ','.join(repr(g) for g in groups), |
|
190 |
{'x': user.eid}, 'x') |
|
191 |
return user |
|
192 |
||
193 |
def login(self, login, password=None): |
|
194 |
assert not self.config['use-google-auth'] |
|
195 |
req = FakeRequest(vreg=self.vreg) |
|
196 |
req.form['__login'] = login |
|
197 |
req.form['__password'] = password or login |
|
198 |
return self.session_manager.open_session(req) |