salt/tests/unit/states/test_htpasswd.py

86 lines
2.8 KiB
Python
Raw Normal View History

2015-03-13 21:18:50 +00:00
# -*- coding: utf-8 -*-
'''
: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
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
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
'''
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,
'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,
'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)