testWorklist.py 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
##############################################################################
#
# Copyright (c) 2007 Nexedi SA and Contributors. 
# All Rights Reserved.
#          Romain Courteaud <romain@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.Sequence import SequenceList
from zExceptions import BadRequest
from Products.ERP5Type.Tool.ClassTool import _aq_reset
from Testing.ZopeTestCase.PortalTestCase import PortalTestCase

class TestWorklist(ERP5TypeTestCase):

  run_all_test = 1
  quiet = 1
  login = PortalTestCase.login

  checked_portal_type = 'Organisation'
46
  checked_validation_state = 'draft'
47 48 49 50 51
  checked_workflow = 'validation_workflow'
  worklist_assignor_id = 'assignor_worklist'
  actbox_assignor_name = 'assignor_todo'
  worklist_owner_id = 'owner_worklist'
  actbox_owner_name = 'owner_todo'
52 53
  worklist_assignor_owner_id = 'assignor_owner_worklist'
  actbox_assignor_owner_name = 'assignor_owner_todo'
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  def getTitle(self):
    return "Worklist"

  def getBusinessTemplateList(self):
    """
    Return list of bt5 to install
    """
    return ('erp5_base',)

  def getUserFolder(self):
    """
    Return the user folder
    """
    return getattr(self.getPortal(), 'acl_users', None)

  def createManagerAndLogin(self):
    """
    Create a simple user in user_folder with manager rights.
    This user will be used to initialize data in the method afterSetup
    """
    self.getUserFolder()._doAddUser('manager', '', ['Manager'], [])
    self.login('manager')

  def createERP5Users(self, user_dict):
    """
    Create all ERP5 users needed for the test.
    ERP5 user = Person object + Assignment object in erp5 person_module.
    """
    portal = self.getPortal()
    module = portal.getDefaultModule("Person")
    # Create the Person.
    for user_login, user_data in user_dict.items():
      # Create the Person.
      self.logMessage("Create user: %s" % user_login)
      person = module.newContent(
        portal_type='Person', 
        reference=user_login, 
        password='hackme',
      )
      # Create the Assignment.
      assignment = person.newContent( 
        portal_type = 'Assignment',
        group = "%s" % user_data[0],
        function = "%s" % user_data[1],
        start_date = '01/01/1900',
        stop_date = '01/01/2900',
      )
102
      assignment.open()
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    # Reindexing is required for the security to work
    get_transaction().commit()
    self.tic()

  def createUsers(self):
    """
    Create all users needed for the test
    """
    self.createERP5Users(self.getUserDict())

  def getUserDict(self):
    """
    Return dict of users needed for the test
    """
    user_dict = {
      'foo': [None, None],
      'bar': [None, None],
    }
    return user_dict

  def stepLoginAsFoo(self, sequence=None, sequence_list=None, **kw):
    self.login("foo")

  def stepLoginAsBar(self, sequence=None,
                       sequence_list=None, **kw):
    self.login("bar")

  def createDocument(self):
    module = self.getPortal().getDefaultModule(self.checked_portal_type)
132 133 134
    result = module.newContent(portal_type=self.checked_portal_type)
    assert result.getValidationState() == self.checked_validation_state
    return result
135

136 137 138 139 140 141
  def getWorklistDocumentCountFromActionName(self, action_name):
    self.assertEquals(action_name[-1], ')')
    left_parenthesis_offset = action_name.rfind('(')
    self.assertNotEquals(left_parenthesis_offset, -1)
    return int(action_name[left_parenthesis_offset + 1:-1])

142 143 144 145 146 147
  def createWorklist(self):
    workflow = self.getWorkflowTool()[self.checked_workflow]
    worklists = workflow.worklists

    for worklist_id, actbox_name, role in [
          (self.worklist_assignor_id, self.actbox_assignor_name, 'Assignor'),
148 149
          (self.worklist_owner_id, self.actbox_owner_name, 'Owner'),
          (self.worklist_assignor_owner_id, self.actbox_assignor_owner_name, 'Assignor; Owner')]:
150 151
      worklists.addWorklist(worklist_id)
      worklist_definition = worklists._getOb(worklist_id)
152 153
      worklist_definition.setProperties('',
          actbox_name='%s (%%(count)s)' % (actbox_name, ),
154
          props={'guard_roles': role,
155 156
                 'var_match_portal_type': self.checked_portal_type,
                 'var_match_validation_state': self.checked_validation_state})
