testBug.py 16.9 KB
Newer Older
1 2
##############################################################################
#
3 4 5
# Copyright (c) 2007-2008 Nexedi SA and Contributors. All Rights Reserved.
#                       Kevin Deldycke <kevin_AT_nexedi_DOT_com>
#                       Rafael Monnerat <rafael@nexedi.com>
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
#
# 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.
#
##############################################################################


31
import unittest
32 33 34
from DateTime import DateTime
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import SequenceList
35
from Products.ERP5Type.tests.utils import DummyMailHost
36 37 38
from AccessControl.SecurityManagement import newSecurityManager


39
class TestBug(ERP5TypeTestCase):
40
  """
41
    ERP5 unit tests for Bug module (part of erp5_forge business template).
42 43 44 45
  """
  # pseudo constants
  RUN_ALL_TEST = 1
  QUIET = 1
46 47 48
  person_portal_type = "Person" 
  project_portal_type = "Project"
  organisation_portal_type  = "Organisation"
49 50 51 52 53

  def getTitle(self):
    """
      Return the title of the current test set.
    """
54
    return "Bug"
55 56 57 58 59

  def getBusinessTemplateList(self):
    """
      Return the list of required business templates.
    """
60 61
    return ( 'erp5_base'
           , 'erp5_forge'
62 63 64 65
           , 'erp5_base'
           , 'erp5_pdm'
           , 'erp5_trade'
           , 'erp5_project'
66
           )
67 68 69 70 71 72

  def afterSetUp(self, quiet=QUIET, run=RUN_ALL_TEST):
    """
      Initialize the ERP5 site.
    """
    self.login()
73 74
    self.datetime = DateTime() 
    self.portal = self.getPortal()
Kevin Deldycke's avatar
Kevin Deldycke committed
75
    self.workflow_tool = self.portal.portal_workflow
76 77 78 79 80
    # Use a dummy mailhost to not send mail notification to the guy how run unit test
    if 'MailHost' in self.portal.objectIds():
      self.portal.manage_delObjects(['MailHost'])
      self.portal._setObject('MailHost', DummyMailHost('MailHost'))

81 82 83
  ##################################
  ##  Usefull methods
  ##################################
84 85 86 87 88 89 90 91 92 93
#  def login(self, quiet=QUIET, run=RUN_ALL_TEST):
#    """
#      Create a new manager user and login.
#    """
#    user_name = 'kevin'
#    user_folder = self.getPortal().acl_users
#    user_folder._doAddUser(user_name, '', ['Manager', 'Owner', 'Assignor'], [])
#    user = user_folder.getUserById(user_name).__of__(user_folder)
#    newSecurityManager(None, user)
#
94 95 96
  ##################################
  ##  Basic steps
  ##################################
97
  def stepTic(self, **kw):
98
    """
99
      Flush activity queue.
100
    """
