2015-03-13 21:18:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Alexander Pyatkin <asp@thexyz.net>
|
2015-03-13 21:18:50 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +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-03-13 21:18:50 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.htpasswd as htpasswd
|
2015-03-13 21:18:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class HtpasswdTestCase(TestCase, LoaderModuleMockMixin):
|
2015-03-13 21:18:50 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.states.htpasswd
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {htpasswd: {'__opts__': {'test': False}}}
|
2015-03-13 21:18:50 +00:00
|
|
|
|
|
|
|
def test_user_exists_already(self):
|
|
|
|
'''
|
|
|
|
Test if it returns True when user already exists in htpasswd file
|
|
|
|
'''
|
|
|
|
|
|
|
|
mock = MagicMock(return_value={'retcode': 0})
|
|
|
|
|
|
|
|
with patch.dict(htpasswd.__salt__, {'file.grep': mock}):
|
|
|
|
ret = htpasswd.user_exists('larry', 'badpass',
|
|
|
|
'/etc/httpd/htpasswd')
|
|
|
|
expected = {'name': 'larry',
|
|
|
|
'result': True,
|
|
|
|
'comment': 'User already known',
|
|
|
|
'changes': {}}
|
|
|
|
self.assertEqual(ret, expected)
|
|
|
|
|
|
|
|
def test_new_user_success(self):
|
|
|
|
'''
|
|
|
|
Test if it returns True when new user is added to htpasswd file
|
|
|
|
'''
|
|
|
|
|
|
|
|
mock_grep = MagicMock(return_value={'retcode': 1})
|
|
|
|
mock_useradd = MagicMock(return_value={'retcode': 0,
|
|
|
|
'stderr': 'Success'})
|
|
|
|
|
|
|
|
with patch.dict(htpasswd.__salt__,
|
|
|
|
{'file.grep': mock_grep,
|
2016-08-26 18:52:11 +00:00
|
|
|
'webutil.useradd': mock_useradd}):
|
2015-03-13 21:18:50 +00:00
|
|
|
ret = htpasswd.user_exists('larry', 'badpass',
|
|
|
|
'/etc/httpd/htpasswd')
|
|
|
|
expected = {'name': 'larry',
|
|
|
|
'result': True,
|
|
|
|
'comment': 'Success',
|
|
|
|
'changes': {'larry': True}}
|
|
|
|
self.assertEqual(ret, expected)
|
|
|
|
|
|
|
|
def test_new_user_error(self):
|
|
|
|
'''
|
|
|
|
Test if it returns False when adding user to htpasswd failed
|
|
|
|
'''
|
|
|
|
|
|
|
|
mock_grep = MagicMock(return_value={'retcode': 1})
|
|
|
|
mock_useradd = MagicMock(return_value={'retcode': 1,
|
|
|
|
'stderr': 'Error'})
|
|
|
|
|
|
|
|
with patch.dict(htpasswd.__salt__,
|
|
|
|
{'file.grep': mock_grep,
|
2016-08-26 18:52:11 +00:00
|
|
|
'webutil.useradd': mock_useradd}):
|
2015-03-13 21:18:50 +00:00
|
|
|
ret = htpasswd.user_exists('larry', 'badpass',
|
|
|
|
'/etc/httpd/htpasswd')
|
|
|
|
expected = {'name': 'larry',
|
|
|
|
'result': False,
|
|
|
|
'comment': 'Error',
|
|
|
|
'changes': {}}
|
|
|
|
self.assertEqual(ret, expected)
|