testPasswordTool.py 17.6 KB
Newer Older
Aurel's avatar
Aurel committed
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
##############################################################################
#
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#          Aurelien Calonne <aurel@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
30
import transaction
Aurel's avatar
Aurel committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from zLOG import LOG
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import DummyMailHost
from DateTime import DateTime

class TestPasswordTool(ERP5TypeTestCase):
  """
  Test reset of password
  """
  run_all_test = 1
  quiet = 1

  def getBusinessTemplateList(self):
    return ('erp5_base', )

  def getTitle(self):
    return "Password Tool"


  def afterSetUp(self):
    portal = self.getPortal()
    if 'MailHost' in portal.objectIds():
      portal.manage_delObjects(['MailHost'])
    portal._setObject('MailHost', DummyMailHost('MailHost'))
    portal.email_from_address = 'site@example.invalid'
Aurel's avatar
Aurel committed
59
    self.portal.portal_caches.clearAllCache()
Aurel's avatar
Aurel committed
60 61

  def beforeTearDown(self):
62
    transaction.abort()
Aurel's avatar
Aurel committed
63 64
    # clear modules if necessary
    self.portal.person_module.manage_delObjects(list(self.portal.person_module.objectIds()))
65 66
    # reset password tool internal structure
    self.portal.portal_password.password_request_dict.clear()
67
    transaction.commit()
Aurel's avatar
Aurel committed
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 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
    self.tic()

  def getUserFolder(self):
    """Returns the acl_users. """
    return self.getPortal().acl_users

  def _assertUserExists(self, login, password):
    """Checks that a user with login and password exists and can log in to the
    system.
    """
    from Products.PluggableAuthService.interfaces.plugins import\
                                                      IAuthenticationPlugin
    uf = self.getUserFolder()
    self.assertNotEquals(uf.getUserById(login, None), None)
    for plugin_name, plugin in uf._getOb('plugins').listPlugins(
                                IAuthenticationPlugin ):
      if plugin.authenticateCredentials(
                  {'login':login, 'password':password}) is not None:
        break
    else:
      self.fail("No plugin could authenticate '%s' with password '%s'" %
              (login, password))

  def _assertUserDoesNotExists(self, login, password):
    """Checks that a user with login and password does not exists and cannot
    log in to the system.
    """
    from Products.PluggableAuthService.interfaces.plugins import\
                                                        IAuthenticationPlugin
    uf = self.getUserFolder()
    for plugin_name, plugin in uf._getOb('plugins').listPlugins(
                              IAuthenticationPlugin ):
      if plugin.authenticateCredentials(
                {'login':login, 'password':password}) is not None:
        self.fail(
           "Plugin %s should not have authenticated '%s' with password '%s'" %
           (plugin_name, login, password))

  def stepAddUser(self, sequence=None, sequence_list=None, **kw):
    """
    Create a user
    """
    person = self.portal.person_module.newContent(portal_type="Person",
                                    reference="userA",
                                    password="passwordA",
                                    default_email_text="userA@example.invalid")
    assignment = person.newContent(portal_type='Assignment')
    assignment.open()

  def stepCheckPasswordToolExists(self, sequence=None, sequence_list=None, **kw):
    """
    Check existence of password tool
    """
    self.failUnless(self.getPasswordTool() is not None)

  def stepCheckUserLogin(self, sequence=None, sequence_list=None, **kw):
    """
    Check existence of password tool
    """
    self._assertUserExists('userA', 'passwordA')
    
  def stepCheckUserLoginWithNewPassword(self, sequence=None, sequence_list=None, **kw):
    """
    Check existence of password tool
    """
    self._assertUserExists('userA', 'secret')

  def stepCheckUserNotLoginWithBadPassword(self, sequence=None, sequence_list=None, **kw):
    """
    Check existence of password tool
    """
    self._assertUserDoesNotExists('userA', 'secret')

  def stepCheckUserNotLoginWithFormerPassword(self, sequence=None, sequence_list=None, **kw):
    """
    Check existence of password tool
    """
    self._assertUserDoesNotExists('userA', 'passwordA')

  def stepLostPassword(self, sequence=None, sequence_list=None, **kw):
    """
    Required a new password
    """
    self.portal.portal_password.mailPasswordResetRequest(user_login="userA")

  def stepTryLostPasswordWithBadUser(self, sequence=None, sequence_list=None, **kw):
    """
    Required a new password
    """
    self.portal.portal_password.mailPasswordResetRequest(user_login="userZ")

  def stepCheckNoMailSent(self, sequence=None, sequence_list=None, **kw):
    """
    Check mail has not been sent after fill in wrong the form password
    """
    last_message = self.portal.MailHost._last_message
    self.assertEquals((), last_message)

  def stepCheckMailSent(self, sequence=None, sequence_list=None, **kw):
    """
    Check mail has been sent after fill in the form password
    """
    last_message = self.portal.MailHost._last_message
    self.assertNotEquals((), last_message)
    mfrom, mto, messageText = last_message
173
    self.assertEquals('Portal Administrator <site@example.invalid>', mfrom)
