test_user.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. # Import Salt Libs
  9. import salt.states.user as user
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, Mock, patch
  13. from tests.support.unit import TestCase
  14. log = logging.getLogger(__name__)
  15. class UserTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Validate the user state
  18. """
  19. def setup_loader_modules(self):
  20. return {user: {}}
  21. def test_present(self):
  22. """
  23. Test to ensure that the named user is present with
  24. the specified properties
  25. """
  26. ret = {"name": "salt", "changes": {}, "result": False, "comment": ""}
  27. mock_false = MagicMock(return_value=False)
  28. mock_empty_list = MagicMock(return_value=[])
  29. with patch.dict(user.__grains__, {"kernel": "Linux"}):
  30. with patch.dict(
  31. user.__salt__,
  32. {
  33. "group.info": mock_false,
  34. "user.info": mock_empty_list,
  35. "user.chkey": mock_empty_list,
  36. "user.add": mock_false,
  37. },
  38. ):
  39. ret.update(
  40. {"comment": "The following group(s) are" " not present: salt"}
  41. )
  42. self.assertDictEqual(user.present("salt", groups=["salt"]), ret)
  43. mock_false = MagicMock(
  44. side_effect=[
  45. {"key": "value"},
  46. {"key": "value"},
  47. {"key": "value"},
  48. False,
  49. False,
  50. ]
  51. )
  52. with patch.object(user, "_changes", mock_false):
  53. with patch.dict(user.__opts__, {"test": True}):
  54. ret.update(
  55. {
  56. "comment": "The following user attributes are set "
  57. "to be changed:\n"
  58. "key: value\n",
  59. "result": None,
  60. }
  61. )
  62. self.assertDictEqual(user.present("salt"), ret)
  63. with patch.dict(user.__opts__, {"test": False}):
  64. # pylint: disable=repr-flag-used-in-string
  65. comment = "These values could not be changed: {0!r}".format(
  66. {"key": "value"}
  67. )
  68. # pylint: enable=repr-flag-used-in-string
  69. ret.update({"comment": comment, "result": False})
  70. self.assertDictEqual(user.present("salt"), ret)
  71. with patch.dict(user.__opts__, {"test": True}):
  72. ret.update(
  73. {
  74. "comment": "User salt set to" " be added",
  75. "result": None,
  76. }
  77. )
  78. self.assertDictEqual(user.present("salt"), ret)
  79. with patch.dict(user.__opts__, {"test": False}):
  80. ret.update(
  81. {
  82. "comment": "Failed to create new" " user salt",
  83. "result": False,
  84. }
  85. )
  86. self.assertDictEqual(user.present("salt"), ret)
  87. def test_present_invalid_uid_change(self):
  88. mock_info = MagicMock(
  89. side_effect=[
  90. {
  91. "uid": 5000,
  92. "gid": 5000,
  93. "groups": ["foo"],
  94. "home": "/home/foo",
  95. "fullname": "Foo Bar",
  96. }
  97. ]
  98. )
  99. dunder_salt = {
  100. "user.info": mock_info,
  101. "file.group_to_gid": MagicMock(side_effect=["foo"]),
  102. "file.gid_to_group": MagicMock(side_effect=[5000]),
  103. }
  104. # side_effect used because these mocks should only be called once
  105. with patch.dict(user.__grains__, {"kernel": "Linux"}), patch.dict(
  106. user.__salt__, dunder_salt
  107. ):
  108. ret = user.present("foo", uid=5001)
  109. # State should have failed
  110. self.assertFalse(ret["result"])
  111. # Only one of uid/gid should have been flagged in the comment
  112. self.assertEqual(ret["comment"].count("not permitted"), 1)
  113. def test_present_invalid_gid_change(self):
  114. mock_info = MagicMock(
  115. side_effect=[
  116. {
  117. "uid": 5000,
  118. "gid": 5000,
  119. "groups": ["foo"],
  120. "home": "/home/foo",
  121. "fullname": "Foo Bar",
  122. }
  123. ]
  124. )
  125. dunder_salt = {
  126. "user.info": mock_info,
  127. "file.group_to_gid": MagicMock(side_effect=["foo"]),
  128. "file.gid_to_group": MagicMock(side_effect=[5000]),
  129. }
  130. # side_effect used because these mocks should only be called once
  131. with patch.dict(user.__grains__, {"kernel": "Linux"}), patch.dict(
  132. user.__salt__, dunder_salt
  133. ):
  134. ret = user.present("foo", gid=5001)
  135. # State should have failed
  136. self.assertFalse(ret["result"])
  137. # Only one of uid/gid should have been flagged in the comment
  138. self.assertEqual(ret["comment"].count("not permitted"), 1)
  139. def test_present_invalid_uid_gid_change(self):
  140. mock_info = MagicMock(
  141. side_effect=[
  142. {
  143. "uid": 5000,
  144. "gid": 5000,
  145. "groups": ["foo"],
  146. "home": "/home/foo",
  147. "fullname": "Foo Bar",
  148. }
  149. ]
  150. )
  151. dunder_salt = {
  152. "user.info": mock_info,
  153. "file.group_to_gid": MagicMock(side_effect=["foo"]),
  154. "file.gid_to_group": MagicMock(side_effect=[5000]),
  155. }
  156. # side_effect used because these mocks should only be called once
  157. with patch.dict(user.__grains__, {"kernel": "Linux"}), patch.dict(
  158. user.__salt__, dunder_salt
  159. ):
  160. ret = user.present("foo", uid=5001, gid=5001)
  161. # State should have failed
  162. self.assertFalse(ret["result"])
  163. # Both the uid and gid should have been flagged in the comment
  164. self.assertEqual(ret["comment"].count("not permitted"), 2)
  165. def test_present_uid_gid_change(self):
  166. before = {
  167. "uid": 5000,
  168. "gid": 5000,
  169. "groups": ["foo"],
  170. "home": "/home/foo",
  171. "fullname": "Foo Bar",
  172. }
  173. after = {
  174. "uid": 5001,
  175. "gid": 5001,
  176. "groups": ["othergroup"],
  177. "home": "/home/foo",
  178. "fullname": "Foo Bar",
  179. }
  180. # user.info should be called 4 times. Once the first time that
  181. # _changes() is called, once before and after changes are applied (to
  182. # get the before/after for the changes dict, and one last time to
  183. # confirm that no changes still need to be made.
  184. mock_info = MagicMock(side_effect=[before, before, after, after])
  185. mock_group_to_gid = MagicMock(side_effect=["foo", "othergroup"])
  186. mock_gid_to_group = MagicMock(side_effect=[5000, 5001])
  187. dunder_salt = {
  188. "user.info": mock_info,
  189. "user.chuid": Mock(),
  190. "user.chgid": Mock(),
  191. "file.group_to_gid": mock_group_to_gid,
  192. "file.gid_to_group": mock_gid_to_group,
  193. }
  194. # side_effect used because these mocks should only be called once
  195. with patch.dict(user.__grains__, {"kernel": "Linux"}), patch.dict(
  196. user.__salt__, dunder_salt
  197. ), patch.dict(user.__opts__, {"test": False}), patch(
  198. "os.path.isdir", MagicMock(return_value=True)
  199. ):
  200. ret = user.present(
  201. "foo", uid=5001, gid=5001, allow_uid_change=True, allow_gid_change=True
  202. )
  203. self.assertEqual(
  204. ret,
  205. {
  206. "comment": "Updated user foo",
  207. "changes": {"gid": 5001, "uid": 5001, "groups": ["othergroup"]},
  208. "name": "foo",
  209. "result": True,
  210. },
  211. )
  212. def test_absent(self):
  213. """
  214. Test to ensure that the named user is absent
  215. """
  216. ret = {"name": "salt", "changes": {}, "result": None, "comment": ""}
  217. mock = MagicMock(side_effect=[True, True, False])
  218. mock1 = MagicMock(return_value=False)
  219. with patch.dict(
  220. user.__salt__,
  221. {"user.info": mock, "user.delete": mock1, "group.info": mock1},
  222. ):
  223. with patch.dict(user.__opts__, {"test": True}):
  224. ret.update({"comment": "User salt set for removal"})
  225. self.assertDictEqual(user.absent("salt"), ret)
  226. with patch.dict(user.__opts__, {"test": False}):
  227. ret.update({"comment": "Failed to remove user salt", "result": False})
  228. self.assertDictEqual(user.absent("salt"), ret)
  229. ret.update({"comment": "User salt is not present", "result": True})
  230. self.assertDictEqual(user.absent("salt"), ret)
  231. def test_changes(self):
  232. """
  233. Test salt.states.user._changes
  234. """
  235. mock_info = MagicMock(
  236. return_value={
  237. "uid": 5000,
  238. "gid": 5000,
  239. "groups": ["foo"],
  240. "home": "/home/foo",
  241. "fullname": "Foo Bar",
  242. }
  243. )
  244. shadow_info = MagicMock(
  245. return_value={"min": 2, "max": 88888, "inact": 77, "warn": 14}
  246. )
  247. shadow_hash = MagicMock(return_value="abcd")
  248. dunder_salt = {
  249. "user.info": mock_info,
  250. "shadow.info": shadow_info,
  251. "shadow.default_hash": shadow_hash,
  252. "file.group_to_gid": MagicMock(side_effect=["foo"]),
  253. "file.gid_to_group": MagicMock(side_effect=[5000]),
  254. }
  255. def mock_exists(*args):
  256. return True
  257. # side_effect used because these mocks should only be called once
  258. with patch.dict(user.__grains__, {"kernel": "Linux"}), patch.dict(
  259. user.__salt__, dunder_salt
  260. ), patch.dict(user.__opts__, {"test": False}), patch(
  261. "os.path.isdir", mock_exists
  262. ):
  263. ret = user._changes("foo", maxdays=999999, inactdays=0, warndays=7)
  264. assert ret == {
  265. "maxdays": 999999,
  266. "mindays": 0,
  267. "fullname": "",
  268. "warndays": 7,
  269. "inactdays": 0,
  270. }