util: set_log_perms tests

This commit is contained in:
Dan Bungert 2023-10-03 18:57:09 -06:00
parent 4a4e8ba886
commit 8ab052c200
1 changed files with 97 additions and 1 deletions

View File

@ -13,7 +13,15 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from subiquitycore.file_util import copy_file_if_exists
from pathlib import Path
from unittest.mock import Mock, patch
from subiquitycore.file_util import (
_DEF_GROUP,
_DEF_PERMS_FILE,
copy_file_if_exists,
set_log_perms,
)
from subiquitycore.tests import SubiTestCase
@ -29,3 +37,91 @@ class TestCopy(SubiTestCase):
def test_copied_non_exist_src(self):
copy_file_if_exists("/does/not/exist", "/ditto")
@patch("subiquitycore.file_util.os.getuid", new=Mock(return_value=0))
class TestLogPerms(SubiTestCase):
def setUp(self):
chmod = patch("subiquitycore.file_util.os.chmod")
self.chmod = chmod.start()
self.addCleanup(chmod.stop)
chown = patch("subiquitycore.file_util.os.chown")
self.chown = chown.start()
self.addCleanup(chown.stop)
getgrnam = patch("subiquitycore.file_util.grp.getgrnam")
self.getgrnam = getgrnam.start()
self.addCleanup(getgrnam.stop)
self.mock_gid = 10
self.getgrnam.return_value = Mock(gr_gid=self.mock_gid)
def test_defaults_group(self):
target = self.tmp_dir()
set_log_perms(target)
self.getgrnam.assert_called_once_with(_DEF_GROUP)
def test_defaults_file(self):
target = self.tmp_path("file")
Path(target).touch()
set_log_perms(target)
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_defaults_dir(self):
target = self.tmp_dir()
set_log_perms(target)
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE | 0o110)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_group_write_file(self):
target = self.tmp_path("file")
Path(target).touch()
set_log_perms(target, group_write=True)
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE | 0o020)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_group_write_dir(self):
target = self.tmp_dir()
set_log_perms(target, group_write=True)
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE | 0o130)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_nogroup_write_file(self):
target = self.tmp_path("file")
Path(target).touch()
set_log_perms(target, group_write=False)
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_nogroup_write_dir(self):
target = self.tmp_dir()
set_log_perms(target, group_write=False)
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE | 0o110)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_mode_file(self):
target = self.tmp_path("file")
Path(target).touch()
set_log_perms(target, mode=0o510)
self.chmod.assert_called_once_with(target, 0o510)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_mode_dir(self):
target = self.tmp_dir()
set_log_perms(target, mode=0o510)
self.chmod.assert_called_once_with(target, 0o510)
self.chown.assert_called_once_with(target, -1, self.mock_gid)
def test_group_file(self):
self.getgrnam.return_value = Mock(gr_gid=11)
target = self.tmp_path("file")
Path(target).touch()
set_log_perms(target, group="group1")
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE)
self.chown.assert_called_once_with(target, -1, 11)
def test_group_dir(self):
self.getgrnam.return_value = Mock(gr_gid=11)
target = self.tmp_dir()
set_log_perms(target, group="group1")
self.chmod.assert_called_once_with(target, _DEF_PERMS_FILE | 0o110)
self.chown.assert_called_once_with(target, -1, 11)