Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
jonra1993 committed Jul 29, 2023
2 parents b368f5c + 9b513b2 commit dc0e434
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
8 changes: 3 additions & 5 deletions backend/app/test/api/test_login.py
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"

Expand All @@ -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):
Expand All @@ -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
39 changes: 39 additions & 0 deletions backend/app/test/api/test_user.py
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

0 comments on commit dc0e434

Please sign in to comment.