From 8ab052c2000aa4b9dd67e6c19ef584b487864f7f Mon Sep 17 00:00:00 2001 From: Dan Bungert Date: Tue, 3 Oct 2023 18:57:09 -0600 Subject: [PATCH] util: set_log_perms tests --- subiquitycore/tests/test_file_util.py | 98 ++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/subiquitycore/tests/test_file_util.py b/subiquitycore/tests/test_file_util.py index ea0b4849..2d522eef 100644 --- a/subiquitycore/tests/test_file_util.py +++ b/subiquitycore/tests/test_file_util.py @@ -13,7 +13,15 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -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)