test_win_lgpo.py 15 KB

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