-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed all tests except test_user_creation_limit
- Loading branch information
Showing
7 changed files
with
74 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,68 +3,63 @@ | |
from collections.abc import Generator | ||
from datetime import datetime | ||
from typing import Any, Literal | ||
from uuid import uuid4 | ||
from zoneinfo import ZoneInfo | ||
|
||
import boto3 | ||
import pytest | ||
from httpx import ASGITransport, AsyncClient | ||
from moto import mock_aws | ||
from mypy_boto3_s3 import S3Client | ||
from polyfactory.pytest_plugin import register_fixture | ||
from sqlmodel import SQLModel, create_engine | ||
|
||
from smolvault.auth.models import NewUserDTO | ||
from smolvault.clients.database import ( | ||
DatabaseClient, | ||
FileMetadataRecord, | ||
) | ||
from smolvault.main import app | ||
from smolvault.models import FileMetadata | ||
|
||
from .factories import UserFactory | ||
|
||
user_factory_fixture = register_fixture(UserFactory, name="user_factory") | ||
|
||
|
||
class TestDatabaseClient(DatabaseClient): | ||
def __init__(self, filename: str) -> None: | ||
self.engine = create_engine(f"sqlite:///{filename}", echo=False, connect_args={"check_same_thread": False}) | ||
def __init__(self) -> None: | ||
self.engine = create_engine("sqlite:///test.db", echo=False, connect_args={"check_same_thread": False}) | ||
SQLModel.metadata.create_all(self.engine) | ||
|
||
|
||
@pytest.fixture | ||
@pytest.fixture(scope="module") | ||
def anyio_backend() -> Literal["asyncio"]: | ||
return "asyncio" | ||
|
||
|
||
@pytest.fixture | ||
def temp_db(monkeypatch: pytest.MonkeyPatch) -> Generator[TestDatabaseClient, Any, Any]: | ||
db_filename = f"test-{uuid4().hex}.db" | ||
os.environ["SMOLVAULT_DB"] = db_filename | ||
monkeypatch.setenv("SMOLVAULT_DB", db_filename) | ||
client = TestDatabaseClient(db_filename) | ||
yield client | ||
pathlib.Path(db_filename).unlink() | ||
def db_client() -> TestDatabaseClient: | ||
return TestDatabaseClient() | ||
|
||
|
||
@pytest.fixture | ||
def _user(temp_db: TestDatabaseClient) -> None: | ||
user = NewUserDTO( | ||
username="testuser", | ||
password="testpassword", # type: ignore # noqa: S106 | ||
email="[email protected]", | ||
full_name="John Smith", | ||
) | ||
temp_db.add_user(user) | ||
def user(user_factory: UserFactory, db_client: TestDatabaseClient) -> tuple[str, str]: | ||
user = user_factory.build() | ||
db_client.add_user(user) | ||
return user.username, user.password.get_secret_value() | ||
|
||
|
||
@pytest.fixture | ||
def client(_user: None) -> AsyncClient: | ||
@pytest.fixture(scope="module") | ||
def client() -> AsyncClient: | ||
app.dependency_overrides[DatabaseClient] = TestDatabaseClient | ||
return AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") # type: ignore | ||
|
||
|
||
@pytest.fixture | ||
async def access_token(client: AsyncClient) -> str: | ||
async def access_token(client: AsyncClient, user: tuple[str, str]) -> str: | ||
username, password = user | ||
response = await client.post( | ||
"/token", | ||
data={"username": "testuser", "password": "testpassword"}, | ||
data={"username": username, "password": password}, | ||
) | ||
return response.json()["access_token"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,18 +9,6 @@ | |
from smolvault.models import FileMetadata | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_read_root(client: AsyncClient, access_token: str) -> None: | ||
response = await client.get("/", headers={"Authorization": f"Bearer {access_token}"}) | ||
assert response.status_code == 200 | ||
assert response.json() == { | ||
"email": "[email protected]", | ||
"full_name": "John Smith", | ||
"username": "testuser", | ||
"id": 1, | ||
} | ||
|
||
|
||
@pytest.mark.anyio | ||
@pytest.mark.usefixtures("_test_bucket") | ||
async def test_list_files( | ||
|
@@ -49,12 +37,13 @@ async def test_get_file( | |
access_token: str, | ||
) -> None: | ||
filename = f"{uuid4().hex[:6]}-camera.png" | ||
await client.post( | ||
response = await client.post( | ||
"/file/upload", | ||
files={"file": (filename, camera_img, "image/png")}, | ||
data={"tags": "camera,photo"}, | ||
headers={"Authorization": f"Bearer {access_token}"}, | ||
) | ||
assert response.status_code == 201 | ||
response = await client.get( | ||
"/file/original", | ||
params={"filename": filename}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.