chore: fix lint
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
from io import BytesIO
|
||||
from types import SimpleNamespace
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from fastapi import UploadFile
|
||||
from PIL import Image
|
||||
from pytest_mock.plugin import MockerFixture
|
||||
from starlette.datastructures import Headers
|
||||
|
||||
from lnbits.core.crud import create_account
|
||||
from lnbits.core.crud.assets import get_user_asset, get_user_assets_count
|
||||
@@ -19,24 +20,34 @@ async def _create_user() -> str:
|
||||
return user_id
|
||||
|
||||
|
||||
def _make_upload_file(
|
||||
contents: bytes,
|
||||
*,
|
||||
filename: str,
|
||||
content_type: str | None,
|
||||
) -> UploadFile:
|
||||
headers = (
|
||||
Headers({"content-type": content_type}) if content_type is not None else None
|
||||
)
|
||||
return UploadFile(BytesIO(contents), filename=filename, headers=headers)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_create_user_asset_validates_upload_constraints(
|
||||
settings: Settings, mocker: MockerFixture
|
||||
):
|
||||
file_without_type = SimpleNamespace(
|
||||
content_type=None,
|
||||
filename="a.txt",
|
||||
read=mocker.AsyncMock(return_value=b"hello"),
|
||||
)
|
||||
file_without_type = _make_upload_file(b"hello", filename="a.txt", content_type=None)
|
||||
with pytest.raises(ValueError, match="File must have a content type."):
|
||||
await create_user_asset("user-1", file_without_type, is_public=False)
|
||||
|
||||
bad_type = SimpleNamespace(
|
||||
content_type="application/x-msdownload",
|
||||
bad_type = _make_upload_file(
|
||||
b"hello",
|
||||
filename="bad.bin",
|
||||
read=mocker.AsyncMock(return_value=b"hello"),
|
||||
content_type="application/x-msdownload",
|
||||
)
|
||||
with pytest.raises(ValueError, match="File type 'application/x-msdownload' not allowed."):
|
||||
with pytest.raises(
|
||||
ValueError, match="File type 'application/x-msdownload' not allowed."
|
||||
):
|
||||
await create_user_asset("user-1", bad_type, is_public=False)
|
||||
|
||||
original_max_assets = settings.lnbits_max_assets_per_user
|
||||
@@ -47,27 +58,25 @@ async def test_create_user_asset_validates_upload_constraints(
|
||||
settings.lnbits_max_asset_size_mb = 1
|
||||
settings.lnbits_assets_no_limit_users = []
|
||||
limited_user = await _create_user()
|
||||
allowed_type = SimpleNamespace(
|
||||
content_type="text/plain",
|
||||
filename="ok.txt",
|
||||
read=mocker.AsyncMock(return_value=b"hello"),
|
||||
allowed_type = _make_upload_file(
|
||||
b"hello", filename="ok.txt", content_type="text/plain"
|
||||
)
|
||||
await create_user_asset(limited_user, allowed_type, is_public=False)
|
||||
|
||||
blocked_by_count = SimpleNamespace(
|
||||
content_type="text/plain",
|
||||
blocked_by_count = _make_upload_file(
|
||||
b"again",
|
||||
filename="again.txt",
|
||||
read=mocker.AsyncMock(return_value=b"again"),
|
||||
content_type="text/plain",
|
||||
)
|
||||
with pytest.raises(ValueError, match="Max upload count of 1 exceeded."):
|
||||
await create_user_asset(limited_user, blocked_by_count, is_public=False)
|
||||
|
||||
settings.lnbits_max_asset_size_mb = 0.000001
|
||||
oversized_user = await _create_user()
|
||||
large_file = SimpleNamespace(
|
||||
content_type="text/plain",
|
||||
large_file = _make_upload_file(
|
||||
b"0123456789",
|
||||
filename="ok.txt",
|
||||
read=mocker.AsyncMock(return_value=b"0123456789"),
|
||||
content_type="text/plain",
|
||||
)
|
||||
with pytest.raises(ValueError, match="File limit of 1e-06MB exceeded."):
|
||||
await create_user_asset(oversized_user, large_file, is_public=False)
|
||||
@@ -84,11 +93,7 @@ async def test_create_user_asset_success(mocker: MockerFixture):
|
||||
"lnbits.core.services.assets.thumbnail_from_bytes",
|
||||
return_value=None,
|
||||
)
|
||||
file = SimpleNamespace(
|
||||
content_type="text/plain",
|
||||
filename="hello.txt",
|
||||
read=mocker.AsyncMock(return_value=b"hello"),
|
||||
)
|
||||
file = _make_upload_file(b"hello", filename="hello.txt", content_type="text/plain")
|
||||
|
||||
asset = await create_user_asset(user_id, file, is_public=True)
|
||||
stored = await get_user_asset(user_id, asset.id)
|
||||
|
||||
Reference in New Issue
Block a user