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.

80 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import mock
  5. from openerp.tests.common import TransactionCase
  6. MOCK_PATH = 'openerp.addons.auth_totp.models.res_users_authenticator.pyotp'
  7. class TestResUsersAuthenticator(TransactionCase):
  8. def _new_authenticator(self, extra_values=None):
  9. base_values = {
  10. 'name': 'Test Name',
  11. 'secret_key': 'Test Key',
  12. 'user_id': self.env.ref('base.user_root').id,
  13. }
  14. if extra_values is not None:
  15. base_values.update(extra_values)
  16. return self.env['res.users.authenticator'].create(base_values)
  17. def test_check_has_user(self):
  18. '''Should delete record when it no longer has a user_id'''
  19. test_auth = self._new_authenticator()
  20. test_auth.user_id = False
  21. self.assertFalse(test_auth.exists())
  22. def test_validate_conf_code_empty_recordset(self):
  23. '''Should return False if recordset is empty'''
  24. test_auth = self.env['res.users.authenticator']
  25. self.assertFalse(test_auth.validate_conf_code('Test Code'))
  26. @mock.patch(MOCK_PATH)
  27. def test_validate_conf_code_match(self, pyotp_mock):
  28. '''Should return True if code matches at least one record in set'''
  29. test_auth = self._new_authenticator()
  30. test_auth_2 = self._new_authenticator({'name': 'Test Name 2'})
  31. test_set = test_auth + test_auth_2
  32. pyotp_mock.TOTP().verify.side_effect = (True, False)
  33. self.assertTrue(test_set.validate_conf_code('Test Code'))
  34. pyotp_mock.TOTP().verify.side_effect = (True, True)
  35. self.assertTrue(test_set.validate_conf_code('Test Code'))
  36. @mock.patch(MOCK_PATH)
  37. def test_validate_conf_code_no_match(self, pyotp_mock):
  38. '''Should return False if code does not match any records in set'''
  39. test_auth = self._new_authenticator()
  40. pyotp_mock.TOTP().verify.return_value = False
  41. self.assertFalse(test_auth.validate_conf_code('Test Code'))
  42. @mock.patch(MOCK_PATH)
  43. def test_validate_conf_code_pyotp_use(self, pyotp_mock):
  44. '''Should call PyOTP 2x/record with correct arguments until match'''
  45. test_auth = self._new_authenticator()
  46. test_auth_2 = self._new_authenticator({
  47. 'name': 'Test Name 2',
  48. 'secret_key': 'Test Key 2',
  49. })
  50. test_auth_3 = self._new_authenticator({
  51. 'name': 'Test Name 3',
  52. 'secret_key': 'Test Key 3',
  53. })
  54. test_set = test_auth + test_auth_2 + test_auth_3
  55. pyotp_mock.TOTP().verify.side_effect = (False, True, True)
  56. pyotp_mock.reset_mock()
  57. test_set.validate_conf_code('Test Code')
  58. pyotp_calls = [
  59. mock.call('Test Key'),
  60. mock.call().verify('Test Code'),
  61. mock.call('Test Key 2'),
  62. mock.call().verify('Test Code'),
  63. ]
  64. self.assertEqual(pyotp_mock.TOTP.mock_calls, pyotp_calls)