test_mac_shadow.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration tests for mac_shadow
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import datetime
  7. from tests.support.case import ModuleCase
  8. from tests.support.helpers import (
  9. destructiveTest,
  10. random_string,
  11. runs_on,
  12. skip_if_binaries_missing,
  13. skip_if_not_root,
  14. slowTest,
  15. )
  16. TEST_USER = random_string("RS-", lowercase=False)
  17. NO_USER = random_string("RS-", lowercase=False)
  18. @skip_if_not_root
  19. @runs_on(kernel="Darwin")
  20. @skip_if_binaries_missing("dscl", "pwpolicy")
  21. class MacShadowModuleTest(ModuleCase):
  22. """
  23. Validate the mac_shadow module
  24. """
  25. def setUp(self):
  26. """
  27. Get current settings
  28. """
  29. self.run_function("user.add", [TEST_USER])
  30. def tearDown(self):
  31. """
  32. Reset to original settings
  33. """
  34. self.run_function("user.delete", [TEST_USER])
  35. @slowTest
  36. def test_info(self):
  37. """
  38. Test shadow.info
  39. """
  40. # Correct Functionality
  41. ret = self.run_function("shadow.info", [TEST_USER])
  42. self.assertEqual(ret["name"], TEST_USER)
  43. # User does not exist
  44. ret = self.run_function("shadow.info", [NO_USER])
  45. self.assertEqual(ret["name"], "")
  46. @destructiveTest
  47. @slowTest
  48. def test_get_account_created(self):
  49. """
  50. Test shadow.get_account_created
  51. """
  52. # Correct Functionality
  53. text_date = self.run_function("shadow.get_account_created", [TEST_USER])
  54. self.assertNotEqual(text_date, "Invalid Timestamp")
  55. obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S")
  56. self.assertIsInstance(obj_date, datetime.date)
  57. # User does not exist
  58. self.assertEqual(
  59. self.run_function("shadow.get_account_created", [NO_USER]),
  60. "ERROR: User not found: {0}".format(NO_USER),
  61. )
  62. @destructiveTest
  63. @slowTest
  64. def test_get_last_change(self):
  65. """
  66. Test shadow.get_last_change
  67. """
  68. # Correct Functionality
  69. text_date = self.run_function("shadow.get_last_change", [TEST_USER])
  70. self.assertNotEqual(text_date, "Invalid Timestamp")
  71. obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S")
  72. self.assertIsInstance(obj_date, datetime.date)
  73. # User does not exist
  74. self.assertEqual(
  75. self.run_function("shadow.get_last_change", [NO_USER]),
  76. "ERROR: User not found: {0}".format(NO_USER),
  77. )
  78. @destructiveTest
  79. @slowTest
  80. def test_get_login_failed_last(self):
  81. """
  82. Test shadow.get_login_failed_last
  83. """
  84. # Correct Functionality
  85. text_date = self.run_function("shadow.get_login_failed_last", [TEST_USER])
  86. self.assertNotEqual(text_date, "Invalid Timestamp")
  87. obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S")
  88. self.assertIsInstance(obj_date, datetime.date)
  89. # User does not exist
  90. self.assertEqual(
  91. self.run_function("shadow.get_login_failed_last", [NO_USER]),
  92. "ERROR: User not found: {0}".format(NO_USER),
  93. )
  94. @destructiveTest
  95. @slowTest
  96. def test_get_login_failed_count(self):
  97. """
  98. Test shadow.get_login_failed_count
  99. """
  100. # Correct Functionality
  101. self.assertEqual(
  102. self.run_function("shadow.get_login_failed_count", [TEST_USER]), "0"
  103. )
  104. # User does not exist
  105. self.assertEqual(
  106. self.run_function("shadow.get_login_failed_count", [NO_USER]),
  107. "ERROR: User not found: {0}".format(NO_USER),
  108. )
  109. @destructiveTest
  110. @slowTest
  111. def test_get_set_maxdays(self):
  112. """
  113. Test shadow.get_maxdays
  114. Test shadow.set_maxdays
  115. """
  116. # Correct Functionality
  117. self.assertTrue(self.run_function("shadow.set_maxdays", [TEST_USER, 20]))
  118. self.assertEqual(self.run_function("shadow.get_maxdays", [TEST_USER]), 20)
  119. # User does not exist
  120. self.assertEqual(
  121. self.run_function("shadow.set_maxdays", [NO_USER, 7]),
  122. "ERROR: User not found: {0}".format(NO_USER),
  123. )
  124. self.assertEqual(
  125. self.run_function("shadow.get_maxdays", [NO_USER]),
  126. "ERROR: User not found: {0}".format(NO_USER),
  127. )
  128. @destructiveTest
  129. @slowTest
  130. def test_get_set_change(self):
  131. """
  132. Test shadow.get_change
  133. Test shadow.set_change
  134. """
  135. # Correct Functionality
  136. self.assertTrue(
  137. self.run_function("shadow.set_change", [TEST_USER, "02/11/2011"])
  138. )
  139. self.assertEqual(
  140. self.run_function("shadow.get_change", [TEST_USER]), "02/11/2011"
  141. )
  142. # User does not exist
  143. self.assertEqual(
  144. self.run_function("shadow.set_change", [NO_USER, "02/11/2012"]),
  145. "ERROR: User not found: {0}".format(NO_USER),
  146. )
  147. self.assertEqual(
  148. self.run_function("shadow.get_change", [NO_USER]),
  149. "ERROR: User not found: {0}".format(NO_USER),
  150. )
  151. @destructiveTest
  152. @slowTest
  153. def test_get_set_expire(self):
  154. """
  155. Test shadow.get_expire
  156. Test shadow.set_expire
  157. """
  158. # Correct Functionality
  159. self.assertTrue(
  160. self.run_function("shadow.set_expire", [TEST_USER, "02/11/2011"])
  161. )
  162. self.assertEqual(
  163. self.run_function("shadow.get_expire", [TEST_USER]), "02/11/2011"
  164. )
  165. # User does not exist
  166. self.assertEqual(
  167. self.run_function("shadow.set_expire", [NO_USER, "02/11/2012"]),
  168. "ERROR: User not found: {0}".format(NO_USER),
  169. )
  170. self.assertEqual(
  171. self.run_function("shadow.get_expire", [NO_USER]),
  172. "ERROR: User not found: {0}".format(NO_USER),
  173. )
  174. @destructiveTest
  175. @slowTest
  176. def test_del_password(self):
  177. """
  178. Test shadow.del_password
  179. """
  180. # Correct Functionality
  181. self.assertTrue(self.run_function("shadow.del_password", [TEST_USER]))
  182. self.assertEqual(self.run_function("shadow.info", [TEST_USER])["passwd"], "*")
  183. # User does not exist
  184. self.assertEqual(
  185. self.run_function("shadow.del_password", [NO_USER]),
  186. "ERROR: User not found: {0}".format(NO_USER),
  187. )
  188. @destructiveTest
  189. @slowTest
  190. def test_set_password(self):
  191. """
  192. Test shadow.set_password
  193. """
  194. # Correct Functionality
  195. self.assertTrue(
  196. self.run_function("shadow.set_password", [TEST_USER, "Pa$$W0rd"])
  197. )
  198. # User does not exist
  199. self.assertEqual(
  200. self.run_function("shadow.set_password", [NO_USER, "P@SSw0rd"]),
  201. "ERROR: User not found: {0}".format(NO_USER),
  202. )