Add tests for bytestring and integer passwords

This commit is contained in:
Twangboy 2022-11-02 09:40:26 -06:00 committed by Megan Wilhite
parent 4dcd9f6e2c
commit 5810903f08
2 changed files with 33 additions and 1 deletions

View file

@ -1093,8 +1093,12 @@ def unzip(
source = zfile.read(target)
os.symlink(source, os.path.join(dest, target))
continue
# file.extract is expecting the password to be a bytestring
if password:
password = password.encode()
if isinstance(password, int):
password = str(password)
if isinstance(password, str):
password = password.encode()
zfile.extract(target, dest, password)
if extract_perms:
if not salt.utils.platform.is_windows():

View file

@ -392,6 +392,34 @@ def test_unzip_password():
mock().extract.assert_called_once_with(expected_file_name, dest, b"spongebob")
def test_unzip_password_bytestring():
mock = ZipFileMock()
target = "/tmp/salt.zip"
dest = "/tmp/dest"
# This is the file inside target zip file, returned by ZipFileMock.namelist
expected_file_name = "salt"
with patch("zipfile.ZipFile", mock):
ret = archive.unzip(target, dest, password=b"spongebob", extract_perms=False)
assert [expected_file_name] == ret
# mock needs to be mock() because contextlib.closing appears to actually
# call the mock
mock().extract.assert_called_once_with(expected_file_name, dest, b"spongebob")
def test_unzip_password_integer():
mock = ZipFileMock()
target = "/tmp/salt.zip"
dest = "/tmp/dest"
# This is the file inside target zip file, returned by ZipFileMock.namelist
expected_file_name = "salt"
with patch("zipfile.ZipFile", mock):
ret = archive.unzip(target, dest, password=12345, extract_perms=False)
assert [expected_file_name] == ret
# mock needs to be mock() because contextlib.closing appears to actually
# call the mock
mock().extract.assert_called_once_with(expected_file_name, dest, b"12345")
def test_unzip_raises_exception_if_not_found():
mock = MagicMock(return_value="salt")
with patch.dict(archive.__salt__, {"cmd.run": mock}):