Aurel's avatar
Aurel committed
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    self.assertEquals(['userA@example.invalid'], mto)


  def stepGoToRandomAddress(self, sequence=None, sequence_list=None, **kw):
    """
    Call method that change the password
    We don't check use of random url in mail here as we have on request
    But random is also check by changeUserPassword, so it's the same
    """
    key = self.portal.portal_password.password_request_dict.keys()[0]
    self.portal.portal_password.changeUserPassword(user_login="userA",
                                                   password="secret",
                                                   password_confirmation="secret",
                                                   password_key=key)
    # reset cache
    self.portal.portal_caches.clearAllCache()


  def stepGoToRandomAddressWithBadUserName(self, sequence=None, sequence_list=None, **kw):
    """
    Call method that change the password with a bad user name
    This must not work
    """
    key = self.portal.portal_password.password_request_dict.keys()[0]
    sequence.edit(key=key)
    self.portal.portal_password.changeUserPassword(user_login="userZ",
                                                   password="secret",
                                                   password_confirmation="secret",
                                                   password_key=key)
    # reset cache
    self.portal.portal_caches.clearAllCache()


  def stepGoToRandomAddressTwice(self, sequence=None, sequence_list=None, **kw):
    """
    As we already change password, this must npot work anylonger
    """
    key = sequence.get('key')
    self.portal.portal_password.changeUserPassword(user_login="userA",
                                                   password="passwordA",
                                                   password_confirmation="passwordA",
                                                   password_key=key)
    # reset cache
    self.portal.portal_caches.clearAllCache()


  def stepGoToBadRandomAddress(self, sequence=None, sequence_list=None, **kw):
    """
    Try to reset a password with bad random part
    """
    self.portal.portal_password.changeUserPassword(user_login="userA",
                                                   password="secret",
                                                   password_confirmation="secret",
                                                   password_key="toto")
    # reset cache
    self.portal.portal_caches.clearAllCache()



  def stepModifyExpirationDate(self, sequence=None, sequence_list=None, **kw):
    """
    Change expiration date so that reset of password is not available
    """
    # save key for url
    key = self.portal.portal_password.password_request_dict.keys()[0]
    sequence.edit(key=key)
    # modify date
    for k, v in self.portal.portal_password.password_request_dict.items():
      login, date = v
      date = DateTime() - 1
244
      self.portal.portal_password.password_request_dict[k] = (login, date)
Aurel's avatar
Aurel committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258

  def stepSimulateExpirationAlarm(self, sequence=None, sequence_list=None, **kw):
    """
    Simulate alarm wich remove expired request
    """
    self.portal.portal_password.removeExpiredRequests()

  def stepCheckNoRequestRemains(self, sequence=None, sequence_list=None, **kw):
    """
    after alarm all expired request must have been removed
    """
    self.assertEqual(len(self.portal.portal_password.password_request_dict), 0)


259 260 261 262 263
  def stepLogout(self, sequence=None, sequence_list=None, **kw):
    """
    Logout
    """
    self.logout()
Aurel's avatar
Aurel committed
264 265 266 267 268 269 270 271 272 273 274

  # tests
  def test_01_checkPasswordTool(self, quiet=quiet, run=run_all_test):
    if not run: return
    if not quiet:
      message = 'Test Password Tool'
      ZopeTestCase._print('\n%s ' % message)
      LOG('Testing... ', 0, message)
    sequence_list = SequenceList()
    sequence_string = 'CheckPasswordToolExists '  \
                      'AddUser Tic ' \
275
                      'Logout ' \
Aurel's avatar
Aurel committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                      'CheckUserLogin CheckUserNotLoginWithBadPassword ' \
                      'TryLostPasswordWithBadUser Tic ' \
                      'CheckNoMailSent ' \
                      'GoToBadRandomAddress Tic ' \
                      'CheckUserLogin CheckUserNotLoginWithBadPassword ' \
                      'LostPassword Tic ' \
                      'CheckMailSent GoToRandomAddress Tic '  \
                      'CheckUserLoginWithNewPassword ' \
                      'CheckUserNotLoginWithFormerPassword ' \
                      'GoToRandomAddressTwice Tic ' \
                      'CheckUserLoginWithNewPassword ' \
                      'CheckUserNotLoginWithFormerPassword ' \

    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)


  def test_02_checkPasswordToolDateExpired(self, quiet=quiet, run=run_all_test):
    if not run: return
    if not quiet:
      message = 'Test no login reset if date expired'
      ZopeTestCase._print('\n%s ' % message)
      LOG('Testing... ', 0, message)
    sequence_list = SequenceList()
    sequence_string = 'CheckPasswordToolExists '  \
                      'AddUser Tic ' \
302
                      'Logout ' \
Aurel's avatar
Aurel committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
                      'CheckUserLogin CheckUserNotLoginWithBadPassword ' \
                      'LostPassword Tic ' \
                      'CheckMailSent ' \
                      'ModifyExpirationDate ' \
                      'GoToRandomAddress Tic '  \
                      'CheckUserLogin CheckUserNotLoginWithBadPassword ' \
                      
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)


  def test_03_checkPasswordToolAlarm(self, quiet=quiet, run=run_all_test):
    if not run: return
    if not quiet:
      message = 'Test alarm remove expired request'
      ZopeTestCase._print('\n%s ' % message)
      LOG('Testing... ', 0, message)
    sequence_list = SequenceList()
    sequence_string = 'CheckPasswordToolExists '  \
                      'AddUser Tic ' \
