salt/tests/unit/modules/test_htpasswd.py

63 lines
2.0 KiB
Python
Raw Normal View History

2015-01-29 09:19:32 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
2015-01-30 15:23:55 +00:00
# Import Python libs
from __future__ import absolute_import
2015-01-29 09:19:32 +00:00
# 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-01-29 09:19:32 +00:00
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.modules.htpasswd as htpasswd
2015-01-29 09:19:32 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class HtpasswdTestCase(TestCase, LoaderModuleMockMixin):
2015-01-29 09:19:32 +00:00
'''
Test cases for salt.modules.htpasswd
'''
def setup_loader_modules(self):
return {htpasswd: {}}
2015-01-29 09:19:32 +00:00
# 'useradd' function tests: 1
def test_useradd(self):
'''
Test if it adds an HTTP user using the htpasswd command
'''
mock = MagicMock(return_value={'out': 'Salt'})
2017-04-10 13:00:57 +00:00
with patch.dict(htpasswd.__salt__, {'cmd.run_all': mock}), \
patch('os.path.exists', MagicMock(return_value=True)):
2015-01-29 10:30:21 +00:00
self.assertDictEqual(htpasswd.useradd('/etc/httpd/htpasswd',
2015-01-29 09:19:32 +00:00
'larry', 'badpassword'),
2015-01-29 10:30:21 +00:00
{'out': 'Salt'})
2015-01-29 09:19:32 +00:00
2015-03-13 21:18:50 +00:00
# 'userdel' function tests: 2
2015-01-29 09:19:32 +00:00
def test_userdel(self):
'''
Test if it delete an HTTP user from the specified htpasswd file.
'''
mock = MagicMock(return_value='Salt')
2017-04-10 13:00:57 +00:00
with patch.dict(htpasswd.__salt__, {'cmd.run': mock}), \
patch('os.path.exists', MagicMock(return_value=True)):
2015-01-29 09:19:32 +00:00
self.assertEqual(htpasswd.userdel('/etc/httpd/htpasswd',
'larry'), ['Salt'])
2015-03-13 21:18:50 +00:00
def test_userdel_missing_htpasswd(self):
'''
Test if it returns error when no htpasswd file exists
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.exists', MagicMock(return_value=False)):
self.assertEqual(htpasswd.userdel('/etc/httpd/htpasswd', 'larry'),
'Error: The specified htpasswd file does not exist')