test_win_lgpo.py 13 KB

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