server/test/unittest_hooks.py
branchstable
changeset 3315 59220b704562
parent 2923 b97a0f8dd4dc
child 2968 0e3460341023
child 3526 dfb2ebb765e2
equal deleted inserted replaced
3298:caef98aa4a98 3315:59220b704562
    35                           'DELETE CWRType X WHERE X name "relation_type"')
    35                           'DELETE CWRType X WHERE X name "relation_type"')
    36         self.assertRaises(RepositoryError, self.execute,
    36         self.assertRaises(RepositoryError, self.execute,
    37                           'DELETE CWGroup X WHERE X name "owners"')
    37                           'DELETE CWGroup X WHERE X name "owners"')
    38 
    38 
    39     def test_delete_required_relations_subject(self):
    39     def test_delete_required_relations_subject(self):
    40         self.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y, X in_state S '
    40         self.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y '
    41                      'WHERE Y name "users", S name "activated"')
    41                      'WHERE Y name "users"')
    42         self.commit()
    42         self.commit()
    43         self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"')
    43         self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"')
    44         self.assertRaises(ValidationError, self.commit)
    44         self.assertRaises(ValidationError, self.commit)
    45         self.execute('DELETE X in_group Y WHERE X login "toto"')
    45         self.execute('DELETE X in_group Y WHERE X login "toto"')
    46         self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
    46         self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
    57     def test_missing_required_relations_subject_inline(self):
    57     def test_missing_required_relations_subject_inline(self):
    58         # missing in_group relation
    58         # missing in_group relation
    59         self.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
    59         self.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
    60         self.assertRaises(ValidationError,
    60         self.assertRaises(ValidationError,
    61                           self.commit)
    61                           self.commit)
    62 
       
    63     def test_delete_if_singlecard1(self):
       
    64         self.assertEquals(self.repo.schema['in_state'].inlined, False)
       
    65         ueid = self.create_user('toto')
       
    66         self.commit()
       
    67         self.execute('SET X in_state S WHERE S name "deactivated", X eid %(x)s', {'x': ueid})
       
    68         rset = self.execute('Any S WHERE X in_state S, X eid %(x)s', {'x': ueid})
       
    69         self.assertEquals(len(rset), 1)
       
    70         self.commit()
       
    71         self.assertRaises(Exception, self.execute, 'SET X in_state S WHERE S name "deactivated", X eid %s' % ueid)
       
    72         rset2 = self.execute('Any S WHERE X in_state S, X eid %(x)s', {'x': ueid})
       
    73         self.assertEquals(rset.rows, rset2.rows)
       
    74 
    62 
    75     def test_inlined(self):
    63     def test_inlined(self):
    76         self.assertEquals(self.repo.schema['sender'].inlined, True)
    64         self.assertEquals(self.repo.schema['sender'].inlined, True)
    77         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    65         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    78         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    66         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
   152         self.assertEquals(entity.descr, u'yo')
   140         self.assertEquals(entity.descr, u'yo')
   153         self.execute('SET A descr "R&D<p>yo" WHERE A eid %s' % entity.eid)
   141         self.execute('SET A descr "R&D<p>yo" WHERE A eid %s' % entity.eid)
   154         entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0)
   142         entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0)
   155         self.assertEquals(entity.descr, u'R&amp;D<p>yo</p>')
   143         self.assertEquals(entity.descr, u'R&amp;D<p>yo</p>')
   156 
   144 
       
   145 
       
   146     def test_metadata_cwuri(self):
       
   147         eid = self.execute('INSERT Note X')[0][0]
       
   148         cwuri = self.execute('Any U WHERE X eid %s, X cwuri U' % eid)[0][0]
       
   149         self.assertEquals(cwuri, self.repo.config['base-url'] + 'eid/%s' % eid)
       
   150 
       
   151     def test_metadata_creation_modification_date(self):
       
   152         _now = datetime.now()
       
   153         eid = self.execute('INSERT Note X')[0][0]
       
   154         creation_date, modification_date = self.execute('Any CD, MD WHERE X eid %s, '
       
   155                                                         'X creation_date CD, '
       
   156                                                         'X modification_date MD' % eid)[0]
       
   157         self.assertEquals((creation_date - _now).seconds, 0)
       
   158         self.assertEquals((modification_date - _now).seconds, 0)
       
   159 
       
   160     def test_metadata__date(self):
       
   161         _now = datetime.now()
       
   162         eid = self.execute('INSERT Note X')[0][0]
       
   163         creation_date = self.execute('Any D WHERE X eid %s, X creation_date D' % eid)[0][0]
       
   164         self.assertEquals((creation_date - _now).seconds, 0)
       
   165 
       
   166     def test_metadata_created_by(self):
       
   167         eid = self.execute('INSERT Note X')[0][0]
       
   168         self.commit() # fire operations
       
   169         rset = self.execute('Any U WHERE X eid %s, X created_by U' % eid)
       
   170         self.assertEquals(len(rset), 1) # make sure we have only one creator
       
   171         self.assertEquals(rset[0][0], self.session.user.eid)
       
   172 
       
   173     def test_metadata_owned_by(self):
       
   174         eid = self.execute('INSERT Note X')[0][0]
       
   175         self.commit() # fire operations
       
   176         rset = self.execute('Any U WHERE X eid %s, X owned_by U' % eid)
       
   177         self.assertEquals(len(rset), 1) # make sure we have only one owner
       
   178         self.assertEquals(rset[0][0], self.session.user.eid)
   157 
   179 
   158 
   180 
   159 class UserGroupHooksTC(RepositoryBasedTC):
   181 class UserGroupHooksTC(RepositoryBasedTC):
   160 
   182 
   161     def test_user_synchronization(self):
   183     def test_user_synchronization(self):
   478         self.execute('SET DEF cardinality "?1" '
   500         self.execute('SET DEF cardinality "?1" '
   479                      'WHERE DEF relation_type RT, DEF from_entity E,'
   501                      'WHERE DEF relation_type RT, DEF from_entity E,'
   480                      'RT name "prenom", E name "Personne"')
   502                      'RT name "prenom", E name "Personne"')
   481         self.commit()
   503         self.commit()
   482 
   504 
   483 
       
   484 class WorkflowHooksTC(RepositoryBasedTC):
       
   485 
       
   486     def setUp(self):
       
   487         RepositoryBasedTC.setUp(self)
       
   488         self.s_activated = self.execute('State X WHERE X name "activated"')[0][0]
       
   489         self.s_deactivated = self.execute('State X WHERE X name "deactivated"')[0][0]
       
   490         self.s_dummy = self.execute('INSERT State X: X name "dummy", X state_of E WHERE E name "CWUser"')[0][0]
       
   491         self.create_user('stduser')
       
   492         # give access to users group on the user's wf transitions
       
   493         # so we can test wf enforcing on euser (managers don't have anymore this
       
   494         # enforcement
       
   495         self.execute('SET X require_group G WHERE G name "users", X transition_of ET, ET name "CWUser"')
       
   496         self.commit()
       
   497 
       
   498     def tearDown(self):
       
   499         self.execute('DELETE X require_group G WHERE G name "users", X transition_of ET, ET name "CWUser"')
       
   500         self.commit()
       
   501         RepositoryBasedTC.tearDown(self)
       
   502 
       
   503     def test_set_initial_state(self):
       
   504         ueid = self.execute('INSERT CWUser E: E login "x", E upassword "x", E in_group G '
       
   505                             'WHERE G name "users"')[0][0]
       
   506         self.failIf(self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s',
       
   507                                  {'x' : ueid}))
       
   508         self.commit()
       
   509         initialstate = self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s',
       
   510                                     {'x' : ueid})[0][0]
       
   511         self.assertEquals(initialstate, u'activated')
       
   512 
       
   513     def test_initial_state(self):
       
   514         cnx = self.login('stduser')
       
   515         cu = cnx.cursor()
       
   516         self.assertRaises(ValidationError, cu.execute,
       
   517                           'INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, '
       
   518                           'X in_state S WHERE S name "deactivated"', {'pwd': 'oops'})
       
   519         cnx.close()
       
   520         # though managers can do whatever he want
       
   521         self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, '
       
   522                      'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'})
       
   523         self.commit()
       
   524 
       
   525     # test that the workflow is correctly enforced
       
   526     def test_transition_checking1(self):
       
   527         cnx = self.login('stduser')
       
   528         cu = cnx.cursor()
       
   529         ueid = cnx.user(self.current_session()).eid
       
   530         self.assertRaises(ValidationError,
       
   531                           cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   532                           {'x': ueid, 's': self.s_activated}, 'x')
       
   533         cnx.close()
       
   534 
       
   535     def test_transition_checking2(self):
       
   536         cnx = self.login('stduser')
       
   537         cu = cnx.cursor()
       
   538         ueid = cnx.user(self.current_session()).eid
       
   539         self.assertRaises(ValidationError,
       
   540                           cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   541                           {'x': ueid, 's': self.s_dummy}, 'x')
       
   542         cnx.close()
       
   543 
       
   544     def test_transition_checking3(self):
       
   545         cnx = self.login('stduser')
       
   546         cu = cnx.cursor()
       
   547         ueid = cnx.user(self.current_session()).eid
       
   548         cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   549                       {'x': ueid, 's': self.s_deactivated}, 'x')
       
   550         cnx.commit()
       
   551         self.assertRaises(ValidationError,
       
   552                           cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   553                           {'x': ueid, 's': self.s_deactivated}, 'x')
       
   554         # get back now
       
   555         cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   556                       {'x': ueid, 's': self.s_activated}, 'x')
       
   557         cnx.commit()
       
   558         cnx.close()
       
   559 
       
   560     def test_transition_checking4(self):
       
   561         cnx = self.login('stduser')
       
   562         cu = cnx.cursor()
       
   563         ueid = cnx.user(self.current_session()).eid
       
   564         cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   565                    {'x': ueid, 's': self.s_deactivated}, 'x')
       
   566         cnx.commit()
       
   567         self.assertRaises(ValidationError,
       
   568                           cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   569                           {'x': ueid, 's': self.s_dummy}, 'x')
       
   570         # get back now
       
   571         cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   572                       {'x': ueid, 's': self.s_activated}, 'x')
       
   573         cnx.commit()
       
   574         cnx.close()
       
   575 
       
   576     def test_transition_information(self):
       
   577         ueid = self.session.user.eid
       
   578         self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   579                       {'x': ueid, 's': self.s_deactivated}, 'x')
       
   580         self.commit()
       
   581         rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid})
       
   582         self.assertEquals(len(rset), 2)
       
   583         tr = rset.get_entity(1, 0)
       
   584         #tr.complete()
       
   585         self.assertEquals(tr.comment, None)
       
   586         self.assertEquals(tr.from_state[0].eid, self.s_activated)
       
   587         self.assertEquals(tr.to_state[0].eid, self.s_deactivated)
       
   588 
       
   589         self.session.set_shared_data('trcomment', u'il est pas sage celui-la')
       
   590         self.session.set_shared_data('trcommentformat', u'text/plain')
       
   591         self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   592                      {'x': ueid, 's': self.s_activated}, 'x')
       
   593         self.commit()
       
   594         rset = self.execute('TrInfo T ORDERBY T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid})
       
   595         self.assertEquals(len(rset), 3)
       
   596         tr = rset.get_entity(2, 0)
       
   597         #tr.complete()
       
   598         self.assertEquals(tr.comment, u'il est pas sage celui-la')
       
   599         self.assertEquals(tr.comment_format, u'text/plain')
       
   600         self.assertEquals(tr.from_state[0].eid, self.s_deactivated)
       
   601         self.assertEquals(tr.to_state[0].eid, self.s_activated)
       
   602         self.assertEquals(tr.owned_by[0].login, 'admin')
       
   603 
       
   604     def test_transition_information_on_creation(self):
       
   605         ueid = self.create_user('toto')
       
   606         rset = self.execute('TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': ueid})
       
   607         self.assertEquals(len(rset), 1)
       
   608         tr = rset.get_entity(0, 0)
       
   609         #tr.complete()
       
   610         self.assertEquals(tr.comment, None)
       
   611         self.assertEquals(tr.from_state, [])
       
   612         self.assertEquals(tr.to_state[0].eid, self.s_activated)
       
   613 
       
   614     def test_std_users_can_create_trinfo(self):
       
   615         self.create_user('toto')
       
   616         cnx = self.login('toto')
       
   617         cu = cnx.cursor()
       
   618         self.failUnless(cu.execute("INSERT Note X: X type 'a', X in_state S WHERE S name 'todo'"))
       
   619         cnx.commit()
       
   620 
       
   621     def test_metadata_cwuri(self):
       
   622         eid = self.execute('INSERT Note X')[0][0]
       
   623         cwuri = self.execute('Any U WHERE X eid %s, X cwuri U' % eid)[0][0]
       
   624         self.assertEquals(cwuri, self.repo.config['base-url'] + 'eid/%s' % eid)
       
   625 
       
   626     def test_metadata_creation_modification_date(self):
       
   627         _now = datetime.now()
       
   628         eid = self.execute('INSERT Note X')[0][0]
       
   629         creation_date, modification_date = self.execute('Any CD, MD WHERE X eid %s, '
       
   630                                                         'X creation_date CD, '
       
   631                                                         'X modification_date MD' % eid)[0]
       
   632         self.assertEquals((creation_date - _now).seconds, 0)
       
   633         self.assertEquals((modification_date - _now).seconds, 0)
       
   634 
       
   635     def test_metadata__date(self):
       
   636         _now = datetime.now()
       
   637         eid = self.execute('INSERT Note X')[0][0]
       
   638         creation_date = self.execute('Any D WHERE X eid %s, X creation_date D' % eid)[0][0]
       
   639         self.assertEquals((creation_date - _now).seconds, 0)
       
   640 
       
   641     def test_metadata_created_by(self):
       
   642         eid = self.execute('INSERT Note X')[0][0]
       
   643         self.commit() # fire operations
       
   644         rset = self.execute('Any U WHERE X eid %s, X created_by U' % eid)
       
   645         self.assertEquals(len(rset), 1) # make sure we have only one creator
       
   646         self.assertEquals(rset[0][0], self.session.user.eid)
       
   647 
       
   648     def test_metadata_owned_by(self):
       
   649         eid = self.execute('INSERT Note X')[0][0]
       
   650         self.commit() # fire operations
       
   651         rset = self.execute('Any U WHERE X eid %s, X owned_by U' % eid)
       
   652         self.assertEquals(len(rset), 1) # make sure we have only one owner
       
   653         self.assertEquals(rset[0][0], self.session.user.eid)
       
   654 
       
   655 if __name__ == '__main__':
   505 if __name__ == '__main__':
   656     unittest_main()
   506     unittest_main()