1
0

test_useradd.py 8.4 KB

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