test_webutil.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.webutil as htpasswd
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class HtpasswdTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.webutil
  16. """
  17. def setup_loader_modules(self):
  18. return {htpasswd: {}}
  19. # 'useradd' function tests: 1
  20. def test_useradd(self):
  21. """
  22. Test if it adds an HTTP user using the htpasswd command
  23. """
  24. mock = MagicMock(return_value={"out": "Salt"})
  25. with patch.dict(htpasswd.__salt__, {"cmd.run_all": mock}), patch(
  26. "os.path.exists", MagicMock(return_value=True)
  27. ):
  28. self.assertDictEqual(
  29. htpasswd.useradd("/etc/httpd/htpasswd", "larry", "badpassword"),
  30. {"out": "Salt"},
  31. )
  32. # 'userdel' function tests: 2
  33. def test_userdel(self):
  34. """
  35. Test if it delete an HTTP user from the specified htpasswd file.
  36. """
  37. mock = MagicMock(return_value="Salt")
  38. with patch.dict(htpasswd.__salt__, {"cmd.run": mock}), patch(
  39. "os.path.exists", MagicMock(return_value=True)
  40. ):
  41. self.assertEqual(htpasswd.userdel("/etc/httpd/htpasswd", "larry"), ["Salt"])
  42. def test_userdel_missing_htpasswd(self):
  43. """
  44. Test if it returns error when no htpasswd file exists
  45. """
  46. with patch("os.path.exists", MagicMock(return_value=False)):
  47. self.assertEqual(
  48. htpasswd.userdel("/etc/httpd/htpasswd", "larry"),
  49. "Error: The specified htpasswd file does not exist",
  50. )