test_win_lgpo.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Shane Lee <slee@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import pytest
  7. import salt.config
  8. import salt.ext.six as six
  9. import salt.loader
  10. import salt.states.win_lgpo as win_lgpo
  11. import salt.utils.platform
  12. import salt.utils.stringutils
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import patch
  15. from tests.support.unit import TestCase, skipIf
  16. class WinLGPOComparePoliciesTestCase(TestCase):
  17. """
  18. Test cases for the win_lgpo state
  19. """
  20. def test__compare_policies_string(self):
  21. """
  22. ``_compare_policies`` should only return ``True`` when the string values
  23. are the same. All other scenarios should return ``False``
  24. """
  25. compare_string = "Salty test"
  26. # Same
  27. self.assertTrue(win_lgpo._compare_policies(compare_string, compare_string))
  28. # Different
  29. self.assertFalse(win_lgpo._compare_policies(compare_string, "Not the same"))
  30. # List
  31. self.assertFalse(win_lgpo._compare_policies(compare_string, ["item1", "item2"]))
  32. # Dict
  33. self.assertFalse(win_lgpo._compare_policies(compare_string, {"key": "value"}))
  34. # None
  35. self.assertFalse(win_lgpo._compare_policies(compare_string, None))
  36. def test__compare_policies_list(self):
  37. """
  38. ``_compare_policies`` should only return ``True`` when the lists are the
  39. same. All other scenarios should return ``False``
  40. """
  41. compare_list = ["Salty", "test"]
  42. # Same
  43. self.assertTrue(win_lgpo._compare_policies(compare_list, compare_list))
  44. # Different
  45. self.assertFalse(
  46. win_lgpo._compare_policies(compare_list, ["Not", "the", "same"])
  47. )
  48. # String
  49. self.assertFalse(win_lgpo._compare_policies(compare_list, "Not a list"))
  50. # Dict
  51. self.assertFalse(win_lgpo._compare_policies(compare_list, {"key": "value"}))
  52. # None
  53. self.assertFalse(win_lgpo._compare_policies(compare_list, None))
  54. def test__compare_policies_dict(self):
  55. """
  56. ``_compare_policies`` should only return ``True`` when the dicts are the
  57. same. All other scenarios should return ``False``
  58. """
  59. compare_dict = {"Salty": "test"}
  60. # Same
  61. self.assertTrue(win_lgpo._compare_policies(compare_dict, compare_dict))
  62. # Different
  63. self.assertFalse(win_lgpo._compare_policies(compare_dict, {"key": "value"}))
  64. # String
  65. self.assertFalse(win_lgpo._compare_policies(compare_dict, "Not a dict"))
  66. # List
  67. self.assertFalse(win_lgpo._compare_policies(compare_dict, ["Not", "a", "dict"]))
  68. # None
  69. self.assertFalse(win_lgpo._compare_policies(compare_dict, None))
  70. def test__compare_policies_integer(self):
  71. """
  72. ``_compare_policies`` should only return ``True`` when the integer
  73. values are the same. All other scenarios should return ``False``
  74. """
  75. compare_integer = 1
  76. # Same
  77. self.assertTrue(win_lgpo._compare_policies(compare_integer, compare_integer))
  78. # Different
  79. self.assertFalse(win_lgpo._compare_policies(compare_integer, 0))
  80. # List
  81. self.assertFalse(
  82. win_lgpo._compare_policies(compare_integer, ["item1", "item2"])
  83. )
  84. # Dict
  85. self.assertFalse(win_lgpo._compare_policies(compare_integer, {"key": "value"}))
  86. # None
  87. self.assertFalse(win_lgpo._compare_policies(compare_integer, None))
  88. @pytest.mark.destructive_test
  89. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  90. class WinLGPOPolicyElementNames(TestCase, LoaderModuleMockMixin):
  91. """
  92. Test variations of the Point and Print Restrictions policy when Not
  93. Configured (NC)
  94. """
  95. @classmethod
  96. def setUpClass(cls):
  97. # We're going to actually use the loader, without grains (slow)
  98. opts = salt.config.DEFAULT_MINION_OPTS.copy()
  99. utils = salt.loader.utils(opts)
  100. modules = salt.loader.minion_mods(opts, utils=utils)
  101. cls.loader_dicts = {
  102. win_lgpo: {"__opts__": opts, "__salt__": modules, "__utils__": utils}
  103. }
  104. @classmethod
  105. def tearDownClass(cls):
  106. cls.loader_dicts = None
  107. def setup_loader_modules(self):
  108. return self.loader_dicts
  109. def setUp(self):
  110. computer_policy = {"Point and Print Restrictions": "Not Configured"}
  111. with patch.dict(win_lgpo.__opts__, {"test": False}):
  112. win_lgpo.set_(name="nc_state", computer_policy=computer_policy)
  113. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  114. def test_current_element_naming_style(self):
  115. computer_policy = {
  116. "Point and Print Restrictions": {
  117. "Users can only point and print to these servers": True,
  118. "Enter fully qualified server names separated by "
  119. "semicolons": "fakeserver1;fakeserver2",
  120. "Users can only point and print to machines in their " "forest": True,
  121. "When installing drivers for a new connection": "Show warning and elevation prompt",
  122. "When updating drivers for an existing connection": "Show warning only",
  123. }
  124. }
  125. with patch.dict(win_lgpo.__opts__, {"test": False}):
  126. result = win_lgpo.set_(name="test_state", computer_policy=computer_policy)
  127. result = win_lgpo._convert_to_unicode(result)
  128. expected = {
  129. "Point and Print Restrictions": {
  130. "Enter fully qualified server names separated by "
  131. "semicolons": "fakeserver1;fakeserver2",
  132. "When installing drivers for a new connection": "Show warning and elevation prompt",
  133. "Users can only point and print to machines in " "their forest": True,
  134. "Users can only point and print to these servers": True,
  135. "When updating drivers for an existing connection": "Show warning only",
  136. }
  137. }
  138. self.assertDictEqual(
  139. result["changes"]["new"]["Computer Configuration"], expected
  140. )
  141. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  142. def test_old_element_naming_style(self):
  143. computer_policy = {
  144. "Point and Print Restrictions": {
  145. "Users can only point and print to these servers": True,
  146. "Enter fully qualified server names separated by "
  147. "semicolons": "fakeserver1;fakeserver2",
  148. "Users can only point and print to machines in their " "forest": True,
  149. # Here's the old one
  150. "Security Prompts: When installing drivers for a new connection": "Show warning and elevation prompt",
  151. "When updating drivers for an existing connection": "Show warning only",
  152. }
  153. }
  154. with patch.dict(win_lgpo.__opts__, {"test": False}):
  155. result = win_lgpo.set_(name="test_state", computer_policy=computer_policy)
  156. if six.PY2:
  157. result = win_lgpo._convert_to_unicode(result)
  158. expected = {
  159. "Point and Print Restrictions": {
  160. "Enter fully qualified server names separated by "
  161. "semicolons": "fakeserver1;fakeserver2",
  162. "When installing drivers for a new connection": "Show warning and elevation prompt",
  163. "Users can only point and print to machines in " "their forest": True,
  164. "Users can only point and print to these servers": True,
  165. "When updating drivers for an existing connection": "Show warning only",
  166. }
  167. }
  168. self.assertDictEqual(
  169. result["changes"]["new"]["Computer Configuration"], expected
  170. )
  171. expected = (
  172. "The LGPO module changed the way it gets policy element names.\n"
  173. '"Security Prompts: When installing drivers for a new connection" is no longer valid.\n'
  174. 'Please use "When installing drivers for a new connection" instead.\n'
  175. "The following policies changed:\n"
  176. "Point and Print Restrictions"
  177. )
  178. self.assertEqual(result["comment"], expected)
  179. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  180. def test_invalid_elements(self):
  181. computer_policy = {
  182. "Point and Print Restrictions": {
  183. "Invalid element spongebob": True,
  184. "Invalid element squidward": False,
  185. }
  186. }
  187. with patch.dict(win_lgpo.__opts__, {"test": False}):
  188. result = win_lgpo.set_(name="test_state", computer_policy=computer_policy)
  189. expected = {
  190. "changes": {},
  191. "comment": "Invalid element name: Invalid element squidward\n"
  192. "Invalid element name: Invalid element spongebob",
  193. "name": "test_state",
  194. "result": False,
  195. }
  196. self.assertDictEqual(result["changes"], expected["changes"])
  197. self.assertIn("Invalid element squidward", result["comment"])
  198. self.assertIn("Invalid element spongebob", result["comment"])
  199. self.assertFalse(expected["result"])
  200. @pytest.mark.destructive_test
  201. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  202. class WinLGPOPolicyElementNamesTestTrue(TestCase, LoaderModuleMockMixin):
  203. """
  204. Test variations of the Point and Print Restrictions policy when Not
  205. Configured (NC)
  206. """
  207. @classmethod
  208. def setUpClass(cls):
  209. # We're going to actually use the loader, without grains (slow)
  210. opts = salt.config.DEFAULT_MINION_OPTS.copy()
  211. utils = salt.loader.utils(opts)
  212. modules = salt.loader.minion_mods(opts, utils=utils)
  213. cls.loader_dicts = {
  214. win_lgpo: {"__opts__": opts, "__salt__": modules, "__utils__": utils}
  215. }
  216. cls.configured = False
  217. @classmethod
  218. def tearDownClass(cls):
  219. cls.loader_dicts = None
  220. cls.configured = False
  221. def setup_loader_modules(self):
  222. return self.loader_dicts
  223. def setUp(self):
  224. if not self.configured:
  225. computer_policy = {
  226. "Point and Print Restrictions": {
  227. "Users can only point and print to these servers": True,
  228. "Enter fully qualified server names separated by "
  229. "semicolons": "fakeserver1;fakeserver2",
  230. "Users can only point and print to machines in their "
  231. "forest": True,
  232. "When installing drivers for a new connection": "Show warning and elevation prompt",
  233. "When updating drivers for an existing connection": "Show warning only",
  234. }
  235. }
  236. with patch.dict(win_lgpo.__opts__, {"test": False}):
  237. win_lgpo.set_(name="nc_state", computer_policy=computer_policy)
  238. self.configured = True
  239. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  240. def test_current_element_naming_style(self):
  241. computer_policy = {
  242. "Point and Print Restrictions": {
  243. "Users can only point and print to these servers": True,
  244. "Enter fully qualified server names separated by "
  245. "semicolons": "fakeserver1;fakeserver2",
  246. "Users can only point and print to machines in their " "forest": True,
  247. "When installing drivers for a new connection": "Show warning and elevation prompt",
  248. "When updating drivers for an existing connection": "Show warning only",
  249. }
  250. }
  251. with patch.dict(win_lgpo.__opts__, {"test": True}):
  252. result = win_lgpo.set_(name="test_state", computer_policy=computer_policy)
  253. expected = {
  254. "changes": {},
  255. "comment": "All specified policies are properly configured",
  256. }
  257. self.assertDictEqual(result["changes"], expected["changes"])
  258. self.assertTrue(result["result"])
  259. self.assertEqual(result["comment"], expected["comment"])
  260. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  261. def test_old_element_naming_style(self):
  262. computer_policy = {
  263. "Point and Print Restrictions": {
  264. "Users can only point and print to these servers": True,
  265. "Enter fully qualified server names separated by "
  266. "semicolons": "fakeserver1;fakeserver2",
  267. "Users can only point and print to machines in their " "forest": True,
  268. # Here's the old one
  269. "Security Prompts: When installing drivers for a new connection": "Show warning and elevation prompt",
  270. "When updating drivers for an existing connection": "Show warning only",
  271. }
  272. }
  273. with patch.dict(win_lgpo.__opts__, {"test": True}):
  274. result = win_lgpo.set_(name="test_state", computer_policy=computer_policy)
  275. expected = {
  276. "changes": {},
  277. "comment": "The LGPO module changed the way it gets policy element names.\n"
  278. '"Security Prompts: When installing drivers for a new connection" is no longer valid.\n'
  279. 'Please use "When installing drivers for a new connection" instead.\n'
  280. "All specified policies are properly configured",
  281. }
  282. self.assertDictEqual(result["changes"], expected["changes"])
  283. self.assertTrue(result["result"])
  284. self.assertEqual(result["comment"], expected["comment"])
  285. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  286. def test_invalid_elements(self):
  287. computer_policy = {
  288. "Point and Print Restrictions": {
  289. "Invalid element spongebob": True,
  290. "Invalid element squidward": False,
  291. }
  292. }
  293. with patch.dict(win_lgpo.__opts__, {"test": True}):
  294. result = win_lgpo.set_(name="test_state", computer_policy=computer_policy)
  295. expected = {
  296. "changes": {},
  297. "comment": "Invalid element name: Invalid element squidward\n"
  298. "Invalid element name: Invalid element spongebob",
  299. "name": "test_state",
  300. "result": False,
  301. }
  302. self.assertDictEqual(result["changes"], expected["changes"])
  303. self.assertIn("Invalid element squidward", result["comment"])
  304. self.assertIn("Invalid element spongebob", result["comment"])
  305. self.assertFalse(expected["result"])