author | Sandrine Ribeau <sandrine.ribeau@logilab.fr> |
Tue, 21 Apr 2009 20:13:03 -0700 | |
changeset 1418 | 71807bfd14c1 |
parent 1251 | af40e615dc89 |
child 1398 | 5fe84a5f7035 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
"""functional tests for core hooks |
|
3 |
||
4 |
note: most schemahooks.py hooks are actually tested in unittest_migrations.py |
|
5 |
""" |
|
6 |
||
7 |
from logilab.common.testlib import TestCase, unittest_main |
|
8 |
from cubicweb.devtools.apptest import RepositoryBasedTC, get_versions |
|
9 |
||
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
10 |
from cubicweb import ConnectionError, RepositoryError, ValidationError, AuthenticationError, BadConnectionId |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
11 |
from cubicweb.server.sqlutils import SQL_PREFIX |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
12 |
from cubicweb.server.repository import Repository |
0 | 13 |
|
14 |
orig_get_versions = Repository.get_versions |
|
15 |
||
16 |
def setup_module(*args): |
|
17 |
Repository.get_versions = get_versions |
|
18 |
||
19 |
def teardown_module(*args): |
|
20 |
Repository.get_versions = orig_get_versions |
|
21 |
||
22 |
||
23 |
||
24 |
class CoreHooksTC(RepositoryBasedTC): |
|
25 |
||
26 |
def test_delete_internal_entities(self): |
|
27 |
self.assertRaises(RepositoryError, self.execute, |
|
28 |
'DELETE EEType X WHERE X name "EEType"') |
|
29 |
self.assertRaises(RepositoryError, self.execute, |
|
30 |
'DELETE ERType X WHERE X name "relation_type"') |
|
31 |
self.assertRaises(RepositoryError, self.execute, |
|
32 |
'DELETE EGroup X WHERE X name "owners"') |
|
33 |
||
34 |
def test_delete_required_relations_subject(self): |
|
35 |
self.execute('INSERT EUser X: X login "toto", X upassword "hop", X in_group Y, X in_state S ' |
|
36 |
'WHERE Y name "users", S name "activated"') |
|
37 |
self.commit() |
|
38 |
self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"') |
|
39 |
self.assertRaises(ValidationError, self.commit) |
|
40 |
self.execute('DELETE X in_group Y WHERE X login "toto"') |
|
41 |
self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"') |
|
42 |
self.commit() |
|
43 |
||
44 |
def test_delete_required_relations_object(self): |
|
45 |
self.skip('no sample in the schema ! YAGNI ? Kermaat ?') |
|
46 |
||
47 |
def test_static_vocabulary_check(self): |
|
48 |
self.assertRaises(ValidationError, |
|
49 |
self.execute, |
|
50 |
'SET X composite "whatever" WHERE X from_entity FE, FE name "EUser", X relation_type RT, RT name "in_group"') |
|
51 |
||
52 |
def test_missing_required_relations_subject_inline(self): |
|
53 |
# missing in_group relation |
|
54 |
self.execute('INSERT EUser X: X login "toto", X upassword "hop"') |
|
55 |
self.assertRaises(ValidationError, |
|
56 |
self.commit) |
|
57 |
||
58 |
def test_delete_if_singlecard1(self): |
|
59 |
self.assertEquals(self.repo.schema['in_state'].inlined, False) |
|
60 |
ueid, = self.execute('INSERT EUser X: X login "toto", X upassword "hop", X in_group Y, X in_state S ' |
|
61 |
'WHERE Y name "users", S name "activated"')[0] |
|
62 |
self.commit() |
|
63 |
self.execute('SET X in_state S WHERE S name "deactivated", X eid %(x)s', {'x': ueid}) |
|
64 |
rset = self.execute('Any S WHERE X in_state S, X eid %(x)s', {'x': ueid}) |
|
65 |
self.assertEquals(len(rset), 1) |
|
66 |
self.assertRaises(Exception, self.execute, 'SET X in_state S WHERE S name "deactivated", X eid %s' % ueid) |
|
67 |
rset2 = self.execute('Any S WHERE X in_state S, X eid %(x)s', {'x': ueid}) |
|
68 |
self.assertEquals(rset.rows, rset2.rows) |
|
69 |
||
70 |
def test_inlined(self): |
|
71 |
self.assertEquals(self.repo.schema['sender'].inlined, True) |
|
72 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
73 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
74 |
eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
75 |
'WHERE Y is EmailAddress, P is EmailPart')[0][0] |
|
76 |
self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress') |
|
77 |
rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid) |
|
78 |
self.assertEquals(len(rset), 1) |
|
79 |
||
80 |
def test_composite_1(self): |
|
81 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
82 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
83 |
self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
84 |
'WHERE Y is EmailAddress, P is EmailPart') |
|
85 |
self.failUnless(self.execute('Email X WHERE X sender Y')) |
|
86 |
self.commit() |
|
87 |
self.execute('DELETE Email X') |
|
88 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
89 |
self.assertEquals(len(rset), 1) |
|
90 |
self.commit() |
|
91 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
92 |
self.assertEquals(len(rset), 0) |
|
93 |
||
94 |
def test_composite_2(self): |
|
95 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
96 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
97 |
self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
98 |
'WHERE Y is EmailAddress, P is EmailPart') |
|
99 |
self.commit() |
|
100 |
self.execute('DELETE Email X') |
|
101 |
self.execute('DELETE EmailPart X') |
|
102 |
self.commit() |
|
103 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
104 |
self.assertEquals(len(rset), 0) |
|
105 |
||
106 |
def test_composite_redirection(self): |
|
107 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
|
108 |
self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
|
109 |
self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
|
110 |
'WHERE Y is EmailAddress, P is EmailPart') |
|
111 |
self.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, X recipients Y ' |
|
112 |
'WHERE Y is EmailAddress') |
|
113 |
self.commit() |
|
114 |
self.execute('DELETE X parts Y WHERE X messageid "<1234>"') |
|
115 |
self.execute('SET X parts Y WHERE X messageid "<2345>"') |
|
116 |
self.commit() |
|
117 |
rset = self.execute('Any X WHERE X is EmailPart') |
|
118 |
self.assertEquals(len(rset), 1) |
|
119 |
self.assertEquals(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>') |
|
120 |
||
121 |
def test_unsatisfied_constraints(self): |
|
122 |
self.execute('INSERT ENFRDef X: X from_entity FE, X relation_type RT, X to_entity TE ' |
|
123 |
'WHERE FE name "Affaire", RT name "concerne", TE name "String"') |
|
124 |
self.assertRaises(ValidationError, |
|
125 |
self.commit) |
|
126 |
||
127 |
||
128 |
def test_html_tidy_hook(self): |
|
129 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "yo"').get_entity(0, 0) |
|
130 |
self.assertEquals(entity.descr, u'yo') |
|
131 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "<b>yo"').get_entity(0, 0) |
|
132 |
self.assertEquals(entity.descr, u'<b>yo</b>') |
|
133 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "<b>yo</b>"').get_entity(0, 0) |
|
134 |
self.assertEquals(entity.descr, u'<b>yo</b>') |
|
135 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "<b>R&D</b>"').get_entity(0, 0) |
|
136 |
self.assertEquals(entity.descr, u'<b>R&D</b>') |
|
137 |
xml = u"<div>c'est <b>l'été" |
|
138 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr %(d)s', |
|
139 |
{'d': xml}).get_entity(0, 0) |
|
140 |
self.assertEquals(entity.descr, u"<div>c'est <b>l'été</b></div>") |
|
141 |
||
142 |
def test_nonregr_html_tidy_hook_no_update(self): |
|
143 |
entity = self.execute('INSERT Affaire A: A descr_format "text/html", A descr "yo"').get_entity(0, 0) |
|
144 |
self.assertEquals(entity.descr, u'yo') |
|
145 |
self.execute('SET A ref "REF" WHERE A eid %s' % entity.eid) |
|
146 |
entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
|
147 |
self.assertEquals(entity.descr, u'yo') |
|
148 |
self.execute('SET A descr "R&D<p>yo" WHERE A eid %s' % entity.eid) |
|
149 |
entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
|
150 |
self.assertEquals(entity.descr, u'R&D<p>yo</p>') |
|
151 |
||
152 |
||
153 |
||
154 |
class UserGroupHooksTC(RepositoryBasedTC): |
|
155 |
||
156 |
def test_user_synchronization(self): |
|
157 |
self.create_user('toto', password='hop', commit=False) |
|
158 |
self.assertRaises(AuthenticationError, |
|
159 |
self.repo.connect, u'toto', 'hop') |
|
160 |
self.commit() |
|
161 |
cnxid = self.repo.connect(u'toto', 'hop') |
|
162 |
self.failIfEqual(cnxid, self.cnxid) |
|
163 |
self.execute('DELETE EUser X WHERE X login "toto"') |
|
164 |
self.repo.execute(cnxid, 'State X') |
|
165 |
self.commit() |
|
166 |
self.assertRaises(BadConnectionId, |
|
167 |
self.repo.execute, cnxid, 'State X') |
|
168 |
||
169 |
def test_user_group_synchronization(self): |
|
170 |
user = self.session.user |
|
171 |
self.assertEquals(user.groups, set(('managers',))) |
|
172 |
self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
173 |
self.assertEquals(user.groups, set(('managers',))) |
|
174 |
self.commit() |
|
175 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
176 |
self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid) |
|
177 |
self.assertEquals(user.groups, set(('managers', 'guests'))) |
|
178 |
self.commit() |
|
179 |
self.assertEquals(user.groups, set(('managers',))) |
|
180 |
||
181 |
def test_user_composite_owner(self): |
|
182 |
ueid = self.create_user('toto') |
|
183 |
# composite of euser should be owned by the euser regardless of who created it |
|
184 |
self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", U use_email X ' |
|
185 |
'WHERE U login "toto"') |
|
186 |
self.commit() |
|
187 |
self.assertEquals(self.execute('Any A WHERE X owned_by U, U use_email X,' |
|
188 |
'U login "toto", X address A')[0][0], |
|
189 |
'toto@logilab.fr') |
|
190 |
||
191 |
def test_no_created_by_on_deleted_entity(self): |
|
192 |
eid = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0] |
|
193 |
self.execute('DELETE EmailAddress X WHERE X eid %s' % eid) |
|
194 |
self.commit() |
|
195 |
self.failIf(self.execute('Any X WHERE X created_by Y, X eid >= %(x)s', {'x': eid})) |
|
196 |
||
197 |
class EPropertyHooksTC(RepositoryBasedTC): |
|
198 |
||
199 |
def test_unexistant_eproperty(self): |
|
200 |
ex = self.assertRaises(ValidationError, |
|
201 |
self.execute, 'INSERT EProperty X: X pkey "bla.bla", X value "hop", X for_user U') |
|
202 |
self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
|
203 |
ex = self.assertRaises(ValidationError, |
|
204 |
self.execute, 'INSERT EProperty X: X pkey "bla.bla", X value "hop"') |
|
205 |
self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
|
206 |
||
207 |
def test_site_wide_eproperty(self): |
|
208 |
ex = self.assertRaises(ValidationError, |
|
209 |
self.execute, 'INSERT EProperty X: X pkey "ui.site-title", X value "hop", X for_user U') |
|
210 |
self.assertEquals(ex.errors, {'for_user': "site-wide property can't be set for user"}) |
|
211 |
||
212 |
def test_bad_type_eproperty(self): |
|
213 |
ex = self.assertRaises(ValidationError, |
|
214 |
self.execute, 'INSERT EProperty X: X pkey "ui.language", X value "hop", X for_user U') |
|
215 |
self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
|
216 |
ex = self.assertRaises(ValidationError, |
|
217 |
self.execute, 'INSERT EProperty X: X pkey "ui.language", X value "hop"') |
|
218 |
self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
|
219 |
||
220 |
||
221 |
class SchemaHooksTC(RepositoryBasedTC): |
|
222 |
||
223 |
def test_duplicate_etype_error(self): |
|
224 |
# check we can't add a EEType or ERType entity if it already exists one |
|
225 |
# with the same name |
|
226 |
# |
|
227 |
# according to hook order, we'll get a repository or validation error |
|
228 |
self.assertRaises((ValidationError, RepositoryError), |
|
229 |
self.execute, 'INSERT EEType X: X name "Societe"') |
|
230 |
self.assertRaises((ValidationError, RepositoryError), |
|
231 |
self.execute, 'INSERT ERType X: X name "in_group"') |
|
232 |
||
233 |
def test_validation_unique_constraint(self): |
|
234 |
self.assertRaises(ValidationError, |
|
235 |
self.execute, 'INSERT EUser X: X login "admin"') |
|
236 |
try: |
|
237 |
self.execute('INSERT EUser X: X login "admin"') |
|
238 |
except ValidationError, ex: |
|
239 |
self.assertIsInstance(ex.entity, int) |
|
240 |
self.assertEquals(ex.errors, {'login': 'the value "admin" is already used, use another one'}) |
|
241 |
||
242 |
||
243 |
class SchemaModificationHooksTC(RepositoryBasedTC): |
|
244 |
copy_schema = True |
|
245 |
||
246 |
def setUp(self): |
|
247 |
if not hasattr(self, '_repo'): |
|
248 |
# first initialization |
|
249 |
repo = self.repo # set by the RepositoryBasedTC metaclass |
|
250 |
# force to read schema from the database |
|
251 |
repo.config._cubes = None |
|
252 |
repo.fill_schema() |
|
253 |
RepositoryBasedTC.setUp(self) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
254 |
|
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
255 |
def index_exists(self, etype, attr, unique=False): |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
256 |
dbhelper = self.session.pool.source('system').dbhelper |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
257 |
sqlcursor = self.session.pool['system'] |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
258 |
return dbhelper.index_exists(sqlcursor, SQL_PREFIX + etype, SQL_PREFIX + attr, unique=unique) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
259 |
|
0 | 260 |
def test_base(self): |
261 |
schema = self.repo.schema |
|
262 |
dbhelper = self.session.pool.source('system').dbhelper |
|
263 |
sqlcursor = self.session.pool['system'] |
|
264 |
self.failIf(schema.has_entity('Societe2')) |
|
265 |
self.failIf(schema.has_entity('concerne2')) |
|
266 |
# schema should be update on insertion (after commit) |
|
267 |
self.execute('INSERT EEType X: X name "Societe2", X description "", X meta FALSE, X final FALSE') |
|
268 |
self.execute('INSERT ERType X: X name "concerne2", X description "", X meta FALSE, X final FALSE, X symetric FALSE') |
|
269 |
self.failIf(schema.has_entity('Societe2')) |
|
270 |
self.failIf(schema.has_entity('concerne2')) |
|
271 |
self.execute('SET X read_permission G WHERE X is EEType, X name "Societe2", G is EGroup') |
|
272 |
self.execute('SET X read_permission G WHERE X is ERType, X name "concerne2", G is EGroup') |
|
273 |
self.execute('SET X add_permission G WHERE X is EEType, X name "Societe2", G is EGroup, G name "managers"') |
|
274 |
self.execute('SET X add_permission G WHERE X is ERType, X name "concerne2", G is EGroup, G name "managers"') |
|
275 |
self.execute('SET X delete_permission G WHERE X is EEType, X name "Societe2", G is EGroup, G name "owners"') |
|
276 |
self.execute('SET X delete_permission G WHERE X is ERType, X name "concerne2", G is EGroup, G name "owners"') |
|
277 |
# have to commit before adding definition relations |
|
278 |
self.commit() |
|
279 |
self.failUnless(schema.has_entity('Societe2')) |
|
280 |
self.failUnless(schema.has_relation('concerne2')) |
|
281 |
self.execute('INSERT EFRDef X: X cardinality "11", X defaultval "noname", X indexed TRUE, X relation_type RT, X from_entity E, X to_entity F ' |
|
282 |
'WHERE RT name "nom", E name "Societe2", F name "String"') |
|
283 |
concerne2_rdef_eid = self.execute( |
|
284 |
'INSERT ENFRDef X: X cardinality "**", X relation_type RT, X from_entity E, X to_entity E ' |
|
285 |
'WHERE RT name "concerne2", E name "Societe2"')[0][0] |
|
286 |
self.execute('INSERT ENFRDef X: X cardinality "?*", X relation_type RT, X from_entity E, X to_entity C ' |
|
287 |
'WHERE RT name "comments", E name "Societe2", C name "Comment"') |
|
288 |
self.failIf('nom' in schema['Societe2'].subject_relations()) |
|
289 |
self.failIf('concerne2' in schema['Societe2'].subject_relations()) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
290 |
self.failIf(self.index_exists('Societe2', 'nom')) |
0 | 291 |
self.commit() |
292 |
self.failUnless('nom' in schema['Societe2'].subject_relations()) |
|
293 |
self.failUnless('concerne2' in schema['Societe2'].subject_relations()) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
294 |
self.failUnless(self.index_exists('Societe2', 'nom')) |
0 | 295 |
# now we should be able to insert and query Societe2 |
296 |
s2eid = self.execute('INSERT Societe2 X: X nom "logilab"')[0][0] |
|
297 |
self.execute('Societe2 X WHERE X nom "logilab"') |
|
298 |
self.execute('SET X concerne2 X WHERE X nom "logilab"') |
|
299 |
rset = self.execute('Any X WHERE X concerne2 Y') |
|
300 |
self.assertEquals(rset.rows, [[s2eid]]) |
|
301 |
# check that when a relation definition is deleted, existing relations are deleted |
|
302 |
self.execute('INSERT ENFRDef X: X cardinality "**", X relation_type RT, X from_entity E, X to_entity E ' |
|
303 |
'WHERE RT name "concerne2", E name "Societe"') |
|
304 |
self.commit() |
|
305 |
self.execute('DELETE ENFRDef X WHERE X eid %(x)s', {'x': concerne2_rdef_eid}, 'x') |
|
306 |
self.commit() |
|
307 |
self.failUnless('concerne2' in schema['Societe'].subject_relations()) |
|
308 |
self.failIf('concerne2' in schema['Societe2'].subject_relations()) |
|
309 |
self.failIf(self.execute('Any X WHERE X concerne2 Y')) |
|
310 |
# schema should be cleaned on delete (after commit) |
|
311 |
self.execute('DELETE EEType X WHERE X name "Societe2"') |
|
312 |
self.execute('DELETE ERType X WHERE X name "concerne2"') |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
313 |
self.failUnless(self.index_exists('Societe2', 'nom')) |
0 | 314 |
self.failUnless(schema.has_entity('Societe2')) |
315 |
self.failUnless(schema.has_relation('concerne2')) |
|
316 |
self.commit() |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
317 |
self.failIf(self.index_exists('Societe2', 'nom')) |
0 | 318 |
self.failIf(schema.has_entity('Societe2')) |
319 |
self.failIf(schema.has_entity('concerne2')) |
|
320 |
||
321 |
def test_is_instance_of_insertions(self): |
|
322 |
seid = self.execute('INSERT SubDivision S: S nom "subdiv"')[0][0] |
|
323 |
is_etypes = [etype for etype, in self.execute('Any ETN WHERE X eid %s, X is ET, ET name ETN' % seid)] |
|
324 |
self.assertEquals(is_etypes, ['SubDivision']) |
|
325 |
instanceof_etypes = [etype for etype, in self.execute('Any ETN WHERE X eid %s, X is_instance_of ET, ET name ETN' % seid)] |
|
326 |
self.assertEquals(sorted(instanceof_etypes), ['Division', 'Societe', 'SubDivision']) |
|
327 |
snames = [name for name, in self.execute('Any N WHERE S is Societe, S nom N')] |
|
328 |
self.failIf('subdiv' in snames) |
|
329 |
snames = [name for name, in self.execute('Any N WHERE S is Division, S nom N')] |
|
330 |
self.failIf('subdiv' in snames) |
|
331 |
snames = [name for name, in self.execute('Any N WHERE S is_instance_of Societe, S nom N')] |
|
332 |
self.failUnless('subdiv' in snames) |
|
333 |
snames = [name for name, in self.execute('Any N WHERE S is_instance_of Division, S nom N')] |
|
334 |
self.failUnless('subdiv' in snames) |
|
335 |
||
336 |
||
337 |
def test_perms_synchronization_1(self): |
|
338 |
schema = self.repo.schema |
|
339 |
self.assertEquals(schema['EUser'].get_groups('read'), set(('managers', 'users'))) |
|
340 |
self.failUnless(self.execute('Any X, Y WHERE X is EEType, X name "EUser", Y is EGroup, Y name "users"')[0]) |
|
341 |
self.execute('DELETE X read_permission Y WHERE X is EEType, X name "EUser", Y name "users"') |
|
342 |
self.assertEquals(schema['EUser'].get_groups('read'), set(('managers', 'users', ))) |
|
343 |
self.commit() |
|
344 |
self.assertEquals(schema['EUser'].get_groups('read'), set(('managers', ))) |
|
345 |
self.execute('SET X read_permission Y WHERE X is EEType, X name "EUser", Y name "users"') |
|
346 |
self.commit() |
|
347 |
self.assertEquals(schema['EUser'].get_groups('read'), set(('managers', 'users',))) |
|
348 |
||
349 |
def test_perms_synchronization_2(self): |
|
350 |
schema = self.repo.schema['in_group'] |
|
351 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
352 |
self.execute('DELETE X read_permission Y WHERE X is ERType, X name "in_group", Y name "guests"') |
|
353 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
354 |
self.commit() |
|
355 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users'))) |
|
356 |
self.execute('SET X read_permission Y WHERE X is ERType, X name "in_group", Y name "guests"') |
|
357 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users'))) |
|
358 |
self.commit() |
|
359 |
self.assertEquals(schema.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
360 |
||
361 |
def test_nonregr_user_edit_itself(self): |
|
362 |
ueid = self.session.user.eid |
|
363 |
groupeids = [eid for eid, in self.execute('EGroup G WHERE G name in ("managers", "users")')] |
|
364 |
self.execute('DELETE X in_group Y WHERE X eid %s' % ueid) |
|
365 |
self.execute('SET X surname "toto" WHERE X eid %s' % ueid) |
|
366 |
self.execute('SET X in_group Y WHERE X eid %s, Y name "managers"' % ueid) |
|
367 |
self.commit() |
|
368 |
eeid = self.execute('Any X WHERE X is EEType, X name "EEType"')[0][0] |
|
369 |
self.execute('DELETE X read_permission Y WHERE X eid %s' % eeid) |
|
370 |
self.execute('SET X final FALSE WHERE X eid %s' % eeid) |
|
371 |
self.execute('SET X read_permission Y WHERE X eid %s, Y eid in (%s, %s)' |
|
372 |
% (eeid, groupeids[0], groupeids[1])) |
|
373 |
self.commit() |
|
374 |
self.execute('Any X WHERE X is EEType, X name "EEType"') |
|
375 |
||
376 |
# schema modification hooks tests ######################################### |
|
377 |
||
378 |
def test_uninline_relation(self): |
|
379 |
dbhelper = self.session.pool.source('system').dbhelper |
|
380 |
sqlcursor = self.session.pool['system'] |
|
381 |
# Personne inline2 Affaire inline |
|
382 |
# insert a person without inline2 relation (not mandatory) |
|
383 |
self.execute('INSERT Personne X: X nom "toto"') |
|
384 |
peid = self.execute('INSERT Personne X: X nom "tutu"')[0][0] |
|
385 |
aeid = self.execute('INSERT Affaire X: X ref "tata"')[0][0] |
|
386 |
self.execute('SET X inline2 Y WHERE X eid %(x)s, Y eid %(y)s', {'x': peid, 'y': aeid}) |
|
387 |
self.failUnless(self.schema['inline2'].inlined) |
|
388 |
try: |
|
389 |
try: |
|
390 |
self.execute('SET X inlined FALSE WHERE X name "inline2"') |
|
391 |
self.failUnless(self.schema['inline2'].inlined) |
|
392 |
self.commit() |
|
393 |
self.failIf(self.schema['inline2'].inlined) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
394 |
self.failIf(self.index_exists('Personne', 'inline2')) |
0 | 395 |
rset = self.execute('Any X, Y WHERE X inline2 Y') |
396 |
self.assertEquals(len(rset), 1) |
|
397 |
self.assertEquals(rset.rows[0], [peid, aeid]) |
|
398 |
except: |
|
399 |
import traceback |
|
400 |
traceback.print_exc() |
|
401 |
raise |
|
402 |
finally: |
|
403 |
self.execute('SET X inlined TRUE WHERE X name "inline2"') |
|
404 |
self.failIf(self.schema['inline2'].inlined) |
|
405 |
self.commit() |
|
406 |
self.failUnless(self.schema['inline2'].inlined) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
407 |
self.failUnless(self.index_exists('Personne', 'inline2')) |
0 | 408 |
rset = self.execute('Any X, Y WHERE X inline2 Y') |
409 |
self.assertEquals(len(rset), 1) |
|
410 |
self.assertEquals(rset.rows[0], [peid, aeid]) |
|
411 |
||
412 |
def test_indexed_change(self): |
|
413 |
dbhelper = self.session.pool.source('system').dbhelper |
|
414 |
sqlcursor = self.session.pool['system'] |
|
415 |
try: |
|
416 |
self.execute('SET X indexed TRUE WHERE X relation_type R, R name "sujet"') |
|
417 |
self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
418 |
self.failIf(self.index_exists('Affaire', 'sujet')) |
0 | 419 |
self.commit() |
420 |
self.failUnless(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
421 |
self.failUnless(self.index_exists('Affaire', 'sujet')) |
0 | 422 |
finally: |
423 |
self.execute('SET X indexed FALSE WHERE X relation_type R, R name "sujet"') |
|
424 |
self.failUnless(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
425 |
self.failUnless(self.index_exists('Affaire', 'sujet')) |
0 | 426 |
self.commit() |
427 |
self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
428 |
self.failIf(self.index_exists('Affaire', 'sujet')) |
0 | 429 |
|
430 |
def test_unique_change(self): |
|
431 |
dbhelper = self.session.pool.source('system').dbhelper |
|
432 |
sqlcursor = self.session.pool['system'] |
|
433 |
try: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
434 |
try: |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
435 |
self.execute('INSERT EConstraint X: X cstrtype CT, DEF constrained_by X ' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
436 |
'WHERE CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
437 |
'RT name "sujet", E name "Affaire"') |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
438 |
self.failIf(self.schema['Affaire'].has_unique_values('sujet')) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
439 |
self.failIf(self.index_exists('Affaire', 'sujet', unique=True)) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
440 |
self.commit() |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
441 |
self.failUnless(self.schema['Affaire'].has_unique_values('sujet')) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
442 |
self.failUnless(self.index_exists('Affaire', 'sujet', unique=True)) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
443 |
except: |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
444 |
import traceback |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
445 |
traceback.print_exc() |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
446 |
raise |
0 | 447 |
finally: |
448 |
self.execute('DELETE DEF constrained_by X WHERE X cstrtype CT, ' |
|
449 |
'CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
|
450 |
'RT name "sujet", E name "Affaire"') |
|
451 |
self.failUnless(self.schema['Affaire'].has_unique_values('sujet')) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
452 |
self.failUnless(self.index_exists('Affaire', 'sujet', unique=True)) |
0 | 453 |
self.commit() |
454 |
self.failIf(self.schema['Affaire'].has_unique_values('sujet')) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
455 |
self.failIf(self.index_exists('Affaire', 'sujet', unique=True)) |
0 | 456 |
|
457 |
||
458 |
class WorkflowHooksTC(RepositoryBasedTC): |
|
459 |
||
460 |
def setUp(self): |
|
461 |
RepositoryBasedTC.setUp(self) |
|
462 |
self.s_activated = self.execute('State X WHERE X name "activated"')[0][0] |
|
463 |
self.s_deactivated = self.execute('State X WHERE X name "deactivated"')[0][0] |
|
464 |
self.s_dummy = self.execute('INSERT State X: X name "dummy", X state_of E WHERE E name "EUser"')[0][0] |
|
465 |
self.create_user('stduser') |
|
466 |
# give access to users group on the user's wf transitions |
|
467 |
# so we can test wf enforcing on euser (managers don't have anymore this |
|
468 |
# enforcement |
|
469 |
self.execute('SET X require_group G WHERE G name "users", X transition_of ET, ET name "EUser"') |
|
470 |
self.commit() |
|
471 |
||
472 |
def tearDown(self): |
|
473 |
self.execute('DELETE X require_group G WHERE G name "users", X transition_of ET, ET name "EUser"') |
|
474 |
self.commit() |
|
475 |
RepositoryBasedTC.tearDown(self) |
|
476 |
||
477 |
def test_set_initial_state(self): |
|
478 |
ueid = self.execute('INSERT EUser E: E login "x", E upassword "x", E in_group G ' |
|
479 |
'WHERE G name "users"')[0][0] |
|
480 |
self.failIf(self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s', |
|
481 |
{'x' : ueid})) |
|
482 |
self.commit() |
|
483 |
initialstate = self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s', |
|
484 |
{'x' : ueid})[0][0] |
|
485 |
self.assertEquals(initialstate, u'activated') |
|
486 |
||
487 |
def test_initial_state(self): |
|
488 |
cnx = self.login('stduser') |
|
489 |
cu = cnx.cursor() |
|
490 |
self.assertRaises(ValidationError, cu.execute, |
|
491 |
'INSERT EUser X: X login "badaboum", X upassword %(pwd)s, ' |
|
492 |
'X in_state S WHERE S name "deactivated"', {'pwd': 'oops'}) |
|
493 |
cnx.close() |
|
494 |
# though managers can do whatever he want |
|
495 |
self.execute('INSERT EUser X: X login "badaboum", X upassword %(pwd)s, ' |
|
496 |
'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'}) |
|
497 |
self.commit() |
|
498 |
||
499 |
# test that the workflow is correctly enforced |
|
500 |
def test_transition_checking1(self): |
|
501 |
cnx = self.login('stduser') |
|
502 |
cu = cnx.cursor() |
|
503 |
ueid = cnx.user(self.current_session()).eid |
|
504 |
self.assertRaises(ValidationError, |
|
505 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
506 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
507 |
cnx.close() |
|
508 |
||
509 |
def test_transition_checking2(self): |
|
510 |
cnx = self.login('stduser') |
|
511 |
cu = cnx.cursor() |
|
512 |
ueid = cnx.user(self.current_session()).eid |
|
513 |
self.assertRaises(ValidationError, |
|
514 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
515 |
{'x': ueid, 's': self.s_dummy}, 'x') |
|
516 |
cnx.close() |
|
517 |
||
518 |
def test_transition_checking3(self): |
|
519 |
cnx = self.login('stduser') |
|
520 |
cu = cnx.cursor() |
|
521 |
ueid = cnx.user(self.current_session()).eid |
|
522 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
523 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
524 |
cnx.commit() |
|
525 |
self.assertRaises(ValidationError, |
|
526 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
527 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
528 |
# get back now |
|
529 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
530 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
531 |
cnx.commit() |
|
532 |
cnx.close() |
|
533 |
||
534 |
def test_transition_checking4(self): |
|
535 |
cnx = self.login('stduser') |
|
536 |
cu = cnx.cursor() |
|
537 |
ueid = cnx.user(self.current_session()).eid |
|
538 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
539 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
540 |
cnx.commit() |
|
541 |
self.assertRaises(ValidationError, |
|
542 |
cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
543 |
{'x': ueid, 's': self.s_dummy}, 'x') |
|
544 |
# get back now |
|
545 |
cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
546 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
547 |
cnx.commit() |
|
548 |
cnx.close() |
|
549 |
||
550 |
def test_transition_information(self): |
|
551 |
ueid = self.session.user.eid |
|
552 |
self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
553 |
{'x': ueid, 's': self.s_deactivated}, 'x') |
|
554 |
self.commit() |
|
555 |
rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
556 |
self.assertEquals(len(rset), 2) |
|
557 |
tr = rset.get_entity(1, 0) |
|
558 |
#tr.complete() |
|
559 |
self.assertEquals(tr.comment, None) |
|
560 |
self.assertEquals(tr.from_state[0].eid, self.s_activated) |
|
561 |
self.assertEquals(tr.to_state[0].eid, self.s_deactivated) |
|
562 |
||
563 |
self.session.set_shared_data('trcomment', u'il est pas sage celui-la') |
|
564 |
self.session.set_shared_data('trcommentformat', u'text/plain') |
|
565 |
self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
566 |
{'x': ueid, 's': self.s_activated}, 'x') |
|
567 |
self.commit() |
|
568 |
rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
569 |
self.assertEquals(len(rset), 3) |
|
570 |
tr = rset.get_entity(2, 0) |
|
571 |
#tr.complete() |
|
572 |
self.assertEquals(tr.comment, u'il est pas sage celui-la') |
|
573 |
self.assertEquals(tr.comment_format, u'text/plain') |
|
574 |
self.assertEquals(tr.from_state[0].eid, self.s_deactivated) |
|
575 |
self.assertEquals(tr.to_state[0].eid, self.s_activated) |
|
576 |
self.assertEquals(tr.owned_by[0].login, 'admin') |
|
577 |
||
578 |
def test_transition_information_on_creation(self): |
|
579 |
ueid = self.create_user('toto') |
|
580 |
rset = self.execute('TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid}) |
|
581 |
self.assertEquals(len(rset), 1) |
|
582 |
tr = rset.get_entity(0, 0) |
|
583 |
#tr.complete() |
|
584 |
self.assertEquals(tr.comment, None) |
|
585 |
self.assertEquals(tr.from_state, []) |
|
586 |
self.assertEquals(tr.to_state[0].eid, self.s_activated) |
|
587 |
||
588 |
def test_std_users_can_create_trinfo(self): |
|
589 |
self.create_user('toto') |
|
590 |
cnx = self.login('toto') |
|
591 |
cu = cnx.cursor() |
|
592 |
self.failUnless(cu.execute("INSERT Note X: X type 'a', X in_state S WHERE S name 'todo'")) |
|
593 |
cnx.commit() |
|
594 |
||
595 |
if __name__ == '__main__': |
|
596 |
unittest_main() |