entities/test/unittest_wfobjs.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 25 Aug 2009 13:35:01 +0200
branch3.5
changeset 2983 b458631fe347
parent 2982 1768dbe8e9b3
child 2984 31c24caf6961
permissions -rw-r--r--
[wf] test in_state consistency
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
from cubicweb.devtools.apptest import EnvBasedTC
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
from cubicweb import ValidationError
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
     4
def add_wf(self, etype, name=None):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
     5
    if name is None:
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
     6
        name = unicode(etype)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
     7
    wf = self.execute('INSERT Workflow X: X name %(n)s', {'n': name}).get_entity(0, 0)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
     8
    self.execute('SET WF workflow_of ET WHERE WF eid %(wf)s, ET name %(et)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
     9
                 {'wf': wf.eid, 'et': etype})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    10
    return wf
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    11
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    12
def parse_hist(wfhist):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    13
    return [(ti.previous_state.name, ti.new_state.name,
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    14
             ti.transition and ti.transition.name, ti.comment)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    15
            for ti in wfhist]
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    16
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    17
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    18
class WorkflowBuildingTC(EnvBasedTC):
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    19
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    20
    def test_wf_construction(self):
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    21
        wf = add_wf(self, 'Company')
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    22
        foo = wf.add_state(u'foo', initial=True)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    23
        bar = wf.add_state(u'bar')
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    24
        self.assertEquals(wf.state_by_name('bar').eid, bar.eid)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    25
        self.assertEquals(wf.state_by_name('barrr'), None)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    26
        baz = wf.add_transition(u'baz', (foo,), bar, ('managers',))
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    27
        self.assertEquals(wf.transition_by_name('baz').eid, baz.eid)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    28
        self.assertEquals(len(baz.require_group), 1)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    29
        self.assertEquals(baz.require_group[0].name, 'managers')
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    30
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    31
    def test_duplicated_state(self):
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    32
        wf = add_wf(self, 'Company')
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    33
        wf.add_state(u'foo', initial=True)
2979
94277323239d test two different wfs may have a state of the same name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2977
diff changeset
    34
        self.commit()
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    35
        wf.add_state(u'foo')
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    36
        ex = self.assertRaises(ValidationError, self.commit)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    37
        # XXX enhance message
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    38
        self.assertEquals(ex.errors, {'state_of': 'unique constraint S name N, Y state_of O, Y name N failed'})
2979
94277323239d test two different wfs may have a state of the same name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2977
diff changeset
    39
        # no pb if not in the same workflow
94277323239d test two different wfs may have a state of the same name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2977
diff changeset
    40
        wf2 = add_wf(self, 'Company')
94277323239d test two different wfs may have a state of the same name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2977
diff changeset
    41
        foo = wf2.add_state(u'foo', initial=True)
