You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import mock
  5. from contextlib import contextmanager
  6. from openerp.tests.common import TransactionCase
  7. from ..controllers import main
  8. IMPORT = 'openerp.addons.password_security.controllers.main'
  9. class EndTestException(Exception):
  10. """ It allows for isolation of resources by raise """
  11. class TestPasswordSecuritySession(TransactionCase):
  12. def setUp(self):
  13. super(TestPasswordSecuritySession, self).setUp()
  14. self.PasswordSecuritySession = main.PasswordSecuritySession
  15. self.password_security_session = self.PasswordSecuritySession()
  16. self.passwd = 'I am a password!'
  17. self.fields = [
  18. {'name': 'new_password', 'value': self.passwd},
  19. ]
  20. @contextmanager
  21. def mock_assets(self):
  22. """ It mocks and returns assets used by this controller """
  23. with mock.patch('%s.request' % IMPORT) as request:
  24. yield {
  25. 'request': request,
  26. }
  27. def test_change_password_check(self):
  28. """ It should check password on request user """
  29. with self.mock_assets() as assets:
  30. check_password = assets['request'].env.user.check_password
  31. check_password.side_effect = EndTestException
  32. with self.assertRaises(EndTestException):
  33. self.password_security_session.change_password(self.fields)
  34. check_password.assert_called_once_with(
  35. self.passwd,
  36. )
  37. def test_change_password_return(self):
  38. """ It should return result of super """
  39. with self.mock_assets():
  40. with mock.patch.object(main.Session, 'change_password') as chg:
  41. res = self.password_security_session.change_password(
  42. self.fields
  43. )
  44. self.assertEqual(chg(), res)