101
    self.tic()
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  def stepLoginUsualUser(self, **kw):
    portal = self.getPortal()
    uf = portal.acl_users
    if not uf.getUser('dummy'):
      uf._doAddUser('manager', '', ['Manager'], [])
      self.login('manager')
      person_module = portal.getDefaultModule(self.person_portal_type)
      person = person_module.newContent(id='dummy', title='dummy',
                                        reference='dummy')
      portal.portal_categories.group.newContent(id='dummy',
                                                codification='DUMMY')
      
      person.setEmailText('loggedperson@localhost')
      assignment = person.newContent(title='dummy', group='dummy',
                                     portal_type='Assignment',
                                     start_date='1980-01-01',
                                     stop_date='2099-12-31')
      assignment.open()
      get_transaction().commit()
      self.tic()
      module_list = []
      portal_type_list = []
      for portal_type in (self.project_portal_type,
                          self.organisation_portal_type,):
        module = portal.getDefaultModule(portal_type)
        module_list.append(module)
        portal_type_list.append(portal_type)
        portal_type_list.append(module.getPortalType())

      for portal_type in portal_type_list:
        ti = portal.portal_types[portal_type]
        ti.addRole('Auditor;Author;Assignee;Assignor,Manager', '', 'Dummy',
                   '', 'group/dummy', 'ERP5Type_getSecurityCategoryFromAssignment',
                   '')
        ti.updateRoleMapping()

      get_transaction().commit()
      self.tic()
      portal.portal_caches.clearAllCache()

    self.login('dummy')

  def stepCreateProject(self,sequence=None, sequence_list=None, \
                        **kw):
    """
    Create a project
    """
    project_portal_type = "Project"
    portal = self.getPortal()
    module = portal.getDefaultModule(project_portal_type)
    project = module.newContent(
        portal_type=project_portal_type,
        title = 'Project')
    sequence.edit(project=project) 

  def createPerson(self):
    """
      Create a person.
    """
    portal_type = 'Person'
    person_module = self.portal.getDefaultModule(portal_type)
    person = person_module.newContent(portal_type = portal_type )
    return person

  def stepCreatePerson1(self, sequence=None, sequence_list=None, **kw):
    """
      Create one Person
    """
    person = self.createPerson()
    project = sequence.get("project")
    self.createPersonAssignment(person=person, project=project)
    person.setDefaultEmailText("person1@localhost")
    sequence.edit(person1 = person)

  def stepCreatePerson2(self, sequence=None, sequence_list=None, **kw):
    """
      Create Person 2
    """
    person = self.createPerson()
    project = sequence.get("project")
    person.setDefaultEmailText("person2@localhost")
    sequence.edit(person2 = person)

  def createPersonAssignment(self,person=None,project=None ):
    """
      Create a person Assigment and Assign to a Project.
    """
    # Create Assignment
    assignment = person.newContent(portal_type='Assignment')
    assignment.setDestinationProjectValue(project)
    assignment.setStartDate(DateTime()-365)
    assignment.setStopDate(DateTime()+365)
    self.portal.portal_workflow.doActionFor(assignment, 'open_action')

  def stepCheckBugNotification(self, sequence=None,
                                         sequence_list=None, **kw):
    """
    Check that notification works
    """
    bug = sequence.get('bug')
    last_message = self.portal.MailHost._last_message
    self.assertNotEquals((), last_message)
    mfrom, mto, messageText = last_message
    self.assertEquals('postmaster@localhost', mfrom)
    self.assertEquals(['person1@localhost'], mto)
208
    self.failUnless(bug.getTitle().replace(" ", "_") in messageText)
209 210 211 212 213 214

  def stepCheckBugMessageNotification(self, sequence=None,
                                         sequence_list=None, **kw):
    """
    Check that notification works
    """
215
    bug = sequence.get('bug')
216 217 218 219 220
    last_message = self.portal.MailHost._last_message
    self.assertNotEquals((), last_message)
    mfrom, mto, messageText = last_message
    self.assertEquals('person2@localhost', mfrom)
    self.assertEquals(['person1@localhost'], mto)
221
    self.failUnless(bug.getTitle().replace(" ", "_") in messageText)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

  def stepSetSourceProject(self, sequence=None, sequence_list=None, **kw):
    """
      Set Source Project to a Bug
    """
    bug = sequence.get('bug')
    project = sequence.get('project')
    bug.setSourceProjectValue(project)

  def stepSetRequester(self, sequence=None, sequence_list=None, **kw):
    """
      Set Source Project to a Bug
    """
    bug = sequence.get('bug')
    person2 = sequence.get('person2')
    bug.setDestinationValue(person2)
238

239
  def stepCreateBug(self, sequence=None, sequence_list=None, **kw):
240
    """
241
      Create a dummy bug
242
    """
243 244
    portal_type = 'Bug'
    bug_module = self.portal.getDefaultModule(portal_type)
245 246 247 248 249
    bug = bug_module.newContent( portal_type = portal_type
                               , title  = 'Bug Title'
                               , description = 'Bug Description'
                               , start_date = self.datetime
                               , stop_date = self.datetime
250 251 252
                               )
    sequence.edit(bug = bug)

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  def stepCreateBugMessage(self, sequence=None, sequence_list=None, **kw):
    """
      Create a dummy Bug Message
    """
    portal_type = 'Bug Line'
    bug = sequence.get('bug')
    person2 = sequence.get('person2')
    bug_message = bug.newContent( portal_type = portal_type
                                  , title  = 'Bug Message'
                                  , text_content = 'Bug Description'
                                )
    # usually the person who is creates the message is setted as source
    # by BugLine_init
    bug_message.setSourceValue(person2)
    sequence.edit(bug_message = bug_message)

  def stepPostBugMessage(self, sequence=None, sequence_list=None, **kw):
    """
      Post the bug message.
    """
    bug_message = sequence.get('bug_message')
    self.workflow_tool.doActionFor(bug_message, 'start_action')

