1
0

test_pw_user.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.pw_user as pw_user
  9. from salt.exceptions import CommandExecutionError
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase, skipIf
  14. try:
  15. import pwd
  16. HAS_PWD = True
  17. except ImportError:
  18. HAS_PWD = False
  19. @skipIf(not HAS_PWD, "These tests can only run on systems with the python pwd module")
  20. class PwUserTestCase(TestCase, LoaderModuleMockMixin):
  21. """
  22. Test cases for salt.modules.pw_user
  23. """
  24. def setup_loader_modules(self):
  25. return {pw_user: {}}
  26. def test_add(self):
  27. """
  28. Test for adding a user
  29. """
  30. with patch.dict(pw_user.__grains__, {"os_family": "RedHat"}):
  31. mock = MagicMock(return_value=0)
  32. with patch.dict(pw_user.__salt__, {"cmd.retcode": mock}):
  33. self.assertTrue(pw_user.add("a"))
  34. def test_delete(self):
  35. """
  36. Test for deleting a user
  37. """
  38. mock = MagicMock(return_value=0)
  39. with patch.dict(pw_user.__salt__, {"cmd.retcode": mock}):
  40. self.assertTrue(pw_user.delete("A"))
  41. def test_getent(self):
  42. """
  43. Test if user.getent already have a value
  44. """
  45. mock_user = "saltdude"
  46. class MockData(object):
  47. pw_name = mock_user
  48. with patch("pwd.getpwall", MagicMock(return_value=[MockData()])):
  49. with patch.dict(pw_user.__context__, {"user.getent": mock_user}):
  50. self.assertEqual(pw_user.getent(), mock_user)
  51. with patch.object(pw_user, "info", MagicMock(return_value=mock_user)):
  52. self.assertEqual(pw_user.getent(True)[0], mock_user)
  53. def test_chuid(self):
  54. """
  55. Test if user id given is same as previous id
  56. """
  57. mock = MagicMock(return_value={"uid": "A"})
  58. with patch.object(pw_user, "info", mock):
  59. self.assertTrue(pw_user.chuid("name", "A"))
  60. mock = MagicMock(return_value=None)
  61. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  62. mock = MagicMock(side_effect=[{"uid": "A"}, {"uid": "A"}])
  63. with patch.object(pw_user, "info", mock):
  64. self.assertFalse(pw_user.chuid("name", "B"))
  65. mock = MagicMock(return_value=None)
  66. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  67. mock = MagicMock(side_effect=[{"uid": "A"}, {"uid": "B"}])
  68. with patch.object(pw_user, "info", mock):
  69. self.assertTrue(pw_user.chuid("name", "A"))
  70. def test_chgid(self):
  71. """
  72. Test if group id given is same as previous id
  73. """
  74. mock = MagicMock(return_value={"gid": 1})
  75. with patch.object(pw_user, "info", mock):
  76. self.assertTrue(pw_user.chgid("name", 1))
  77. mock = MagicMock(return_value=None)
  78. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  79. mock = MagicMock(side_effect=[{"gid": 2}, {"gid": 2}])
  80. with patch.object(pw_user, "info", mock):
  81. self.assertFalse(pw_user.chgid("name", 1))
  82. mock = MagicMock(return_value=None)
  83. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  84. mock = MagicMock(side_effect=[{"gid": 1}, {"gid": 2}])
  85. with patch.object(pw_user, "info", mock):
  86. self.assertTrue(pw_user.chgid("name", 1))
  87. def test_chshell(self):
  88. """
  89. Test if shell given is same as previous shell
  90. """
  91. mock = MagicMock(return_value={"shell": "A"})
  92. with patch.object(pw_user, "info", mock):
  93. self.assertTrue(pw_user.chshell("name", "A"))
  94. mock = MagicMock(return_value=None)
  95. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  96. mock = MagicMock(side_effect=[{"shell": "B"}, {"shell": "B"}])
  97. with patch.object(pw_user, "info", mock):
  98. self.assertFalse(pw_user.chshell("name", "A"))
  99. mock = MagicMock(return_value=None)
  100. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  101. mock = MagicMock(side_effect=[{"shell": "A"}, {"shell": "B"}])
  102. with patch.object(pw_user, "info", mock):
  103. self.assertTrue(pw_user.chshell("name", "A"))
  104. def test_chhome(self):
  105. """
  106. Test if home directory given is same as previous home directory
  107. """
  108. mock = MagicMock(return_value={"home": "A"})
  109. with patch.object(pw_user, "info", mock):
  110. self.assertTrue(pw_user.chhome("name", "A"))
  111. mock = MagicMock(return_value=None)
  112. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  113. mock = MagicMock(side_effect=[{"home": "B"}, {"home": "B"}])
  114. with patch.object(pw_user, "info", mock):
  115. self.assertFalse(pw_user.chhome("name", "A"))
  116. mock = MagicMock(return_value=None)
  117. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  118. mock = MagicMock(side_effect=[{"home": "A"}, {"home": "B"}])
  119. with patch.object(pw_user, "info", mock):
  120. self.assertTrue(pw_user.chhome("name", "A"))
  121. def test_chgroups(self):
  122. """
  123. Test if no group needs to be added
  124. """
  125. mock = MagicMock(return_value=False)
  126. with patch.dict(pw_user.__salt__, {"cmd.retcode": mock}):
  127. mock = MagicMock(return_value=["a", "b", "c", "d"])
  128. with patch.object(pw_user, "list_groups", mock):
  129. self.assertTrue(pw_user.chgroups("name", "a, b, c, d"))
  130. mock = MagicMock(return_value=False)
  131. with patch.dict(pw_user.__salt__, {"cmd.retcode": mock}):
  132. mock = MagicMock(return_value=["a", "b"])
  133. with patch.object(pw_user, "list_groups", mock):
  134. self.assertTrue(pw_user.chgroups("name", "a, b, c"))
  135. def test_chfullname(self):
  136. """
  137. Change the user's Full Name
  138. """
  139. mock = MagicMock(return_value=False)
  140. with patch.object(pw_user, "_get_gecos", mock):
  141. self.assertFalse(pw_user.chfullname("name", "fullname"))
  142. mock = MagicMock(return_value={"fullname": "fullname"})
  143. with patch.object(pw_user, "_get_gecos", mock):
  144. self.assertTrue(pw_user.chfullname("name", "fullname"))
  145. mock = MagicMock(return_value={"fullname": "Unicøde name ①③②"})
  146. with patch.object(pw_user, "_get_gecos", mock):
  147. self.assertTrue(pw_user.chfullname("name", "Unicøde name ①③②"))
  148. mock = MagicMock(return_value={"fullname": "fullname"})
  149. with patch.object(pw_user, "_get_gecos", mock):
  150. mock = MagicMock(return_value=None)
  151. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  152. mock = MagicMock(return_value={"fullname": "fullname2"})
  153. with patch.object(pw_user, "info", mock):
  154. self.assertFalse(pw_user.chfullname("name", "fullname1"))
  155. mock = MagicMock(return_value={"fullname": "fullname2"})
  156. with patch.object(pw_user, "_get_gecos", mock):
  157. mock = MagicMock(return_value=None)
  158. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  159. mock = MagicMock(return_value={"fullname": "fullname2"})
  160. with patch.object(pw_user, "info", mock):
  161. self.assertFalse(pw_user.chfullname("name", "fullname1"))
  162. def test_chroomnumber(self):
  163. """
  164. Change the user's Room Number
  165. """
  166. mock = MagicMock(return_value=False)
  167. with patch.object(pw_user, "_get_gecos", mock):
  168. self.assertFalse(pw_user.chroomnumber("name", 1))
  169. mock = MagicMock(return_value={"roomnumber": "Unicøde room ①③②"})
  170. with patch.object(pw_user, "_get_gecos", mock):
  171. self.assertTrue(pw_user.chroomnumber("name", "Unicøde room ①③②"))
  172. mock = MagicMock(return_value={"roomnumber": "1"})
  173. with patch.object(pw_user, "_get_gecos", mock):
  174. self.assertTrue(pw_user.chroomnumber("name", 1))
  175. mock = MagicMock(return_value={"roomnumber": "2"})
  176. with patch.object(pw_user, "_get_gecos", mock):
  177. mock = MagicMock(return_value=None)
  178. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  179. mock = MagicMock(return_value={"roomnumber": "3"})
  180. with patch.object(pw_user, "info", mock):
  181. self.assertFalse(pw_user.chroomnumber("name", 1))
  182. mock = MagicMock(return_value={"roomnumber": "3"})
  183. with patch.object(pw_user, "_get_gecos", mock):
  184. mock = MagicMock(return_value=None)
  185. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  186. mock = MagicMock(return_value={"roomnumber": "3"})
  187. with patch.object(pw_user, "info", mock):
  188. self.assertFalse(pw_user.chroomnumber("name", 1))
  189. def test_chworkphone(self):
  190. """
  191. Change the user's Work Phone
  192. """
  193. mock = MagicMock(return_value=False)
  194. with patch.object(pw_user, "_get_gecos", mock):
  195. self.assertFalse(pw_user.chworkphone("name", 1))
  196. mock = MagicMock(return_value={"workphone": "1"})
  197. with patch.object(pw_user, "_get_gecos", mock):
  198. self.assertTrue(pw_user.chworkphone("name", 1))
  199. mock = MagicMock(return_value={"workphone": "Unicøde phone number ①③②"})
  200. with patch.object(pw_user, "_get_gecos", mock):
  201. self.assertTrue(pw_user.chworkphone("name", "Unicøde phone number ①③②"))
  202. mock = MagicMock(return_value={"workphone": "2"})
  203. with patch.object(pw_user, "_get_gecos", mock):
  204. mock = MagicMock(return_value=None)
  205. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  206. mock = MagicMock(return_value={"workphone": "3"})
  207. with patch.object(pw_user, "info", mock):
  208. self.assertFalse(pw_user.chworkphone("name", 1))
  209. mock = MagicMock(return_value={"workphone": "3"})
  210. with patch.object(pw_user, "_get_gecos", mock):
  211. mock = MagicMock(return_value=None)
  212. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  213. mock = MagicMock(return_value={"workphone": "3"})
  214. with patch.object(pw_user, "info", mock):
  215. self.assertFalse(pw_user.chworkphone("name", 1))
  216. def test_chhomephone(self):
  217. """
  218. Change the user's Home Phone
  219. """
  220. mock = MagicMock(return_value=False)
  221. with patch.object(pw_user, "_get_gecos", mock):
  222. self.assertFalse(pw_user.chhomephone("name", 1))
  223. mock = MagicMock(return_value={"homephone": "1"})
  224. with patch.object(pw_user, "_get_gecos", mock):
  225. self.assertTrue(pw_user.chhomephone("name", 1))
  226. mock = MagicMock(return_value={"homephone": "Unicøde phone number ①③②"})
  227. with patch.object(pw_user, "_get_gecos", mock):
  228. self.assertTrue(pw_user.chhomephone("name", "Unicøde phone number ①③②"))
  229. mock = MagicMock(return_value={"homephone": "2"})
  230. with patch.object(pw_user, "_get_gecos", mock):
  231. mock = MagicMock(return_value=None)
  232. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  233. mock = MagicMock(return_value={"homephone": "3"})
  234. with patch.object(pw_user, "info", mock):
  235. self.assertFalse(pw_user.chhomephone("name", 1))
  236. mock = MagicMock(return_value={"homephone": "3"})
  237. with patch.object(pw_user, "_get_gecos", mock):
  238. mock = MagicMock(return_value=None)
  239. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  240. mock = MagicMock(return_value={"homephone": "3"})
  241. with patch.object(pw_user, "info", mock):
  242. self.assertFalse(pw_user.chhomephone("name", 1))
  243. def test_info(self):
  244. """
  245. Return user information
  246. """
  247. self.assertEqual(pw_user.info("name"), {})
  248. mock = MagicMock(
  249. return_value=pwd.struct_passwd(
  250. (
  251. "_TEST_GROUP",
  252. "*",
  253. 83,
  254. 83,
  255. "AMaViS Daemon",
  256. "/var/virusmails",
  257. "/usr/bin/false",
  258. )
  259. )
  260. )
  261. with patch.object(pwd, "getpwnam", mock):
  262. mock = MagicMock(return_value="Group Name")
  263. with patch.object(pw_user, "list_groups", mock):
  264. self.assertEqual(pw_user.info("name")["name"], "_TEST_GROUP")
  265. def test_list_groups(self):
  266. """
  267. Return a list of groups the named user belongs to
  268. """
  269. mock_group = "saltgroup"
  270. with patch(
  271. "salt.utils.user.get_group_list", MagicMock(return_value=[mock_group])
  272. ):
  273. self.assertEqual(pw_user.list_groups("name"), [mock_group])
  274. def test_list_users(self):
  275. """
  276. Return a list of all users
  277. """
  278. mock_user = "saltdude"
  279. class MockData(object):
  280. pw_name = mock_user
  281. with patch("pwd.getpwall", MagicMock(return_value=[MockData()])):
  282. self.assertEqual(pw_user.list_users(), [mock_user])
  283. def test_rename(self):
  284. """
  285. Change the username for a named user
  286. """
  287. mock = MagicMock(return_value=False)
  288. with patch.object(pw_user, "info", mock):
  289. self.assertRaises(CommandExecutionError, pw_user.rename, "name", 1)
  290. mock = MagicMock(return_value=True)
  291. with patch.object(pw_user, "info", mock):
  292. self.assertRaises(CommandExecutionError, pw_user.rename, "name", 1)
  293. mock = MagicMock(return_value=None)
  294. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  295. mock = MagicMock(side_effect=[{"name": ""}, False, {"name": "name"}])
  296. with patch.object(pw_user, "info", mock):
  297. self.assertTrue(pw_user.rename("name", "name"))
  298. mock = MagicMock(return_value=None)
  299. with patch.dict(pw_user.__salt__, {"cmd.run": mock}):
  300. mock = MagicMock(side_effect=[{"name": ""}, False, {"name": ""}])
  301. with patch.object(pw_user, "info", mock):
  302. self.assertFalse(pw_user.rename("name", "name"))