test_nacl.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 TestCase
  7. from tests.support.mock import (
  8. MagicMock,
  9. patch
  10. )
  11. # Import Salt libs
  12. import salt.renderers.nacl as nacl
  13. class NaclTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. unit test NaCl renderer
  16. '''
  17. def setup_loader_modules(self):
  18. return {nacl: {}}
  19. def test__decrypt_object(self):
  20. '''
  21. test _decrypt_object
  22. '''
  23. secret = 'Use more salt.'
  24. crypted = 'NACL[MRN3cc+fmdxyQbz6WMF+jq1hKdU5X5BBI7OjK+atvHo1ll+w1gZ7XyWtZVfq9gK9rQaMfkDxmidJKwE0Mw==]'
  25. secret_map = {'secret': secret}
  26. crypted_map = {'secret': crypted}
  27. secret_list = [secret]
  28. crypted_list = [crypted]
  29. with patch.dict(nacl.__salt__, {'nacl.dec': MagicMock(return_value=secret)}):
  30. self.assertEqual(nacl._decrypt_object(secret), secret)
  31. self.assertEqual(nacl._decrypt_object(crypted), secret)
  32. self.assertEqual(nacl._decrypt_object(crypted_map), secret_map)
  33. self.assertEqual(nacl._decrypt_object(crypted_list), secret_list)
  34. self.assertEqual(nacl._decrypt_object(None), None)
  35. def test_render(self):
  36. '''
  37. test render
  38. '''
  39. secret = 'Use more salt.'
  40. crypted = 'NACL[MRN3cc+fmdxyQbz6WMF+jq1hKdU5X5BBI7OjK+atvHo1ll+w1gZ7XyWtZVfq9gK9rQaMfkDxmidJKwE0Mw==]'
  41. with patch.dict(nacl.__salt__, {'nacl.dec': MagicMock(return_value=secret)}):
  42. self.assertEqual(nacl.render(crypted), secret)