author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 09 Jul 2009 19:36:05 +0200 | |
branch | stable |
changeset 2363 | 1190b312bfe3 |
parent 1977 | 606923dff11b |
child 2058 | 7ef12c03447c |
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 |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
:copyright: 2008-2009 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 |
|
1132 | 9 |
from logilab.common.testlib import TestCase |
0 | 10 |
|
11 |
import os, os.path as osp |
|
12 |
import time |
|
13 |
from shutil import copy |
|
14 |
||
15 |
# additional monkey patches necessary in regular cubicweb environment |
|
16 |
from cubicweb.server import rqlannotation |
|
17 |
from cubicweb.goa.overrides import rqlannotation as goarqlannotation |
|
444 | 18 |
rqlannotation.SQLGenAnnotator = goarqlannotation.SQLGenAnnotator |
0 | 19 |
rqlannotation.set_qdata = goarqlannotation.set_qdata |
20 |
||
21 |
try: |
|
22 |
from google.appengine.api import apiproxy_stub_map |
|
23 |
from google.appengine.api import datastore_file_stub |
|
24 |
from google.appengine.ext import db as gdb |
|
25 |
from cubicweb.goa import db, do_monkey_patch |
|
26 |
import_appengine_failed = None |
|
27 |
except ImportError, exc: |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
28 |
# XXX necessary ? |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
29 |
class db: |
0 | 30 |
class Model: |
31 |
pass |
|
32 |
class DummyProperty: |
|
33 |
def __init__(self, *args, **kwargs): |
|
34 |
pass |
|
35 |
TextProperty = DummyProperty |
|
36 |
StringProperty = DummyProperty |
|
37 |
BlobProperty = DummyProperty |
|
38 |
DateProperty = DummyProperty |
|
39 |
ReferenceProperty = DummyProperty |
|
40 |
SelfReferenceProperty = DummyProperty |
|
41 |
import_appengine_failed = 'cannot import appengine: %s' % exc |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
42 |
|
0 | 43 |
|
44 |
from cubicweb.devtools.fake import FakeRequest |
|
45 |
from cubicweb.goa.goavreg import GAERegistry |
|
46 |
from cubicweb.goa.goaconfig import GAEConfiguration |
|
47 |
from cubicweb.goa.dbinit import (create_user, create_groups, fix_entities, |
|
48 |
init_persistent_schema, insert_versions) |
|
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 |
if import_appengine_failed: |
|
86 |
self.skip(import_appengine_failed) |
|
87 |
# Ensure we're in UTC. |
|
88 |
os.environ['TZ'] = 'UTC' |
|
89 |
time.tzset() |
|
90 |
if osp.exists(self.DS_TEMPL_FILE): |
|
91 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
92 |
need_ds_init = False |
|
93 |
self._set_ds_file(self.DS_FILE) |
|
94 |
else: |
|
95 |
need_ds_init = True |
|
96 |
self._set_ds_file(self.DS_TEMPL_FILE) |
|
97 |
# from google.appengine.api import mail_stub |
|
98 |
# 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
|
99 |
# from google3.apphosting.api import user_service_stub |
0 | 100 |
# # Use a fresh stub UserService. |
101 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
102 |
# 'user', user_service_stub.UserServiceStub()) |
|
103 |
os.environ['AUTH_DOMAIN'] = self.AUTH_DOMAIN |
|
104 |
os.environ['USER_EMAIL'] = self.LOGGED_IN_USER |
|
105 |
# # Use a fresh urlfetch stub. |
|
106 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
107 |
# 'urlfetch', urlfetch_stub.URLFetchServiceStub()) |
|
108 |
# # Use a fresh mail stub. |
|
109 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
110 |
# 'mail', mail_stub.MailServiceStub()) |
|
111 |
if self.MODEL_CLASSES is None: |
|
112 |
raise Exception('GAEBasedTC should set MODEL_CLASSES class attribute') |
|
113 |
gdb._kind_map = {} |
|
114 |
self.config = self.config or GAEConfiguration('toto') |
|
115 |
self.config.init_log(logging.CRITICAL) |
|
116 |
self.schema = self.config.load_schema(self.MODEL_CLASSES, |
|
117 |
self.load_schema_hook) |
|
118 |
self.vreg = GAERegistry(self.config) |
|
119 |
self.vreg.schema = self.schema |
|
120 |
self.vreg.load_module(db) |
|
121 |
from cubicweb.goa.appobjects import sessions |
|
122 |
self.vreg.load_module(sessions) |
|
123 |
from cubicweb.entities import authobjs, schemaobjs |
|
124 |
self.vreg.load_module(authobjs) |
|
125 |
self.vreg.load_module(schemaobjs) |
|
126 |
if self.config['use-google-auth']: |
|
127 |
from cubicweb.goa.appobjects import gauthservice |
|
128 |
self.vreg.load_module(gauthservice) |
|
129 |
if self.LOAD_APP_MODULES is not None: |
|
130 |
for module in self.LOAD_APP_MODULES: |
|
131 |
self.vreg.load_module(module) |
|
132 |
for cls in self.MODEL_CLASSES: |
|
133 |
self.vreg.load_object(cls) |
|
134 |
self.session_manager = self.vreg.select_component('sessionmanager') |
|
135 |
if need_ds_init: |
|
136 |
# create default groups and create entities according to the schema |
|
137 |
create_groups() |
|
138 |
if not self.config['use-google-auth']: |
|
139 |
create_user(self.LOGGED_IN_USER, 'toto', ('users', 'managers')) |
|
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() |
|
145 |
ssession = self.config.repo_session(self.session.sessionid) |
|
146 |
ssession.set_pool() |
|
147 |
init_persistent_schema(ssession, self.schema) |
|
148 |
insert_versions(ssession, self.config) |
|
149 |
ssession.commit() |
|
150 |
fix_entities(self.schema) |
|
151 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
152 |
self._set_ds_file(self.DS_FILE) |
|
153 |
else: |
|
154 |
if not self.config['use-google-auth']: |
|
155 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
156 |
else: |
|
157 |
req = FakeRequest(vreg=self.vreg) |
|
158 |
self.session = self.session_manager.open_session(req) |
|
159 |
self.user = self.session.user() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
160 |
|
0 | 161 |
def tearDown(self): |
162 |
self.session.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
163 |
|
0 | 164 |
def request(self): |
165 |
req = FakeRequest(vreg=self.vreg) |
|
166 |
req.set_connection(self.session, self.user) |
|
167 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
168 |
|
0 | 169 |
def add_entity(self, etype, **kwargs): |
170 |
cu = self.session.cursor() |
|
171 |
rql = 'INSERT %s X' % etype |
|
172 |
if kwargs: |
|
173 |
rql += ': %s' % ', '.join('X %s %%(%s)s' % (key, key) for key in kwargs) |
|
174 |
rset = cu.execute(rql, kwargs) |
|
175 |
return rset.get_entity(0, 0) |
|
176 |
||
177 |
def execute(self, *args): |
|
178 |
return self.session.cursor().execute(*args) |
|
179 |
||
180 |
def commit(self): |
|
181 |
self.session.commit() |
|
182 |
||
183 |
def rollback(self): |
|
184 |
self.session.rollback() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
185 |
|
0 | 186 |
def create_user(self, login, groups=('users',), req=None): |
187 |
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
|
188 |
user = self.add_entity('CWUser', upassword=str(login), login=unicode(login)) |
0 | 189 |
cu = self.session.cursor() |
190 |
cu.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
|
191 |
% ','.join(repr(g) for g in groups), |
|
192 |
{'x': user.eid}, 'x') |
|
193 |
return user |
|
194 |
||
195 |
def login(self, login, password=None): |
|
196 |
assert not self.config['use-google-auth'] |
|
197 |
req = FakeRequest(vreg=self.vreg) |
|
198 |
req.form['__login'] = login |
|
199 |
req.form['__password'] = password or login |
|
200 |
return self.session_manager.open_session(req) |