test_win_lgpo_auditpol.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 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(not salt.utils.platform.is_windows(), 'System is not Windows')
  15. class WinLgpoAuditpolTestCase(TestCase, LoaderModuleMockMixin):
  16. def setup_loader_modules(self):
  17. return {
  18. win_lgpo_auditpol: {
  19. '__context__': {},
  20. '__salt__': {
  21. 'cmd.run_all': salt.modules.cmdmod.run_all
  22. }}}
  23. def test_get_settings(self):
  24. names = win_lgpo_auditpol._get_valid_names()
  25. ret = win_lgpo_auditpol.get_settings(category='All')
  26. for name in names:
  27. self.assertIn(name, [k.lower() for k in ret])
  28. def test_get_settings_invalid_category(self):
  29. self.assertRaises(
  30. KeyError,
  31. win_lgpo_auditpol.get_settings,
  32. category='Fake Category')
  33. def test_get_setting(self):
  34. names = win_lgpo_auditpol._get_valid_names()
  35. for name in names:
  36. ret = win_lgpo_auditpol.get_setting(name)
  37. self.assertIn(ret, settings)
  38. def test_get_setting_invalid_name(self):
  39. self.assertRaises(
  40. KeyError,
  41. win_lgpo_auditpol.get_setting,
  42. name='Fake Name')
  43. def test_set_setting(self):
  44. names = ['Credential Validation', 'IPsec Driver', 'File System', 'SAM']
  45. mock_set = MagicMock(return_value={'retcode': 0, 'stdout': 'Success'})
  46. with patch.object(salt.modules.cmdmod, 'run_all', mock_set):
  47. with patch.object(win_lgpo_auditpol, '_get_valid_names',
  48. return_value=[k.lower() for k in names]):
  49. for name in names:
  50. value = random.choice(settings)
  51. win_lgpo_auditpol.set_setting(name=name, value=value)
  52. switches = win_lgpo_auditpol.settings[value]
  53. cmd = 'auditpol /set /subcategory:"{0}" {1}' \
  54. ''.format(name, switches)
  55. mock_set.assert_called_once_with(cmd=cmd, python_shell=True)
  56. mock_set.reset_mock()
  57. def test_set_setting_invalid_setting(self):
  58. names = ['Credential Validation', 'IPsec Driver', 'File System']
  59. with patch.object(win_lgpo_auditpol, '_get_valid_names',
  60. return_value=[k.lower() for k in names]):
  61. self.assertRaises(
  62. KeyError,
  63. win_lgpo_auditpol.set_setting,
  64. name='Fake Name',
  65. value='No Auditing')
  66. def test_set_setting_invalid_value(self):
  67. names = ['Credential Validation', 'IPsec Driver', 'File System']
  68. with patch.object(win_lgpo_auditpol, '_get_valid_names',
  69. return_value=[k.lower() for k in names]):
  70. self.assertRaises(
  71. KeyError,
  72. win_lgpo_auditpol.set_setting,
  73. name='Credential Validation',
  74. value='Fake Value')
  75. def test_get_auditpol_dump(self):
  76. names = win_lgpo_auditpol._get_valid_names()
  77. dump = win_lgpo_auditpol.get_auditpol_dump()
  78. for name in names:
  79. found = False
  80. for line in dump:
  81. if name.lower() in line.lower():
  82. found = True
  83. break
  84. self.assertTrue(found)