Skip to content

Commit

Permalink
Add login and user tests, update gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
AndPuQing committed Jan 28, 2024
1 parent 5992f5b commit e205a2e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
app.egg-info
*.pyc
coverage.xml
48 changes: 48 additions & 0 deletions backend/app/app/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -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": "[email protected]", "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": "[email protected]", "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"] == "[email protected]"
19 changes: 19 additions & 0 deletions backend/app/app/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions backend/app/app/web/api/endpoints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand All @@ -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:
"""
Expand Down

0 comments on commit e205a2e

Please sign in to comment.