Make fixtures function scoped and add new tests

This commit is contained in:
R. Richard 2019-05-22 22:31:55 +02:00
parent 9cb8ef0e72
commit b44fd200c6
2 changed files with 73 additions and 67 deletions

View File

@ -41,7 +41,7 @@ class FileManager: # pylint: disable=too-many-instance-attributes
@allowed_directories.setter
def allowed_directories(self, directories):
self._allowed_directories = directories
self._allowed_directories = [realpath(directory) for directory in directories]
@property
def filename(self):

View File

@ -1,97 +1,103 @@
# pylint: disable=redefined-outer-name
from secrets import token_urlsafe
from os.path import join
from os import chdir, mkdir, symlink
from pathlib import Path
from shutil import rmtree
from os.path import join, realpath
from os import mkdir, rmdir, remove, symlink
from tempfile import TemporaryDirectory
import pytest
from filemanager import FileManager
from file_manager import FileManager
WORKDIR = realpath('test_filemanager')
class ManagerContext:
def __init__(self, folder, manager):
self.folder = folder
self.manager = manager
def workdir_pref(path):
return join(WORKDIR, path)
def join(self, path):
return join(self.folder, path)
@pytest.fixture(scope='module')
def manager():
@pytest.fixture()
def context():
dirs = []
mkdir(WORKDIR)
for i in range(3):
node = workdir_pref('dir_'+str(i).zfill(2))
with TemporaryDirectory() as workdir:
chdir(workdir)
for name in ["allowed", "excluded", "invis"]:
node = join(workdir, name)
mkdir(node)
Path(join(node, 'empty.txt')).touch()
Path(join(node, 'empty.bin')).touch()
dirs.append(node)
yield FileManager(dirs[0], dirs[:-1], exclude=['*/dir_01/*', '*.bin'])
rmtree(WORKDIR)
yield ManagerContext(
workdir,
FileManager(dirs[0], dirs[:-1], exclude=['*/excluded/*'])
)
@pytest.mark.parametrize('subdir', ['dir_00', 'dir_01'])
def test_select_allowed_dirs(manager, subdir):
manager.workdir = workdir_pref(subdir)
assert manager.workdir == workdir_pref(subdir)
newdir = workdir_pref(join(subdir, 'deep'))
@pytest.mark.parametrize('subdir', ['allowed/', 'excluded/'])
def test_select_allowed_dirs(context, subdir):
context.manager.workdir = context.join(subdir)
assert context.manager.workdir == context.join(subdir)
newdir = context.join(subdir+'deep')
mkdir(newdir)
manager.workdir = newdir
assert manager.workdir == newdir
rmdir(newdir)
context.manager.workdir = newdir
assert context.manager.workdir == newdir
@pytest.mark.parametrize('excdir', ['/', workdir_pref('dir_02')])
def test_select_excluded_dirs(manager, excdir):
allowed = manager.allowed_directories
@pytest.mark.parametrize('invdir', ['', 'invis'])
def test_select_forbidden_dirs(context, invdir):
fullpath = context.join(invdir)
with pytest.raises(OSError):
manager.workdir = excdir
assert manager.workdir != excdir
manager.allowed_directories = allowed+[excdir]
manager.workdir = excdir
assert manager.workdir == excdir
manager.allowed_directories = allowed
context.manager.workdir = fullpath
assert context.manager.workdir != fullpath
context.manager.allowed_directories += [fullpath]
context.manager.workdir = fullpath
assert context.manager.workdir == fullpath
@pytest.mark.parametrize('filename', ['another.txt', '*.txt'])
def test_select_allowed_files(manager, filename):
manager.workdir = workdir_pref('dir_00')
newfile = workdir_pref(join('dir_00', filename))
Path(newfile).touch()
assert filename in manager.files
manager.filename = filename
assert manager.filename == filename
remove(newfile)
def test_select_allowed_files(context, filename):
Path(context.join('allowed/'+filename)).touch()
assert filename in context.manager.files
context.manager.filename = filename
assert context.manager.filename == filename
@pytest.mark.parametrize('path', [
['dir_00', 'illegal.bin'],
['dir_01', 'legal.txt']
{'dir': 'allowed/', 'file': 'illegal.bin'},
{'dir': 'excluded/', 'file': 'legal.txt'},
{'dir': 'allowed/', 'file': token_urlsafe(16)+'.bin'},
{'dir': 'excluded/', 'file': token_urlsafe(16)+'.txt'},
{'dir': 'allowed/', 'file': token_urlsafe(32)+'.bin'},
{'dir': 'excluded/', 'file': token_urlsafe(32)+'.txt'}
])
def test_select_excluded_files(manager, path):
manager.workdir = workdir_pref(path[0])
newfile = workdir_pref(join(path[0], path[1]))
Path(newfile).touch()
assert path[1] not in manager.files
def test_select_excluded_files(context, path):
context.manager.workdir = context.join(path['dir'])
context.manager.exclude = ['*/excluded/*', '*.bin']
Path(context.join(path['dir']+path['file'])).touch()
assert path['file'] not in context.manager.files
with pytest.raises(OSError):
manager.filename = path[1]
remove(newfile)
context.manager.filename = path['file']
@pytest.mark.parametrize('path', [
['dir_02/empty.txt', 'dir_00/link.txt'],
['dir_01/empty.txt', 'dir_00/link.bin']
{'src': 'excluded/empty.txt', 'dst': 'allowed/link.txt'},
{'src': 'invis/empty.txt', 'dst': 'allowed/link.txt'},
{'src': 'excluded/empty.txt', 'dst': 'allowed/'+token_urlsafe(16)+'.txt'},
{'src': 'invis/empty.txt', 'dst': 'allowed/'+token_urlsafe(16)+'.txt'},
{'src': 'excluded/empty.txt', 'dst': 'allowed/'+token_urlsafe(32)+'.txt'},
{'src': 'invis/empty.txt', 'dst': 'allowed/'+token_urlsafe(32)+'.txt'}
])
def test_select_excluded_symlinks(manager, path):
manager.workdir = workdir_pref('dir_00')
link = workdir_pref(path[1])
symlink(workdir_pref(path[0]), link)
assert path[1] not in manager.files
remove(link)
def test_select_excluded_symlinks(context, path):
symlink(context.join(path['src']), context.join(path['dst']))
assert path['dst'] not in context.manager.files
def test_read_write_file(manager):
def test_read_write_file(context):
for _ in range(128):
manager.workdir = workdir_pref('dir_00')
manager.filename = 'empty.txt'
context.manager.filename = 'empty.txt'
content = token_urlsafe(32)
manager.file_contents = content
assert manager.file_contents == content
with open(workdir_pref('dir_00/empty.txt'), "r") as ifile:
context.manager.file_contents = content
assert context.manager.file_contents == content
with open(context.join('allowed/empty.txt'), "r") as ifile:
assert ifile.read() == content