testWorklist.py 12 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 46 47 48 49 50
##############################################################################
#
# 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'
  checked_workflow = 'validation_workflow'
  worklist_assignor_id = 'assignor_worklist'
  actbox_assignor_name = 'assignor_todo'
  worklist_owner_id = 'owner_worklist'
  actbox_owner_name = 'owner_todo'
51 52
  worklist_assignor_owner_id = 'assignor_owner_worklist'
  actbox_assignor_owner_name = 'assignor_owner_todo'
53 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 102 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 132

  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',
      )
      assignment.openAction()
    # 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)
    return module.newContent(portal_type=self.checked_portal_type)

133 134 135 136 137 138
  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])

139 140 141 142 143 144
  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'),
145 146
          (self.worklist_owner_id, self.actbox_owner_name, 'Owner'),
          (self.worklist_assignor_owner_id, self.actbox_assignor_owner_name, 'Assignor; Owner')]:
147 148
      worklists.addWorklist(worklist_id)
      worklist_definition = worklists._getOb(worklist_id)
149 150
      worklist_definition.setProperties('',
          actbox_name='%s (%%(count)s)' % (actbox_name, ),
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
          props={'guard_roles': role,
                 'var_match_portal_type': self.checked_portal_type})

  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()
    self.portal.portal_caches.clearAllCache()

    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)
183 184
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
185 186
      self.assertEquals(len(entry_list), 0)
      self.logMessage("Check %s worklist as Owner" % user_id)
187 188
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
189
      self.assertEquals(len(entry_list), 1)
190 191
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
192 193 194 195 196 197 198 199 200 201 202 203
      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'])
204 205
    self.logMessage("Give manager Assignor role")
    document.manage_addLocalRoles('manager', ['Assignor'])
206 207 208 209 210 211 212 213 214 215
    document.reindexObject()
    get_transaction().commit()
    self.tic()
    self.portal.portal_caches.clearAllCache()
    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)
216 217
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
218 219 220
      self.assertEquals(len(entry_list), 1)
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
221
      self.logMessage("Check %s worklist as Owner" % user_id)
222 223
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
224
      self.assertEquals(len(entry_list), 1)
225 226 227 228 229 230
      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)
231 232
      self.assertEquals(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
233 234 235 236 237
      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)
238 239
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
240 241
      self.assertEquals(len(entry_list), 0)
      self.logMessage("Check %s worklist as Owner" % user_id)
242 243
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
244 245 246 247 248 249
      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)
250 251
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
252
      self.assertEquals(len(entry_list), 1)
253 254
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
255
      self.logMessage("Check %s worklist as Owner" % user_id)
256 257
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
      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()
    self.portal.portal_caches.clearAllCache()
    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)
278 279
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
280 281 282
      self.assertEquals(len(entry_list), 1)
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
283
      self.logMessage("Check %s worklist as Owner" % user_id)
284 285
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
286
      self.assertEquals(len(entry_list), 1)
287 288
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
289 290 291 292 293
      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)
294 295
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
296 297
      self.assertEquals(len(entry_list), 0)
      self.logMessage("Check %s worklist as Owner" % user_id)
298 299
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
300 301 302 303 304 305
      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)
306 307
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_assignor_name)]
308
      self.assertEquals(len(entry_list), 1)
309 310
      self.assertTrue(
        self.getWorklistDocumentCountFromActionName(entry_list[0]['name']), 1)
311
      self.logMessage("Check %s worklist as Owner" % user_id)
312 313
      entry_list = [x for x in result \
                    if x['name'].startswith(self.actbox_owner_name)]
314 315 316 317 318 319 320
      self.assertEquals(len(entry_list), 0)
      self.logout()

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