Tests: switched to using f-strings.
Previously, it was necessary to support older versions of Python for compatibility. F-strings were released in Python 3.6. Python 3.5 was marked as unsupported by the end of 2020, so now it's possible to start using f-strings safely for better readability and performance.
This commit is contained in:
@@ -3,6 +3,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from unit.applications.proto import TestApplicationProto
|
||||
from unit.option import option
|
||||
|
||||
|
||||
class TestStaticChroot(TestApplicationProto):
|
||||
@@ -10,22 +11,22 @@ class TestStaticChroot(TestApplicationProto):
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_method_fixture(self, temp_dir):
|
||||
os.makedirs(temp_dir + '/assets/dir')
|
||||
Path(temp_dir + '/assets/index.html').write_text('0123456789')
|
||||
Path(temp_dir + '/assets/dir/file').write_text('blah')
|
||||
os.makedirs(f'{temp_dir}/assets/dir')
|
||||
Path(f'{temp_dir}/assets/index.html').write_text('0123456789')
|
||||
Path(f'{temp_dir}/assets/dir/file').write_text('blah')
|
||||
|
||||
self.test_path = '/' + os.path.relpath(Path(__file__))
|
||||
self.test_path = f'/{os.path.relpath(Path(__file__))}'
|
||||
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {"*:7080": {"pass": "routes"}},
|
||||
"routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
|
||||
"routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}],
|
||||
}
|
||||
)
|
||||
|
||||
def update_action(self, share, chroot):
|
||||
def update_action(self, chroot, share=f'{option.temp_dir}/assets$uri'):
|
||||
return self.conf(
|
||||
{"share": share, "chroot": chroot},
|
||||
{'chroot': chroot, 'share': share},
|
||||
'routes/0/action',
|
||||
)
|
||||
|
||||
@@ -38,9 +39,7 @@ class TestStaticChroot(TestApplicationProto):
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'default chroot'
|
||||
assert self.get(url='/index.html')['status'] == 200, 'default chroot 2'
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + "/assets$uri", temp_dir + "/assets/dir"
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/dir')
|
||||
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'chroot'
|
||||
assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2'
|
||||
@@ -48,101 +47,89 @@ class TestStaticChroot(TestApplicationProto):
|
||||
|
||||
def test_share_chroot_array(self, temp_dir):
|
||||
assert 'success' in self.update_action(
|
||||
["/blah", temp_dir + "/assets$uri"], temp_dir + "/assets/dir"
|
||||
f'{temp_dir}/assets/dir', ["/blah", f'{temp_dir}/assets$uri']
|
||||
)
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'share array'
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
["/blah", temp_dir + '/assets$uri'], temp_dir + '/assets/$host'
|
||||
f'{temp_dir}/assets/$host',
|
||||
['/blah', f'{temp_dir}/assets$uri'],
|
||||
)
|
||||
assert self.get_custom('/dir/file', 'dir') == 200, 'array variable'
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
["/blah", "/blah2"], temp_dir + "/assets/dir"
|
||||
f'{temp_dir}/assets/dir', ['/blah', '/blah2']
|
||||
)
|
||||
assert self.get()['status'] != 200, 'share array bad'
|
||||
|
||||
def test_static_chroot_permission(self, is_su, temp_dir):
|
||||
if is_su:
|
||||
pytest.skip('does\'t work under root')
|
||||
pytest.skip("does't work under root")
|
||||
|
||||
os.chmod(temp_dir + '/assets/dir', 0o100)
|
||||
os.chmod(f'{temp_dir}/assets/dir', 0o100)
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + "/assets$uri", temp_dir + "/assets/dir"
|
||||
f'{temp_dir}/assets/dir'
|
||||
), 'configure chroot'
|
||||
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'chroot'
|
||||
|
||||
def test_static_chroot_empty(self, temp_dir):
|
||||
assert 'success' in self.update_action(temp_dir + "/assets$uri", "")
|
||||
assert 'success' in self.update_action('')
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'empty absolute'
|
||||
|
||||
assert 'success' in self.update_action(".$uri", "")
|
||||
assert 'success' in self.update_action("", ".$uri")
|
||||
assert self.get(url=self.test_path)['status'] == 200, 'empty relative'
|
||||
|
||||
def test_static_chroot_relative(self, is_su, temp_dir):
|
||||
if is_su:
|
||||
pytest.skip('does\'t work under root')
|
||||
pytest.skip("Does't work under root.")
|
||||
|
||||
assert 'success' in self.update_action(temp_dir + "/assets$uri", ".")
|
||||
assert 'success' in self.update_action('.')
|
||||
assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
|
||||
|
||||
assert 'success' in self.conf({"share": ".$uri"}, 'routes/0/action')
|
||||
assert self.get(url=self.test_path)['status'] == 200, 'relative share'
|
||||
|
||||
assert 'success' in self.update_action(".$uri", ".")
|
||||
assert 'success' in self.update_action(".", ".$uri")
|
||||
assert self.get(url=self.test_path)['status'] == 200, 'relative'
|
||||
|
||||
def test_static_chroot_variables(self, temp_dir):
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + '/assets$uri', temp_dir + '/assets/$host'
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/$host')
|
||||
assert self.get_custom('/dir/file', 'dir') == 200
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + '/assets$uri', temp_dir + '/assets/${host}'
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/${{host}}')
|
||||
assert self.get_custom('/dir/file', 'dir') == 200
|
||||
|
||||
def test_static_chroot_variables_buildin_start(self, temp_dir):
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + '/assets/dir/$host', '$uri/assets/dir'
|
||||
'$uri/assets/dir',
|
||||
f'{temp_dir}/assets/dir/$host',
|
||||
)
|
||||
assert self.get_custom(temp_dir, 'file') == 200
|
||||
|
||||
def test_static_chroot_variables_buildin_mid(self, temp_dir):
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + '/assets$uri', temp_dir + '/$host/dir'
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/$host/dir')
|
||||
assert self.get_custom('/dir/file', 'assets') == 200
|
||||
|
||||
def test_static_chroot_variables_buildin_end(self, temp_dir):
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + '/assets$uri', temp_dir + '/assets/$host'
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/$host')
|
||||
assert self.get_custom('/dir/file', 'dir') == 200
|
||||
|
||||
def test_static_chroot_slash(self, temp_dir):
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + "/assets$uri", temp_dir + "/assets/dir/"
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/dir/')
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'slash end'
|
||||
assert self.get(url='/dirxfile')['status'] == 403, 'slash end bad'
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + "/assets$uri", temp_dir + "/assets/dir"
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/dir')
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'no slash end'
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + "/assets$uri", temp_dir + "/assets/dir/"
|
||||
)
|
||||
assert 'success' in self.update_action(f'{temp_dir}/assets/dir/')
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'slash end 2'
|
||||
assert self.get(url='/dirxfile')['status'] == 403, 'slash end 2 bad'
|
||||
|
||||
assert 'success' in self.update_action(
|
||||
temp_dir + "///assets/////$uri", temp_dir + "//assets////dir///"
|
||||
f'{temp_dir}//assets////dir///', f'{temp_dir}///assets/////$uri'
|
||||
)
|
||||
assert self.get(url='/dir/file')['status'] == 200, 'multiple slashes'
|
||||
|
||||
@@ -160,9 +147,5 @@ class TestStaticChroot(TestApplicationProto):
|
||||
'routes/0/action',
|
||||
), 'configure mount error'
|
||||
|
||||
assert 'error' in self.update_action(
|
||||
temp_dir + '/assets$uri', temp_dir + '/assets/d$r$uri'
|
||||
)
|
||||
assert 'error' in self.update_action(
|
||||
temp_dir + '/assets$uri', temp_dir + '/assets/$$uri'
|
||||
)
|
||||
assert 'error' in self.update_action(f'{temp_dir}/assets/d$r$uri')
|
||||
assert 'error' in self.update_action(f'{temp_dir}/assets/$$uri')
|
||||
|
||||
Reference in New Issue
Block a user