-
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.
wip: trying to isolate users from one another in test scenarios. todo…
…: generate mock users using polyfactory
- Loading branch information
Showing
11 changed files
with
78 additions
and
28 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
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 |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
import pathlib | ||
from collections.abc import Generator | ||
from datetime import datetime | ||
from typing import Any | ||
from typing import Any, Literal | ||
from uuid import uuid4 | ||
from zoneinfo import ZoneInfo | ||
|
||
import boto3 | ||
|
@@ -22,24 +23,38 @@ | |
|
||
|
||
class TestDatabaseClient(DatabaseClient): | ||
def __init__(self) -> None: | ||
self.engine = create_engine("sqlite:///test.db", echo=False, connect_args={"check_same_thread": False}) | ||
def __init__(self, filename: str) -> None: | ||
self.engine = create_engine(f"sqlite:///{filename}", echo=False, connect_args={"check_same_thread": False}) | ||
SQLModel.metadata.create_all(self.engine) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def _user() -> None: | ||
client = TestDatabaseClient() | ||
@pytest.fixture | ||
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() | ||
|
||
|
||
@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", | ||
) | ||
client.add_user(user) | ||
temp_db.add_user(user) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
@pytest.fixture | ||
def client(_user: None) -> AsyncClient: | ||
app.dependency_overrides[DatabaseClient] = TestDatabaseClient | ||
return AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") # type: ignore | ||
|
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
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
from httpx import AsyncClient | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
@pytest.fixture | ||
async def user_john(client: AsyncClient) -> str: | ||
""" | ||
Creates a new user 'John' and returns the access token for John. | ||
|
@@ -29,7 +29,7 @@ async def user_john(client: AsyncClient) -> str: | |
return response.json()["access_token"] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
@pytest.fixture | ||
async def user_jane(client: AsyncClient) -> str: | ||
""" | ||
Creates a new user 'Jane' and returns the access token for Jane. | ||
|
@@ -51,7 +51,7 @@ async def user_jane(client: AsyncClient) -> str: | |
return response.json()["access_token"] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
@pytest.fixture | ||
async def user_jack(client: AsyncClient) -> str: | ||
""" | ||
Creates a new user 'Jack' and returns the access token for Jack. | ||
|
@@ -76,7 +76,7 @@ async def user_jack(client: AsyncClient) -> str: | |
return response.json()["access_token"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.anyio | ||
@pytest.mark.usefixtures("_test_bucket") | ||
async def test_get_file(client: AsyncClient, camera_img: bytes, user_john: str, user_jane: str) -> None: | ||
""" | ||
|
@@ -128,7 +128,7 @@ async def _fully_populated_user_bucket( | |
bytes_uploaded += img_size | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.anyio | ||
@pytest.mark.usefixtures("_fully_populated_user_bucket") | ||
async def test_user_over_daily_upload_limit(client: AsyncClient, camera_img: bytes, user_jack: str) -> None: | ||
""" | ||
|
@@ -144,3 +144,23 @@ async def test_user_over_daily_upload_limit(client: AsyncClient, camera_img: byt | |
) | ||
assert response.status_code == 400 | ||
assert response.json() == {"error": "Upload limit exceeded"} | ||
|
||
|
||
@pytest.mark.anyio | ||
@pytest.mark.usefixtures("_test_bucket") | ||
async def test_user_creation_limit(client: AsyncClient, user_john: str, user_jane: str, user_jack: str) -> None: | ||
""" | ||
Test that the system blocks new user creation if the user limit has been reached. | ||
""" | ||
|
||
user_data = { | ||
"username": "kate", | ||
"password": "testpassword", | ||
"email": "[email protected]", | ||
"full_name": "Kate Smith", | ||
} | ||
response = await client.post( | ||
"/users/new", | ||
json=user_data, | ||
) | ||
assert response.status_code == 400 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.