94277323239d test two different wfs may have a state of the same name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2977
diff changeset
    42
        self.commit()
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    43
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    44
    def test_duplicated_transition(self):
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    45
        wf = add_wf(self, 'Company')
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    46
        foo = wf.add_state(u'foo', initial=True)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    47
        bar = wf.add_state(u'bar')
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    48
        wf.add_transition(u'baz', (foo,), bar, ('managers',))
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    49
        wf.add_transition(u'baz', (bar,), foo)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    50
        ex = self.assertRaises(ValidationError, self.commit)
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    51
        # XXX enhance message
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    52
        self.assertEquals(ex.errors, {'transition_of': 'unique constraint S name N, Y transition_of O, Y name N failed'})
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    53
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    54
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
class WorkflowTC(EnvBasedTC):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
    def setup_database(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
        rschema = self.schema['in_state']
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
        for x, y in rschema.iter_rdefs():
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
            self.assertEquals(rschema.rproperty(x, y, 'cardinality'), '1*')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
        self.member = self.create_user('member')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
    def test_workflow_base(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
        e = self.create_user('toto')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
        self.assertEquals(e.state, 'activated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
        e.change_state('deactivated', u'deactivate 1')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
        self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
        e.change_state('activated', u'activate 1')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
        self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
        e.change_state('deactivated', u'deactivate 2')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
        self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
        e.clear_related_cache('wf_info_for', 'object')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
        self.assertEquals([tr.comment for tr in e.reverse_wf_info_for],
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
                          ['deactivate 1', 'activate 1', 'deactivate 2'])
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
        self.assertEquals(e.latest_trinfo().comment, 'deactivate 2')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
    def test_possible_transitions(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
        user = self.entity('CWUser X')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
        trs = list(user.possible_transitions())
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
        self.assertEquals(len(trs), 1)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
        self.assertEquals(trs[0].name, u'deactivate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
        self.assertEquals(trs[0].destination().name, u'deactivated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
        # test a std user get no possible transition
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
        cnx = self.login('member')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
        # fetch the entity using the new session
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
        trs = list(cnx.user().possible_transitions())
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
        self.assertEquals(len(trs), 0)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
    def _test_manager_deactivate(self, user):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
        user.clear_related_cache('in_state', 'subject')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
        self.assertEquals(len(user.in_state), 1)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
        self.assertEquals(user.state, 'deactivated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
        trinfo = user.latest_trinfo()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
        self.assertEquals(trinfo.previous_state.name, 'activated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
        self.assertEquals(trinfo.new_state.name, 'deactivated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
        self.assertEquals(trinfo.comment, 'deactivate user')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    97
        self.assertEquals(trinfo.comment_format, 'text/plain')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    98
        return trinfo
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
    def test_change_state(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
        user = self.user()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
        user.change_state('deactivated', comment=u'deactivate user')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
        trinfo = self._test_manager_deactivate(user)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
        self.assertEquals(trinfo.transition, None)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   106
    def test_set_in_state_bad_wf(self):
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   107
        wf = add_wf(self, 'CWUser')
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   108
        s = wf.add_state(u'foo', initial=True)
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   109
        self.commit()
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   110
        ex = self.assertRaises(ValidationError, self.session().unsafe_execute,
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   111
                               'SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   112
                               {'x': self.user().eid, 's': s.eid}, 'x')
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   113
        self.assertEquals(ex.errors, {'in_state': "state doesn't belong to entity's workflow. "
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   114
                                      "You may want to set a custom workflow for this entity first."})
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   115
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
    def test_fire_transition(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
        user = self.user()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
        user.fire_transition('deactivate', comment=u'deactivate user')
2982
1768dbe8e9b3 [wf] need to clear cache here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2979
diff changeset
   119
        user.clear_all_caches()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
        self.assertEquals(user.state, 'deactivated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   121
        self._test_manager_deactivate(user)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   122
        trinfo = self._test_manager_deactivate(user)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   123
        self.assertEquals(trinfo.transition.name, 'deactivate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
    # XXX test managers can change state without matching transition
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   126
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
    def _test_stduser_deactivate(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
        ueid = self.member.eid
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   129
        self.create_user('tutu')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
        cnx = self.login('tutu')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
        req = self.request()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
        member = req.entity_from_eid(self.member.eid)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   133
        ex = self.assertRaises(ValidationError,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
                               member.fire_transition, 'deactivate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   135
        self.assertEquals(ex.errors, {'by_transition': "transition may not be fired"})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   136
        cnx.close()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
        cnx = self.login('member')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   138
        req = self.request()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   139
        member = req.entity_from_eid(self.member.eid)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   140
        member.fire_transition('deactivate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
        cnx.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
        ex = self.assertRaises(ValidationError,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
                               member.fire_transition, 'activate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
        self.assertEquals(ex.errors, {'by_transition': "transition may not be fired"})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
    def test_fire_transition_owned_by(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   147
        self.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
                     'X expression "X owned_by U", T condition X '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
                     'WHERE T name "deactivate"')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
        self._test_stduser_deactivate()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   152
    def test_fire_transition_has_update_perm(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
        self.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   154
                     'X expression "U has_update_permission X", T condition X '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
                     'WHERE T name "deactivate"')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   156
        self._test_stduser_deactivate()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
2954
48507919b6e3 [wf] oops, BaseTransition.__init__ may be called for subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   159
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   160
class CustomWorkflowTC(EnvBasedTC):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   161
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   162
    def setup_database(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   163
        self.member = self.create_user('member')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   164
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   165
    def tearDown(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   166
        super(CustomWorkflowTC, self).tearDown()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   167
        self.execute('DELETE X custom_workflow WF')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   168
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   169
    def test_custom_wf_replace_state_no_history(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   170
        """member in inital state with no previous history, state is simply
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   171
        redirected when changing workflow
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   172
        """
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   173
        wf = add_wf(self, 'CWUser')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   174
        wf.add_state('asleep', initial=True)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   175
        self.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   176
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   177
        self.member.clear_all_caches()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   178
        self.assertEquals(self.member.state, 'activated')# no change before commit
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   179
        self.commit()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   180
        self.member.clear_all_caches()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   181
        self.assertEquals(self.member.current_workflow.eid, wf.eid)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   182
        self.assertEquals(self.member.state, 'asleep')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   183
        self.assertEquals(self.member.workflow_history, [])
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   184
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   185
    def test_custom_wf_replace_state_keep_history(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   186
        """member in inital state with some history, state is redirected and
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   187
        state change is recorded to history
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   188
        """
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   189
        self.member.fire_transition('deactivate')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   190
        self.member.fire_transition('activate')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   191
        wf = add_wf(self, 'CWUser')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   192
        wf.add_state('asleep', initial=True)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   193
        self.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   194
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   195
        self.commit()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   196
        self.member.clear_all_caches()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   197
        self.assertEquals(self.member.current_workflow.eid, wf.eid)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   198
        self.assertEquals(self.member.state, 'asleep')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   199
        self.assertEquals(parse_hist(self.member.workflow_history),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   200
                          [('activated', 'deactivated', 'deactivate', None),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   201
                           ('deactivated', 'activated', 'activate', None),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   202
                           ('activated', 'asleep', None, 'workflow changed to "CWUser"')])
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   203
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   204
    def test_custom_wf_shared_state(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   205
        """member in some state shared by the new workflow, nothing has to be
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   206
        done
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   207
        """
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   208
        self.member.fire_transition('deactivate')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   209
        self.assertEquals(self.member.state, 'deactivated')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   210
        wf = add_wf(self, 'CWUser')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   211
        wf.add_state('asleep', initial=True)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   212
        self.execute('SET S state_of WF WHERE S name "deactivated", WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   213
                     {'wf': wf.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   214
        self.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   215
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   216
        self.commit()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   217
        self.member.clear_all_caches()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   218
        self.assertEquals(self.member.current_workflow.eid, wf.eid)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   219
        self.assertEquals(self.member.state, 'deactivated')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   220
        self.assertEquals(parse_hist(self.member.workflow_history),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   221
                          [('activated', 'deactivated', 'deactivate', None)])
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   222
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   223
    def test_custom_wf_no_initial_state(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   224
        """try to set a custom workflow which has no initial state"""
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   225
        self.member.fire_transition('deactivate')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   226
        wf = add_wf(self, 'CWUser')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   227
        wf.add_state('asleep')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   228
        self.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   229
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   230
        ex = self.assertRaises(ValidationError, self.commit)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   231
        self.assertEquals(ex.errors, {'custom_workflow': u'workflow has no initial state'})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   232
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   233
    def test_custom_wf_bad_etype(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   234
        """try to set a custom workflow which has no initial state"""
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   235
        self.member.fire_transition('deactivate')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   236
        wf = add_wf(self, 'Company')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   237
        wf.add_state('asleep', initial=True)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   238
        self.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   239
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   240
        ex = self.assertRaises(ValidationError, self.commit)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   241
        self.assertEquals(ex.errors, {'custom_workflow': 'constraint S is ET, O workflow_of ET failed'})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   242
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   243
    def test_del_custom_wf(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   244
        """member in some state shared by the new workflow, nothing has to be
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   245
        done
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   246
        """
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   247
        self.member.fire_transition('deactivate')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   248
        wf = add_wf(self, 'CWUser')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   249
        wf.add_state('asleep', initial=True)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   250
        self.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   251
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   252
        self.commit()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   253
        self.execute('DELETE X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   254
                     {'wf': wf.eid, 'x': self.member.eid})
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   255
        self.member.clear_all_caches()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   256
        self.assertEquals(self.member.state, 'asleep')# no change before commit
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   257
        self.commit()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   258
        self.member.clear_all_caches()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   259
        self.assertEquals(self.member.current_workflow.name, "CWUser workflow")
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   260
        self.assertEquals(self.member.state, 'activated')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   261
        self.assertEquals(parse_hist(self.member.workflow_history),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   262
                          [('activated', 'deactivated', 'deactivate', None),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   263
                           ('deactivated', 'asleep', None, 'workflow changed to "CWUser"'),
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   264
                           ('asleep', 'activated', None, 'workflow changed to "CWUser workflow"'),])
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   265
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   266
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   267
from cubicweb.devtools.apptest import RepositoryBasedTC
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   268
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   269
class WorkflowHooksTC(RepositoryBasedTC):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   270
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   271
    def setUp(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   272
        RepositoryBasedTC.setUp(self)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   273
        self.wf = self.session.user.current_workflow
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   274
        self.s_activated = self.wf.state_by_name('activated').eid
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   275
        self.s_deactivated = self.wf.state_by_name('deactivated').eid
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   276
        self.s_dummy = self.wf.add_state(u'dummy').eid
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   277
        self.wf.add_transition(u'dummy', (self.s_deactivated,), self.s_dummy)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   278
        ueid = self.create_user('stduser', commit=False)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   279
        # test initial state is set
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   280
        rset = self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s',
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   281
                            {'x' : ueid})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   282
        self.failIf(rset, rset.rows)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   283
        self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   284
        initialstate = self.execute('Any N WHERE S name N, X in_state S, X eid %(x)s',
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   285
                                    {'x' : ueid})[0][0]
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   286
        self.assertEquals(initialstate, u'activated')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   287
        # give access to users group on the user's wf transitions
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   288
        # so we can test wf enforcing on euser (managers don't have anymore this
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   289
        # enforcement
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   290
        self.execute('SET X require_group G '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   291
                     'WHERE G name "users", X transition_of WF, WF eid %(wf)s',
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   292
                     {'wf': self.wf.eid})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   293
        self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   294
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   295
    def tearDown(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   296
        self.execute('DELETE X require_group G '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   297
                     'WHERE G name "users", X transition_of WF, WF eid %(wf)s',
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   298
                     {'wf': self.wf.eid})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   299
        self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   300
        RepositoryBasedTC.tearDown(self)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   301
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   302
    # XXX currently, we've to rely on hooks to set initial state, or to use unsafe_execute
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   303
    # def test_initial_state(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   304
    #     cnx = self.login('stduser')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   305
    #     cu = cnx.cursor()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   306
    #     self.assertRaises(ValidationError, cu.execute,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   307
    #                       'INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   308
    #                       'X in_state S WHERE S name "deactivated"', {'pwd': 'oops'})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   309
    #     cnx.close()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   310
    #     # though managers can do whatever he want
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   311
    #     self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   312
    #                  'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   313
    #     self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   314
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   315
    # test that the workflow is correctly enforced
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   316
    def test_transition_checking1(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   317
        cnx = self.login('stduser')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   318
        user = cnx.user(self.current_session())
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   319
        ex = self.assertRaises(ValidationError,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   320
                               user.fire_transition, 'activate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   321
        self.assertEquals(ex.errors, {'by_transition': u"transition isn't allowed"})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   322
        cnx.close()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   323
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   324
    def test_transition_checking2(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   325
        cnx = self.login('stduser')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   326
        user = cnx.user(self.current_session())
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   327
        assert user.state == 'activated'
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   328
        ex = self.assertRaises(ValidationError,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   329
                               user.fire_transition, 'dummy')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   330
        self.assertEquals(ex.errors, {'by_transition': u"transition isn't allowed"})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   331
        cnx.close()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   332
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   333
    def test_transition_checking3(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   334
        cnx = self.login('stduser')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   335
        session = self.current_session()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   336
        user = cnx.user(session)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   337
        user.fire_transition('deactivate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   338
        cnx.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   339
        session.set_pool()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   340
        ex = self.assertRaises(ValidationError,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   341
                               user.fire_transition, 'deactivate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   342
        self.assertEquals(ex.errors, {'by_transition': u"transition isn't allowed"})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   343
        # get back now
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   344
        user.fire_transition('activate')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   345
        cnx.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   346
        cnx.close()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   347
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   348
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   349
if __name__ == '__main__':
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   350
    from logilab.common.testlib import unittest_main
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   351
    unittest_main()