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.

77 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
  4. from datetime import datetime
  5. from mock import patch
  6. from openerp.fields import Datetime
  7. from openerp.tests.common import TransactionCase
  8. from ..controllers.main import AuthTotpPasswordSecurity
  9. CONTROLLER_PATH = 'openerp.addons.auth_totp_password_security.controllers.main'
  10. MODEL_PATH = 'openerp.addons.password_security.models.res_users.ResUsers'
  11. @patch(CONTROLLER_PATH + '.AuthTotp.mfa_login_post')
  12. class TestAuthTotpPasswordSecurity(TransactionCase):
  13. def setUp(self):
  14. super(TestAuthTotpPasswordSecurity, self).setUp()
  15. self.test_controller = AuthTotpPasswordSecurity()
  16. self.test_user = self.env.ref('base.user_root')
  17. self.test_user.company_id.password_expiration = 1
  18. pass_date = datetime(year=2016, month=1, day=1)
  19. self.test_user.password_write_date = Datetime.to_string(pass_date)
  20. request_patcher = patch(CONTROLLER_PATH + '.request')
  21. self.addCleanup(request_patcher.stop)
  22. self.request_mock = request_patcher.start()
  23. self.request_mock.params = {'login_success': True}
  24. self.request_mock.uid = self.test_user.id
  25. self.request_mock.env = self.env
  26. # Needed when tests are run with no prior requests
  27. base_request_patcher = patch('openerp.http.request')
  28. self.addCleanup(base_request_patcher.stop)
  29. base_request_patcher.start()
  30. def test_mfa_login_post_no_mfa_login(self, super_mock):
  31. """Should return result of super if MFA login not complete"""
  32. test_response = 'Test Response'
  33. super_mock.return_value = test_response
  34. self.request_mock.params = {}
  35. result = self.test_controller.mfa_login_post().get_data()
  36. self.assertEqual(result, test_response)
  37. def test_mfa_login_post_pass_not_expired(self, super_mock):
  38. """Should return result of super if user's password not expired"""
  39. test_response = 'Test Response'
  40. super_mock.return_value = test_response
  41. self.test_user.password_write_date = Datetime.to_string(datetime.now())
  42. result = self.test_controller.mfa_login_post().get_data()
  43. self.assertEqual(result, test_response)
  44. @patch(MODEL_PATH + '.action_expire_password')
  45. def test_mfa_login_post_expired_helper(self, helper_mock, super_mock):
  46. """Should correctly call helper if user's password is expired"""
  47. self.test_controller.mfa_login_post()
  48. helper_mock.assert_called_once_with()
  49. def test_mfa_login_post_expired_log_out(self, super_mock):
  50. """Should log out user and update params if password is expired"""
  51. self.test_controller.mfa_login_post()
  52. self.request_mock.session.logout.assert_called_once_with(keep_db=True)
  53. self.assertFalse(self.request_mock.params['login_success'])
  54. def test_mfa_login_post_expired_redirect(self, super_mock):
  55. """Should return correct redirect if password is expired"""
  56. result = self.test_controller.mfa_login_post().get_data()
  57. expected = self.test_user.partner_id.signup_url
  58. self.assertIn(expected, result)