unit/test/test_static_share.py
Andrei Zeliankou 7934dcabbc 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.
2023-02-21 17:21:29 +00:00

72 lines
2.1 KiB
Python

import os
from pathlib import Path
import pytest
from unit.applications.proto import TestApplicationProto
class TestStaticShare(TestApplicationProto):
prerequisites = {}
@pytest.fixture(autouse=True)
def setup_method_fixture(self, temp_dir):
os.makedirs(f'{temp_dir}/assets/dir')
os.makedirs(f'{temp_dir}/assets/dir2')
Path(f'{temp_dir}/assets/dir/file').write_text('1')
Path(f'{temp_dir}/assets/dir2/file2').write_text('2')
assert 'success' in self.conf(
{
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}],
"applications": {},
}
)
def action_update(self, conf):
assert 'success' in self.conf(conf, 'routes/0/action')
def test_share_array(self, temp_dir):
assert self.get(url='/dir/file')['body'] == '1'
assert self.get(url='/dir2/file2')['body'] == '2'
self.action_update({"share": [f'{temp_dir}/assets/dir$uri']})
assert self.get(url='/file')['body'] == '1'
assert self.get(url='/file2')['status'] == 404
self.action_update(
{
"share": [
f'{temp_dir}/assets/dir$uri',
f'{temp_dir}/assets/dir2$uri',
]
}
)
assert self.get(url='/file')['body'] == '1'
assert self.get(url='/file2')['body'] == '2'
self.action_update(
{
"share": [
f'{temp_dir}/assets/dir2$uri',
f'{temp_dir}/assets/dir3$uri',
]
}
)
assert self.get(url='/file')['status'] == 404
assert self.get(url='/file2')['body'] == '2'
def test_share_array_fallback(self):
self.action_update(
{"share": ["/blah", "/blah2"], "fallback": {"return": 201}}
)
assert self.get()['status'] == 201
def test_share_array_invalid(self):
assert 'error' in self.conf({"share": []}, 'routes/0/action')
assert 'error' in self.conf({"share": {}}, 'routes/0/action')