test_win_lgpo_auditpol.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import random
  4. import salt.modules.cmdmod
  5. import salt.utils.platform
  6. import salt.utils.win_lgpo_auditpol as win_lgpo_auditpol
  7. from tests.support.helpers import slowTest
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import MagicMock, patch
  10. from tests.support.unit import TestCase, skipIf
  11. settings = ["No Auditing", "Success", "Failure", "Success and Failure"]
  12. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  13. class WinLgpoAuditpolTestCase(TestCase, LoaderModuleMockMixin):
  14. def setup_loader_modules(self):
  15. return {
  16. win_lgpo_auditpol: {
  17. "__context__": {},
  18. "__salt__": {"cmd.run_all": salt.modules.cmdmod.run_all},
  19. }
  20. }
  21. def test_get_settings(self):
  22. names = win_lgpo_auditpol._get_valid_names()
  23. ret = win_lgpo_auditpol.get_settings(category="All")
  24. for name in names:
  25. self.assertIn(name, [k.lower() for k in ret])
  26. def test_get_settings_invalid_category(self):
  27. self.assertRaises(
  28. KeyError, win_lgpo_auditpol.get_settings, category="Fake Category"
  29. )
  30. @slowTest
  31. def test_get_setting(self):
  32. names = win_lgpo_auditpol._get_valid_names()
  33. for name in names:
  34. ret = win_lgpo_auditpol.get_setting(name)
  35. self.assertIn(ret, settings)
  36. def test_get_setting_invalid_name(self):
  37. self.assertRaises(KeyError, win_lgpo_auditpol.get_setting, name="Fake Name")
  38. def test_set_setting(self):
  39. names = ["Credential Validation", "IPsec Driver", "File System", "SAM"]
  40. mock_set = MagicMock(return_value={"retcode": 0, "stdout": "Success"})
  41. with patch.object(salt.modules.cmdmod, "run_all", mock_set):
  42. with patch.object(
  43. win_lgpo_auditpol,
  44. "_get_valid_names",
  45. return_value=[k.lower() for k in names],
  46. ):
  47. for name in names:
  48. value = random.choice(settings)
  49. win_lgpo_auditpol.set_setting(name=name, value=value)
  50. switches = win_lgpo_auditpol.settings[value]
  51. cmd = 'auditpol /set /subcategory:"{0}" {1}' "".format(
  52. name, switches
  53. )
  54. mock_set.assert_called_once_with(cmd=cmd, python_shell=True)
  55. mock_set.reset_mock()
  56. def test_set_setting_invalid_setting(self):
  57. names = ["Credential Validation", "IPsec Driver", "File System"]
  58. with patch.object(
  59. win_lgpo_auditpol,
  60. "_get_valid_names",
  61. return_value=[k.lower() for k in names],
  62. ):
  63. self.assertRaises(
  64. KeyError,
  65. win_lgpo_auditpol.set_setting,
  66. name="Fake Name",
  67. value="No Auditing",
  68. )
  69. def test_set_setting_invalid_value(self):
  70. names = ["Credential Validation", "IPsec Driver", "File System"]
  71. with patch.object(
  72. win_lgpo_auditpol,
  73. "_get_valid_names",
  74. return_value=[k.lower() for k in names],
  75. ):
  76. self.assertRaises(
  77. KeyError,
  78. win_lgpo_auditpol.set_setting,
  79. name="Credential Validation",
  80. value="Fake Value",
  81. )
  82. def test_get_auditpol_dump(self):
  83. names = win_lgpo_auditpol._get_valid_names()
  84. dump = win_lgpo_auditpol.get_auditpol_dump()
  85. for name in names:
  86. found = False
  87. for line in dump:
  88. if name.lower() in line.lower():
  89. found = True
  90. break
  91. self.assertTrue(found)