author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Wed, 27 May 2009 12:46:20 +0200 | |
branch | stable |
changeset 1957 | 92d1a15f08f7 |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
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 |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
3 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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 |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
5 |
""" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
6 |
__docformat__ = "restructuredtext en" |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
7 |
|
1132 | 8 |
from logilab.common.testlib import TestCase |
0 | 9 |
|
10 |
import os, os.path as osp |
|
11 |
import time |
|
12 |
from shutil import copy |
|
13 |
||
14 |
# additional monkey patches necessary in regular cubicweb environment |
|
15 |
from cubicweb.server import rqlannotation |
|
16 |
from cubicweb.goa.overrides import rqlannotation as goarqlannotation |
|
444 | 17 |
rqlannotation.SQLGenAnnotator = goarqlannotation.SQLGenAnnotator |
0 | 18 |
rqlannotation.set_qdata = goarqlannotation.set_qdata |
19 |
||
20 |
try: |
|
21 |
from google.appengine.api import apiproxy_stub_map |
|
22 |
from google.appengine.api import datastore_file_stub |
|
23 |
from google.appengine.ext import db as gdb |
|
24 |
from cubicweb.goa import db, do_monkey_patch |
|
25 |
import_appengine_failed = None |
|
26 |
except ImportError, exc: |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
27 |
# XXX necessary ? |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
28 |
class db: |
0 | 29 |
class Model: |
30 |
pass |
|
31 |
class DummyProperty: |
|
32 |
def __init__(self, *args, **kwargs): |
|
33 |
pass |
|
34 |
TextProperty = DummyProperty |
|
35 |
StringProperty = DummyProperty |
|
36 |
BlobProperty = DummyProperty |
|
37 |
DateProperty = DummyProperty |
|
38 |
ReferenceProperty = DummyProperty |
|
39 |
SelfReferenceProperty = DummyProperty |
|
40 |
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
|
41 |
|
0 | 42 |
|
43 |
from cubicweb.devtools.fake import FakeRequest |
|
44 |
from cubicweb.goa.goavreg import GAERegistry |
|
45 |
from cubicweb.goa.goaconfig import GAEConfiguration |
|
46 |
from cubicweb.goa.dbinit import (create_user, create_groups, fix_entities, |
|
47 |
init_persistent_schema, insert_versions) |
|
48 |
||
49 |
import logging |
|
50 |
logger = logging.getLogger() |
|
51 |
logger.setLevel(logging.CRITICAL) |
|
52 |
||
53 |
do_monkey_patch() |
|
54 |
||
55 |
class GAEBasedTC(TestCase): |
|
56 |
APP_ID = u'test_app' |
|
57 |
AUTH_DOMAIN = 'gmail.com' |
|
58 |
LOGGED_IN_USER = u't...@example.com' # set to '' for no logged in user |
|
59 |
MODEL_CLASSES = None |
|
60 |
LOAD_APP_MODULES = None |
|
61 |
config = None |
|
62 |
_DS_TEMPL_FILE = 'tmpdb-template' |
|
63 |
||
64 |
def load_schema_hook(self, loader): |
|
518 | 65 |
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
|
66 |
|
0 | 67 |
@property |
68 |
def DS_FILE(self): |
|
69 |
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
|
70 |
|
0 | 71 |
@property |
72 |
def DS_TEMPL_FILE(self): |
|
73 |
return self._DS_TEMPL_FILE + '_'.join(sorted(cls.__name__ for cls in self.MODEL_CLASSES)) |
|
74 |
||
75 |
def _set_ds_file(self, dsfile): |
|
76 |
# Start with a fresh api proxy. |
|
77 |
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
|
78 |
# Use a fresh stub datastore. |
|
79 |
stub = datastore_file_stub.DatastoreFileStub(self.APP_ID, dsfile, |
|
80 |
dsfile+'.history') |
|
81 |
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
|
82 |
|
0 | 83 |
def setUp(self): |
84 |
if import_appengine_failed: |
|
85 |
self.skip(import_appengine_failed) |
|
86 |
# Ensure we're in UTC. |
|
87 |
os.environ['TZ'] = 'UTC' |
|
88 |
time.tzset() |
|
89 |
if osp.exists(self.DS_TEMPL_FILE): |
|
90 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
91 |
need_ds_init = False |
|
92 |
self._set_ds_file(self.DS_FILE) |
|
93 |
else: |
|
94 |
need_ds_init = True |
|
95 |
self._set_ds_file(self.DS_TEMPL_FILE) |
|
96 |
# from google.appengine.api import mail_stub |
|
97 |
# 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
|
98 |
# from google3.apphosting.api import user_service_stub |
0 | 99 |
# # Use a fresh stub UserService. |
100 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
101 |
# 'user', user_service_stub.UserServiceStub()) |
|
102 |
os.environ['AUTH_DOMAIN'] = self.AUTH_DOMAIN |
|
103 |
os.environ['USER_EMAIL'] = self.LOGGED_IN_USER |
|
104 |
# # Use a fresh urlfetch stub. |
|
105 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
106 |
# 'urlfetch', urlfetch_stub.URLFetchServiceStub()) |
|
107 |
# # Use a fresh mail stub. |
|
108 |
# apiproxy_stub_map.apiproxy.RegisterStub( |
|
109 |
# 'mail', mail_stub.MailServiceStub()) |
|
110 |
if self.MODEL_CLASSES is None: |
|
111 |
raise Exception('GAEBasedTC should set MODEL_CLASSES class attribute') |
|
112 |
gdb._kind_map = {} |
|
113 |
self.config = self.config or GAEConfiguration('toto') |
|
114 |
self.config.init_log(logging.CRITICAL) |
|
115 |
self.schema = self.config.load_schema(self.MODEL_CLASSES, |
|
116 |
self.load_schema_hook) |
|
117 |
self.vreg = GAERegistry(self.config) |
|
118 |
self.vreg.schema = self.schema |
|
119 |
self.vreg.load_module(db) |
|
120 |
from cubicweb.goa.appobjects import sessions |
|
121 |
self.vreg.load_module(sessions) |
|
122 |
from cubicweb.entities import authobjs, schemaobjs |
|
123 |
self.vreg.load_module(authobjs) |
|
124 |
self.vreg.load_module(schemaobjs) |
|
125 |
if self.config['use-google-auth']: |
|
126 |
from cubicweb.goa.appobjects import gauthservice |
|
127 |
self.vreg.load_module(gauthservice) |
|
128 |
if self.LOAD_APP_MODULES is not None: |
|
129 |
for module in self.LOAD_APP_MODULES: |
|
130 |
self.vreg.load_module(module) |
|
131 |
for cls in self.MODEL_CLASSES: |
|
132 |
self.vreg.load_object(cls) |
|
133 |
self.session_manager = self.vreg.select_component('sessionmanager') |
|
134 |
if need_ds_init: |
|
135 |
# create default groups and create entities according to the schema |
|
136 |
create_groups() |
|
137 |
if not self.config['use-google-auth']: |
|
138 |
create_user(self.LOGGED_IN_USER, 'toto', ('users', 'managers')) |
|
139 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
140 |
else: |
|
141 |
req = FakeRequest(vreg=self.vreg) |
|
142 |
self.session = self.session_manager.open_session(req) |
|
143 |
self.user = self.session.user() |
|
144 |
ssession = self.config.repo_session(self.session.sessionid) |
|
145 |
ssession.set_pool() |
|
146 |
init_persistent_schema(ssession, self.schema) |
|
147 |
insert_versions(ssession, self.config) |
|
148 |
ssession.commit() |
|
149 |
fix_entities(self.schema) |
|
150 |
copy(self.DS_TEMPL_FILE, self.DS_FILE) |
|
151 |
self._set_ds_file(self.DS_FILE) |
|
152 |
else: |
|
153 |
if not self.config['use-google-auth']: |
|
154 |
self.session = self.login(self.LOGGED_IN_USER, 'toto') |
|
155 |
else: |
|
156 |
req = FakeRequest(vreg=self.vreg) |
|
157 |
self.session = self.session_manager.open_session(req) |
|
158 |
self.user = self.session.user() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
159 |
|
0 | 160 |
def tearDown(self): |
161 |
self.session.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
162 |
|
0 | 163 |
def request(self): |
164 |
req = FakeRequest(vreg=self.vreg) |
|
165 |
req.set_connection(self.session, self.user) |
|
166 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
167 |
|
0 | 168 |
def add_entity(self, etype, **kwargs): |
169 |
cu = self.session.cursor() |
|
170 |
rql = 'INSERT %s X' % etype |
|
171 |
if kwargs: |
|
172 |
rql += ': %s' % ', '.join('X %s %%(%s)s' % (key, key) for key in kwargs) |
|
173 |
rset = cu.execute(rql, kwargs) |
|
174 |
return rset.get_entity(0, 0) |
|
175 |
||
176 |
def execute(self, *args): |
|
177 |
return self.session.cursor().execute(*args) |
|
178 |
||
179 |
def commit(self): |
|
180 |
self.session.commit() |
|
181 |
||
182 |
def rollback(self): |
|
183 |
self.session.rollback() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
184 |
|
0 | 185 |
def create_user(self, login, groups=('users',), req=None): |
186 |
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
|
187 |
user = self.add_entity('CWUser', upassword=str(login), login=unicode(login)) |
0 | 188 |
cu = self.session.cursor() |
189 |
cu.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
|
190 |
% ','.join(repr(g) for g in groups), |
|
191 |
{'x': user.eid}, 'x') |
|
192 |
return user |
|
193 |
||
194 |
def login(self, login, password=None): |
|
195 |
assert not self.config['use-google-auth'] |
|
196 |
req = FakeRequest(vreg=self.vreg) |
|
197 |
req.form['__login'] = login |
|
198 |
req.form['__password'] = password or login |
|
199 |
return self.session_manager.open_session(req) |