1
0

test_mac_shadow.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- coding: utf-8 -*-
  2. '''
  3. integration tests for mac_shadow
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import datetime
  8. import random
  9. import string
  10. import pytest
  11. # Import Salt Testing libs
  12. from tests.support.unit import skipIf
  13. from tests.support.case import ModuleCase
  14. # Import Salt libs
  15. import salt.utils.path
  16. import salt.utils.platform
  17. from salt.ext.six.moves import range
  18. def __random_string(size=6):
  19. '''
  20. Generates a random username
  21. '''
  22. return 'RS-' + ''.join(
  23. random.choice(string.ascii_uppercase + string.digits)
  24. for x in range(size)
  25. )
  26. TEST_USER = __random_string()
  27. NO_USER = __random_string()
  28. @pytest.mark.skip_if_not_root
  29. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  30. @skipIf(not salt.utils.path.which('dscl'), '\'dscl\' binary not found in $PATH')
  31. @skipIf(not salt.utils.path.which('pwpolicy'), '\'pwpolicy\' binary not found in $PATH')
  32. class MacShadowModuleTest(ModuleCase):
  33. '''
  34. Validate the mac_shadow module
  35. '''
  36. def setUp(self):
  37. '''
  38. Get current settings
  39. '''
  40. self.run_function('user.add', [TEST_USER])
  41. def tearDown(self):
  42. '''
  43. Reset to original settings
  44. '''
  45. self.run_function('user.delete', [TEST_USER])
  46. def test_info(self):
  47. '''
  48. Test shadow.info
  49. '''
  50. # Correct Functionality
  51. ret = self.run_function('shadow.info', [TEST_USER])
  52. self.assertEqual(ret['name'], TEST_USER)
  53. # User does not exist
  54. ret = self.run_function('shadow.info', [NO_USER])
  55. self.assertEqual(ret['name'], '')
  56. @pytest.mark.destructive_test
  57. def test_get_account_created(self):
  58. '''
  59. Test shadow.get_account_created
  60. '''
  61. # Correct Functionality
  62. text_date = self.run_function('shadow.get_account_created', [TEST_USER])
  63. self.assertNotEqual(text_date, 'Invalid Timestamp')
  64. obj_date = datetime.datetime.strptime(text_date, '%Y-%m-%d %H:%M:%S')
  65. self.assertIsInstance(obj_date, datetime.date)
  66. # User does not exist
  67. self.assertEqual(
  68. self.run_function('shadow.get_account_created', [NO_USER]),
  69. 'ERROR: User not found: {0}'.format(NO_USER))
  70. @pytest.mark.destructive_test
  71. def test_get_last_change(self):
  72. '''
  73. Test shadow.get_last_change
  74. '''
  75. # Correct Functionality
  76. text_date = self.run_function('shadow.get_last_change', [TEST_USER])
  77. self.assertNotEqual(text_date, 'Invalid Timestamp')
  78. obj_date = datetime.datetime.strptime(text_date, '%Y-%m-%d %H:%M:%S')
  79. self.assertIsInstance(obj_date, datetime.date)
  80. # User does not exist
  81. self.assertEqual(
  82. self.run_function('shadow.get_last_change', [NO_USER]),
  83. 'ERROR: User not found: {0}'.format(NO_USER))
  84. @pytest.mark.destructive_test
  85. def test_get_login_failed_last(self):
  86. '''
  87. Test shadow.get_login_failed_last
  88. '''
  89. # Correct Functionality
  90. text_date = self.run_function('shadow.get_login_failed_last', [TEST_USER])
  91. self.assertNotEqual(text_date, 'Invalid Timestamp')
  92. obj_date = datetime.datetime.strptime(text_date, '%Y-%m-%d %H:%M:%S')
  93. self.assertIsInstance(obj_date, datetime.date)
  94. # User does not exist
  95. self.assertEqual(
  96. self.run_function('shadow.get_login_failed_last', [NO_USER]),
  97. 'ERROR: User not found: {0}'.format(NO_USER))
  98. @pytest.mark.destructive_test
  99. def test_get_login_failed_count(self):
  100. '''
  101. Test shadow.get_login_failed_count
  102. '''
  103. # Correct Functionality
  104. self.assertEqual(
  105. self.run_function('shadow.get_login_failed_count', [TEST_USER]),
  106. '0')
  107. # User does not exist
  108. self.assertEqual(
  109. self.run_function('shadow.get_login_failed_count', [NO_USER]),
  110. 'ERROR: User not found: {0}'.format(NO_USER))
  111. @pytest.mark.destructive_test
  112. def test_get_set_maxdays(self):
  113. '''
  114. Test shadow.get_maxdays
  115. Test shadow.set_maxdays
  116. '''
  117. # Correct Functionality
  118. self.assertTrue(
  119. self.run_function('shadow.set_maxdays', [TEST_USER, 20]))
  120. self.assertEqual(
  121. self.run_function('shadow.get_maxdays', [TEST_USER]), 20)
  122. # User does not exist
  123. self.assertEqual(
  124. self.run_function('shadow.set_maxdays', [NO_USER, 7]),
  125. 'ERROR: User not found: {0}'.format(NO_USER))
  126. self.assertEqual(
  127. self.run_function('shadow.get_maxdays', [NO_USER]),
  128. 'ERROR: User not found: {0}'.format(NO_USER))
  129. @pytest.mark.destructive_test
  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. self.assertEqual(self.run_function('shadow.get_change', [TEST_USER]),
  139. '02/11/2011')
  140. # User does not exist
  141. self.assertEqual(
  142. self.run_function('shadow.set_change', [NO_USER, '02/11/2012']),
  143. 'ERROR: User not found: {0}'.format(NO_USER))
  144. self.assertEqual(
  145. self.run_function('shadow.get_change', [NO_USER]),
  146. 'ERROR: User not found: {0}'.format(NO_USER))
  147. @pytest.mark.destructive_test
  148. def test_get_set_expire(self):
  149. '''
  150. Test shadow.get_expire
  151. Test shadow.set_expire
  152. '''
  153. # Correct Functionality
  154. self.assertTrue(
  155. self.run_function('shadow.set_expire', [TEST_USER, '02/11/2011']))
  156. self.assertEqual(
  157. self.run_function('shadow.get_expire', [TEST_USER]), '02/11/2011')
  158. # User does not exist
  159. self.assertEqual(
  160. self.run_function('shadow.set_expire', [NO_USER, '02/11/2012']),
  161. 'ERROR: User not found: {0}'.format(NO_USER))
  162. self.assertEqual(
  163. self.run_function('shadow.get_expire', [NO_USER]),
  164. 'ERROR: User not found: {0}'.format(NO_USER))
  165. @pytest.mark.destructive_test
  166. def test_del_password(self):
  167. '''
  168. Test shadow.del_password
  169. '''
  170. # Correct Functionality
  171. self.assertTrue(self.run_function('shadow.del_password', [TEST_USER]))
  172. self.assertEqual(
  173. self.run_function('shadow.info', [TEST_USER])['passwd'], '*')
  174. # User does not exist
  175. self.assertEqual(
  176. self.run_function('shadow.del_password', [NO_USER]),
  177. 'ERROR: User not found: {0}'.format(NO_USER))
  178. @pytest.mark.destructive_test
  179. def test_set_password(self):
  180. '''
  181. Test shadow.set_password
  182. '''
  183. # Correct Functionality
  184. self.assertTrue(
  185. self.run_function('shadow.set_password', [TEST_USER, 'Pa$$W0rd']))
  186. # User does not exist
  187. self.assertEqual(
  188. self.run_function('shadow.set_password', [NO_USER, 'P@SSw0rd']),
  189. 'ERROR: User not found: {0}'.format(NO_USER))