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.

81 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Akretion Mourad EL HADJ MIMOUNE
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.tests.common import TransactionCase
  5. from openerp.tools.config import config
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from cryptography.fernet import Fernet
  10. except ImportError as err:
  11. _logger.debug(err)
  12. class TestKeychain(TransactionCase):
  13. def setUp(self):
  14. super(TestKeychain, self).setUp()
  15. self.keychain = self.env['keychain.account']
  16. self.keychain_backend = self.env['keychain.backend']
  17. def _init_data(self):
  18. return {
  19. "c": True,
  20. "a": "b",
  21. "d": "",
  22. }
  23. def _validate_data(self, data):
  24. return 'c' in data
  25. keychain_clss = self.keychain.__class__
  26. keychain_clss._test_backend_init_data = _init_data
  27. keychain_clss._test_backend_validate_data = _validate_data
  28. keychain_backend_clss = self.keychain_backend.__class__
  29. keychain_backend_clss._backend_name = 'test_backend'
  30. self.keychain._fields['namespace'].selection.append(
  31. ('test_backend', 'test backend')
  32. )
  33. def test_keychain_bakend(self):
  34. """It should work with valid data."""
  35. config['keychain_key_dev'] = Fernet.generate_key()
  36. config['running_env'] = 'dev'
  37. vals = {
  38. 'name': 'backend_test',
  39. 'password': 'test',
  40. 'data': '{"a": "o", "c": "b"}'
  41. }
  42. # we use new because keychain.backend is an abstract model
  43. backend = self.keychain_backend.new(vals)
  44. backend._inverse_keychain()
  45. account = backend._get_existing_keychain()
  46. self.assertEqual(
  47. backend.data, '{"a": "o", "c": "b"}',
  48. 'Account data is not correct')
  49. backend._inverse_password()
  50. self.assertTrue(account, 'Account was not created')
  51. self.assertEqual(
  52. account.clear_password, u'test',
  53. 'Account clear password is not correct')
  54. self.assertEqual(backend.password, u'test')
  55. backend._compute_password()
  56. self.assertEqual(
  57. backend.password, u'******', 'Backend password was not computed')
  58. self.assertEqual(
  59. account.name, u'backend_test dev', 'Account name is not correct')
  60. self.assertEqual(
  61. account.namespace, u'test_backend',
  62. 'Account namespace is not correct')
  63. self.assertEqual(
  64. account.environment, u'dev', 'Account environment is not correct')
  65. self.assertEqual(
  66. account.technical_name, '%s,%s' % (backend._name, backend.id),
  67. 'Account technical_name is not correct')