author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 20 Apr 2010 18:22:41 +0200 | |
changeset 5357 | 6bf3dbd836bc |
parent 4491 | a0f48c31b58a |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
1 |
""" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
2 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2856
diff
changeset
|
3 |
:copyright: 2008-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
4 |
: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:
1802
diff
changeset
|
5 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
6 |
""" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
7 |
__docformat__ = "restructuredtext en" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
8 |
|
2856
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
9 |
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
|
10 |
try: |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
11 |
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
|
12 |
except ImportError: |
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
13 |
raise TestSkipped('Can not import google.appengine. Skip this module') |
0 | 14 |
|
15 |
import os, os.path as osp |
|
16 |
import time |
|
17 |
from shutil import copy |
|
18 |
||
19 |
# additional monkey patches necessary in regular cubicweb environment |
|
20 |
from cubicweb.server import rqlannotation |
|
21 |
from cubicweb.goa.overrides import rqlannotation as goarqlannotation |
|
444 | 22 |
rqlannotation.SQLGenAnnotator = goarqlannotation.SQLGenAnnotator |
0 | 23 |
rqlannotation.set_qdata = goarqlannotation.set_qdata |
24 |
||
2856
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
25 |
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
|
26 |
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
|
27 |
from google.appengine.ext import db as gdb |
0 | 28 |
|
29 |
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
|
30 |
|
ef3d02642b4c
R [goa] skip these tests if appengine can not be imported
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
31 |
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
|
32 |
from cubicweb.goa.goavreg import GAEVRegistry |
0 | 33 |
from cubicweb.goa.goaconfig import GAEConfiguration |
34 |
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
|
35 |
init_persistent_schema, insert_versions) |
0 | 36 |
|
37 |
import logging |
|
38 |
logger = logging.getLogger() |
|
39 |
logger.setLevel(logging.CRITICAL) |
|
40 |
||
41 |
do_monkey_patch() |
|
42 |
||
43 |
class GAEBasedTC(TestCase): |
|
44 |
APP_ID = u'test_app' |
|
45 |
AUTH_DOMAIN = 'gmail.com' |
|
46 |
LOGGED_IN_USER = u't...@example.com' # set to '' for no logged in user |
|
47 |
MODEL_CLASSES = None |
|
48 |
LOAD_APP_MODULES = None |
|
49 |
config = None |
|
50 |
_DS_TEMPL_FILE = 'tmpdb-template' |
|
51 |
||
52 |
def load_schema_hook(self, loader): |
|
518 | 53 |
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
|
54 |
|
0 | 55 |
@property |
56 |
def DS_FILE(self): |
|
57 |
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
|
58 |
|
0 | 59 |
@property |
60 |
def DS_TEMPL_FILE(self): |
|
61 |
return self._DS_TEMPL_FILE + '_'.join(sorted(cls.__name__ for cls in self.MODEL_CLASSES)) |
|
62 |
||
63 |
def _set_ds_file(self, dsfile): |
|
64 |
# Start with a fresh api proxy. |
|
65 |
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
|
66 |
# Use a fresh stub datastore. |
|
67 |
stub = datastore_file_stub.DatastoreFileStub(self.APP_ID, dsfile, |
|
68 |
dsfile+'.history') |
|
69 |
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
|
70 |
|
0 | 71 |
def setUp(self): |
72 |
# Ensure we're in UTC. |
|
73 |
os.environ['TZ'] = 'UTC' |
|
74 |
time.tzset() |
|
75 |
if osp.exists(self.DS_TEMPL_FILE): |
|
76 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
77 |
need_ds_init = False |
|
78 |
self._set_ds_file(self.DS_FILE) |
|
79 |
else: |
|
80 |
need_ds_init = True |
|
81 |
self._set_ds_file(self.DS_TEMPL_FILE) |
|
82 |
# from google.appengine.api import mail_stub |
|
83 |
# 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
|
84 |
# from google3.apphosting.api import user_service_stub |
0 | 85 |
# # Use a fresh stub UserService. |
86 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
87 |
# 'user', user_service_stub.UserServiceStub()) |
|
88 |
os.environ['AUTH_DOMAIN'] = self.AUTH_DOMAIN |
|
89 |
os.environ['USER_EMAIL'] = self.LOGGED_IN_USER |
|
90 |
# # Use a fresh urlfetch stub. |
|
91 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
92 |
# 'urlfetch', urlfetch_stub.URLFetchServiceStub()) |
|
93 |
# # Use a fresh mail stub. |
|
94 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
95 |
# 'mail', mail_stub.MailServiceStub()) |
|
96 |
if self.MODEL_CLASSES is None: |
|
97 |
raise Exception('GAEBasedTC should set MODEL_CLASSES class attribute') |
|
98 |
gdb._kind_map = {} |
|
99 |
self.config = self.config or GAEConfiguration('toto') |
|
100 |
self.config.init_log(logging.CRITICAL) |
|
101 |
self.schema = self.config.load_schema(self.MODEL_CLASSES, |
|
102 |
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
|
103 |
self.vreg = GAEVregistry(self.config) |
0 | 104 |
self.vreg.schema = self.schema |
105 |
self.vreg.load_module(db) |
|
106 |
from cubicweb.goa.appobjects import sessions |
|
107 |
self.vreg.load_module(sessions) |
|
108 |
from cubicweb.entities import authobjs, schemaobjs |
|
109 |
self.vreg.load_module(authobjs) |
|
110 |
self.vreg.load_module(schemaobjs) |
|
111 |
if self.config['use-google-auth']: |
|
112 |
from cubicweb.goa.appobjects import gauthservice |
|
113 |
self.vreg.load_module(gauthservice) |
|
114 |
if self.LOAD_APP_MODULES is not None: |
|
115 |
for module in self.LOAD_APP_MODULES: |
|
116 |
self.vreg.load_module(module) |
|
117 |
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
|
118 |
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
|
119 |
self.session_manager = self.vreg.select('components', 'sessionmanager') |
0 | 120 |
if need_ds_init: |
121 |
# create default groups and create entities according to the schema |
|
122 |
create_groups() |
|
123 |
if not self.config['use-google-auth']: |
|
124 |
create_user(self.LOGGED_IN_USER, 'toto', ('users', 'managers')) |
|
125 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
126 |
else: |
|
127 |
req = FakeRequest(vreg=self.vreg) |
|
128 |
self.session = self.session_manager.open_session(req) |
|
129 |
self.user = self.session.user() |
|
130 |
ssession = self.config.repo_session(self.session.sessionid) |
|
131 |
ssession.set_pool() |
|
132 |
init_persistent_schema(ssession, self.schema) |
|
133 |
insert_versions(ssession, self.config) |
|
134 |
ssession.commit() |
|
135 |
fix_entities(self.schema) |
|
136 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
137 |
self._set_ds_file(self.DS_FILE) |
|
138 |
else: |
|
139 |
if not self.config['use-google-auth']: |
|
140 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
141 |
else: |
|
142 |
req = FakeRequest(vreg=self.vreg) |
|
143 |
self.session = self.session_manager.open_session(req) |
|
144 |
self.user = self.session.user() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
145 |
|
0 | 146 |
def tearDown(self): |
147 |
self.session.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
148 |
|
0 | 149 |
def request(self): |
150 |
req = FakeRequest(vreg=self.vreg) |
|
151 |
req.set_connection(self.session, self.user) |
|
152 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
153 |
|
0 | 154 |
def add_entity(self, etype, **kwargs): |
155 |
cu = self.session.cursor() |
|
156 |
rql = 'INSERT %s X' % etype |
|
157 |
if kwargs: |
|
158 |
rql += ': %s' % ', '.join('X %s %%(%s)s' % (key, key) for key in kwargs) |
|
159 |
rset = cu.execute(rql, kwargs) |
|
160 |
return rset.get_entity(0, 0) |
|
161 |
||
162 |
def execute(self, *args): |
|
163 |
return self.session.cursor().execute(*args) |
|
164 |
||
165 |
def commit(self): |
|
166 |
self.session.commit() |
|
167 |
||
168 |
def rollback(self): |
|
169 |
self.session.rollback() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
170 |
|
0 | 171 |
def create_user(self, login, groups=('users',), req=None): |
172 |
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
|
173 |
user = self.add_entity('CWUser', upassword=str(login), login=unicode(login)) |
0 | 174 |
cu = self.session.cursor() |
175 |
cu.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
|
176 |
% ','.join(repr(g) for g in groups), |
|
177 |
{'x': user.eid}, 'x') |
|
178 |
return user |
|
179 |
||
180 |
def login(self, login, password=None): |
|
181 |
assert not self.config['use-google-auth'] |
|
182 |
req = FakeRequest(vreg=self.vreg) |
|
183 |
req.form['__login'] = login |
|
184 |
req.form['__password'] = password or login |
|
185 |
return self.session_manager.open_session(req) |