Rafael Monnerat's avatar
Rafael Monnerat committed
276
  def stepCheckBugMessageIsDelivered(self, sequence=None, \
277 278
                                                     sequence_list=None, **kw):
    """
Rafael Monnerat's avatar
Rafael Monnerat committed
279
      check if the message is delivered the bug.
280 281
    """
    bug_message = sequence.get('bug_message')
Rafael Monnerat's avatar
Rafael Monnerat committed
282
    self.assertEquals(bug_message.getSimulationState(), 'delivered') 
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

  def stepCheckBugMessage(self, sequence=None, sequence_list=None, **kw):
    """
      Check a dummy bug message
    """
    bug_message = sequence.get('bug_message')
    person = sequence.get('person1')
    self.assertEquals( [ person ] , bug_message.getDestinationValueList())
    self.failUnless( bug_message.getStartDate() is not None)
    #self.assertEquals(bug_message.getSourceValue().getTitle(), 'dummy')

  def stepCheckBugInit(self, sequence=None, sequence_list=None, **kw):
    """
      Create a dummy bug
    """
    bug = sequence.get('bug')
299
    self.assertEquals("#%s" % bug.getId(), bug.getReference())
300
    #self.assertEquals(bug_message.getSourceTradeValue().getTitle(), 'dummy')
301 302

  def stepOpenBug(self, sequence=None, sequence_list=None, **kw):
303
    """
304
      Open the bug.
305
    """
306
    bug = sequence.get('bug')
Nicolas Delaby's avatar
Nicolas Delaby committed
307
    self.workflow_tool.doActionFor(bug, 'confirm_action', send_event=1)
308
    self.assertEquals(bug.getSimulationState(), 'confirmed')
309

310
  def stepAssignBug(self, sequence=None, sequence_list=None, **kw):
311
    """
312
      Close the bug.
313
    """
314
    bug = sequence.get('bug')
Nicolas Delaby's avatar
Nicolas Delaby committed
315
    self.workflow_tool.doActionFor(bug, 'set_ready_action', send_event=1)
316
    self.assertEquals(bug.getSimulationState(), 'ready')
317

318
  def stepResolveBug(self, sequence=None, sequence_list=None, **kw):
319
    """
320
      Close the bug.
321
    """
322
    bug = sequence.get('bug')
Nicolas Delaby's avatar
Nicolas Delaby committed
323
    self.workflow_tool.doActionFor(bug, 'stop_action', send_event=1)
324
    self.assertEquals(bug.getSimulationState(), 'stopped')
325

Nicolas Delaby's avatar
Nicolas Delaby committed
326 327
  def stepReAssignBug(self, sequence=None, sequence_list=None, **kw):
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
328
      Re Assign the bug.
Nicolas Delaby's avatar
Nicolas Delaby committed
329 330 331 332
    """
    bug = sequence.get('bug')
    self.workflow_tool.doActionFor(bug, 're_assign_action', send_event=1)
    self.assertEquals(bug.getSimulationState(), 'ready')
333

334
  def stepCloseBug(self, sequence=None, sequence_list=None, **kw):
335
    """
336
      Close the bug.
337
    """
338
    bug = sequence.get('bug')
Nicolas Delaby's avatar
Nicolas Delaby committed
339
    self.workflow_tool.doActionFor(bug, 'deliver_action', send_event=1)
Rafael Monnerat's avatar
Rafael Monnerat committed
340
    self.assertEquals(bug.getSimulationState(), 'delivered')
341

342 343 344 345 346
  def stepCancelBug(self, sequence=None, sequence_list=None, **kw):
    """
      Cancel the bug.
    """
    bug = sequence.get('bug')
Nicolas Delaby's avatar
Nicolas Delaby committed
347
    self.workflow_tool.doActionFor(bug, 'cancel_action', send_event=1)
348
    self.assertEquals(bug.getSimulationState(), 'cancelled')
349 350

  def stepSetTestedBug(self, sequence=None, sequence_list=None, **kw):
351
    """
352
      Set the bug as unit tested.
353
    """
354 355 356
    bug = sequence.get('bug')
    bug.setTested(True)
    self.assertEquals(bug.getTested(), True)
357

358
  def stepSetOldClosedDate(self, sequence=None, sequence_list=None, **kw):
359
    """
360
      Change Closed Date to a funky old value.
361
    """
362 363
    bug = sequence.get('bug')
    bug.setStopDate(self.datetime - 10)
364
    self.assertEquals(bug.getStopDate().Date(), (self.datetime - 10).Date()) # Check that datetime is fixed
365

366
  def stepCheckClosedDate(self, sequence=None, sequence_list=None, **kw):
367
    """
