|
1 # -*- coding: utf-8 -*- |
|
2 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 """unit tests for cubicweb.web.views.entities module""" |
|
20 |
|
21 from datetime import datetime |
|
22 |
|
23 from six import text_type |
|
24 |
|
25 from logilab.common import tempattr |
|
26 from logilab.common.decorators import clear_cache |
|
27 |
|
28 from cubicweb import Binary |
|
29 from cubicweb.devtools.testlib import CubicWebTC |
|
30 from cubicweb.mttransforms import HAS_TAL |
|
31 from cubicweb.entity import can_use_rest_path |
|
32 from cubicweb.entities import fetch_config |
|
33 from cubicweb.uilib import soup2xhtml |
|
34 from cubicweb.schema import RRQLExpression |
|
35 |
|
36 class EntityTC(CubicWebTC): |
|
37 |
|
38 def setUp(self): |
|
39 super(EntityTC, self).setUp() |
|
40 self.backup_dict = {} |
|
41 for cls in self.vreg['etypes'].iter_classes(): |
|
42 self.backup_dict[cls] = (cls.fetch_attrs, cls.cw_fetch_order) |
|
43 |
|
44 def tearDown(self): |
|
45 super(EntityTC, self).tearDown() |
|
46 for cls in self.vreg['etypes'].iter_classes(): |
|
47 cls.fetch_attrs, cls.cw_fetch_order = self.backup_dict[cls] |
|
48 |
|
49 def test_no_prefill_related_cache_bug(self): |
|
50 with self.admin_access.repo_cnx() as cnx: |
|
51 usine = cnx.create_entity('Usine', lieu=u'Montbeliard') |
|
52 produit = cnx.create_entity('Produit') |
|
53 # usine was prefilled in glob_add_entity |
|
54 # let's simulate produit creation without prefill |
|
55 produit._cw_related_cache.clear() |
|
56 # use add_relations |
|
57 cnx.add_relations([('fabrique_par', [(produit.eid, usine.eid)])]) |
|
58 self.assertEqual(1, len(usine.reverse_fabrique_par)) |
|
59 self.assertEqual(1, len(produit.fabrique_par)) |
|
60 |
|
61 def test_boolean_value(self): |
|
62 with self.admin_access.web_request() as req: |
|
63 e = self.vreg['etypes'].etype_class('CWUser')(req) |
|
64 self.assertTrue(e) |
|
65 |
|
66 def test_yams_inheritance(self): |
|
67 from entities import Note |
|
68 with self.admin_access.web_request() as req: |
|
69 e = self.vreg['etypes'].etype_class('SubNote')(req) |
|
70 self.assertIsInstance(e, Note) |
|
71 e2 = self.vreg['etypes'].etype_class('SubNote')(req) |
|
72 self.assertIs(e.__class__, e2.__class__) |
|
73 |
|
74 def test_has_eid(self): |
|
75 with self.admin_access.web_request() as req: |
|
76 e = self.vreg['etypes'].etype_class('CWUser')(req) |
|
77 self.assertEqual(e.eid, None) |
|
78 self.assertEqual(e.has_eid(), False) |
|
79 e.eid = 'X' |
|
80 self.assertEqual(e.has_eid(), False) |
|
81 e.eid = 0 |
|
82 self.assertEqual(e.has_eid(), True) |
|
83 e.eid = 2 |
|
84 self.assertEqual(e.has_eid(), True) |
|
85 |
|
86 def test_copy(self): |
|
87 with self.admin_access.web_request() as req: |
|
88 req.create_entity('Tag', name=u'x') |
|
89 p = req.create_entity('Personne', nom=u'toto') |
|
90 oe = req.create_entity('Note', type=u'x') |
|
91 req.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s', |
|
92 {'t': oe.eid, 'u': p.eid}) |
|
93 req.execute('SET TAG tags X WHERE X eid %(x)s', {'x': oe.eid}) |
|
94 e = req.create_entity('Note', type=u'z') |
|
95 e.copy_relations(oe.eid) |
|
96 self.assertEqual(len(e.ecrit_par), 1) |
|
97 self.assertEqual(e.ecrit_par[0].eid, p.eid) |
|
98 self.assertEqual(len(e.reverse_tags), 1) |
|
99 # check meta-relations are not copied, set on commit |
|
100 self.assertEqual(len(e.created_by), 0) |
|
101 |
|
102 def test_copy_with_nonmeta_composite_inlined(self): |
|
103 with self.admin_access.web_request() as req: |
|
104 p = req.create_entity('Personne', nom=u'toto') |
|
105 oe = req.create_entity('Note', type=u'x') |
|
106 self.schema['ecrit_par'].rdef('Note', 'Personne').composite = 'subject' |
|
107 req.execute('SET T ecrit_par U WHERE T eid %(t)s, U eid %(u)s', |
|
108 {'t': oe.eid, 'u': p.eid}) |
|
109 e = req.create_entity('Note', type=u'z') |
|
110 e.copy_relations(oe.eid) |
|
111 self.assertFalse(e.ecrit_par) |
|
112 self.assertTrue(oe.ecrit_par) |
|
113 |
|
114 def test_copy_with_composite(self): |
|
115 with self.admin_access.web_request() as req: |
|
116 adeleid = req.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0] |
|
117 e = req.execute('Any X WHERE X eid %(x)s', {'x': req.user.eid}).get_entity(0, 0) |
|
118 self.assertEqual(e.use_email[0].address, "toto@logilab.org") |
|
119 self.assertEqual(e.use_email[0].eid, adeleid) |
|
120 usereid = req.execute('INSERT CWUser X: X login "toto", X upassword "toto", X in_group G ' |
|
121 'WHERE G name "users"')[0][0] |
|
122 e = req.execute('Any X WHERE X eid %(x)s', {'x': usereid}).get_entity(0, 0) |
|
123 e.copy_relations(req.user.eid) |
|
124 self.assertFalse(e.use_email) |
|
125 self.assertFalse(e.primary_email) |
|
126 |
|
127 def test_copy_with_non_initial_state(self): |
|
128 with self.admin_access.web_request() as req: |
|
129 user = req.execute('INSERT CWUser X: X login "toto", X upassword %(pwd)s, X in_group G WHERE G name "users"', |
|
130 {'pwd': 'toto'}).get_entity(0, 0) |
|
131 req.cnx.commit() |
|
132 user.cw_adapt_to('IWorkflowable').fire_transition('deactivate') |
|
133 req.cnx.commit() |
|
134 eid2 = req.execute('INSERT CWUser X: X login "tutu", X upassword %(pwd)s', {'pwd': 'toto'})[0][0] |
|
135 e = req.execute('Any X WHERE X eid %(x)s', {'x': eid2}).get_entity(0, 0) |
|
136 e.copy_relations(user.eid) |
|
137 req.cnx.commit() |
|
138 e.cw_clear_relation_cache('in_state', 'subject') |
|
139 self.assertEqual(e.cw_adapt_to('IWorkflowable').state, 'activated') |
|
140 |
|
141 def test_related_cache_both(self): |
|
142 with self.admin_access.web_request() as req: |
|
143 user = req.execute('Any X WHERE X eid %(x)s', {'x':req.user.eid}).get_entity(0, 0) |
|
144 adeleid = req.execute('INSERT EmailAddress X: X address "toto@logilab.org", U use_email X WHERE U login "admin"')[0][0] |
|
145 req.cnx.commit() |
|
146 self.assertEqual(user._cw_related_cache, {}) |
|
147 email = user.primary_email[0] |
|
148 self.assertEqual(sorted(user._cw_related_cache), ['primary_email_subject']) |
|
149 self.assertEqual(list(email._cw_related_cache), ['primary_email_object']) |
|
150 groups = user.in_group |
|
151 self.assertEqual(sorted(user._cw_related_cache), ['in_group_subject', 'primary_email_subject']) |
|
152 for group in groups: |
|
153 self.assertNotIn('in_group_subject', group._cw_related_cache) |
|
154 user.cw_clear_all_caches() |
|
155 user.related('in_group', entities=True) |
|
156 self.assertIn('in_group_subject', user._cw_related_cache) |
|
157 user.cw_clear_all_caches() |
|
158 user.related('in_group', targettypes=('CWGroup',), entities=True) |
|
159 self.assertNotIn('in_group_subject', user._cw_related_cache) |
|
160 |
|
161 def test_related_limit(self): |
|
162 with self.admin_access.web_request() as req: |
|
163 p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien') |
|
164 for tag in u'abcd': |
|
165 req.create_entity('Tag', name=tag) |
|
166 req.execute('SET X tags Y WHERE X is Tag, Y is Personne') |
|
167 self.assertEqual(len(p.related('tags', 'object', limit=2)), 2) |
|
168 self.assertEqual(len(p.related('tags', 'object')), 4) |
|
169 p.cw_clear_all_caches() |
|
170 self.assertEqual(len(p.related('tags', 'object', entities=True, limit=2)), 2) |
|
171 self.assertEqual(len(p.related('tags', 'object', entities=True)), 4) |
|
172 |
|
173 def test_related_targettypes(self): |
|
174 with self.admin_access.web_request() as req: |
|
175 p = req.create_entity('Personne', nom=u'Loxodonta', prenom=u'Babar') |
|
176 n = req.create_entity('Note', type=u'scratch', ecrit_par=p) |
|
177 t = req.create_entity('Tag', name=u'a tag', tags=(p, n)) |
|
178 req.cnx.commit() |
|
179 with self.admin_access.web_request() as req: |
|
180 t = req.entity_from_eid(t.eid) |
|
181 self.assertEqual(2, t.related('tags').rowcount) |
|
182 self.assertEqual(1, t.related('tags', targettypes=('Personne',)).rowcount) |
|
183 self.assertEqual(1, t.related('tags', targettypes=('Note',)).rowcount) |
|
184 |
|
185 def test_cw_instantiate_relation(self): |
|
186 with self.admin_access.web_request() as req: |
|
187 p1 = req.create_entity('Personne', nom=u'di') |
|
188 p2 = req.create_entity('Personne', nom=u'mascio') |
|
189 t = req.create_entity('Tag', name=u't0', tags=[]) |
|
190 self.assertCountEqual(t.tags, []) |
|
191 t = req.create_entity('Tag', name=u't1', tags=p1) |
|
192 self.assertCountEqual(t.tags, [p1]) |
|
193 t = req.create_entity('Tag', name=u't2', tags=p1.eid) |
|
194 self.assertCountEqual(t.tags, [p1]) |
|
195 t = req.create_entity('Tag', name=u't3', tags=[p1, p2.eid]) |
|
196 self.assertCountEqual(t.tags, [p1, p2]) |
|
197 |
|
198 def test_cw_instantiate_reverse_relation(self): |
|
199 with self.admin_access.web_request() as req: |
|
200 t1 = req.create_entity('Tag', name=u't1') |
|
201 t2 = req.create_entity('Tag', name=u't2') |
|
202 p = req.create_entity('Personne', nom=u'di mascio', reverse_tags=t1) |
|
203 self.assertCountEqual(p.reverse_tags, [t1]) |
|
204 p = req.create_entity('Personne', nom=u'di mascio', reverse_tags=t1.eid) |
|
205 self.assertCountEqual(p.reverse_tags, [t1]) |
|
206 p = req.create_entity('Personne', nom=u'di mascio', reverse_tags=[t1, t2.eid]) |
|
207 self.assertCountEqual(p.reverse_tags, [t1, t2]) |
|
208 |
|
209 def test_fetch_rql(self): |
|
210 Personne = self.vreg['etypes'].etype_class('Personne') |
|
211 Societe = self.vreg['etypes'].etype_class('Societe') |
|
212 Note = self.vreg['etypes'].etype_class('Note') |
|
213 peschema = Personne.e_schema |
|
214 seschema = Societe.e_schema |
|
215 torestore = [] |
|
216 for rdef, card in [(peschema.subjrels['travaille'].rdef(peschema, seschema), '1*'), |
|
217 (peschema.subjrels['connait'].rdef(peschema, peschema), '11'), |
|
218 (peschema.subjrels['evaluee'].rdef(peschema, Note.e_schema), '1*'), |
|
219 (seschema.subjrels['evaluee'].rdef(seschema, Note.e_schema), '1*')]: |
|
220 cm = tempattr(rdef, 'cardinality', card) |
|
221 cm.__enter__() |
|
222 torestore.append(cm) |
|
223 try: |
|
224 with self.admin_access.web_request() as req: |
|
225 user = req.user |
|
226 # testing basic fetch_attrs attribute |
|
227 self.assertEqual(Personne.fetch_rql(user), |
|
228 'Any X,AA,AB,AC ORDERBY AB ' |
|
229 'WHERE X is_instance_of Personne, X modification_date AA, X nom AB, X prenom AC') |
|
230 # testing unknown attributes |
|
231 Personne.fetch_attrs = ('bloug', 'beep') |
|
232 self.assertEqual(Personne.fetch_rql(user), 'Any X WHERE X is_instance_of Personne') |
|
233 # testing one non final relation |
|
234 Personne.fetch_attrs = ('nom', 'prenom', 'travaille') |
|
235 self.assertEqual(Personne.fetch_rql(user), |
|
236 'Any X,AA,AB,AC,AD ORDERBY AA ' |
|
237 'WHERE X is_instance_of Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD') |
|
238 # testing two non final relations |
|
239 Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee') |
|
240 self.assertEqual(Personne.fetch_rql(user), |
|
241 'Any X,AA,AB,AC,AD,AE ORDERBY AB ' |
|
242 'WHERE X is_instance_of Personne, X evaluee AA?, X nom AB, X prenom AC, X travaille AD?, ' |
|
243 'AD nom AE') |
|
244 # testing one non final relation with recursion |
|
245 Personne.fetch_attrs = ('nom', 'prenom', 'travaille') |
|
246 Societe.fetch_attrs = ('nom', 'evaluee') |
|
247 self.assertEqual(Personne.fetch_rql(user), |
|
248 'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ' |
|
249 'WHERE X is_instance_of Personne, X nom AA, X prenom AB, X travaille AC?, ' |
|
250 'AC evaluee AD?, AD modification_date AE, AC nom AF') |
|
251 # testing symmetric relation |
|
252 Personne.fetch_attrs = ('nom', 'connait') |
|
253 self.assertEqual(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AB ' |
|
254 'WHERE X is_instance_of Personne, X connait AA?, X nom AB') |
|
255 # testing optional relation |
|
256 peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '?*' |
|
257 Personne.fetch_attrs = ('nom', 'prenom', 'travaille') |
|
258 Societe.fetch_attrs = ('nom',) |
|
259 self.assertEqual(Personne.fetch_rql(user), |
|
260 'Any X,AA,AB,AC,AD ORDERBY AA WHERE X is_instance_of Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD') |
|
261 # testing relation with cardinality > 1 |
|
262 peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '**' |
|
263 self.assertEqual(Personne.fetch_rql(user), |
|
264 'Any X,AA,AB ORDERBY AA WHERE X is_instance_of Personne, X nom AA, X prenom AB') |
|
265 # XXX test unauthorized attribute |
|
266 finally: |
|
267 # fetch_attrs restored by generic tearDown |
|
268 for cm in torestore: |
|
269 cm.__exit__(None, None, None) |
|
270 |
|
271 def test_related_rql_base(self): |
|
272 Personne = self.vreg['etypes'].etype_class('Personne') |
|
273 Note = self.vreg['etypes'].etype_class('Note') |
|
274 SubNote = self.vreg['etypes'].etype_class('SubNote') |
|
275 self.assertTrue(issubclass(self.vreg['etypes'].etype_class('SubNote'), Note)) |
|
276 Personne.fetch_attrs, Personne.cw_fetch_order = fetch_config(('nom', 'type')) |
|
277 Note.fetch_attrs, Note.cw_fetch_order = fetch_config(('type',)) |
|
278 SubNote.fetch_attrs, SubNote.cw_fetch_order = fetch_config(('type',)) |
|
279 with self.admin_access.web_request() as req: |
|
280 p = req.create_entity('Personne', nom=u'pouet') |
|
281 self.assertEqual(p.cw_related_rql('evaluee'), |
|
282 'Any X,AA,AB ORDERBY AB WHERE E eid %(x)s, E evaluee X, ' |
|
283 'X modification_date AA, X type AB') |
|
284 n = req.create_entity('Note') |
|
285 self.assertEqual(n.cw_related_rql('evaluee', role='object', |
|
286 targettypes=('Societe', 'Personne')), |
|
287 "Any X,AA ORDERBY AB DESC WHERE E eid %(x)s, X evaluee E, " |
|
288 "X is IN(Personne, Societe), X nom AA, " |
|
289 "X modification_date AB") |
|
290 Personne.fetch_attrs, Personne.cw_fetch_order = fetch_config(('nom', )) |
|
291 # XXX |
|
292 self.assertEqual(p.cw_related_rql('evaluee'), |
|
293 'Any X,AA ORDERBY AA DESC ' |
|
294 'WHERE E eid %(x)s, E evaluee X, X modification_date AA') |
|
295 |
|
296 tag = self.vreg['etypes'].etype_class('Tag')(req) |
|
297 self.assertEqual(tag.cw_related_rql('tags', 'subject'), |
|
298 'Any X,AA ORDERBY AA DESC ' |
|
299 'WHERE E eid %(x)s, E tags X, X modification_date AA') |
|
300 self.assertEqual(tag.cw_related_rql('tags', 'subject', ('Personne',)), |
|
301 'Any X,AA,AB ORDERBY AB ' |
|
302 'WHERE E eid %(x)s, E tags X, X is Personne, X modification_date AA, ' |
|
303 'X nom AB') |
|
304 |
|
305 def test_related_rql_ambiguous_cant_use_fetch_order(self): |
|
306 with self.admin_access.web_request() as req: |
|
307 tag = self.vreg['etypes'].etype_class('Tag')(req) |
|
308 for ttype in self.schema['tags'].objects(): |
|
309 self.vreg['etypes'].etype_class(ttype).fetch_attrs = ('modification_date',) |
|
310 self.assertEqual(tag.cw_related_rql('tags', 'subject'), |
|
311 'Any X,AA ORDERBY AA DESC ' |
|
312 'WHERE E eid %(x)s, E tags X, X modification_date AA') |
|
313 |
|
314 def test_related_rql_fetch_ambiguous_rtype(self): |
|
315 etvreg = self.vreg['etypes'] |
|
316 soc_etype = etvreg.etype_class('Societe') |
|
317 with self.admin_access.web_request() as req: |
|
318 soc = soc_etype(req) |
|
319 soc_etype.fetch_attrs = ('fournit',) |
|
320 etvreg.etype_class('Service').fetch_attrs = ('fabrique_par',) |
|
321 etvreg.etype_class('Produit').fetch_attrs = ('fabrique_par',) |
|
322 etvreg.etype_class('Usine').fetch_attrs = ('lieu',) |
|
323 etvreg.etype_class('Personne').fetch_attrs = ('nom',) |
|
324 self.assertEqual(soc.cw_related_rql('fournit', 'subject'), |
|
325 'Any X,A WHERE E eid %(x)s, E fournit X, X fabrique_par A') |
|
326 |
|
327 def test_unrelated_rql_security_1_manager(self): |
|
328 with self.admin_access.web_request() as req: |
|
329 user = req.user |
|
330 rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0] |
|
331 self.assertEqual(rql, |
|
332 'Any O,AA,AB,AC ORDERBY AC DESC ' |
|
333 'WHERE NOT A use_email O, S eid %(x)s, ' |
|
334 'O is_instance_of EmailAddress, O address AA, O alias AB, ' |
|
335 'O modification_date AC') |
|
336 |
|
337 def test_unrelated_rql_security_1_user(self): |
|
338 with self.admin_access.web_request() as req: |
|
339 self.create_user(req, 'toto') |
|
340 with self.new_access('toto').web_request() as req: |
|
341 user = req.user # XXX |
|
342 rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0] |
|
343 self.assertEqual(rql, |
|
344 'Any O,AA,AB,AC ORDERBY AC DESC ' |
|
345 'WHERE NOT A use_email O, S eid %(x)s, ' |
|
346 'O is_instance_of EmailAddress, O address AA, O alias AB, O modification_date AC') |
|
347 user = req.execute('Any X WHERE X login "admin"').get_entity(0, 0) |
|
348 rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0] |
|
349 self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC ' |
|
350 'WHERE NOT A use_email O, S eid %(x)s, ' |
|
351 'O is EmailAddress, O address AA, O alias AB, O modification_date AC, AD eid %(AE)s, ' |
|
352 'EXISTS(S identity AD, NOT AD in_group AF, AF name "guests", AF is CWGroup), A is CWUser') |
|
353 |
|
354 def test_unrelated_rql_security_1_anon(self): |
|
355 with self.new_access('anon').web_request() as req: |
|
356 user = req.user |
|
357 rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0] |
|
358 self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC ' |
|
359 'WHERE NOT A use_email O, S eid %(x)s, ' |
|
360 'O is EmailAddress, O address AA, O alias AB, O modification_date AC, AD eid %(AE)s, ' |
|
361 'EXISTS(S identity AD, NOT AD in_group AF, AF name "guests", AF is CWGroup), A is CWUser') |
|
362 |
|
363 def test_unrelated_rql_security_2(self): |
|
364 with self.admin_access.web_request() as req: |
|
365 email = req.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0) |
|
366 rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0] |
|
367 self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AB ' |
|
368 'WHERE NOT S use_email O, O eid %(x)s, S is_instance_of CWUser, ' |
|
369 'S firstname AA, S login AB, S modification_date AC, S surname AD') |
|
370 req.cnx.commit() |
|
371 rperms = self.schema['EmailAddress'].permissions['read'] |
|
372 clear_cache(self.schema['EmailAddress'], 'get_groups') |
|
373 clear_cache(self.schema['EmailAddress'], 'get_rqlexprs') |
|
374 self.schema['EmailAddress'].permissions['read'] = ('managers', 'users', 'guests',) |
|
375 try: |
|
376 with self.new_access('anon').web_request() as req: |
|
377 email = req.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0) |
|
378 rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0] |
|
379 self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AB ' |
|
380 'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, ' |
|
381 'S firstname AA, S login AB, S modification_date AC, S surname AD, ' |
|
382 'AE eid %(AF)s, EXISTS(S identity AE, NOT AE in_group AG, AG name "guests", AG is CWGroup)') |
|
383 finally: |
|
384 clear_cache(self.schema['EmailAddress'], 'get_groups') |
|
385 clear_cache(self.schema['EmailAddress'], 'get_rqlexprs') |
|
386 self.schema['EmailAddress'].permissions['read'] = rperms |
|
387 |
|
388 def test_cw_linkable_rql(self): |
|
389 with self.admin_access.web_request() as req: |
|
390 email = req.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0) |
|
391 rql = email.cw_linkable_rql('use_email', 'CWUser', 'object')[0] |
|
392 self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AB ' |
|
393 'WHERE O eid %(x)s, S is_instance_of CWUser, ' |
|
394 'S firstname AA, S login AB, S modification_date AC, S surname AD') |
|
395 |
|
396 def test_unrelated_rql_security_nonexistant(self): |
|
397 with self.new_access('anon').web_request() as req: |
|
398 email = self.vreg['etypes'].etype_class('EmailAddress')(req) |
|
399 rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0] |
|
400 self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AB ' |
|
401 'WHERE S is CWUser, ' |
|
402 'S firstname AA, S login AB, S modification_date AC, S surname AD, ' |
|
403 'AE eid %(AF)s, EXISTS(S identity AE, NOT AE in_group AG, AG name "guests", AG is CWGroup)') |
|
404 |
|
405 def test_unrelated_rql_constraints_creation_subject(self): |
|
406 with self.admin_access.web_request() as req: |
|
407 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
408 rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0] |
|
409 self.assertEqual( |
|
410 rql, 'Any O,AA,AB,AC ORDERBY AA DESC WHERE ' |
|
411 'O is_instance_of Personne, O modification_date AA, O nom AB, O prenom AC') |
|
412 |
|
413 def test_unrelated_rql_constraints_creation_object(self): |
|
414 with self.admin_access.web_request() as req: |
|
415 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
416 rql = person.cw_unrelated_rql('connait', 'Personne', 'object')[0] |
|
417 self.assertEqual( |
|
418 rql, 'Any S,AA,AB,AC ORDERBY AA DESC WHERE ' |
|
419 'S is Personne, S modification_date AA, S nom AB, S prenom AC, ' |
|
420 'NOT (S connait AD, AD nom "toto"), AD is Personne, ' |
|
421 'EXISTS(S travaille AE, AE nom "tutu")') |
|
422 |
|
423 def test_unrelated_rql_security_rel_perms(self): |
|
424 '''check `connait` add permission has no effect for a new entity on the |
|
425 unrelated rql''' |
|
426 rdef = self.schema['Personne'].rdef('connait') |
|
427 perm_rrqle = RRQLExpression('U has_update_permission S') |
|
428 with self.temporary_permissions((rdef, {'add': (perm_rrqle,)})): |
|
429 with self.admin_access.web_request() as req: |
|
430 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
431 rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0] |
|
432 self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AA DESC WHERE ' |
|
433 'O is_instance_of Personne, O modification_date AA, O nom AB, ' |
|
434 'O prenom AC') |
|
435 |
|
436 def test_unrelated_rql_constraints_edition_subject(self): |
|
437 with self.admin_access.web_request() as req: |
|
438 person = req.create_entity('Personne', nom=u'sylvain') |
|
439 rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0] |
|
440 self.assertEqual( |
|
441 rql, 'Any O,AA,AB,AC ORDERBY AA DESC WHERE ' |
|
442 'NOT S connait O, S eid %(x)s, O is Personne, ' |
|
443 'O modification_date AA, O nom AB, O prenom AC, ' |
|
444 'NOT S identity O') |
|
445 |
|
446 def test_unrelated_rql_constraints_edition_object(self): |
|
447 with self.admin_access.web_request() as req: |
|
448 person = req.create_entity('Personne', nom=u'sylvain') |
|
449 rql = person.cw_unrelated_rql('connait', 'Personne', 'object')[0] |
|
450 self.assertEqual( |
|
451 rql, 'Any S,AA,AB,AC ORDERBY AA DESC WHERE ' |
|
452 'NOT S connait O, O eid %(x)s, S is Personne, ' |
|
453 'S modification_date AA, S nom AB, S prenom AC, ' |
|
454 'NOT S identity O, NOT (S connait AD, AD nom "toto"), ' |
|
455 'EXISTS(S travaille AE, AE nom "tutu")') |
|
456 |
|
457 def test_unrelated_rql_s_linkto_s(self): |
|
458 with self.admin_access.web_request() as req: |
|
459 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
460 self.vreg['etypes'].etype_class('Personne').fetch_attrs = () |
|
461 soc = req.create_entity('Societe', nom=u'logilab') |
|
462 lt_infos = {('actionnaire', 'subject'): [soc.eid]} |
|
463 rql, args = person.cw_unrelated_rql('associe', 'Personne', 'subject', |
|
464 lt_infos=lt_infos) |
|
465 self.assertEqual(u'Any O ORDERBY O WHERE O is Personne, ' |
|
466 u'EXISTS(AA eid %(SOC)s, O actionnaire AA)', rql) |
|
467 self.assertEqual({'SOC': soc.eid}, args) |
|
468 |
|
469 def test_unrelated_rql_s_linkto_o(self): |
|
470 with self.admin_access.web_request() as req: |
|
471 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
472 self.vreg['etypes'].etype_class('Societe').fetch_attrs = () |
|
473 soc = req.create_entity('Societe', nom=u'logilab') |
|
474 lt_infos = {('contrat_exclusif', 'object'): [soc.eid]} |
|
475 rql, args = person.cw_unrelated_rql('actionnaire', 'Societe', 'subject', |
|
476 lt_infos=lt_infos) |
|
477 self.assertEqual(u'Any O ORDERBY O WHERE NOT A actionnaire O, ' |
|
478 u'O is_instance_of Societe, NOT EXISTS(O eid %(O)s), ' |
|
479 u'A is Personne', rql) |
|
480 self.assertEqual({'O': soc.eid}, args) |
|
481 |
|
482 def test_unrelated_rql_o_linkto_s(self): |
|
483 with self.admin_access.web_request() as req: |
|
484 soc = self.vreg['etypes'].etype_class('Societe')(req) |
|
485 self.vreg['etypes'].etype_class('Personne').fetch_attrs = () |
|
486 person = req.create_entity('Personne', nom=u'florent') |
|
487 lt_infos = {('contrat_exclusif', 'subject'): [person.eid]} |
|
488 rql, args = soc.cw_unrelated_rql('actionnaire', 'Personne', 'object', |
|
489 lt_infos=lt_infos) |
|
490 self.assertEqual(u'Any S ORDERBY S WHERE NOT S actionnaire A, ' |
|
491 u'S is_instance_of Personne, NOT EXISTS(S eid %(S)s), ' |
|
492 u'A is Societe', rql) |
|
493 self.assertEqual({'S': person.eid}, args) |
|
494 |
|
495 def test_unrelated_rql_o_linkto_o(self): |
|
496 with self.admin_access.web_request() as req: |
|
497 soc = self.vreg['etypes'].etype_class('Societe')(req) |
|
498 self.vreg['etypes'].etype_class('Personne').fetch_attrs = () |
|
499 person = req.create_entity('Personne', nom=u'florent') |
|
500 lt_infos = {('actionnaire', 'object'): [person.eid]} |
|
501 rql, args = soc.cw_unrelated_rql('dirige', 'Personne', 'object', |
|
502 lt_infos=lt_infos) |
|
503 self.assertEqual(u'Any S ORDERBY S WHERE NOT S dirige A, ' |
|
504 u'S is_instance_of Personne, EXISTS(S eid %(S)s), ' |
|
505 u'A is Societe', rql) |
|
506 self.assertEqual({'S': person.eid}, args) |
|
507 |
|
508 def test_unrelated_rql_s_linkto_s_no_info(self): |
|
509 with self.admin_access.web_request() as req: |
|
510 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
511 self.vreg['etypes'].etype_class('Personne').fetch_attrs = () |
|
512 soc = req.create_entity('Societe', nom=u'logilab') |
|
513 rql, args = person.cw_unrelated_rql('associe', 'Personne', 'subject') |
|
514 self.assertEqual(u'Any O ORDERBY O WHERE O is_instance_of Personne', rql) |
|
515 self.assertEqual({}, args) |
|
516 |
|
517 def test_unrelated_rql_s_linkto_s_unused_info(self): |
|
518 with self.admin_access.web_request() as req: |
|
519 person = self.vreg['etypes'].etype_class('Personne')(req) |
|
520 self.vreg['etypes'].etype_class('Personne').fetch_attrs = () |
|
521 other_p = req.create_entity('Personne', nom=u'titi') |
|
522 lt_infos = {('dirige', 'subject'): [other_p.eid]} |
|
523 rql, args = person.cw_unrelated_rql('associe', 'Personne', 'subject', |
|
524 lt_infos=lt_infos) |
|
525 self.assertEqual(u'Any O ORDERBY O WHERE O is_instance_of Personne', rql) |
|
526 |
|
527 def test_unrelated_base(self): |
|
528 with self.admin_access.web_request() as req: |
|
529 p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien') |
|
530 e = req.create_entity('Tag', name=u'x') |
|
531 related = [r.eid for r in e.tags] |
|
532 self.assertEqual(related, []) |
|
533 unrelated = [r[0] for r in e.unrelated('tags', 'Personne', 'subject')] |
|
534 self.assertIn(p.eid, unrelated) |
|
535 req.execute('SET X tags Y WHERE X is Tag, Y is Personne') |
|
536 e = req.execute('Any X WHERE X is Tag').get_entity(0, 0) |
|
537 unrelated = [r[0] for r in e.unrelated('tags', 'Personne', 'subject')] |
|
538 self.assertNotIn(p.eid, unrelated) |
|
539 |
|
540 def test_unrelated_limit(self): |
|
541 with self.admin_access.web_request() as req: |
|
542 e = req.create_entity('Tag', name=u'x') |
|
543 req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien') |
|
544 req.create_entity('Personne', nom=u'thenault', prenom=u'sylvain') |
|
545 self.assertEqual(len(e.unrelated('tags', 'Personne', 'subject', limit=1)), |
|
546 1) |
|
547 |
|
548 def test_unrelated_security(self): |
|
549 rperms = self.schema['EmailAddress'].permissions['read'] |
|
550 clear_cache(self.schema['EmailAddress'], 'get_groups') |
|
551 clear_cache(self.schema['EmailAddress'], 'get_rqlexprs') |
|
552 self.schema['EmailAddress'].permissions['read'] = ('managers', 'users', 'guests',) |
|
553 try: |
|
554 with self.admin_access.web_request() as req: |
|
555 email = req.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0) |
|
556 rset = email.unrelated('use_email', 'CWUser', 'object') |
|
557 self.assertEqual([x.login for x in rset.entities()], [u'admin', u'anon']) |
|
558 user = req.user |
|
559 rset = user.unrelated('use_email', 'EmailAddress', 'subject') |
|
560 self.assertEqual([x.address for x in rset.entities()], [u'hop']) |
|
561 self.create_user(req, 'toto') |
|
562 with self.new_access('toto').web_request() as req: |
|
563 email = req.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0) |
|
564 rset = email.unrelated('use_email', 'CWUser', 'object') |
|
565 self.assertEqual([x.login for x in rset.entities()], ['toto']) |
|
566 user = req.user |
|
567 rset = user.unrelated('use_email', 'EmailAddress', 'subject') |
|
568 self.assertEqual([x.address for x in rset.entities()], ['hop']) |
|
569 user = req.execute('Any X WHERE X login "admin"').get_entity(0, 0) |
|
570 rset = user.unrelated('use_email', 'EmailAddress', 'subject') |
|
571 self.assertEqual([x.address for x in rset.entities()], []) |
|
572 with self.new_access('anon').web_request() as req: |
|
573 email = req.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0) |
|
574 rset = email.unrelated('use_email', 'CWUser', 'object') |
|
575 self.assertEqual([x.login for x in rset.entities()], []) |
|
576 user = req.user |
|
577 rset = user.unrelated('use_email', 'EmailAddress', 'subject') |
|
578 self.assertEqual([x.address for x in rset.entities()], []) |
|
579 finally: |
|
580 clear_cache(self.schema['EmailAddress'], 'get_groups') |
|
581 clear_cache(self.schema['EmailAddress'], 'get_rqlexprs') |
|
582 self.schema['EmailAddress'].permissions['read'] = rperms |
|
583 |
|
584 def test_unrelated_new_entity(self): |
|
585 with self.admin_access.web_request() as req: |
|
586 e = self.vreg['etypes'].etype_class('CWUser')(req) |
|
587 unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')] |
|
588 # should be default groups but owners, i.e. managers, users, guests |
|
589 self.assertEqual(len(unrelated), 3) |
|
590 |
|
591 def test_markdown_printable_value_string(self): |
|
592 with self.admin_access.web_request() as req: |
|
593 e = req.create_entity('Card', title=u'rest markdown', |
|
594 content=u'This is [an example](http://example.com/ "Title") inline link`', |
|
595 content_format=u'text/markdown') |
|
596 self.assertEqual( |
|
597 u'<p>This is <a href="http://example.com/" ' |
|
598 u'title="Title">an example</a> inline link`</p>', |
|
599 e.printable_value('content')) |
|
600 |
|
601 def test_printable_value_string(self): |
|
602 with self.admin_access.web_request() as req: |
|
603 e = req.create_entity('Card', title=u'rest test', |
|
604 content=u'du :eid:`1:*ReST*`', |
|
605 content_format=u'text/rest') |
|
606 self.assertEqual(e.printable_value('content'), |
|
607 '<p>du <a class="reference" href="http://testing.fr/cubicweb/cwsource/system">*ReST*</a></p>') |
|
608 e.cw_attr_cache['content'] = 'du <em>html</em> <ref rql="CWUser X">users</ref>' |
|
609 e.cw_attr_cache['content_format'] = 'text/html' |
|
610 self.assertEqual(e.printable_value('content'), |
|
611 'du <em>html</em> <a href="http://testing.fr/cubicweb/view?rql=CWUser%20X">users</a>') |
|
612 e.cw_attr_cache['content'] = 'du *texte*' |
|
613 e.cw_attr_cache['content_format'] = 'text/plain' |
|
614 self.assertEqual(e.printable_value('content').replace("\n", ""), |
|
615 '<p>du *texte*<br/></p>') |
|
616 e.cw_attr_cache['title'] = 'zou' |
|
617 e.cw_attr_cache['content'] = '''\ |
|
618 a title |
|
619 ======= |
|
620 du :eid:`1:*ReST*`''' |
|
621 e.cw_attr_cache['content_format'] = 'text/rest' |
|
622 self.assertEqual(e.printable_value('content', format='text/plain'), |
|
623 e.cw_attr_cache['content']) |
|
624 |
|
625 e.cw_attr_cache['content'] = u'<b>yo (zou éà ;)</b>' |
|
626 e.cw_attr_cache['content_format'] = 'text/html' |
|
627 self.assertEqual(e.printable_value('content', format='text/plain').strip(), |
|
628 u'**yo (zou éà ;)**') |
|
629 if HAS_TAL: |
|
630 e.cw_attr_cache['content'] = '<h1 tal:content="self/title">titre</h1>' |
|
631 e.cw_attr_cache['content_format'] = 'text/cubicweb-page-template' |
|
632 self.assertEqual(e.printable_value('content'), |
|
633 '<h1>zou</h1>') |
|
634 |
|
635 |
|
636 def test_printable_value_bytes(self): |
|
637 with self.admin_access.web_request() as req: |
|
638 e = req.create_entity('FakeFile', data=Binary(b'lambda x: 1'), data_format=u'text/x-python', |
|
639 data_encoding=u'ascii', data_name=u'toto.py') |
|
640 from cubicweb import mttransforms |
|
641 if mttransforms.HAS_PYGMENTS_TRANSFORMS: |
|
642 import pygments |
|
643 if tuple(int(i) for i in pygments.__version__.split('.')[:2]) >= (1, 3): |
|
644 self.assertEqual(e.printable_value('data'), |
|
645 '''<div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mi">1</span> |
|
646 </pre></div>''') |
|
647 else: |
|
648 self.assertEqual(e.printable_value('data'), |
|
649 '''<div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mf">1</span> |
|
650 </pre></div>''') |
|
651 else: |
|
652 self.assertEqual(e.printable_value('data'), |
|
653 '''<pre class="python"> |
|
654 <span style="color: #C00000;">lambda</span> <span style="color: #000000;">x</span><span style="color: #0000C0;">:</span> <span style="color: #0080C0;">1</span> |
|
655 </pre>''') |
|
656 |
|
657 e = req.create_entity('FakeFile', |
|
658 data=Binary(u'*héhéhé*'.encode('utf-8')), |
|
659 data_format=u'text/rest', |
|
660 data_encoding=u'utf-8', data_name=u'toto.txt') |
|
661 self.assertEqual(e.printable_value('data'), |
|
662 u'<p><em>héhéhé</em></p>') |
|
663 |
|
664 def test_printable_value_bad_html(self): |
|
665 """make sure we don't crash if we try to render invalid XHTML strings""" |
|
666 with self.admin_access.web_request() as req: |
|
667 e = req.create_entity('Card', title=u'bad html', content=u'<div>R&D<br>', |
|
668 content_format=u'text/html') |
|
669 tidy = lambda x: x.replace('\n', '') |
|
670 self.assertEqual(tidy(e.printable_value('content')), |
|
671 '<div>R&D<br/></div>') |
|
672 e.cw_attr_cache['content'] = u'yo !! R&D <div> pas fermé' |
|
673 self.assertEqual(tidy(e.printable_value('content')), |
|
674 u'yo !! R&D <div> pas fermé</div>') |
|
675 e.cw_attr_cache['content'] = u'R&D' |
|
676 self.assertEqual(tidy(e.printable_value('content')), u'R&D') |
|
677 e.cw_attr_cache['content'] = u'R&D;' |
|
678 self.assertEqual(tidy(e.printable_value('content')), u'R&D;') |
|
679 e.cw_attr_cache['content'] = u'yo !! R&D <div> pas fermé' |
|
680 self.assertEqual(tidy(e.printable_value('content')), |
|
681 u'yo !! R&D <div> pas fermé</div>') |
|
682 e.cw_attr_cache['content'] = u'été <div> été' |
|
683 self.assertEqual(tidy(e.printable_value('content')), |
|
684 u'été <div> été</div>') |
|
685 e.cw_attr_cache['content'] = u'C'est un exemple sérieux' |
|
686 self.assertEqual(tidy(e.printable_value('content')), |
|
687 u"C'est un exemple sérieux") |
|
688 # make sure valid xhtml is left untouched |
|
689 e.cw_attr_cache['content'] = u'<div>R&D<br/></div>' |
|
690 self.assertEqual(e.printable_value('content'), e.cw_attr_cache['content']) |
|
691 e.cw_attr_cache['content'] = u'<div>été</div>' |
|
692 self.assertEqual(e.printable_value('content'), e.cw_attr_cache['content']) |
|
693 e.cw_attr_cache['content'] = u'été' |
|
694 self.assertEqual(e.printable_value('content'), e.cw_attr_cache['content']) |
|
695 e.cw_attr_cache['content'] = u'hop\r\nhop\nhip\rmomo' |
|
696 self.assertEqual(e.printable_value('content'), u'hop\nhop\nhip\nmomo') |
|
697 |
|
698 def test_printable_value_bad_html_ms(self): |
|
699 with self.admin_access.web_request() as req: |
|
700 e = req.create_entity('Card', title=u'bad html', content=u'<div>R&D<br>', |
|
701 content_format=u'text/html') |
|
702 tidy = lambda x: x.replace('\n', '') |
|
703 e.cw_attr_cache['content'] = u'<div x:foo="bar">ms orifice produces weird html</div>' |
|
704 # Caution! current implementation of soup2xhtml strips first div element |
|
705 content = soup2xhtml(e.printable_value('content'), 'utf-8') |
|
706 self.assertMultiLineEqual(content, u'<div>ms orifice produces weird html</div>') |
|
707 |
|
708 def test_fulltextindex(self): |
|
709 with self.admin_access.web_request() as req: |
|
710 e = self.vreg['etypes'].etype_class('FakeFile')(req) |
|
711 e.cw_attr_cache['description'] = 'du <em>html</em>' |
|
712 e.cw_attr_cache['description_format'] = 'text/html' |
|
713 e.cw_attr_cache['data'] = Binary(b'some <em>data</em>') |
|
714 e.cw_attr_cache['data_name'] = 'an html file' |
|
715 e.cw_attr_cache['data_format'] = 'text/html' |
|
716 e.cw_attr_cache['data_encoding'] = 'ascii' |
|
717 e._cw.transaction_data.clear() |
|
718 words = e.cw_adapt_to('IFTIndexable').get_words() |
|
719 words['C'].sort() |
|
720 self.assertEqual({'C': sorted(['an', 'html', 'file', 'du', 'html', 'some', 'data'])}, |
|
721 words) |
|
722 |
|
723 |
|
724 def test_nonregr_relation_cache(self): |
|
725 with self.admin_access.web_request() as req: |
|
726 p1 = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien') |
|
727 p2 = req.create_entity('Personne', nom=u'toto') |
|
728 req.execute('SET X evaluee Y WHERE X nom "di mascio", Y nom "toto"') |
|
729 self.assertEqual(p1.evaluee[0].nom, "toto") |
|
730 self.assertFalse(p1.reverse_evaluee) |
|
731 |
|
732 def test_complete_relation(self): |
|
733 with self.admin_access.repo_cnx() as cnx: |
|
734 eid = cnx.execute( |
|
735 'INSERT TrInfo X: X comment "zou", X wf_info_for U, X from_state S1, X to_state S2 ' |
|
736 'WHERE U login "admin", S1 name "activated", S2 name "deactivated"')[0][0] |
|
737 trinfo = cnx.execute('Any X WHERE X eid %(x)s', {'x': eid}).get_entity(0, 0) |
|
738 trinfo.complete() |
|
739 self.assertIsInstance(trinfo.cw_attr_cache['creation_date'], datetime) |
|
740 self.assertTrue(trinfo.cw_relation_cached('from_state', 'subject')) |
|
741 self.assertTrue(trinfo.cw_relation_cached('to_state', 'subject')) |
|
742 self.assertTrue(trinfo.cw_relation_cached('wf_info_for', 'subject')) |
|
743 self.assertEqual(trinfo.by_transition, ()) |
|
744 |
|
745 def test_request_cache(self): |
|
746 with self.admin_access.web_request() as req: |
|
747 user = req.execute('CWUser X WHERE X login "admin"').get_entity(0, 0) |
|
748 state = user.in_state[0] |
|
749 samestate = req.execute('State X WHERE X name "activated"').get_entity(0, 0) |
|
750 self.assertIs(state, samestate) |
|
751 |
|
752 def test_rest_path(self): |
|
753 with self.admin_access.web_request() as req: |
|
754 note = req.create_entity('Note', type=u'z') |
|
755 self.assertEqual(note.rest_path(), 'note/%s' % note.eid) |
|
756 # unique attr |
|
757 tag = req.create_entity('Tag', name=u'x') |
|
758 self.assertEqual(tag.rest_path(), 'tag/x') |
|
759 # test explicit rest_attr |
|
760 person = req.create_entity('Personne', prenom=u'john', nom=u'doe') |
|
761 self.assertEqual(person.rest_path(), 'personne/doe') |
|
762 # ambiguity test |
|
763 person2 = req.create_entity('Personne', prenom=u'remi', nom=u'doe') |
|
764 person.cw_clear_all_caches() |
|
765 self.assertEqual(person.rest_path(), text_type(person.eid)) |
|
766 self.assertEqual(person2.rest_path(), text_type(person2.eid)) |
|
767 # unique attr with None value (nom in this case) |
|
768 friend = req.create_entity('Ami', prenom=u'bob') |
|
769 self.assertEqual(friend.rest_path(), text_type(friend.eid)) |
|
770 # 'ref' below is created without the unique but not required |
|
771 # attribute, make sur that the unique _and_ required 'ean' is used |
|
772 # as the rest attribute |
|
773 ref = req.create_entity('Reference', ean=u'42-1337-42') |
|
774 self.assertEqual(ref.rest_path(), 'reference/42-1337-42') |
|
775 |
|
776 def test_can_use_rest_path(self): |
|
777 self.assertTrue(can_use_rest_path(u'zobi')) |
|
778 # don't use rest if we have /, ? or & in the path (breaks mod_proxy) |
|
779 self.assertFalse(can_use_rest_path(u'zo/bi')) |
|
780 self.assertFalse(can_use_rest_path(u'zo&bi')) |
|
781 self.assertFalse(can_use_rest_path(u'zo?bi')) |
|
782 |
|
783 def test_cw_set_attributes(self): |
|
784 with self.admin_access.web_request() as req: |
|
785 person = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien') |
|
786 self.assertEqual(person.prenom, u'adrien') |
|
787 self.assertEqual(person.nom, u'di mascio') |
|
788 person.cw_set(prenom=u'sylvain', nom=u'thénault') |
|
789 person = req.execute('Personne P').get_entity(0, 0) # XXX retreival needed ? |
|
790 self.assertEqual(person.prenom, u'sylvain') |
|
791 self.assertEqual(person.nom, u'thénault') |
|
792 |
|
793 def test_cw_set_relations(self): |
|
794 with self.admin_access.web_request() as req: |
|
795 person = req.create_entity('Personne', nom=u'chauvat', prenom=u'nicolas') |
|
796 note = req.create_entity('Note', type=u'x') |
|
797 note.cw_set(ecrit_par=person) |
|
798 note = req.create_entity('Note', type=u'y') |
|
799 note.cw_set(ecrit_par=person.eid) |
|
800 self.assertEqual(len(person.reverse_ecrit_par), 2) |
|
801 |
|
802 def test_metainformation_and_external_absolute_url(self): |
|
803 with self.admin_access.web_request() as req: |
|
804 note = req.create_entity('Note', type=u'z') |
|
805 metainf = note.cw_metainformation() |
|
806 self.assertEqual(metainf, {'source': {'type': 'native', 'uri': 'system', |
|
807 'use-cwuri-as-url': False}, |
|
808 'type': u'Note', 'extid': None}) |
|
809 self.assertEqual(note.absolute_url(), 'http://testing.fr/cubicweb/note/%s' % note.eid) |
|
810 metainf['source'] = metainf['source'].copy() |
|
811 metainf['source']['base-url'] = 'http://cubicweb2.com/' |
|
812 metainf['extid'] = 1234 |
|
813 self.assertEqual(note.absolute_url(), 'http://cubicweb2.com/note/1234') |
|
814 |
|
815 def test_absolute_url_empty_field(self): |
|
816 with self.admin_access.web_request() as req: |
|
817 card = req.create_entity('Card', wikiid=u'', title=u'test') |
|
818 self.assertEqual(card.absolute_url(), |
|
819 'http://testing.fr/cubicweb/%s' % card.eid) |
|
820 |
|
821 def test_create_and_compare_entity(self): |
|
822 access = self.admin_access |
|
823 with access.web_request() as req: |
|
824 p1 = req.create_entity('Personne', nom=u'fayolle', prenom=u'alexandre') |
|
825 p2 = req.create_entity('Personne', nom=u'campeas', prenom=u'aurelien') |
|
826 note = req.create_entity('Note', type=u'z') |
|
827 p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien', |
|
828 connait=p1, evaluee=[p1, p2], |
|
829 reverse_ecrit_par=note) |
|
830 self.assertEqual(p.nom, 'di mascio') |
|
831 self.assertEqual([c.nom for c in p.connait], ['fayolle']) |
|
832 self.assertEqual(sorted([c.nom for c in p.evaluee]), ['campeas', 'fayolle']) |
|
833 self.assertEqual([c.type for c in p.reverse_ecrit_par], ['z']) |
|
834 req.cnx.commit() |
|
835 with access.web_request() as req: |
|
836 auc = req.execute('Personne P WHERE P prenom "aurelien"').get_entity(0,0) |
|
837 persons = set() |
|
838 persons.add(p1) |
|
839 persons.add(p2) |
|
840 persons.add(auc) |
|
841 self.assertEqual(2, len(persons)) |
|
842 self.assertNotEqual(p1, p2) |
|
843 self.assertEqual(p2, auc) |
|
844 |
|
845 |
|
846 if __name__ == '__main__': |
|
847 from logilab.common.testlib import unittest_main |
|
848 unittest_main() |