test_useradd.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@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.useradd as useradd
  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. class UserAddTestCase(TestCase, LoaderModuleMockMixin):
  20. """
  21. Test cases for salt.modules.useradd
  22. """
  23. def setup_loader_modules(self):
  24. return {useradd: {}}
  25. @classmethod
  26. def setUpClass(cls):
  27. cls.mock_pwall = {
  28. "gid": 0,
  29. "groups": ["root"],
  30. "home": "/root",
  31. "name": "root",
  32. "passwd": "x",
  33. "shell": "/bin/bash",
  34. "uid": 0,
  35. "fullname": "root",
  36. "roomnumber": "",
  37. "workphone": "",
  38. "homephone": "",
  39. "other": "",
  40. }
  41. @classmethod
  42. def tearDownClass(cls):
  43. del cls.mock_pwall
  44. # 'add' function tests: 1
  45. def test_add(self):
  46. """
  47. Test for adding a user
  48. """
  49. with patch.dict(useradd.__grains__, {"kernel": "OpenBSD"}):
  50. mock = MagicMock(return_value={"retcode": 0})
  51. with patch.dict(useradd.__salt__, {"cmd.run_all": mock}):
  52. self.assertTrue(useradd.add("Salt"))
  53. mock = MagicMock(return_value={"retcode": 1})
  54. with patch.dict(useradd.__salt__, {"cmd.run_all": mock}):
  55. self.assertFalse(useradd.add("Salt"))
  56. # 'getent' function tests: 2
  57. @skipIf(HAS_PWD is False, "The pwd module is not available")
  58. def test_getent(self):
  59. """
  60. Test if user.getent already have a value
  61. """
  62. with patch("salt.modules.useradd.__context__", MagicMock(return_value="Salt")):
  63. self.assertTrue(useradd.getent())
  64. @skipIf(HAS_PWD is False, "The pwd module is not available")
  65. def test_getent_user(self):
  66. """
  67. Tests the return information on all users
  68. """
  69. with patch("pwd.getpwall", MagicMock(return_value=[""])):
  70. ret = [
  71. {
  72. "gid": 0,
  73. "groups": ["root"],
  74. "home": "/root",
  75. "name": "root",
  76. "passwd": "x",
  77. "shell": "/bin/bash",
  78. "uid": 0,
  79. "fullname": "root",
  80. "roomnumber": "",
  81. "workphone": "",
  82. "homephone": "",
  83. "other": "",
  84. }
  85. ]
  86. with patch(
  87. "salt.modules.useradd._format_info",
  88. MagicMock(return_value=self.mock_pwall),
  89. ):
  90. self.assertEqual(useradd.getent(), ret)
  91. # 'chuid' function tests: 1
  92. def test_chuid(self):
  93. """
  94. Test if the uid of a user change
  95. """
  96. mock = MagicMock(return_value={"uid": 11})
  97. with patch.object(useradd, "info", mock):
  98. self.assertTrue(useradd.chuid("name", 11))
  99. mock_run = MagicMock(return_value=None)
  100. with patch.dict(useradd.__salt__, {"cmd.run": mock_run}):
  101. mock = MagicMock(side_effect=[{"uid": 11}, {"uid": 11}])
  102. with patch.object(useradd, "info", mock):
  103. self.assertFalse(useradd.chuid("name", 22))
  104. with patch.dict(useradd.__salt__, {"cmd.run": mock_run}):
  105. mock = MagicMock(side_effect=[{"uid": 11}, {"uid": 22}])
  106. with patch.object(useradd, "info", mock):
  107. self.assertTrue(useradd.chuid("name", 11))
  108. # 'chgid' function tests: 1
  109. def test_chgid(self):
  110. """
  111. Test the default group of the user
  112. """
  113. mock = MagicMock(return_value={"gid": 11})
  114. with patch.object(useradd, "info", mock):
  115. self.assertTrue(useradd.chgid("name", 11))
  116. mock_run = MagicMock(return_value=None)
  117. with patch.dict(useradd.__salt__, {"cmd.run": mock_run}):
  118. mock = MagicMock(side_effect=[{"gid": 22}, {"gid": 22}])
  119. with patch.object(useradd, "info", mock):
  120. self.assertFalse(useradd.chgid("name", 11))
  121. with patch.dict(useradd.__salt__, {"cmd.run": mock_run}):
  122. mock = MagicMock(side_effect=[{"gid": 11}, {"gid": 22}])
  123. with patch.object(useradd, "info", mock):
  124. self.assertTrue(useradd.chgid("name", 11))
  125. # 'chshell' function tests: 1
  126. def test_chshell(self):
  127. """
  128. Test the default shell of user
  129. """
  130. mock = MagicMock(return_value={"shell": "/bin/bash"})
  131. with patch.object(useradd, "info", mock):
  132. self.assertTrue(useradd.chshell("name", "/bin/bash"))
  133. mock_run = MagicMock(return_value=None)
  134. with patch.dict(useradd.__salt__, {"cmd.run": mock_run}):
  135. mock = MagicMock(
  136. side_effect=[{"shell": "/bin/bash"}, {"shell": "/bin/bash"}]
  137. )
  138. with patch.object(useradd, "info", mock):
  139. self.assertFalse(useradd.chshell("name", "/usr/bash"))
  140. with patch.dict(useradd.__salt__, {"cmd.run": mock_run}):
  141. mock = MagicMock(
  142. side_effect=[{"shell": "/bin/bash"}, {"shell": "/usr/bash"}]
  143. )
  144. with patch.object(useradd, "info", mock):
  145. self.assertTrue(useradd.chshell("name", "/bin/bash"))
  146. # 'chhome' function tests: 1
  147. def test_chhome(self):
  148. """
  149. Test if home directory given is same as previous home directory
  150. """
  151. mock = MagicMock(return_value={"home": "/root"})
  152. with patch.object(useradd, "info", mock):
  153. self.assertTrue(useradd.chhome("name", "/root"))
  154. mock = MagicMock(return_value=None)
  155. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  156. mock = MagicMock(side_effect=[{"home": "/root"}, {"home": "/root"}])
  157. with patch.object(useradd, "info", mock):
  158. self.assertFalse(useradd.chhome("name", "/user"))
  159. mock = MagicMock(return_value=None)
  160. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  161. mock = MagicMock(side_effect=[{"home": "/root"}, {"home": "/root"}])
  162. with patch.object(useradd, "info", mock):
  163. self.assertTrue(useradd.chhome("name", "/root"))
  164. # 'chgroups' function tests: 1
  165. def test_chgroups(self):
  166. """
  167. Test if user groups changed
  168. """
  169. mock = MagicMock(return_value=["wheel", "root"])
  170. with patch.object(useradd, "list_groups", mock):
  171. self.assertTrue(useradd.chgroups("foo", "wheel,root"))
  172. mock = MagicMock(return_value=["wheel", "root"])
  173. with patch.object(useradd, "list_groups", mock):
  174. with patch.dict(useradd.__grains__, {"kernel": "OpenBSD"}):
  175. mock_runall = MagicMock(return_value={"retcode": False, "stderr": ""})
  176. with patch.dict(useradd.__salt__, {"cmd.run_all": mock_runall}):
  177. self.assertTrue(useradd.chgroups("foo", "wheel,test,root"))
  178. mock_runall = MagicMock(return_value={"retcode": True, "stderr": ""})
  179. with patch.dict(useradd.__salt__, {"cmd.run_all": mock_runall}):
  180. self.assertFalse(useradd.chgroups("foo", "wheel,test,root"))
  181. # 'chfullname' function tests: 1
  182. def test_chfullname(self):
  183. """
  184. Test if the user's Full Name is changed
  185. """
  186. mock = MagicMock(return_value=False)
  187. with patch.object(useradd, "_get_gecos", mock):
  188. self.assertFalse(useradd.chfullname("Salt", "SaltStack"))
  189. mock = MagicMock(return_value={"fullname": "SaltStack"})
  190. with patch.object(useradd, "_get_gecos", mock):
  191. self.assertTrue(useradd.chfullname("Salt", "SaltStack"))
  192. mock = MagicMock(return_value={"fullname": "SaltStack"})
  193. with patch.object(useradd, "_get_gecos", mock):
  194. mock = MagicMock(return_value=None)
  195. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  196. mock = MagicMock(return_value={"fullname": "SaltStack2"})
  197. with patch.object(useradd, "info", mock):
  198. self.assertFalse(useradd.chfullname("Salt", "SaltStack1"))
  199. mock = MagicMock(return_value={"fullname": "SaltStack2"})
  200. with patch.object(useradd, "_get_gecos", mock):
  201. mock = MagicMock(return_value=None)
  202. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  203. mock = MagicMock(return_value={"fullname": "SaltStack2"})
  204. with patch.object(useradd, "info", mock):
  205. self.assertFalse(useradd.chfullname("Salt", "SaltStack1"))
  206. # 'chroomnumber' function tests: 1
  207. def test_chroomnumber(self):
  208. """
  209. Test if the user's Room Number is changed
  210. """
  211. mock = MagicMock(return_value=False)
  212. with patch.object(useradd, "_get_gecos", mock):
  213. self.assertFalse(useradd.chroomnumber("salt", 1))
  214. mock = MagicMock(return_value={"roomnumber": "1"})
  215. with patch.object(useradd, "_get_gecos", mock):
  216. self.assertTrue(useradd.chroomnumber("salt", 1))
  217. mock = MagicMock(return_value={"roomnumber": "2"})
  218. with patch.object(useradd, "_get_gecos", mock):
  219. mock = MagicMock(return_value=None)
  220. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  221. mock = MagicMock(return_value={"roomnumber": "3"})
  222. with patch.object(useradd, "info", mock):
  223. self.assertFalse(useradd.chroomnumber("salt", 1))
  224. mock = MagicMock(return_value={"roomnumber": "3"})
  225. with patch.object(useradd, "_get_gecos", mock):
  226. mock = MagicMock(return_value=None)
  227. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  228. mock = MagicMock(return_value={"roomnumber": "3"})
  229. with patch.object(useradd, "info", mock):
  230. self.assertFalse(useradd.chroomnumber("salt", 1))
  231. # 'chworkphone' function tests: 1
  232. def test_chworkphone(self):
  233. """
  234. Test if the user's Work Phone is changed
  235. """
  236. mock = MagicMock(return_value=False)
  237. with patch.object(useradd, "_get_gecos", mock):
  238. self.assertFalse(useradd.chworkphone("salt", 1))
  239. mock = MagicMock(return_value={"workphone": "1"})
  240. with patch.object(useradd, "_get_gecos", mock):
  241. self.assertTrue(useradd.chworkphone("salt", 1))
  242. mock = MagicMock(return_value={"workphone": "2"})
  243. with patch.object(useradd, "_get_gecos", mock):
  244. mock = MagicMock(return_value=None)
  245. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  246. mock = MagicMock(return_value={"workphone": "3"})
  247. with patch.object(useradd, "info", mock):
  248. self.assertFalse(useradd.chworkphone("salt", 1))
  249. mock = MagicMock(return_value={"workphone": "3"})
  250. with patch.object(useradd, "_get_gecos", mock):
  251. mock = MagicMock(return_value=None)
  252. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  253. mock = MagicMock(return_value={"workphone": "3"})
  254. with patch.object(useradd, "info", mock):
  255. self.assertFalse(useradd.chworkphone("salt", 1))
  256. # 'chhomephone' function tests: 1
  257. def test_chhomephone(self):
  258. """
  259. Test if the user's Home Phone is changed
  260. """
  261. mock = MagicMock(return_value=False)
  262. with patch.object(useradd, "_get_gecos", mock):
  263. self.assertFalse(useradd.chhomephone("salt", 1))
  264. mock = MagicMock(return_value={"homephone": "1"})
  265. with patch.object(useradd, "_get_gecos", mock):
  266. self.assertTrue(useradd.chhomephone("salt", 1))
  267. mock = MagicMock(return_value={"homephone": "2"})
  268. with patch.object(useradd, "_get_gecos", mock):
  269. mock = MagicMock(return_value=None)
  270. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  271. mock = MagicMock(return_value={"homephone": "3"})
  272. with patch.object(useradd, "info", mock):
  273. self.assertFalse(useradd.chhomephone("salt", 1))
  274. mock = MagicMock(return_value={"homephone": "3"})
  275. with patch.object(useradd, "_get_gecos", mock):
  276. mock = MagicMock(return_value=None)
  277. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  278. mock = MagicMock(return_value={"homephone": "3"})
  279. with patch.object(useradd, "info", mock):
  280. self.assertFalse(useradd.chhomephone("salt", 1))
  281. # 'chother' function tests: 1
  282. def test_chother(self):
  283. """
  284. Test if the user's other GECOS attribute is changed
  285. """
  286. mock = MagicMock(return_value=False)
  287. with patch.object(useradd, "_get_gecos", mock):
  288. self.assertFalse(useradd.chother("salt", 1))
  289. mock = MagicMock(return_value={"other": "foobar"})
  290. with patch.object(useradd, "_get_gecos", mock):
  291. self.assertTrue(useradd.chother("salt", "foobar"))
  292. mock = MagicMock(return_value={"other": "foobar2"})
  293. with patch.object(useradd, "_get_gecos", mock):
  294. mock = MagicMock(return_value=None)
  295. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  296. mock = MagicMock(return_value={"other": "foobar3"})
  297. with patch.object(useradd, "info", mock):
  298. self.assertFalse(useradd.chother("salt", "foobar"))
  299. mock = MagicMock(return_value={"other": "foobar3"})
  300. with patch.object(useradd, "_get_gecos", mock):
  301. mock = MagicMock(return_value=None)
  302. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  303. mock = MagicMock(return_value={"other": "foobar3"})
  304. with patch.object(useradd, "info", mock):
  305. self.assertFalse(useradd.chother("salt", "foobar"))
  306. # 'info' function tests: 1
  307. @skipIf(HAS_PWD is False, "The pwd module is not available")
  308. def test_info(self):
  309. """
  310. Test the user information
  311. """
  312. self.assertEqual(useradd.info("username-that-does-not-exist"), {})
  313. mock = MagicMock(
  314. return_value=pwd.struct_passwd(
  315. (
  316. "_TEST_GROUP",
  317. "*",
  318. 83,
  319. 83,
  320. "AMaViS Daemon",
  321. "/var/virusmails",
  322. "/usr/bin/false",
  323. )
  324. )
  325. )
  326. with patch.object(pwd, "getpwnam", mock):
  327. self.assertEqual(
  328. useradd.info("username-that-does-not-exist")["name"], "_TEST_GROUP"
  329. )
  330. # 'list_groups' function tests: 1
  331. def test_list_groups(self):
  332. """
  333. Test if it return a list of groups the named user belongs to
  334. """
  335. with patch("salt.utils.user.get_group_list", MagicMock(return_value="Salt")):
  336. self.assertEqual(useradd.list_groups("name"), "Salt")
  337. # 'list_users' function tests: 1
  338. @skipIf(HAS_PWD is False, "The pwd module is not available")
  339. def test_list_users(self):
  340. """
  341. Test if it returns a list of all users
  342. """
  343. self.assertTrue(useradd.list_users())
  344. # 'list_users' function tests: 1
  345. def test_rename(self):
  346. """
  347. Test if the username for a named user changed
  348. """
  349. mock = MagicMock(return_value=False)
  350. with patch.object(useradd, "info", mock):
  351. self.assertRaises(CommandExecutionError, useradd.rename, "salt", 1)
  352. mock = MagicMock(return_value=True)
  353. with patch.object(useradd, "info", mock):
  354. self.assertRaises(CommandExecutionError, useradd.rename, "salt", 1)
  355. mock = MagicMock(return_value=None)
  356. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  357. mock = MagicMock(side_effect=[False, {"name": ""}, {"name": "salt"}])
  358. with patch.object(useradd, "info", mock):
  359. self.assertTrue(useradd.rename("name", "salt"))
  360. mock = MagicMock(return_value=None)
  361. with patch.dict(useradd.__salt__, {"cmd.run": mock}):
  362. mock = MagicMock(side_effect=[False, {"name": ""}, {"name": ""}])
  363. with patch.object(useradd, "info", mock):
  364. self.assertFalse(useradd.rename("salt", "salt"))
  365. def test_build_gecos_field(self):
  366. """
  367. Test if gecos fields are built correctly (removing trailing commas)
  368. """
  369. test_gecos = {
  370. "fullname": "Testing",
  371. "roomnumber": 1234,
  372. "workphone": 22222,
  373. "homephone": 99999,
  374. }
  375. expected_gecos_fields = "Testing,1234,22222,99999"
  376. self.assertEqual(useradd._build_gecos(test_gecos), expected_gecos_fields)
  377. test_gecos.pop("roomnumber")
  378. test_gecos.pop("workphone")
  379. expected_gecos_fields = "Testing,,,99999"
  380. self.assertEqual(useradd._build_gecos(test_gecos), expected_gecos_fields)
  381. test_gecos.pop("homephone")
  382. expected_gecos_fields = "Testing"
  383. self.assertEqual(useradd._build_gecos(test_gecos), expected_gecos_fields)