368
      Check that the closed date is set as today.
369
    """
370
    bug = sequence.get('bug')
371
    self.assertEquals(bug.getStopDate().Date(), self.datetime.Date())
372

373 374 375 376
  ##################################
  ##  Tests
  ##################################
  def test_01_StopDateUpdatedOnClose(self, quiet=QUIET, run=RUN_ALL_TEST):
377
    """
378
      Test that a closed bug has its stop date property updated.
379 380 381
    """
    if not run: return
    sequence_list = SequenceList()
382
    step_list = [ 'stepCreateBug'
383
                , 'stepCheckBugInit'
384 385 386
                , 'stepOpenBug'
                , 'stepTic'
                , 'stepSetOldClosedDate'
387 388 389 390
                , 'stepAssignBug'
                , 'stepTic'
                , 'stepResolveBug'
                , 'stepTic'
Nicolas Delaby's avatar
Nicolas Delaby committed
391
                , 'stepReAssignBug'
392 393 394
                , 'stepTic'
                , 'stepResolveBug'
                , 'stepTic'
395 396 397
                , 'stepCloseBug'
                , 'stepTic'
                , 'stepCheckClosedDate'
398 399 400 401 402
                ]
    sequence_string = ' '.join(step_list)
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  def test_02_setCheckBugNotification(self, quiet=QUIET, run=RUN_ALL_TEST):
    """
      Test that a closed bug has its stop date property updated.
    """
    if not run: return
    sequence_list = SequenceList()
    step_list = [ 'stepLoginUsualUser'
                , 'stepCreateBug'
                , 'stepCreateProject'
                , 'stepCreatePerson1'
                , 'stepCreatePerson2'
                , 'stepSetSourceProject'
                , 'stepSetRequester'
                , 'stepTic'
                , 'stepOpenBug'
                , 'stepTic'
                , 'stepCheckBugNotification'
                , 'stepAssignBug'
                , 'stepTic'
                , 'stepCheckBugNotification'
                , 'stepResolveBug'
                , 'stepTic'
                , 'stepCheckBugNotification'
Nicolas Delaby's avatar
Nicolas Delaby committed
426
                , 'stepReAssignBug'
427 428 429 430 431 432 433 434 435 436 437 438
                , 'stepTic'
                , 'stepCheckBugNotification'
                , 'stepResolveBug'
                , 'stepTic'
                , 'stepCheckBugNotification'
                , 'stepCloseBug'
                , 'stepTic'
                , 'stepCheckBugNotification'
                ]
    sequence_string = ' '.join(step_list)
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)
439

440
  def test_03_setCheckBugNotification(self, quiet=QUIET, run=RUN_ALL_TEST):
441
    """
442
      Test that a closed bug has its stop date property updated.
443 444 445
    """
    if not run: return
    sequence_list = SequenceList()
446
    step_list = [ 'stepCreateBug'
447 448 449 450 451 452
                , 'stepCreateProject'
                , 'stepCreatePerson1'
                , 'stepCreatePerson2'
                , 'stepSetSourceProject'
                , 'stepSetRequester'
                , 'stepTic'
453 454
                , 'stepOpenBug'
                , 'stepTic'
455 456 457
                , 'stepCheckBugNotification'
                , 'stepCreateBugMessage'
                , 'stepCheckBugMessage'
458
                , 'stepTic'
459 460
                , 'stepPostBugMessage'
                , 'stepTic'
Rafael Monnerat's avatar
Rafael Monnerat committed
461
                , 'stepCheckBugMessageIsDelivered'
462
                , 'stepCheckBugMessageNotification'
463 464 465 466 467
                ]
    sequence_string = ' '.join(step_list)
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
# This tests is not Appliable anymore. It will be kept for a while.
#  def test_04_StopDateUpdatedOnCancel(self, quiet=QUIET, run=RUN_ALL_TEST):
#    """
#      Same test as above but on cancel action (test bug #600).
#    """
#    if  not run: return
#    sequence_list = SequenceList()
#    step_list = [ 'stepCreateBug'
#                , 'stepOpenBug'
#                , 'stepTic'
#                , 'stepSetOldClosedDate'
#                , 'stepCancelBug'
#                , 'stepTic'
#                , 'stepCheckClosedDate'
#                ]
#    sequence_string = ' '.join(step_list)
#    sequence_list.addSequenceString(sequence_string)
#    sequence_list.play(self, quiet=quiet)
486

487 488 489 490
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestBug))
  return suite