test_win_lgpo_auditpol.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. import random
  5. # Import Salt Testing Libs
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
  8. from tests.support.unit import TestCase, skipIf
  9. # Import Salt Libs
  10. import salt.modules.cmdmod
  11. import salt.utils.platform
  12. import salt.utils.win_lgpo_auditpol as win_lgpo_auditpol
  13. settings = ['No Auditing', 'Success', 'Failure', 'Success and Failure']
  14. @skipIf(NO_MOCK, NO_MOCK_REASON)
  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__': {
  22. 'cmd.run_all': salt.modules.cmdmod.run_all
  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,
  32. win_lgpo_auditpol.get_settings,
  33. category='Fake Category')
  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(
  41. KeyError,
  42. win_lgpo_auditpol.get_setting,
  43. name='Fake Name')
  44. def test_set_setting(self):
  45. names = ['Credential Validation', 'IPsec Driver', 'File System', 'SAM']
  46. mock_set = MagicMock(return_value={'retcode': 0, 'stdout': 'Success'})
  47. with patch.object(salt.modules.cmdmod, 'run_all', mock_set):
  48. with patch.object(win_lgpo_auditpol, '_get_valid_names',
  49. return_value=[k.lower() for k in names]):
  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}' \
  55. ''.format(name, switches)
  56. mock_set.assert_called_once_with(cmd=cmd, python_shell=True)
  57. mock_set.reset_mock()
  58. def test_set_setting_invalid_setting(self):
  59. names = ['Credential Validation', 'IPsec Driver', 'File System']
  60. with patch.object(win_lgpo_auditpol, '_get_valid_names',
  61. return_value=[k.lower() for k in names]):
  62. self.assertRaises(
  63. KeyError,
  64. win_lgpo_auditpol.set_setting,
  65. name='Fake Name',
  66. value='No Auditing')
  67. def test_set_setting_invalid_value(self):
  68. names = ['Credential Validation', 'IPsec Driver', 'File System']
  69. with patch.object(win_lgpo_auditpol, '_get_valid_names',
  70. return_value=[k.lower() for k in names]):
  71. self.assertRaises(
  72. KeyError,
  73. win_lgpo_auditpol.set_setting,
  74. name='Credential Validation',
  75. value='Fake Value')
  76. def test_get_auditpol_dump(self):
  77. names = win_lgpo_auditpol._get_valid_names()
  78. dump = win_lgpo_auditpol.get_auditpol_dump()
  79. for name in names:
  80. found = False
  81. for line in dump:
  82. if name.lower() in line.lower():
  83. found = True
  84. break
  85. self.assertTrue(found)