1
0

test_useradd.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import pytest
  4. from tests.support.case import ModuleCase
  5. from tests.support.helpers import (
  6. destructiveTest,
  7. random_string,
  8. requires_system_grains,
  9. runs_on,
  10. skip_if_not_root,
  11. slowTest,
  12. )
  13. @destructiveTest
  14. @skip_if_not_root
  15. @runs_on(kernel="Linux")
  16. class UseraddModuleTestLinux(ModuleCase):
  17. @requires_system_grains
  18. @slowTest
  19. def test_groups_includes_primary(self, grains):
  20. # Let's create a user, which usually creates the group matching the
  21. # name
  22. uname = random_string("RS-", lowercase=False)
  23. if self.run_function("user.add", [uname]) is not True:
  24. # Skip because creating is not what we're testing here
  25. self.run_function("user.delete", [uname, True, True])
  26. self.skipTest("Failed to create user")
  27. try:
  28. uinfo = self.run_function("user.info", [uname])
  29. if grains["os_family"] in ("Suse",):
  30. self.assertIn("users", uinfo["groups"])
  31. else:
  32. self.assertIn(uname, uinfo["groups"])
  33. # This uid is available, store it
  34. uid = uinfo["uid"]
  35. self.run_function("user.delete", [uname, True, True])
  36. # Now, a weird group id
  37. gname = random_string("RS-", lowercase=False)
  38. if self.run_function("group.add", [gname]) is not True:
  39. self.run_function("group.delete", [gname, True, True])
  40. self.skipTest("Failed to create group")
  41. ginfo = self.run_function("group.info", [gname])
  42. # And create the user with that gid
  43. if self.run_function("user.add", [uname, uid, ginfo["gid"]]) is False:
  44. # Skip because creating is not what we're testing here
  45. self.run_function("user.delete", [uname, True, True])
  46. self.skipTest("Failed to create user")
  47. uinfo = self.run_function("user.info", [uname])
  48. self.assertIn(gname, uinfo["groups"])
  49. except AssertionError:
  50. self.run_function("user.delete", [uname, True, True])
  51. raise
  52. @slowTest
  53. def test_user_primary_group(self):
  54. """
  55. Tests the primary_group function
  56. """
  57. name = "saltyuser"
  58. # Create a user to test primary group function
  59. if self.run_function("user.add", [name]) is not True:
  60. self.run_function("user.delete", [name])
  61. self.skipTest("Failed to create a user")
  62. try:
  63. # Test useradd.primary_group
  64. primary_group = self.run_function("user.primary_group", [name])
  65. uid_info = self.run_function("user.info", [name])
  66. self.assertIn(primary_group, uid_info["groups"])
  67. except Exception: # pylint: disable=broad-except
  68. self.run_function("user.delete", [name])
  69. raise
  70. @destructiveTest
  71. @skip_if_not_root
  72. @runs_on(kernel="Windows")
  73. @pytest.mark.windows_whitelisted
  74. class UseraddModuleTestWindows(ModuleCase):
  75. def setUp(self):
  76. self.user_name = random_string("RS-", lowercase=False)
  77. self.group_name = random_string("RS-", lowercase=False)
  78. def tearDown(self):
  79. self.run_function("user.delete", [self.user_name, True, True])
  80. self.run_function("group.delete", [self.group_name])
  81. def _add_user(self):
  82. """
  83. helper class to add user
  84. """
  85. if self.run_function("user.add", [self.user_name]) is False:
  86. # Skip because creating is not what we're testing here
  87. self.skipTest("Failed to create user")
  88. def _add_group(self):
  89. """
  90. helper class to add group
  91. """
  92. if self.run_function("group.add", [self.group_name]) is False:
  93. # Skip because creating is not what we're testing here
  94. self.skipTest("Failed to create group")
  95. @slowTest
  96. def test_add_user(self):
  97. """
  98. Test adding a user
  99. """
  100. self._add_user()
  101. user_list = self.run_function("user.list_users")
  102. self.assertIn(self.user_name, user_list)
  103. @slowTest
  104. def test_add_group(self):
  105. """
  106. Test adding a user
  107. """
  108. self._add_group()
  109. group_list = self.run_function("group.list_groups")
  110. self.assertIn(self.group_name, group_list)
  111. @slowTest
  112. def test_add_user_to_group(self):
  113. """
  114. Test adding a user to a group
  115. """
  116. self._add_group()
  117. # And create the user as a member of that group
  118. self.run_function("user.add", [self.user_name], groups=self.group_name)
  119. user_info = self.run_function("user.info", [self.user_name])
  120. self.assertIn(self.group_name, user_info["groups"])
  121. @slowTest
  122. def test_add_user_addgroup(self):
  123. """
  124. Test adding a user to a group with groupadd
  125. """
  126. self._add_group()
  127. self._add_user()
  128. self.run_function("user.addgroup", [self.user_name, self.group_name])
  129. info = self.run_function("user.info", [self.user_name])
  130. self.assertEqual(info["groups"], [self.group_name])
  131. @slowTest
  132. def test_user_chhome(self):
  133. """
  134. Test changing a users home dir
  135. """
  136. self._add_user()
  137. user_dir = r"c:\salt"
  138. self.run_function("user.chhome", [self.user_name, user_dir])
  139. info = self.run_function("user.info", [self.user_name])
  140. self.assertEqual(info["home"], user_dir)
  141. @slowTest
  142. def test_user_chprofile(self):
  143. """
  144. Test changing a users profile
  145. """
  146. self._add_user()
  147. config = r"c:\salt\config"
  148. self.run_function("user.chprofile", [self.user_name, config])
  149. info = self.run_function("user.info", [self.user_name])
  150. self.assertEqual(info["profile"], config)
  151. @slowTest
  152. def test_user_chfullname(self):
  153. """
  154. Test changing a users fullname
  155. """
  156. self._add_user()
  157. name = "Salt Test"
  158. self.run_function("user.chfullname", [self.user_name, name])
  159. info = self.run_function("user.info", [self.user_name])
  160. self.assertEqual(info["fullname"], name)
  161. @slowTest
  162. def test_user_delete(self):
  163. """
  164. Test deleting a user
  165. """
  166. self._add_user()
  167. self.assertTrue(self.run_function("user.info", [self.user_name])["active"])
  168. self.run_function("user.delete", [self.user_name])
  169. self.assertEqual({}, self.run_function("user.info", [self.user_name]))
  170. @slowTest
  171. def test_user_removegroup(self):
  172. """
  173. Test removing a group
  174. """
  175. self._add_user()
  176. self._add_group()
  177. self.run_function("user.addgroup", [self.user_name, self.group_name])
  178. self.assertIn(
  179. self.group_name, self.run_function("user.list_groups", [self.user_name])
  180. )
  181. self.run_function("user.removegroup", [self.user_name, self.group_name])
  182. self.assertNotIn(
  183. self.group_name, self.run_function("user.list_groups", [self.user_name])
  184. )
  185. @slowTest
  186. def test_user_rename(self):
  187. """
  188. Test changing a users name
  189. """
  190. self._add_user()
  191. name = "newuser"
  192. self.run_function("user.rename", [self.user_name, name])
  193. info = self.run_function("user.info", [name])
  194. self.assertTrue(info["active"])
  195. # delete new user
  196. self.run_function("user.delete", [name, True, True])
  197. @slowTest
  198. def test_user_setpassword(self):
  199. """
  200. Test setting a password
  201. """
  202. self._add_user()
  203. passwd = "sup3rs3cr3T!"
  204. self.assertTrue(self.run_function("user.setpassword", [self.user_name, passwd]))