323
                      'Logout ' \
Aurel's avatar
Aurel committed
324 325 326 327 328 329 330 331 332 333 334 335
                      'CheckUserLogin CheckUserNotLoginWithBadPassword ' \
                      'LostPassword Tic ' \
                      'CheckMailSent ' \
                      'ModifyExpirationDate ' \
                      'SimulateExpirationAlarm ' \
                      'CheckNoRequestRemains ' \
                      'GoToRandomAddressTwice Tic '  \
                      'CheckUserLogin CheckUserNotLoginWithBadPassword ' \
                      
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  def test_two_concurrent_password_reset(self):
    personA = self.portal.person_module.newContent(portal_type="Person",
                                    reference="userA",
                                    password="passwordA",
                                    default_email_text="userA@example.invalid")
    assignment = personA.newContent(portal_type='Assignment')
    assignment.open()

    personB = self.portal.person_module.newContent(portal_type="Person",
                                    reference="userB",
                                    password="passwordB",
                                    default_email_text="userB@example.invalid")
    assignment = personB.newContent(portal_type='Assignment')
    assignment.open()
    transaction.commit()
    self.tic()

    self._assertUserExists('userA', 'passwordA')
    self._assertUserExists('userB', 'passwordB')
    
    self.assertEquals(0, len(self.portal.portal_password.password_request_dict))
    self.portal.portal_password.mailPasswordResetRequest(user_login="userA")
    self.assertEquals(1, len(self.portal.portal_password.password_request_dict))
    key_a = self.portal.portal_password.password_request_dict.keys()[0]
    transaction.commit()
    self.tic()

    self.portal.portal_password.mailPasswordResetRequest(user_login="userB")
    possible_key_list =\
        self.portal.portal_password.password_request_dict.keys()
    self.assertEquals(2, len(possible_key_list))
    key_b = [k for k in possible_key_list if k != key_a][0]
    transaction.commit()
    self.tic()

    self._assertUserExists('userA', 'passwordA')
    self._assertUserExists('userB', 'passwordB')

    self.portal.portal_password.changeUserPassword(user_login="userA",
                                                   password="newA",
                                                   password_confirmation="newA",
                                                   password_key=key_a)
    transaction.commit()
    self.tic()

    self._assertUserExists('userA', 'newA')
    self._assertUserExists('userB', 'passwordB')

    self.portal.portal_password.changeUserPassword(user_login="userB",
                                                   password="newB",
                                                   password_confirmation="newB",
                                                   password_key=key_b)
    transaction.commit()
    self.tic()

    self._assertUserExists('userA', 'newA')
    self._assertUserExists('userB', 'newB')

Aurel's avatar
Aurel committed
394

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  def test_login_with_trailing_space(self):
    person = self.portal.person_module.newContent(portal_type="Person",
                                    reference="userZ ",
                                    password="passwordZ",
                                    default_email_text="userA@example.invalid")
    assignment = person.newContent(portal_type='Assignment')
    assignment.open()

    transaction.commit()
    self.tic()

    self._assertUserExists('userZ ', 'passwordZ')
    
    self.assertEquals(0, len(self.portal.portal_password.password_request_dict))
    # No reset should be send if trailing space is not entered
    self.portal.portal_password.mailPasswordResetRequest(user_login="userZ")
    self.assertEquals(0, len(self.portal.portal_password.password_request_dict))
    self.portal.portal_password.mailPasswordResetRequest(user_login="userZ ")
    self.assertEquals(1, len(self.portal.portal_password.password_request_dict))

    key_a = self.portal.portal_password.password_request_dict.keys()[0]
    transaction.commit()
    self.tic()

    self._assertUserExists('userZ ', 'passwordZ')

    # Check that password is not changed if trailing space is not entered
    self.portal.portal_password.changeUserPassword(user_login="userZ",
                                                   password="newZ",
                                                   password_confirmation="newZ",
                                                   password_key=key_a)
    transaction.commit()
    self.tic()
    self._assertUserExists('userZ ', 'passwordZ')

    # Check that password is changed if trailing space is entered
    self.portal.portal_password.changeUserPassword(user_login="userZ ",
                                                   password="newZ2",
                                                   password_confirmation="newZ2",
                                                   password_key=key_a)
    transaction.commit()
    self.tic()
    self._assertUserExists('userZ ', 'newZ2')

439 440 441 442 443 444 445 446 447 448 449 450
class TestPasswordToolWithCRM(TestPasswordTool):
  """
  Test reset of password
  """

  def getBusinessTemplateList(self):
    return ('erp5_base', 'erp5_crm',)

  def getTitle(self):
    return "Password Tool with CRM"


Aurel's avatar
Aurel committed
451 452 453
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestPasswordTool))
454
  suite.addTest(unittest.makeSuite(TestPasswordToolWithCRM))
Aurel's avatar
Aurel committed
455
  return suite