test_nacl.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import skipIf, TestCase
  7. from tests.support.mock import (
  8. NO_MOCK,
  9. NO_MOCK_REASON,
  10. MagicMock,
  11. patch
  12. )
  13. # Import Salt libs
  14. import salt.renderers.nacl as nacl
  15. @skipIf(NO_MOCK, NO_MOCK_REASON)
  16. class NaclTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. unit test NaCl renderer
  19. '''
  20. def setup_loader_modules(self):
  21. return {nacl: {}}
  22. def test__decrypt_object(self):
  23. '''
  24. test _decrypt_object
  25. '''
  26. secret = 'Use more salt.'
  27. crypted = 'NACL[MRN3cc+fmdxyQbz6WMF+jq1hKdU5X5BBI7OjK+atvHo1ll+w1gZ7XyWtZVfq9gK9rQaMfkDxmidJKwE0Mw==]'
  28. secret_map = {'secret': secret}
  29. crypted_map = {'secret': crypted}
  30. secret_list = [secret]
  31. crypted_list = [crypted]
  32. with patch.dict(nacl.__salt__, {'nacl.dec': MagicMock(return_value=secret)}):
  33. self.assertEqual(nacl._decrypt_object(secret), secret)
  34. self.assertEqual(nacl._decrypt_object(crypted), secret)
  35. self.assertEqual(nacl._decrypt_object(crypted_map), secret_map)
  36. self.assertEqual(nacl._decrypt_object(crypted_list), secret_list)
  37. self.assertEqual(nacl._decrypt_object(None), None)
  38. def test_render(self):
  39. '''
  40. test render
  41. '''
  42. secret = 'Use more salt.'
  43. crypted = 'NACL[MRN3cc+fmdxyQbz6WMF+jq1hKdU5X5BBI7OjK+atvHo1ll+w1gZ7XyWtZVfq9gK9rQaMfkDxmidJKwE0Mw==]'
  44. with patch.dict(nacl.__salt__, {'nacl.dec': MagicMock(return_value=secret)}):
  45. self.assertEqual(nacl.render(crypted), secret)