From e205a2e4a403a4f6c536696a82745c536053240c Mon Sep 17 00:00:00 2001 From: PuQing Date: Mon, 29 Jan 2024 01:49:12 +0800 Subject: [PATCH] Add login and user tests, update gitignore --- backend/.gitignore | 1 + backend/app/app/tests/test_login.py | 48 ++++++++++++++++++++++ backend/app/app/tests/test_user.py | 19 +++++++++ backend/app/app/web/api/endpoints/users.py | 3 ++ 4 files changed, 71 insertions(+) create mode 100644 backend/app/app/tests/test_login.py create mode 100644 backend/app/app/tests/test_user.py diff --git a/backend/.gitignore b/backend/.gitignore index 41d8e2e..3bf83f8 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,4 @@ __pycache__ app.egg-info *.pyc +coverage.xml diff --git a/backend/app/app/tests/test_login.py b/backend/app/app/tests/test_login.py new file mode 100644 index 0000000..79eb4f3 --- /dev/null +++ b/backend/app/app/tests/test_login.py @@ -0,0 +1,48 @@ +import pytest +from fastapi import FastAPI +from httpx import AsyncClient +from starlette import status + + +@pytest.mark.anyio +async def test_login(client: AsyncClient, fastapi_app: FastAPI) -> None: + """ + Checks the health endpoint. + + :param client: client for the app. + :param fastapi_app: current FastAPI application. + """ + response = await client.post( + "/api/login/access-token", + data={"username": "admin@localhost.com", "password": "admin"}, + ) + + assert response.status_code == status.HTTP_200_OK + assert "access_token" in response.json() + assert "token_type" in response.json() + assert response.json()["token_type"] == "bearer" + + +@pytest.mark.anyio +async def test_token(client: AsyncClient, fastapi_app: FastAPI) -> None: + """ + Checks the health endpoint. + + :param client: client for the app. + :param fastapi_app: current FastAPI application. + """ + + response = await client.post( + "/api/login/access-token", + data={"username": "admin@localhost.com", "password": "admin"}, + ) + + token = response.json()["access_token"] + + response = await client.post( + "/api/login/test-token", + headers={"Authorization": f"Bearer {token}"}, + ) + + assert response.status_code == status.HTTP_200_OK + assert response.json()["email"] == "admin@localhost.com" diff --git a/backend/app/app/tests/test_user.py b/backend/app/app/tests/test_user.py new file mode 100644 index 0000000..069376e --- /dev/null +++ b/backend/app/app/tests/test_user.py @@ -0,0 +1,19 @@ +import pytest +from fastapi import FastAPI +from httpx import AsyncClient +from starlette import status + + +@pytest.mark.anyio +async def test_create_user(client: AsyncClient, fastapi_app: FastAPI) -> None: + """ + Checks the health endpoint. + + :param client: client for the app. + :param fastapi_app: current FastAPI application. + """ + response = await client.get( + "/api/users/", headers={"skip": "0", "limit": "10"} + ) + + assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/app/app/web/api/endpoints/users.py b/backend/app/app/web/api/endpoints/users.py index fa1cb84..298fcb3 100644 --- a/backend/app/app/web/api/endpoints/users.py +++ b/backend/app/app/web/api/endpoints/users.py @@ -2,6 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException from sqlmodel import select +from starlette import status from app.core.config import settings from app.models import ( @@ -26,6 +27,7 @@ "/", dependencies=[Depends(get_current_active_superuser)], response_model=list[UserOut], + status_code=status.HTTP_200_OK, ) def read_users( session: SessionDep, @@ -44,6 +46,7 @@ def read_users( "/", dependencies=[Depends(get_current_active_superuser)], response_model=UserOut, + status_code=status.HTTP_201_CREATED, ) def create_user(*, session: SessionDep, user_in: UserCreate) -> Any: """