mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-05 14:19:56 -05:00
Squashed commit of the following:
commit 54d06bf8045fceaa2684348d110792e6833b170c
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 16:46:14 2022 -0700
Can't delete files when WROL Mode is enabled.
commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 16:37:42 2022 -0700
Fixing bug where files could not be deleted in series.
commit eed818c50682b19de9f18da2d4daedc54408cc49
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 16:26:31 2022 -0700
Fixing fake_now to reset now() when complete.
commit 183e98cbec25784720ebaeabfac8af4a3fc87da8
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 16:08:14 2022 -0700
Files can be deleted and downloaded.
commit c5bcd25a3da041f42b9e11eae804aa359ab9e985
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 14:41:07 2022 -0700
Files can be deleted or downloaded.
commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 14:11:05 2022 -0700
File is opened in new tab when file is selected.
commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 13:32:32 2022 -0700
Removing files sort because React FE sorts them itself.
commit 4e04753234b11d615c5992bc08e9dd9fb7226c05
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 13:29:52 2022 -0700
Duplicate directories are ignored.
commit c2f7d7929f37655e5c2d11767a512571263b35ef
Author: Roland <roland@learningselfreliance.com>
Date: Sat Jan 29 12:22:38 2022 -0700
Multiple directories can be requested.
commit 31678139c0799d0034d564cc1f8ae448234df90c
Author: Roland <roland@learningselfreliance.com>
Date: Fri Jan 28 17:21:33 2022 -0700
Files are displayed with the correct modified time. Files can be recursively viewed.
commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4
Author: Roland <roland@learningselfreliance.com>
Date: Thu Jan 27 22:37:19 2022 -0700
Files can be viewed. Folders display their children.
This commit is contained in:
parent
2f6b461e4f
commit
dc888c6392
20 changed files with 566 additions and 16 deletions
0
modules/files/test/__init__.py
Normal file
0
modules/files/test/__init__.py
Normal file
116
modules/files/test/test_api.py
Normal file
116
modules/files/test/test_api.py
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
import json
|
||||
from http import HTTPStatus
|
||||
from itertools import zip_longest
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from wrolpi.errors import API_ERRORS, WROLModeEnabled
|
||||
|
||||
|
||||
def test_list_files_api(test_client, make_files_structure, test_directory):
|
||||
files = [
|
||||
'archives/bar.txt',
|
||||
'archives/baz/bar.txt',
|
||||
'archives/baz/foo.txt',
|
||||
'archives/foo.txt',
|
||||
'empty directory/',
|
||||
'videos/other video.mp4',
|
||||
'videos/some video.mp4',
|
||||
]
|
||||
files = make_files_structure(files)
|
||||
files[0].write_text('bar contents')
|
||||
|
||||
def check_get_files(directories, expected_files):
|
||||
request, response = test_client.post('/api/files', content=json.dumps({'directories': directories}))
|
||||
assert not response.json.get('errors')
|
||||
results = sorted(response.json['files'], key=lambda i: i['key'])
|
||||
for f1, f2 in zip_longest(results, expected_files):
|
||||
if f1 is None or f2 is None:
|
||||
assert f1 == f2
|
||||
for key in f2.keys():
|
||||
assert f1[key] == f2[key], f'{f1} != {f2}'
|
||||
|
||||
# Requesting no directories results in the top-level results.
|
||||
expected = [dict(key='archives/'), dict(key='empty directory/'), dict(key='videos/')]
|
||||
check_get_files([], expected)
|
||||
# empty directory is empty
|
||||
check_get_files(['empty directory'], expected)
|
||||
|
||||
expected = [
|
||||
dict(key='archives/'),
|
||||
dict(key='archives/bar.txt', size=12),
|
||||
dict(key='archives/baz/'),
|
||||
dict(key='archives/foo.txt'),
|
||||
dict(key='empty directory/'),
|
||||
dict(key='videos/'),
|
||||
]
|
||||
check_get_files(['archives'], expected)
|
||||
|
||||
# Sub-directories are supported.
|
||||
expected = [
|
||||
dict(key='archives/'),
|
||||
dict(key='archives/bar.txt', size=12),
|
||||
dict(key='archives/baz/'),
|
||||
dict(key='archives/baz/bar.txt'),
|
||||
dict(key='archives/baz/foo.txt'),
|
||||
dict(key='archives/foo.txt'),
|
||||
dict(key='empty directory/'),
|
||||
dict(key='videos/'),
|
||||
]
|
||||
check_get_files(['archives/baz'], expected)
|
||||
|
||||
expected = [
|
||||
dict(key='archives/'),
|
||||
dict(key='archives/bar.txt', size=12),
|
||||
dict(key='archives/baz/'),
|
||||
dict(key='archives/foo.txt'),
|
||||
dict(key='empty directory/'),
|
||||
dict(key='videos/'),
|
||||
dict(key='videos/other video.mp4'),
|
||||
dict(key='videos/some video.mp4'),
|
||||
]
|
||||
check_get_files(['archives', 'videos'], expected)
|
||||
# Order does not matter.
|
||||
check_get_files(['archives', 'videos', 'empty directory'], expected)
|
||||
check_get_files(['archives', 'empty directory', 'videos'], expected)
|
||||
|
||||
|
||||
def test_delete_file(test_client, make_files_structure, test_directory):
|
||||
files = ['bar.txt', 'baz/']
|
||||
make_files_structure(files)
|
||||
|
||||
request, response = test_client.post('/api/files/delete', content=json.dumps({'file': 'bar.txt'}))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert not (test_directory / 'bar.txt').is_file()
|
||||
assert (test_directory / 'baz').is_dir()
|
||||
|
||||
request, response = test_client.post('/api/files/delete', content=json.dumps({'file': 'baz'}))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert not (test_directory / 'bar.txt').is_file()
|
||||
assert (test_directory / 'baz').is_dir()
|
||||
|
||||
request, response = test_client.post('/api/files/delete', content=json.dumps({'file': 'bad file'}))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'file', [
|
||||
'',
|
||||
]
|
||||
)
|
||||
def test_delete_invalid_file(test_client, file):
|
||||
with mock.patch('modules.files.api.lib.delete_file') as mock_delete_file:
|
||||
request, response = test_client.post('/api/files/delete', content=json.dumps({'file': file}))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
mock_delete_file.assert_not_called()
|
||||
|
||||
|
||||
def test_delete_wrol_mode(test_client):
|
||||
"""
|
||||
Can't delete a file when WROL Mode is enabled.
|
||||
"""
|
||||
with mock.patch('wrolpi.common.wrol_mode_enabled', lambda: True):
|
||||
request, response = test_client.post('/api/files/delete', content=json.dumps({'file': 'foo'}))
|
||||
assert response.status_code == HTTPStatus.FORBIDDEN
|
||||
assert response.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
124
modules/files/test/test_lib.py
Normal file
124
modules/files/test/test_lib.py
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from modules.files import lib
|
||||
from wrolpi.errors import InvalidFile
|
||||
|
||||
|
||||
def test_list_files(make_files_structure, test_directory):
|
||||
files = [
|
||||
'archives/bar.txt',
|
||||
'archives/baz/bar.txt',
|
||||
'archives/baz/foo.txt',
|
||||
'archives/foo.txt',
|
||||
'empty directory/',
|
||||
'videos/other video.mp4',
|
||||
'videos/some video.mp4',
|
||||
]
|
||||
make_files_structure(files)
|
||||
|
||||
def check_list_files(path, expected):
|
||||
result = lib.list_files(path)
|
||||
result = sorted(result)
|
||||
assert [str(i.relative_to(test_directory)) for i in result] == expected
|
||||
|
||||
check_list_files([], ['archives', 'empty directory', 'videos'])
|
||||
# Falsey directories are ignored.
|
||||
check_list_files([None, ''], ['archives', 'empty directory', 'videos'])
|
||||
check_list_files(['empty directory'], ['archives', 'empty directory', 'videos'])
|
||||
check_list_files(['empty directory'], ['archives', 'empty directory', 'videos'])
|
||||
# Trailing slash is ignored.
|
||||
check_list_files(['empty directory/'], ['archives', 'empty directory', 'videos'])
|
||||
|
||||
check_list_files(['archives'], [
|
||||
'archives',
|
||||
'archives/bar.txt',
|
||||
'archives/baz',
|
||||
'archives/foo.txt',
|
||||
'empty directory',
|
||||
'videos',
|
||||
])
|
||||
check_list_files(['archives/baz'], [
|
||||
'archives',
|
||||
'archives/bar.txt',
|
||||
'archives/baz',
|
||||
'archives/baz/bar.txt',
|
||||
'archives/baz/foo.txt',
|
||||
'archives/foo.txt',
|
||||
'empty directory',
|
||||
'videos',
|
||||
])
|
||||
# Including duplicate directories does not change results.
|
||||
check_list_files(['archives/baz', 'archives'], [
|
||||
'archives',
|
||||
'archives/bar.txt',
|
||||
'archives/baz',
|
||||
'archives/baz/bar.txt',
|
||||
'archives/baz/foo.txt',
|
||||
'archives/foo.txt',
|
||||
'empty directory',
|
||||
'videos',
|
||||
])
|
||||
check_list_files(['videos'], [
|
||||
'archives',
|
||||
'empty directory',
|
||||
'videos',
|
||||
'videos/other video.mp4',
|
||||
'videos/some video.mp4',
|
||||
])
|
||||
# All files and directories are listed.
|
||||
check_list_files(['videos', 'archives/baz'], [
|
||||
'archives',
|
||||
'archives/bar.txt',
|
||||
'archives/baz',
|
||||
'archives/baz/bar.txt',
|
||||
'archives/baz/foo.txt',
|
||||
'archives/foo.txt',
|
||||
'empty directory',
|
||||
'videos',
|
||||
'videos/other video.mp4',
|
||||
'videos/some video.mp4',
|
||||
])
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'directories,expected', [
|
||||
([Path('foo')], [Path('foo')]),
|
||||
([Path('foo'), Path('bar')], [Path('foo'), Path('bar')]),
|
||||
([Path('foo'), Path('foo/bar'), Path('baz')], [Path('foo/bar'), Path('baz')]),
|
||||
([Path('foo/bar'), Path('foo'), Path('baz')], [Path('foo/bar'), Path('baz')]),
|
||||
([Path('baz'), Path('foo/bar'), Path('foo')], [Path('baz'), Path('foo/bar')]),
|
||||
([Path('foo/bar'), Path('foo'), Path('foo/bar/baz')], [Path('foo/bar/baz')]),
|
||||
]
|
||||
)
|
||||
def test_filter_parent_directories(directories, expected):
|
||||
assert lib.filter_parent_directories(directories) == expected
|
||||
|
||||
|
||||
def test_delete_file(make_files_structure, test_directory):
|
||||
"""
|
||||
File in the media directory can be deleted.
|
||||
"""
|
||||
files = [
|
||||
'archives/foo.txt',
|
||||
'bar.txt',
|
||||
'baz/',
|
||||
]
|
||||
make_files_structure(files)
|
||||
|
||||
lib.delete_file('bar.txt')
|
||||
assert (test_directory / 'archives/foo.txt').is_file()
|
||||
assert not (test_directory / 'bar.txt').is_file()
|
||||
assert (test_directory / 'baz').is_dir()
|
||||
|
||||
lib.delete_file('archives/foo.txt')
|
||||
assert not (test_directory / 'archives/foo.txt').is_file()
|
||||
assert not (test_directory / 'bar.txt').is_file()
|
||||
|
||||
with pytest.raises(InvalidFile):
|
||||
lib.delete_file('baz')
|
||||
with pytest.raises(InvalidFile):
|
||||
lib.delete_file('does not exist')
|
||||
|
||||
assert (test_directory / 'baz').is_dir()
|
||||
Loading…
Add table
Add a link
Reference in a new issue