2015-05-14 15:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Rahul Handay <rahulha@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
2018-01-19 05:49:04 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-05-14 15:58:37 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 22:28:46 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-05-14 15:58:37 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-02-19 22:28:46 +00:00
|
|
|
import salt.states.user as user
|
2015-05-14 15:58:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 22:28:46 +00:00
|
|
|
class UserTestCase(TestCase, LoaderModuleMockMixin):
|
2015-05-14 15:58:37 +00:00
|
|
|
'''
|
|
|
|
Validate the user state
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {user: {}}
|
2017-02-19 22:28:46 +00:00
|
|
|
|
2015-05-14 15:58:37 +00:00
|
|
|
def test_present(self):
|
|
|
|
'''
|
|
|
|
Test to ensure that the named user is present with
|
|
|
|
the specified properties
|
|
|
|
'''
|
|
|
|
ret = {'name': 'salt',
|
|
|
|
'changes': {},
|
|
|
|
'result': False,
|
|
|
|
'comment': ''}
|
2017-05-09 22:00:24 +00:00
|
|
|
mock_false = MagicMock(return_value=False)
|
|
|
|
mock_empty_list = MagicMock(return_value=[])
|
|
|
|
with patch.dict(user.__grains__, {"kernel": 'Linux'}):
|
|
|
|
with patch.dict(user.__salt__, {'group.info': mock_false,
|
|
|
|
'user.info': mock_empty_list,
|
|
|
|
"user.chkey": mock_empty_list,
|
|
|
|
'user.add': mock_false}):
|
|
|
|
ret.update({'comment': 'The following group(s) are'
|
|
|
|
' not present: salt'})
|
|
|
|
self.assertDictEqual(user.present('salt', groups=['salt']), ret)
|
2015-05-14 15:58:37 +00:00
|
|
|
|
2017-05-09 22:00:24 +00:00
|
|
|
mock_false = MagicMock(side_effect=[{'key': 'value'}, {'key': 'value'},
|
|
|
|
{'key': 'value'}, False, False])
|
|
|
|
with patch.object(user, '_changes', mock_false):
|
|
|
|
with patch.dict(user.__opts__, {"test": True}):
|
|
|
|
ret.update(
|
|
|
|
{'comment': 'The following user attributes are set '
|
|
|
|
'to be changed:\n'
|
|
|
|
'key: value\n',
|
|
|
|
'result': None})
|
|
|
|
self.assertDictEqual(user.present('salt'), ret)
|
2015-05-14 15:58:37 +00:00
|
|
|
|
2017-05-09 22:00:24 +00:00
|
|
|
with patch.dict(user.__opts__, {"test": False}):
|
2018-01-19 05:49:04 +00:00
|
|
|
# pylint: disable=repr-flag-used-in-string
|
|
|
|
comment = (
|
|
|
|
'These values could not be changed: {0!r}'
|
|
|
|
.format({'key': 'value'})
|
|
|
|
)
|
|
|
|
# pylint: enable=repr-flag-used-in-string
|
|
|
|
ret.update({'comment': comment, 'result': False})
|
2015-05-14 15:58:37 +00:00
|
|
|
self.assertDictEqual(user.present('salt'), ret)
|
|
|
|
|
|
|
|
with patch.dict(user.__opts__, {"test": True}):
|
|
|
|
ret.update({'comment': 'User salt set to'
|
|
|
|
' be added', 'result': None})
|
|
|
|
self.assertDictEqual(user.present('salt'), ret)
|
|
|
|
|
|
|
|
with patch.dict(user.__opts__, {"test": False}):
|
|
|
|
ret.update({'comment': 'Failed to create new'
|
|
|
|
' user salt', 'result': False})
|
|
|
|
self.assertDictEqual(user.present('salt'), ret)
|
|
|
|
|
|
|
|
def test_absent(self):
|
|
|
|
'''
|
|
|
|
Test to ensure that the named user is absent
|
|
|
|
'''
|
|
|
|
ret = {'name': 'salt',
|
|
|
|
'changes': {},
|
|
|
|
'result': None,
|
|
|
|
'comment': ''}
|
|
|
|
mock = MagicMock(side_effect=[True, True, False])
|
|
|
|
mock1 = MagicMock(return_value=False)
|
|
|
|
with patch.dict(user.__salt__, {'user.info': mock,
|
|
|
|
'user.delete': mock1,
|
|
|
|
'group.info': mock1}):
|
|
|
|
with patch.dict(user.__opts__, {"test": True}):
|
|
|
|
ret.update({'comment': 'User salt set for removal'})
|
|
|
|
self.assertDictEqual(user.absent('salt'), ret)
|
|
|
|
|
|
|
|
with patch.dict(user.__opts__, {"test": False}):
|
|
|
|
ret.update({'comment': 'Failed to remove user salt',
|
|
|
|
'result': False})
|
|
|
|
self.assertDictEqual(user.absent('salt'), ret)
|
|
|
|
|
|
|
|
ret.update({'comment': 'User salt is not present',
|
|
|
|
'result': True})
|
|
|
|
self.assertDictEqual(user.absent('salt'), ret)
|