hooks/test/unittest_hooks.py
changeset 6366 1806148d6ce8
parent 6142 8bc6eac1fac1
parent 6340 470d8e828fda
child 6638 404aff81236c
equal deleted inserted replaced
6333:e3994fcc21c3 6366:1806148d6ce8
    47         self.execute('DELETE X in_group Y WHERE X login "toto"')
    47         self.execute('DELETE X in_group Y WHERE X login "toto"')
    48         self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
    48         self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
    49         self.commit()
    49         self.commit()
    50 
    50 
    51     def test_delete_required_relations_object(self):
    51     def test_delete_required_relations_object(self):
    52         self.skip('no sample in the schema ! YAGNI ? Kermaat ?')
    52         self.skipTest('no sample in the schema ! YAGNI ? Kermaat ?')
    53 
    53 
    54     def test_static_vocabulary_check(self):
    54     def test_static_vocabulary_check(self):
    55         self.assertRaises(ValidationError,
    55         self.assertRaises(ValidationError,
    56                           self.execute,
    56                           self.execute,
    57                           'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"')
    57                           'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"')
    61         self.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
    61         self.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
    62         self.assertRaises(ValidationError,
    62         self.assertRaises(ValidationError,
    63                           self.commit)
    63                           self.commit)
    64 
    64 
    65     def test_inlined(self):
    65     def test_inlined(self):
    66         self.assertEquals(self.repo.schema['sender'].inlined, True)
    66         self.assertEqual(self.repo.schema['sender'].inlined, True)
    67         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    67         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    68         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    68         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    69         eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    69         eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    70                             'WHERE Y is EmailAddress, P is EmailPart')[0][0]
    70                             'WHERE Y is EmailAddress, P is EmailPart')[0][0]
    71         self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress')
    71         self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress')
    72         rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid)
    72         rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid)
    73         self.assertEquals(len(rset), 1)
    73         self.assertEqual(len(rset), 1)
    74 
    74 
    75     def test_composite_1(self):
    75     def test_composite_1(self):
    76         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    76         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    77         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    77         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    78         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    78         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    79                      'WHERE Y is EmailAddress, P is EmailPart')
    79                      'WHERE Y is EmailAddress, P is EmailPart')
    80         self.failUnless(self.execute('Email X WHERE X sender Y'))
    80         self.failUnless(self.execute('Email X WHERE X sender Y'))
    81         self.commit()
    81         self.commit()
    82         self.execute('DELETE Email X')
    82         self.execute('DELETE Email X')
    83         rset = self.execute('Any X WHERE X is EmailPart')
    83         rset = self.execute('Any X WHERE X is EmailPart')
    84         self.assertEquals(len(rset), 1)
    84         self.assertEqual(len(rset), 1)
    85         self.commit()
    85         self.commit()
    86         rset = self.execute('Any X WHERE X is EmailPart')
    86         rset = self.execute('Any X WHERE X is EmailPart')
    87         self.assertEquals(len(rset), 0)
    87         self.assertEqual(len(rset), 0)
    88 
    88 
    89     def test_composite_2(self):
    89     def test_composite_2(self):
    90         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    90         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    91         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    91         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    92         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    92         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    94         self.commit()
    94         self.commit()
    95         self.execute('DELETE Email X')
    95         self.execute('DELETE Email X')
    96         self.execute('DELETE EmailPart X')
    96         self.execute('DELETE EmailPart X')
    97         self.commit()
    97         self.commit()
    98         rset = self.execute('Any X WHERE X is EmailPart')
    98         rset = self.execute('Any X WHERE X is EmailPart')
    99         self.assertEquals(len(rset), 0)
    99         self.assertEqual(len(rset), 0)
   100 
   100 
   101     def test_composite_redirection(self):
   101     def test_composite_redirection(self):
   102         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
   102         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
   103         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
   103         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
   104         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
   104         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
   108         self.commit()
   108         self.commit()
   109         self.execute('DELETE X parts Y WHERE X messageid "<1234>"')
   109         self.execute('DELETE X parts Y WHERE X messageid "<1234>"')
   110         self.execute('SET X parts Y WHERE X messageid "<2345>"')
   110         self.execute('SET X parts Y WHERE X messageid "<2345>"')
   111         self.commit()
   111         self.commit()
   112         rset = self.execute('Any X WHERE X is EmailPart')
   112         rset = self.execute('Any X WHERE X is EmailPart')
   113         self.assertEquals(len(rset), 1)
   113         self.assertEqual(len(rset), 1)
   114         self.assertEquals(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>')
   114         self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>')
   115 
   115 
   116     def test_unsatisfied_constraints(self):
   116     def test_unsatisfied_constraints(self):
   117         releid = self.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0]
   117         releid = self.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0]
   118         ex = self.assertRaises(ValidationError, self.commit)
   118         ex = self.assertRaises(ValidationError, self.commit)
   119         self.assertEquals(ex.errors,
   119         self.assertEqual(ex.errors,
   120                           {'in_group-object': u'RQLConstraint NOT O name "owners" failed'})
   120                           {'in_group-object': u'RQLConstraint NOT O name "owners" failed'})
   121 
   121 
   122     def test_html_tidy_hook(self):
   122     def test_html_tidy_hook(self):
   123         req = self.request()
   123         req = self.request()
   124         entity = req.create_entity('Workflow', name=u'wf1', description_format=u'text/html',
   124         entity = req.create_entity('Workflow', name=u'wf1', description_format=u'text/html',
   125                                  description=u'yo')
   125                                  description=u'yo')
   126         self.assertEquals(entity.description, u'yo')
   126         self.assertEqual(entity.description, u'yo')
   127         entity = req.create_entity('Workflow', name=u'wf2', description_format=u'text/html',
   127         entity = req.create_entity('Workflow', name=u'wf2', description_format=u'text/html',
   128                                  description=u'<b>yo')
   128                                  description=u'<b>yo')
   129         self.assertEquals(entity.description, u'<b>yo</b>')
   129         self.assertEqual(entity.description, u'<b>yo</b>')
   130         entity = req.create_entity('Workflow', name=u'wf3', description_format=u'text/html',
   130         entity = req.create_entity('Workflow', name=u'wf3', description_format=u'text/html',
   131                                  description=u'<b>yo</b>')
   131                                  description=u'<b>yo</b>')
   132         self.assertEquals(entity.description, u'<b>yo</b>')
   132         self.assertEqual(entity.description, u'<b>yo</b>')
   133         entity = req.create_entity('Workflow', name=u'wf4', description_format=u'text/html',
   133         entity = req.create_entity('Workflow', name=u'wf4', description_format=u'text/html',
   134                                  description=u'<b>R&D</b>')
   134                                  description=u'<b>R&D</b>')
   135         self.assertEquals(entity.description, u'<b>R&amp;D</b>')
   135         self.assertEqual(entity.description, u'<b>R&amp;D</b>')
   136         entity = req.create_entity('Workflow', name=u'wf5', description_format=u'text/html',
   136         entity = req.create_entity('Workflow', name=u'wf5', description_format=u'text/html',
   137                                  description=u"<div>c&apos;est <b>l'ét&eacute;")
   137                                  description=u"<div>c&apos;est <b>l'ét&eacute;")
   138         self.assertEquals(entity.description, u"<div>c'est <b>l'été</b></div>")
   138         self.assertEqual(entity.description, u"<div>c'est <b>l'été</b></div>")
   139 
   139 
   140     def test_nonregr_html_tidy_hook_no_update(self):
   140     def test_nonregr_html_tidy_hook_no_update(self):
   141         entity = self.request().create_entity('Workflow', name=u'wf1', description_format=u'text/html',
   141         entity = self.request().create_entity('Workflow', name=u'wf1', description_format=u'text/html',
   142                                  description=u'yo')
   142                                  description=u'yo')
   143         entity.set_attributes(name=u'wf2')
   143         entity.set_attributes(name=u'wf2')
   144         self.assertEquals(entity.description, u'yo')
   144         self.assertEqual(entity.description, u'yo')
   145         entity.set_attributes(description=u'R&D<p>yo')
   145         entity.set_attributes(description=u'R&D<p>yo')
   146         entity.cw_attr_cache.pop('description')
   146         entity.cw_attr_cache.pop('description')
   147         self.assertEquals(entity.description, u'R&amp;D<p>yo</p>')
   147         self.assertEqual(entity.description, u'R&amp;D<p>yo</p>')
   148 
       
   149 
   148 
   150     def test_metadata_cwuri(self):
   149     def test_metadata_cwuri(self):
   151         entity = self.request().create_entity('Workflow', name=u'wf1')
   150         entity = self.request().create_entity('Workflow', name=u'wf1')
   152         self.assertEquals(entity.cwuri, self.repo.config['base-url'] + 'eid/%s' % entity.eid)
   151         self.assertEqual(entity.cwuri, self.repo.config['base-url'] + 'eid/%s' % entity.eid)
   153 
   152 
   154     def test_metadata_creation_modification_date(self):
   153     def test_metadata_creation_modification_date(self):
   155         _now = datetime.now()
   154         _now = datetime.now()
   156         entity = self.request().create_entity('Workflow', name=u'wf1')
   155         entity = self.request().create_entity('Workflow', name=u'wf1')
   157         self.assertEquals((entity.creation_date - _now).seconds, 0)
   156         self.assertEqual((entity.creation_date - _now).seconds, 0)
   158         self.assertEquals((entity.modification_date - _now).seconds, 0)
   157         self.assertEqual((entity.modification_date - _now).seconds, 0)
   159 
   158 
   160     def test_metadata_created_by(self):
   159     def test_metadata_created_by(self):
   161         entity = self.request().create_entity('Bookmark', title=u'wf1', path=u'/view')
   160         entity = self.request().create_entity('Bookmark', title=u'wf1', path=u'/view')
   162         self.commit() # fire operations
   161         self.commit() # fire operations
   163         self.assertEquals(len(entity.created_by), 1) # make sure we have only one creator
   162         self.assertEqual(len(entity.created_by), 1) # make sure we have only one creator
   164         self.assertEquals(entity.created_by[0].eid, self.session.user.eid)
   163         self.assertEqual(entity.created_by[0].eid, self.session.user.eid)
   165 
   164 
   166     def test_metadata_owned_by(self):
   165     def test_metadata_owned_by(self):
   167         entity = self.request().create_entity('Bookmark', title=u'wf1', path=u'/view')
   166         entity = self.request().create_entity('Bookmark', title=u'wf1', path=u'/view')
   168         self.commit() # fire operations
   167         self.commit() # fire operations
   169         self.assertEquals(len(entity.owned_by), 1) # make sure we have only one owner
   168         self.assertEqual(len(entity.owned_by), 1) # make sure we have only one owner
   170         self.assertEquals(entity.owned_by[0].eid, self.session.user.eid)
   169         self.assertEqual(entity.owned_by[0].eid, self.session.user.eid)
   171 
   170 
   172     def test_user_login_stripped(self):
   171     def test_user_login_stripped(self):
   173         u = self.create_user('  joe  ')
   172         u = self.create_user('  joe  ')
   174         tname = self.execute('Any L WHERE E login L, E eid %(e)s',
   173         tname = self.execute('Any L WHERE E login L, E eid %(e)s',
   175                              {'e': u.eid})[0][0]
   174                              {'e': u.eid})[0][0]
   176         self.assertEquals(tname, 'joe')
   175         self.assertEqual(tname, 'joe')
   177         self.execute('SET X login " jijoe " WHERE X eid %(x)s', {'x': u.eid})
   176         self.execute('SET X login " jijoe " WHERE X eid %(x)s', {'x': u.eid})
   178         tname = self.execute('Any L WHERE E login L, E eid %(e)s',
   177         tname = self.execute('Any L WHERE E login L, E eid %(e)s',
   179                              {'e': u.eid})[0][0]
   178                              {'e': u.eid})[0][0]
   180         self.assertEquals(tname, 'jijoe')
   179         self.assertEqual(tname, 'jijoe')
   181 
   180 
   182 
   181 
   183 
   182 
   184 class UserGroupHooksTC(CubicWebTC):
   183 class UserGroupHooksTC(CubicWebTC):
   185 
   184 
   196         self.assertRaises(BadConnectionId,
   195         self.assertRaises(BadConnectionId,
   197                           self.repo.execute, cnxid, 'State X')
   196                           self.repo.execute, cnxid, 'State X')
   198 
   197 
   199     def test_user_group_synchronization(self):
   198     def test_user_group_synchronization(self):
   200         user = self.session.user
   199         user = self.session.user
   201         self.assertEquals(user.groups, set(('managers',)))
   200         self.assertEqual(user.groups, set(('managers',)))
   202         self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid)
   201         self.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid)
   203         self.assertEquals(user.groups, set(('managers',)))
   202         self.assertEqual(user.groups, set(('managers',)))
   204         self.commit()
   203         self.commit()
   205         self.assertEquals(user.groups, set(('managers', 'guests')))
   204         self.assertEqual(user.groups, set(('managers', 'guests')))
   206         self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid)
   205         self.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid)
   207         self.assertEquals(user.groups, set(('managers', 'guests')))
   206         self.assertEqual(user.groups, set(('managers', 'guests')))
   208         self.commit()
   207         self.commit()
   209         self.assertEquals(user.groups, set(('managers',)))
   208         self.assertEqual(user.groups, set(('managers',)))
   210 
   209 
   211     def test_user_composite_owner(self):
   210     def test_user_composite_owner(self):
   212         ueid = self.create_user('toto').eid
   211         ueid = self.create_user('toto').eid
   213         # composite of euser should be owned by the euser regardless of who created it
   212         # composite of euser should be owned by the euser regardless of who created it
   214         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", U use_email X '
   213         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", U use_email X '
   215                      'WHERE U login "toto"')
   214                      'WHERE U login "toto"')
   216         self.commit()
   215         self.commit()
   217         self.assertEquals(self.execute('Any A WHERE X owned_by U, U use_email X,'
   216         self.assertEqual(self.execute('Any A WHERE X owned_by U, U use_email X,'
   218                                        'U login "toto", X address A')[0][0],
   217                                        'U login "toto", X address A')[0][0],
   219                           'toto@logilab.fr')
   218                           'toto@logilab.fr')
   220 
   219 
   221     def test_no_created_by_on_deleted_entity(self):
   220     def test_no_created_by_on_deleted_entity(self):
   222         eid = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0]
   221         eid = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0]
   228 class CWPropertyHooksTC(CubicWebTC):
   227 class CWPropertyHooksTC(CubicWebTC):
   229 
   228 
   230     def test_unexistant_eproperty(self):
   229     def test_unexistant_eproperty(self):
   231         ex = self.assertRaises(ValidationError,
   230         ex = self.assertRaises(ValidationError,
   232                           self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop", X for_user U')
   231                           self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop", X for_user U')
   233         self.assertEquals(ex.errors, {'pkey-subject': 'unknown property key'})
   232         self.assertEqual(ex.errors, {'pkey-subject': 'unknown property key'})
   234         ex = self.assertRaises(ValidationError,
   233         ex = self.assertRaises(ValidationError,
   235                           self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop"')
   234                           self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop"')
   236         self.assertEquals(ex.errors, {'pkey-subject': 'unknown property key'})
   235         self.assertEqual(ex.errors, {'pkey-subject': 'unknown property key'})
   237 
   236 
   238     def test_site_wide_eproperty(self):
   237     def test_site_wide_eproperty(self):
   239         ex = self.assertRaises(ValidationError,
   238         ex = self.assertRaises(ValidationError,
   240                                self.execute, 'INSERT CWProperty X: X pkey "ui.site-title", X value "hop", X for_user U')
   239                                self.execute, 'INSERT CWProperty X: X pkey "ui.site-title", X value "hop", X for_user U')
   241         self.assertEquals(ex.errors, {'for_user-subject': "site-wide property can't be set for user"})
   240         self.assertEqual(ex.errors, {'for_user-subject': "site-wide property can't be set for user"})
   242 
   241 
   243     def test_bad_type_eproperty(self):
   242     def test_bad_type_eproperty(self):
   244         ex = self.assertRaises(ValidationError,
   243         ex = self.assertRaises(ValidationError,
   245                                self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop", X for_user U')
   244                                self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop", X for_user U')
   246         self.assertEquals(ex.errors, {'value-subject': u'unauthorized value'})
   245         self.assertEqual(ex.errors, {'value-subject': u'unauthorized value'})
   247         ex = self.assertRaises(ValidationError,
   246         ex = self.assertRaises(ValidationError,
   248                           self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop"')
   247                           self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop"')
   249         self.assertEquals(ex.errors, {'value-subject': u'unauthorized value'})
   248         self.assertEqual(ex.errors, {'value-subject': u'unauthorized value'})
   250 
   249 
   251 
   250 
   252 class SchemaHooksTC(CubicWebTC):
   251 class SchemaHooksTC(CubicWebTC):
   253 
   252 
   254     def test_duplicate_etype_error(self):
   253     def test_duplicate_etype_error(self):
   264                           self.execute, 'INSERT CWUser X: X login "admin"')
   263                           self.execute, 'INSERT CWUser X: X login "admin"')
   265         try:
   264         try:
   266             self.execute('INSERT CWUser X: X login "admin"')
   265             self.execute('INSERT CWUser X: X login "admin"')
   267         except ValidationError, ex:
   266         except ValidationError, ex:
   268             self.assertIsInstance(ex.entity, int)
   267             self.assertIsInstance(ex.entity, int)
   269             self.assertEquals(ex.errors, {'login-subject': 'the value "admin" is already used, use another one'})
   268             self.assertEqual(ex.errors, {'login-subject': 'the value "admin" is already used, use another one'})
   270 
   269 
   271 
   270 
   272 if __name__ == '__main__':
   271 if __name__ == '__main__':
   273     unittest_main()
   272     unittest_main()