test_win_lgpo_auditpol.py 3.8 KB

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