157

158 159 160
  def clearCache(self):
    self.portal.portal_caches.clearAllCache()

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  def test_01_worklist(self, quiet=0, run=run_all_test):
    """
    Test the permission of the building module.
    """
    if not run: 
      return

    workflow_tool = self.portal.portal_workflow

    self.logMessage("Create Manager")
    self.createManagerAndLogin()
    self.createUsers()
    self.logMessage("Create worklist")
    self.createWorklist()
    self.logMessage("Create document as Manager")
    document = self.createDocument()

    get_transaction().commit()
    self.tic()
180
    self.clearCache()
181 182 183 184 185 186 187 188 189

    result = workflow_tool.listActions(object=document)
    self.logout()

    # Users can not see worklist as they are not Assignor
    for user_id in ('manager', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
190 191
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
192 193
      self.assertEquals(len(entry_list), 0)
      self.logMessage("Check %s worklist as Owner" % user_id)
194 195
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
196
      self.assertEquals(len(entry_list), 1)
197 198
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
199 200 201 202 203 204 205 206 207 208 209 210
      self.logout()
    for user_id in ('foo', 'bar'):
      self.logMessage("Check %s worklist" % user_id)
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.assertEquals(result, [])
      self.logout()

    # Define foo as Assignor
    self.login('manager')
    self.logMessage("Give foo Assignor role")
    document.manage_addLocalRoles('foo', ['Assignor'])
211 212
    self.logMessage("Give manager Assignor role")
    document.manage_addLocalRoles('manager', ['Assignor'])
213 214 215
    document.reindexObject()
    get_transaction().commit()
    self.tic()
216
    self.clearCache()
217 218 219 220 221 222
    self.logout()

    for user_id in ('manager', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
223 224
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
225 226 227
      self.assertEquals(len(entry_list), 1)
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
228
      self.logMessage("Check %s worklist as Owner" % user_id)
229 230
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
231
      self.assertEquals(len(entry_list), 1)
232 233 234 235 236 237
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
      self.logMessage("Check %s worklist as Owner and Assignor" % user_id)
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_owner_name)]
      self.assertEquals(len(entry_list), 1)
238 239
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
240 241 242 243 244
      self.logout()
    for user_id in ('bar', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
245 246
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
247 248
      self.assertEquals(len(entry_list), 0)
      self.logMessage("Check %s worklist as Owner" % user_id)
249 250
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
251 252 253 254 255 256
      self.assertEquals(len(entry_list), 0)
      self.logout()
    for user_id in ('foo', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
257 258
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
259
      self.assertEquals(len(entry_list), 1)
260 261
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
262
      self.logMessage("Check %s worklist as Owner" % user_id)
263 264
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
265 266 267 268 269 270 271 272 273 274 275 276
      self.assertEquals(len(entry_list), 0)
      self.logout()

    # Define foo and bar as Assignee
    self.login('manager')
    self.logMessage("Give foo Assignee role")
    document.manage_addLocalRoles('foo', ['Assignee'])
    self.logMessage("Give bar Assignee role")
    document.manage_addLocalRoles('bar', ['Assignee'])
    document.reindexObject()
    get_transaction().commit()
    self.tic()
277
    self.clearCache()
278 279 280 281 282 283 284
    self.logout()

    # Users can not see worklist as they are not Assignor
    for user_id in ('manager', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
285 286
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
287 288 289
      self.assertEquals(len(entry_list), 1)
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
290
      self.logMessage("Check %s worklist as Owner" % user_id)
291 292
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
293
      self.assertEquals(len(entry_list), 1)
294 295
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
296 297 298 299 300
      self.logout()
    for user_id in ('bar', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
301 302
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
303 304
      self.assertEquals(len(entry_list), 0)
      self.logMessage("Check %s worklist as Owner" % user_id)
305 306
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
307 308 309 310 311 312
      self.assertEquals(len(entry_list), 0)
      self.logout()
    for user_id in ('foo', ):
      self.login(user_id)
      result = workflow_tool.listActions(object=document)
      self.logMessage("Check %s worklist as Assignor" % user_id)
313 314
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
315
      self.assertEquals(len(entry_list), 1)
316 317
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
318
      self.logMessage("Check %s worklist as Owner" % user_id)
319 320
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
321 322 323 324 325 326 327
      self.assertEquals(len(entry_list), 0)
      self.logout()

def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestWorklist))
  return suite