-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import pytest | ||
from httpx import AsyncClient | ||
from app.main import app | ||
from app.core.config import settings | ||
|
||
url = "http://fastapi.localhost/api/v1" | ||
|
||
|
@@ -15,10 +16,8 @@ class TestPostLogin: | |
"method, endpoint, data, expected_status, expected_response", | ||
[ | ||
("post", "/login", {"email": "[email protected]", "password": "123456"}, 400, {"detail": "Email or Password incorrect"}), | ||
("post", "/login", {"email": "[email protected]", "password": "admin"}, 200, None), # Add expected JSON response for successful login | ||
# ("get", "/some_endpoint", None, 200, {"result": "success"}), | ||
# ("put", "/another_endpoint", {"key": "value"}, 204, None), | ||
# ("delete", "/delete_endpoint", None, 204, None), | ||
("post", "/login", {"email": settings.FIRST_SUPERUSER_EMAIL, "password": settings.FIRST_SUPERUSER_PASSWORD}, 200, None), # Add expected JSON response for successful login | ||
("post", "/login/new_access_token", {"refresh_token": ""}, 403, {"detail": "Refresh token invalid"}), | ||
], | ||
) | ||
async def test(self, test_client, method, endpoint, data, expected_status, expected_response): | ||
|
@@ -32,7 +31,6 @@ async def test(self, test_client, method, endpoint, data, expected_status, expec | |
else: # Default to POST | ||
response = await client.post(endpoint, json=data) | ||
|
||
await client.aclose() | ||
assert response.status_code == expected_status | ||
if expected_response is not None: | ||
assert response.json() == expected_response |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
from httpx import AsyncClient | ||
from app.main import app | ||
from app.core.config import settings | ||
|
||
url = "http://fastapi.localhost/api/v1" | ||
|
||
@pytest.fixture(scope='function') | ||
async def test_client() -> AsyncClient: | ||
async with AsyncClient(app=app, base_url=url) as client: | ||
yield client | ||
|
||
@pytest.mark.asyncio | ||
class TestPostLogin: | ||
@pytest.mark.parametrize( | ||
"method, endpoint, data, expected_status, expected_response", | ||
[ | ||
("get", "/user", None, 200, None), | ||
("get", "/user/list", None, 200, None), | ||
("get", "/user/list/by_role_name?user_status=active&page=1&size=50", None, 200, None), | ||
], | ||
) | ||
async def test(self, test_client, method, endpoint, data, expected_status, expected_response): | ||
async for client in test_client: | ||
credentials = {"email": settings.FIRST_SUPERUSER_EMAIL, "password": settings.FIRST_SUPERUSER_PASSWORD} | ||
response = await client.post("/login", json=credentials) | ||
access_token = response.json()["data"]["access_token"] | ||
if method == "get": | ||
response = await client.get(endpoint, headers={"Authorization": f"Bearer {access_token}"}) | ||
elif method == "put": | ||
response = await client.put(endpoint, json=data, headers={"Authorization": f"Bearer {access_token}"}) | ||
elif method == "delete": | ||
response = await client.delete(endpoint, headers={"Authorization": f"Bearer {access_token}"}) | ||
else: # Default to POST | ||
response = await client.post(endpoint, json=data, headers={"Authorization": f"Bearer {access_token}"}) | ||
|
||
assert response.status_code == expected_status | ||
if expected_response is not None: | ||
assert response.json() == expected_response |