PyCIRCLean/tests/test_kittengroomer.py

332 lines
13 KiB
Python
Raw Normal View History

2016-12-05 17:19:39 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2016-12-09 02:12:08 +01:00
import os
2017-07-13 23:01:02 +02:00
import unittest.mock as mock
2016-12-09 02:12:08 +01:00
2016-12-05 17:19:39 +01:00
import pytest
from kittengroomer import FileBase, KittenGroomerBase
2017-07-17 15:17:20 +02:00
from kittengroomer.helpers import ImplementationRequired
2016-12-05 17:19:39 +01:00
2016-12-09 02:12:08 +01:00
skip = pytest.mark.skip
xfail = pytest.mark.xfail
fixture = pytest.fixture
2016-12-05 17:19:39 +01:00
2016-12-09 02:12:08 +01:00
class TestFileBase:
2016-12-05 17:19:39 +01:00
2017-07-13 23:01:02 +02:00
@fixture(scope='class')
def src_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('src').strpath
@fixture(scope='class')
def dest_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('dest').strpath
2016-12-09 02:12:08 +01:00
@fixture
2017-07-13 23:36:43 +02:00
def tmpfile_path(self, tmpdir):
file_path = tmpdir.join('test.txt')
2016-12-09 02:12:08 +01:00
file_path.write('testing')
2017-07-13 23:36:43 +02:00
return file_path.strpath
2016-12-09 02:12:08 +01:00
@fixture
2017-07-17 15:17:20 +02:00
def symlink_file_path(self, tmpdir, tmpfile_path):
symlink_path = tmpdir.join('symlinked')
symlink_path = symlink_path.strpath
os.symlink(tmpfile_path, symlink_path)
return symlink_path
2016-12-09 02:12:08 +01:00
@fixture
def text_file(self):
with mock.patch(
'kittengroomer.helpers.magic.from_file',
return_value='text/plain'
):
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
return file
2016-12-09 02:12:08 +01:00
2017-07-14 20:06:55 +02:00
# Constructor behavior
2017-07-13 23:01:02 +02:00
@mock.patch('kittengroomer.helpers.magic')
2017-07-13 23:36:43 +02:00
def test_init_identify_filename(self, mock_libmagic):
"""Init should identify the filename correctly for src_path."""
2017-07-13 23:01:02 +02:00
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
assert file.filename == 'test.txt'
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_init_identify_extension(self, mock_libmagic):
"""Init should identify the extension for src_path."""
2017-07-13 23:36:43 +02:00
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
assert file.extension == '.txt'
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_init_uppercase_extension(self, mock_libmagic):
2017-07-13 18:56:45 +02:00
"""Init should coerce uppercase extension to lowercase"""
2017-07-13 23:36:43 +02:00
src_path = 'src/TEST.TXT'
dst_path = 'dst/TEST.TXT'
file = FileBase(src_path, dst_path)
assert file.extension == '.txt'
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_has_extension_true(self, mock_libmagic):
"""If the file has an extension, has_extension should == True."""
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
assert file.has_extension is True
@mock.patch('kittengroomer.helpers.magic')
def test_has_extension_false(self, mock_libmagic):
"""If the file has no extension, has_extensions should == False."""
src_path = 'src/test'
dst_path = 'dst/test'
file = FileBase(src_path, dst_path)
assert file.has_extension is False
def test_init_file_doesnt_exist(self):
"""Init should raise an exception if the file doesn't exist."""
2017-07-13 18:56:45 +02:00
with pytest.raises(FileNotFoundError):
FileBase('', '')
2017-07-13 18:56:45 +02:00
def test_init_srcpath_is_directory(self, tmpdir):
"""Init should raise an exception if given a path to a directory."""
2017-07-13 18:56:45 +02:00
with pytest.raises(IsADirectoryError):
FileBase(tmpdir.strpath, tmpdir.strpath)
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_init_symlink(self, mock_libmagic, symlink_file_path):
"""Init should properly identify symlinks."""
2017-07-13 23:36:43 +02:00
file = FileBase(symlink_file_path, '')
assert file.mimetype == 'inode/symlink'
2016-12-09 02:12:08 +01:00
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_is_symlink_attribute(self, mock_libmagic, symlink_file_path):
2017-07-13 18:56:45 +02:00
"""If a file is a symlink, is_symlink should return True."""
2017-07-13 23:36:43 +02:00
file = FileBase(symlink_file_path, '')
assert file.is_symlink is True
2016-12-09 02:12:08 +01:00
2017-07-14 20:06:55 +02:00
def test_init_mimetype_attribute_assigned_correctly(self):
2017-07-13 18:56:45 +02:00
"""When libmagic returns a given mimetype, the mimetype should be
assigned properly."""
2017-07-13 23:36:43 +02:00
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='text/plain'):
file = FileBase('', '')
2017-07-16 20:25:16 +02:00
assert file.mimetype == 'text/plain'
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
def test_maintype_and_subtype_attributes(self):
"""If a file has a full mimetype, maintype and subtype should ==
the appropriate values."""
2017-07-14 22:40:26 +02:00
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='text/plain'):
file = FileBase('', '')
assert file.maintype == 'text'
assert file.subtype == 'plain'
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
def test_has_mimetype_no_full_type(self):
"""If a file doesn't have a full mimetype has_mimetype should == False."""
2017-07-14 22:40:26 +02:00
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='data'):
file = FileBase('', '')
assert file.has_mimetype is False
2017-07-13 18:56:45 +02:00
2017-07-16 20:25:16 +02:00
def test_has_mimetype_mimetype_is_none(self):
"""If a file doesn't have a full mimetype has_mimetype should == False."""
with mock.patch('kittengroomer.helpers.FileBase._determine_mimetype',
return_value=None):
file = FileBase('', '')
assert file.has_mimetype is False
2017-07-14 20:06:55 +02:00
# File properties
def get_property_doesnt_exist(self, text_file):
2017-07-14 20:06:55 +02:00
"""Trying to get a property that doesn't exist should return None."""
assert text_file.get_property('thing') is None
2017-07-13 18:56:45 +02:00
def get_property_builtin(self, text_file):
"""Getting a property that's been set should return that property."""
assert text_file.get_property('is_dangerous') is False
2017-07-13 18:56:45 +02:00
def get_property_user_defined(self, text_file):
"""Getting a user defined property should return that property."""
text_file._user_defined = {'thing': True}
assert text_file.get_property('thing') is True
2017-07-13 18:56:45 +02:00
def set_property_user_defined(self, text_file):
2017-07-14 20:06:55 +02:00
"""Setting a non-default property should make it available for
get_property."""
text_file.set_property('thing', True)
assert text_file.get_property('thing') is True
2017-07-13 18:56:45 +02:00
def set_property_builtin(self, text_file):
2017-07-14 20:06:55 +02:00
"""Setting a builtin property should assign that property."""
text_file.set_property('is_dangerous', True)
assert text_file.get_property('is_dangerous') is True
2017-07-13 18:56:45 +02:00
def test_add_new_description(self, text_file):
"""Adding a new description should add it to the list of description strings."""
text_file.add_description('thing')
2017-07-20 19:38:54 +02:00
assert text_file.get_property('description_string') == 'thing'
2017-07-13 18:56:45 +02:00
def test_add_description_exists(self, text_file):
"""Adding a description that already exists shouldn't duplicate it."""
text_file.add_description('thing')
text_file.add_description('thing')
2017-07-20 19:38:54 +02:00
assert text_file.get_property('description_string') == 'thing'
def test_add_multiple_descriptions(self, text_file):
text_file.add_description('thing')
text_file.add_description('foo')
assert text_file.get_property('description_string') == 'thing, foo'
2017-07-13 18:56:45 +02:00
2017-07-17 15:17:20 +02:00
def test_add_description_not_string(self, text_file):
"""Adding a description that isn't a string should raise an error."""
with pytest.raises(TypeError):
text_file.add_description(123)
def test_add_new_error(self, text_file):
"""Adding a new error should add it to the dict of errors."""
text_file.add_error(Exception, 'thing')
assert text_file.get_property('_errors') == {Exception: 'thing'}
def test_normal_file_mark_dangerous(self, text_file):
"""Marking a file dangerous should identify it as dangerous."""
text_file.make_dangerous()
assert text_file.is_dangerous is True
def test_normal_file_mark_dangerous_filename_change(self, text_file):
"""Marking a file dangerous should mangle the filename."""
filename = text_file.filename
text_file.make_dangerous()
assert text_file.filename == 'DANGEROUS_{}_DANGEROUS'.format(filename)
def test_normal_file_mark_dangerous_add_description(self, text_file):
2017-07-17 15:17:20 +02:00
"""Marking a file as dangerous and passing in a description should add
that description to the file."""
text_file.make_dangerous('thing')
2017-07-20 19:38:54 +02:00
assert text_file.get_property('description_string') == 'thing'
2017-07-13 18:56:45 +02:00
def test_dangerous_file_mark_dangerous(self, text_file):
2017-07-17 15:17:20 +02:00
"""Marking a dangerous file as dangerous should do nothing, and the
file should remain dangerous."""
text_file.make_dangerous()
text_file.make_dangerous()
assert text_file.is_dangerous is True
2017-07-13 18:56:45 +02:00
2017-07-17 15:17:20 +02:00
def test_force_ext_change_filepath(self, text_file):
"""Force_ext should modify the path of the file to end in the
new extension."""
text_file.force_ext('.test')
assert text_file.dst_path.endswith('.test')
def test_force_ext_add_dot(self, text_file):
"""Force_ext should add a dot to an extension given without one."""
text_file.force_ext('test')
assert text_file.dst_path.endswith('.test')
def test_force_ext_change_extension_attr(self, text_file):
"""Force_ext should modify the extension attribute"""
text_file.force_ext('.thing')
assert text_file.extension == '.thing'
def test_force_ext_no_change(self, text_file):
"""Force_ext should do nothing if the current extension is the same
as the new extension."""
text_file.force_ext('.txt')
assert text_file.extension == '.txt'
assert '.txt.txt' not in text_file.dst_path
2017-02-16 23:27:00 +01:00
def test_safe_copy_calls_copy(self, src_dir_path, dest_dir_path):
2017-07-17 15:17:20 +02:00
"""Calling safe_copy should copy the file from the correct path to
the correct destination path."""
file_path = os.path.join(src_dir_path, 'test.txt')
with open(file_path, 'w+') as file:
file.write('')
dst_path = os.path.join(dest_dir_path, 'test.txt')
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='text/plain'):
file = FileBase(file_path, dst_path)
with mock.patch('kittengroomer.helpers.shutil.copy') as mock_copy:
file.safe_copy()
mock_copy.assert_called_once_with(file_path, dst_path)
2017-07-14 20:06:55 +02:00
2017-08-05 00:02:31 +02:00
def test_safe_copy_removes_exec_perms(self):
"""`safe_copy` should create a file that doesn't have any of the
executable bits set."""
pass
def test_safe_copy_makedir_doesnt_exist(self):
"""Calling safe_copy should create intermediate directories in the path
if they don't exist."""
pass
def test_safe_copy_makedir_exists(self):
"""Calling safe_copy when some intermediate directories exist should
result in the creation of the full path and the file."""
pass
def test_create_metadata_file_new(self):
pass
2017-03-10 19:13:38 +01:00
def test_create_metadata_file_already_exists(self):
2017-07-13 18:56:45 +02:00
pass
class TestLogging:
2017-07-13 18:56:45 +02:00
def test_computehash(self):
2017-07-17 15:17:20 +02:00
"""Computehash should return the correct sha256 hash of a given file."""
2017-07-13 18:56:45 +02:00
pass
2016-12-05 17:19:39 +01:00
class TestKittenGroomerBase:
2016-12-09 02:12:08 +01:00
2017-07-13 23:01:02 +02:00
@fixture(scope='class')
def src_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('src').strpath
2016-12-05 17:19:39 +01:00
2017-07-13 23:01:02 +02:00
@fixture(scope='class')
def dest_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('dest').strpath
2016-12-05 17:19:39 +01:00
2016-12-09 22:13:54 +01:00
@fixture
2017-07-17 15:17:20 +02:00
def groomer(self, src_dir_path, dest_dir_path):
2017-07-13 23:01:02 +02:00
return KittenGroomerBase(src_dir_path, dest_dir_path)
2016-12-09 22:13:54 +01:00
2017-07-17 15:17:20 +02:00
def test_list_all_files_includes_file(self, tmpdir, groomer):
"""Calling list_all_files should include files in the given path."""
2016-12-09 22:13:54 +01:00
file = tmpdir.join('test.txt')
file.write('testing')
2017-07-17 15:17:20 +02:00
files = groomer.list_all_files(tmpdir.strpath)
2017-07-13 23:01:02 +02:00
assert file.strpath in files
2017-07-17 15:17:20 +02:00
def test_list_all_files_excludes_dir(self, tmpdir, groomer):
"""Calling list_all_files shouldn't include directories in the given
path."""
2016-12-09 22:13:54 +01:00
testdir = tmpdir.join('testdir')
os.mkdir(testdir.strpath)
2017-07-17 15:17:20 +02:00
files = groomer.list_all_files(tmpdir.strpath)
2016-12-09 22:13:54 +01:00
assert testdir.strpath not in files
2017-07-17 15:17:20 +02:00
def test_safe_remove(self, groomer, src_dir_path):
"""Calling safe_remove should not raise an Exception if trying to
remove a file that doesn't exist."""
groomer.safe_remove(os.path.join(src_dir_path, 'thing'))
def test_safe_mkdir_file_exists(self, groomer, dest_dir_path):
"""Calling safe_mkdir should not overwrite an existing directory."""
filepath = os.path.join(dest_dir_path, 'thing')
os.mkdir(filepath)
groomer.safe_mkdir(filepath)
def test_processdir_not_implemented(self, groomer):
"""Calling processdir should raise an Implementation Required error."""
with pytest.raises(ImplementationRequired):
groomer.processdir('.', '.')