test/unittest_entity.py
changeset 7794 aed065b97f12
parent 7791 31bb51ea5485
child 7798 8930f7a284dd
equal deleted inserted replaced
7792:163d25c9fdd2 7794:aed065b97f12
   177             cm.__enter__()
   177             cm.__enter__()
   178             torestore.append(cm)
   178             torestore.append(cm)
   179         try:
   179         try:
   180             # testing basic fetch_attrs attribute
   180             # testing basic fetch_attrs attribute
   181             self.assertEqual(Personne.fetch_rql(user),
   181             self.assertEqual(Personne.fetch_rql(user),
   182                               'Any X,AA,AB,AC ORDERBY AA ASC '
   182                               'Any X,AA,AB,AC ORDERBY AA '
   183                               'WHERE X is Personne, X nom AA, X prenom AB, X modification_date AC')
   183                               'WHERE X is Personne, X nom AA, X prenom AB, X modification_date AC')
   184             # testing unknown attributes
   184             # testing unknown attributes
   185             Personne.fetch_attrs = ('bloug', 'beep')
   185             Personne.fetch_attrs = ('bloug', 'beep')
   186             self.assertEqual(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   186             self.assertEqual(Personne.fetch_rql(user), 'Any X WHERE X is Personne')
   187             # testing one non final relation
   187             # testing one non final relation
   188             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   188             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   189             self.assertEqual(Personne.fetch_rql(user),
   189             self.assertEqual(Personne.fetch_rql(user),
   190                               'Any X,AA,AB,AC,AD ORDERBY AA ASC '
   190                               'Any X,AA,AB,AC,AD ORDERBY AA '
   191                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   191                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   192             # testing two non final relations
   192             # testing two non final relations
   193             Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee')
   193             Personne.fetch_attrs = ('nom', 'prenom', 'travaille', 'evaluee')
   194             self.assertEqual(Personne.fetch_rql(user),
   194             self.assertEqual(Personne.fetch_rql(user),
   195                              'Any X,AA,AB,AC,AD,AE ORDERBY AA ASC '
   195                              'Any X,AA,AB,AC,AD,AE ORDERBY AA '
   196                              'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   196                              'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   197                              'X evaluee AE?')
   197                              'X evaluee AE?')
   198             # testing one non final relation with recursion
   198             # testing one non final relation with recursion
   199             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   199             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   200             Societe.fetch_attrs = ('nom', 'evaluee')
   200             Societe.fetch_attrs = ('nom', 'evaluee')
   201             self.assertEqual(Personne.fetch_rql(user),
   201             self.assertEqual(Personne.fetch_rql(user),
   202                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA ASC,AF DESC '
   202                               'Any X,AA,AB,AC,AD,AE,AF ORDERBY AA,AF DESC '
   203                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   203                               'WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD, '
   204                               'AC evaluee AE?, AE modification_date AF'
   204                               'AC evaluee AE?, AE modification_date AF'
   205                               )
   205                               )
   206             # testing symmetric relation
   206             # testing symmetric relation
   207             Personne.fetch_attrs = ('nom', 'connait')
   207             Personne.fetch_attrs = ('nom', 'connait')
   208             self.assertEqual(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AA ASC '
   208             self.assertEqual(Personne.fetch_rql(user), 'Any X,AA,AB ORDERBY AA '
   209                               'WHERE X is Personne, X nom AA, X connait AB?')
   209                               'WHERE X is Personne, X nom AA, X connait AB?')
   210             # testing optional relation
   210             # testing optional relation
   211             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '?*'
   211             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '?*'
   212             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   212             Personne.fetch_attrs = ('nom', 'prenom', 'travaille')
   213             Societe.fetch_attrs = ('nom',)
   213             Societe.fetch_attrs = ('nom',)
   214             self.assertEqual(Personne.fetch_rql(user),
   214             self.assertEqual(Personne.fetch_rql(user),
   215                               'Any X,AA,AB,AC,AD ORDERBY AA ASC WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   215                               'Any X,AA,AB,AC,AD ORDERBY AA WHERE X is Personne, X nom AA, X prenom AB, X travaille AC?, AC nom AD')
   216             # testing relation with cardinality > 1
   216             # testing relation with cardinality > 1
   217             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '**'
   217             peschema.subjrels['travaille'].rdef(peschema, seschema).cardinality = '**'
   218             self.assertEqual(Personne.fetch_rql(user),
   218             self.assertEqual(Personne.fetch_rql(user),
   219                               'Any X,AA,AB ORDERBY AA ASC WHERE X is Personne, X nom AA, X prenom AB')
   219                               'Any X,AA,AB ORDERBY AA WHERE X is Personne, X nom AA, X prenom AB')
   220             # XXX test unauthorized attribute
   220             # XXX test unauthorized attribute
   221         finally:
   221         finally:
   222             # fetch_attrs restored by generic tearDown
   222             # fetch_attrs restored by generic tearDown
   223             for cm in torestore:
   223             for cm in torestore:
   224                 cm.__exit__(None, None, None)
   224                 cm.__exit__(None, None, None)
   231         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', 'type'))
   231         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', 'type'))
   232         Note.fetch_attrs, Note.fetch_order = fetch_config(('type',))
   232         Note.fetch_attrs, Note.fetch_order = fetch_config(('type',))
   233         SubNote.fetch_attrs, SubNote.fetch_order = fetch_config(('type',))
   233         SubNote.fetch_attrs, SubNote.fetch_order = fetch_config(('type',))
   234         p = self.request().create_entity('Personne', nom=u'pouet')
   234         p = self.request().create_entity('Personne', nom=u'pouet')
   235         self.assertEqual(p.cw_related_rql('evaluee'),
   235         self.assertEqual(p.cw_related_rql('evaluee'),
   236                           'Any X,AA,AB ORDERBY AA ASC WHERE E eid %(x)s, E evaluee X, '
   236                          'Any X,AA,AB ORDERBY AA WHERE E eid %(x)s, E evaluee X, '
   237                           'X type AA, X modification_date AB')
   237                          'X type AA, X modification_date AB')
       
   238         n = self.request().create_entity('Note')
       
   239         self.assertEqual(n.cw_related_rql('evaluee', role='object',
       
   240                                           targettypes=('Societe', 'Personne')),
       
   241                          "Any X,AA ORDERBY AB DESC WHERE E eid %(x)s, X evaluee E, "
       
   242                          "X is IN('Personne', 'Societe'), X nom AA, "
       
   243                          "X modification_date AB")
   238         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', ))
   244         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', ))
   239         # XXX
   245         # XXX
   240         self.assertEqual(p.cw_related_rql('evaluee'),
   246         self.assertEqual(p.cw_related_rql('evaluee'),
   241                           'Any X,AA ORDERBY AA DESC '
   247                           'Any X,AA ORDERBY AA DESC '
   242                           'WHERE E eid %(x)s, E evaluee X, X modification_date AA')
   248                           'WHERE E eid %(x)s, E evaluee X, X modification_date AA')
   244         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   250         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   245         self.assertEqual(tag.cw_related_rql('tags', 'subject'),
   251         self.assertEqual(tag.cw_related_rql('tags', 'subject'),
   246                           'Any X,AA ORDERBY AA DESC '
   252                           'Any X,AA ORDERBY AA DESC '
   247                           'WHERE E eid %(x)s, E tags X, X modification_date AA')
   253                           'WHERE E eid %(x)s, E tags X, X modification_date AA')
   248         self.assertEqual(tag.cw_related_rql('tags', 'subject', ('Personne',)),
   254         self.assertEqual(tag.cw_related_rql('tags', 'subject', ('Personne',)),
   249                           'Any X,AA,AB ORDERBY AA ASC '
   255                           'Any X,AA,AB ORDERBY AA '
   250                           'WHERE E eid %(x)s, E tags X, X is IN (Personne), X nom AA, '
   256                           'WHERE E eid %(x)s, E tags X, X is Personne, X nom AA, '
   251                           'X modification_date AB')
   257                           'X modification_date AB')
   252 
   258 
   253     def test_related_rql_ambiguous_cant_use_fetch_order(self):
   259     def test_related_rql_ambiguous_cant_use_fetch_order(self):
   254         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   260         tag = self.vreg['etypes'].etype_class('Tag')(self.request())
   255         for ttype in self.schema['tags'].objects():
   261         for ttype in self.schema['tags'].objects():
   272 
   278 
   273     def test_unrelated_rql_security_1_manager(self):
   279     def test_unrelated_rql_security_1_manager(self):
   274         user = self.request().user
   280         user = self.request().user
   275         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   281         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   276         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   282         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   277                          'WHERE NOT EXISTS(ZZ use_email O), S eid %(x)s, '
   283                          'WHERE NOT ZZ use_email O, S eid %(x)s, '
   278                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   284                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   279 
   285 
   280     def test_unrelated_rql_security_1_user(self):
   286     def test_unrelated_rql_security_1_user(self):
   281         req = self.request()
   287         req = self.request()
   282         self.create_user(req, 'toto')
   288         self.create_user(req, 'toto')
   283         self.login('toto')
   289         self.login('toto')
   284         user = req.user
   290         user = req.user
   285         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   291         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   286         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   292         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   287                          'WHERE NOT EXISTS(ZZ use_email O), S eid %(x)s, '
   293                          'WHERE NOT ZZ use_email O, S eid %(x)s, '
   288                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   294                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC')
   289         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   295         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   290         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   296         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   291         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   297         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   292                          'WHERE NOT EXISTS(ZZ use_email O, ZZ is CWUser), S eid %(x)s, '
   298                          'WHERE NOT ZZ use_email O, S eid %(x)s, '
   293                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC, A eid %(B)s, '
   299                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC, AD eid %(AE)s, '
   294                          'EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   300                          'EXISTS(S identity AD, NOT AD in_group AF, AF name "guests", AF is CWGroup), ZZ is CWUser')
   295 
   301 
   296     def test_unrelated_rql_security_1_anon(self):
   302     def test_unrelated_rql_security_1_anon(self):
   297         self.login('anon')
   303         self.login('anon')
   298         user = self.request().user
   304         user = self.request().user
   299         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   305         rql = user.cw_unrelated_rql('use_email', 'EmailAddress', 'subject')[0]
   300         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   306         self.assertEqual(rql, 'Any O,AA,AB,AC ORDERBY AC DESC '
   301                          'WHERE NOT EXISTS(ZZ use_email O, ZZ is CWUser), S eid %(x)s, '
   307                          'WHERE NOT ZZ use_email O, S eid %(x)s, '
   302                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC, A eid %(B)s, '
   308                          'O is EmailAddress, O address AA, O alias AB, O modification_date AC, AD eid %(AE)s, '
   303                          'EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   309                          'EXISTS(S identity AD, NOT AD in_group AF, AF name "guests", AF is CWGroup), ZZ is CWUser')
   304 
   310 
   305     def test_unrelated_rql_security_2(self):
   311     def test_unrelated_rql_security_2(self):
   306         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   312         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   307         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   313         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   308         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   314         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   309                          'WHERE NOT EXISTS(S use_email O), O eid %(x)s, S is CWUser, '
   315                          'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, '
   310                          'S login AA, S firstname AB, S surname AC, S modification_date AD')
   316                          'S login AA, S firstname AB, S surname AC, S modification_date AD')
   311         self.login('anon')
   317         self.login('anon')
   312         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   318         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   313         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   319         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   314         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   320         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   315                          'WHERE NOT EXISTS(S use_email O), O eid %(x)s, S is CWUser, '
   321                          'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, '
   316                          'S login AA, S firstname AB, S surname AC, S modification_date AD, '
   322                          'S login AA, S firstname AB, S surname AC, S modification_date AD, '
   317                          'A eid %(B)s, EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   323                          'AE eid %(AF)s, EXISTS(S identity AE, NOT AE in_group AG, AG name "guests", AG is CWGroup)')
   318 
   324 
   319     def test_unrelated_rql_security_nonexistant(self):
   325     def test_unrelated_rql_security_nonexistant(self):
   320         self.login('anon')
   326         self.login('anon')
   321         email = self.vreg['etypes'].etype_class('EmailAddress')(self.request())
   327         email = self.vreg['etypes'].etype_class('EmailAddress')(self.request())
   322         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   328         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   323         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   329         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   324                          'WHERE S is CWUser, '
   330                          'WHERE S is CWUser, '
   325                          'S login AA, S firstname AB, S surname AC, S modification_date AD, '
   331                          'S login AA, S firstname AB, S surname AC, S modification_date AD, '
   326                          'A eid %(B)s, EXISTS(S identity A, NOT A in_group C, C name "guests", C is CWGroup)')
   332                          'AE eid %(AF)s, EXISTS(S identity AE, NOT AE in_group AG, AG name "guests", AG is CWGroup)')
   327 
   333 
   328     def test_unrelated_rql_constraints_creation_subject(self):
   334     def test_unrelated_rql_constraints_creation_subject(self):
   329         person = self.vreg['etypes'].etype_class('Personne')(self.request())
   335         person = self.vreg['etypes'].etype_class('Personne')(self.request())
   330         rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0]
   336         rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0]
   331         self.assertEqual(
   337         self.assertEqual(
   336         person = self.vreg['etypes'].etype_class('Personne')(self.request())
   342         person = self.vreg['etypes'].etype_class('Personne')(self.request())
   337         rql = person.cw_unrelated_rql('connait', 'Personne', 'object')[0]
   343         rql = person.cw_unrelated_rql('connait', 'Personne', 'object')[0]
   338         self.assertEqual(
   344         self.assertEqual(
   339             rql, 'Any S,AA,AB,AC ORDERBY AC DESC WHERE '
   345             rql, 'Any S,AA,AB,AC ORDERBY AC DESC WHERE '
   340             'S is Personne, S nom AA, S prenom AB, S modification_date AC, '
   346             'S is Personne, S nom AA, S prenom AB, S modification_date AC, '
   341             'NOT (S connait A, A nom "toto"), A is Personne, EXISTS(S travaille B, B nom "tutu")')
   347             'NOT (S connait AD, AD nom "toto"), AD is Personne, '
       
   348             'EXISTS(S travaille AE, AE nom "tutu")')
   342 
   349 
   343     def test_unrelated_rql_constraints_edition_subject(self):
   350     def test_unrelated_rql_constraints_edition_subject(self):
   344         person = self.request().create_entity('Personne', nom=u'sylvain')
   351         person = self.request().create_entity('Personne', nom=u'sylvain')
   345         rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0]
   352         rql = person.cw_unrelated_rql('connait', 'Personne', 'subject')[0]
   346         self.assertEqual(
   353         self.assertEqual(
   347             rql, 'Any O,AA,AB,AC ORDERBY AC DESC WHERE '
   354             rql, 'Any O,AA,AB,AC ORDERBY AC DESC WHERE '
   348             'NOT EXISTS(S connait O), S eid %(x)s, O is Personne, '
   355             'NOT S connait O, S eid %(x)s, O is Personne, '
   349             'O nom AA, O prenom AB, O modification_date AC, '
   356             'O nom AA, O prenom AB, O modification_date AC, '
   350             'NOT S identity O')
   357             'NOT S identity O')
   351 
   358 
   352     def test_unrelated_rql_constraints_edition_object(self):
   359     def test_unrelated_rql_constraints_edition_object(self):
   353         person = self.request().create_entity('Personne', nom=u'sylvain')
   360         person = self.request().create_entity('Personne', nom=u'sylvain')
   354         rql = person.cw_unrelated_rql('connait', 'Personne', 'object')[0]
   361         rql = person.cw_unrelated_rql('connait', 'Personne', 'object')[0]
   355         self.assertEqual(
   362         self.assertEqual(
   356             rql, 'Any S,AA,AB,AC ORDERBY AC DESC WHERE '
   363             rql, 'Any S,AA,AB,AC ORDERBY AC DESC WHERE '
   357             'NOT EXISTS(S connait O), O eid %(x)s, S is Personne, '
   364             'NOT S connait O, O eid %(x)s, S is Personne, '
   358             'S nom AA, S prenom AB, S modification_date AC, '
   365             'S nom AA, S prenom AB, S modification_date AC, '
   359             'NOT S identity O, NOT (S connait A, A nom "toto"), '
   366             'NOT S identity O, NOT (S connait AD, AD nom "toto"), '
   360             'EXISTS(S travaille B, B nom "tutu")')
   367             'EXISTS(S travaille AE, AE nom "tutu")')
   361 
   368 
   362     def test_unrelated_base(self):
   369     def test_unrelated_base(self):
   363         req = self.request()
   370         req = self.request()
   364         p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   371         p = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
   365         e = req.create_entity('Tag', name=u'x')
   372         e = req.create_entity('Tag', name=u'x')