Skip to content

Commit

Permalink
upload tests and README
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard0803 committed Dec 5, 2023
1 parent 09d2f1e commit f94f4b9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ An example of .env file:
RELOAD="True"
PORT="8000"
ENVIRONMENT="dev"
EMAIL_ADDRESS= "YOUR-EMAIL-ADDRESS"
EMAIL_APP_PASSWORD= "YOUR-EMAIL-PASSWORD-FOR-APPs"
```

You can read more about BaseSettings class here: https://pydantic-docs.helpmanual.io/usage/settings/
Expand Down
27 changes: 25 additions & 2 deletions gestao/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from httpx import AsyncClient
from starlette import status

from gestao.db.models.user import User
from gestao.tests.utils import generate_fake_user
from gestao.web.api.login.utils import generate_password, send_email

Expand All @@ -19,9 +20,9 @@ def test_generate_password_correct() -> None:

def test_generate_password_incorrect() -> None:
try:
password = generate_password("5")
generate_password("5")
except Exception as e:
assert type(e) == TypeError
assert isinstance(e, TypeError)


def test_send_email_incorrect() -> None:
Expand All @@ -44,6 +45,8 @@ async def test_login_user_correct(client: AsyncClient, fastapi_app: FastAPI) ->
response = await client.post(url, json=user)
assert response.status_code == 200
user_data = response.json()
user_instance = User(**user_data)
assert isinstance(user_instance, User)
user_credentials = {
"registration": user_data["registration"],
"password": user_data["password"],
Expand Down Expand Up @@ -77,3 +80,23 @@ async def test_recover_password_incorrect(
url = fastapi_app.url_path_for("recover_password")
response = await client.post(url, json=user_credentials)
assert response.status_code == 404



@pytest.mark.anyio
async def test_recover_password_AuthenticationError(client: AsyncClient, fastapi_app: FastAPI) -> None:
url = fastapi_app.url_path_for("create_user")
user = generate_fake_user()
response = await client.post(url, json=user)
assert response.status_code == 200
user_data = response.json()
user_instance = User(**user_data)
assert isinstance(user_instance, User)

user_credentials = {
"email": user_data['email'],
}

url = fastapi_app.url_path_for("recover_password")
response = await client.post(url, json=user_credentials)
assert response.status_code == 400
49 changes: 40 additions & 9 deletions gestao/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from starlette import status
from uuid import uuid4

from gestao.db.models.user import User
from gestao.tests.utils import generate_fake_user


Expand All @@ -24,10 +25,15 @@ async def test_get_user_correct(client: AsyncClient, fastapi_app: FastAPI) -> No
assert response.status_code == 200
user_data = response.json()
user_id = user_data["id"]
user_instance = User(**user_data)
assert isinstance(user_instance, User)

url = fastapi_app.url_path_for("get_user", user_id=user_id)
response = await client.get(url)
assert response.status_code == 200
user_data = response.json()
user_instance = User(**user_data)
assert isinstance(user_instance, User)


@pytest.mark.anyio
Expand All @@ -43,6 +49,9 @@ async def test_create_user_correct(client: AsyncClient, fastapi_app: FastAPI) ->
user = generate_fake_user()
response = await client.post(url, json=user)
assert response.status_code == 200
user_data = response.json()
user_instance = User(**user_data)
assert isinstance(user_instance, User)


@pytest.mark.anyio
Expand All @@ -54,6 +63,17 @@ async def test_create_user_incorrect(client: AsyncClient, fastapi_app: FastAPI)
assert response.status_code == 422


@pytest.mark.anyio
async def test_create_user_UniqueViolation(client: AsyncClient, fastapi_app: FastAPI) -> None:
url = fastapi_app.url_path_for("create_user")
user = generate_fake_user()
response = await client.post(url, json=user)
assert response.status_code == 200

response = await client.post(url, json=user)
assert response.status_code == 400


@pytest.mark.anyio
async def test_update_user_correct(client: AsyncClient, fastapi_app: FastAPI) -> None:
url = fastapi_app.url_path_for("create_user")
Expand All @@ -62,6 +82,8 @@ async def test_update_user_correct(client: AsyncClient, fastapi_app: FastAPI) ->
assert response.status_code == 200
user_data = response.json()
user_id = user_data["id"]
user_instance = User(**user_data)
assert isinstance(user_instance, User)

url = fastapi_app.url_path_for("update_user", user_id=user_id)
response = await client.put(
Expand All @@ -71,6 +93,9 @@ async def test_update_user_correct(client: AsyncClient, fastapi_app: FastAPI) ->
},
)
assert response.status_code == 200
user_data = response.json()
user_instance = User(**user_data)
assert isinstance(user_instance, User)


@pytest.mark.anyio
Expand All @@ -88,6 +113,8 @@ async def test_delete_user_correct(client: AsyncClient, fastapi_app: FastAPI) ->
assert response.status_code == 200
user_data = response.json()
user_id = user_data["id"]
user_instance = User(**user_data)
assert isinstance(user_instance, User)

url = fastapi_app.url_path_for("delete_user", user_id=user_id)
response = await client.delete(url)
Expand All @@ -109,12 +136,23 @@ async def test_disable_user_correct(client: AsyncClient, fastapi_app: FastAPI) -
assert response.status_code == 200
user_data = response.json()
var = user_data["id"]
user_instance = User(**user_data)
assert isinstance(user_instance, User)

url = fastapi_app.url_path_for("disable_user", user_id=var)
response = await client.patch(url)
assert response.status_code == 200


@pytest.mark.anyio
async def test_disable_user_incorrect(
client: AsyncClient, fastapi_app: FastAPI
) -> None:
url = fastapi_app.url_path_for("disable_user", user_id=str(uuid4()))
response = await client.patch(url)
assert response.status_code == 404


@pytest.mark.anyio
async def test_enable_user_correct(client: AsyncClient, fastapi_app: FastAPI) -> None:
url = fastapi_app.url_path_for("create_user")
Expand All @@ -123,21 +161,14 @@ async def test_enable_user_correct(client: AsyncClient, fastapi_app: FastAPI) ->
assert response.status_code == 200
user_data = response.json()
var = user_data["id"]
user_instance = User(**user_data)
assert isinstance(user_instance, User)

url = fastapi_app.url_path_for("enable_user", user_id=var)
response = await client.patch(url)
assert response.status_code == 200


@pytest.mark.anyio
async def test_disable_user_incorrect(
client: AsyncClient, fastapi_app: FastAPI
) -> None:
url = fastapi_app.url_path_for("disable_user", user_id=str(uuid4()))
response = await client.patch(url)
assert response.status_code == 404


@pytest.mark.anyio
async def test_enable_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None:
url = fastapi_app.url_path_for("enable_user", user_id=str(uuid4()))
Expand Down
2 changes: 1 addition & 1 deletion gestao/web/api/login/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def send_email(
) -> None:
email_data = {
"email_address": os.getenv("EMAIL_ADDRESS"),
"email_password": os.getenv("EMAIL_PASSWORD"),
"email_password": os.getenv("EMAIL_APP_PASSWORD"),
"email_subject": "recover-password SG-SINDPOL",
"email_body": f"""
<img src="{logo_path}" width=200 style="height: auto;">
Expand Down
6 changes: 5 additions & 1 deletion gestao/web/api/login/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from smtplib import SMTPAuthenticationError
from fastapi import APIRouter, HTTPException, Request

from gestao.db.models.user import User
Expand All @@ -23,7 +24,7 @@ async def login_user(login_data: AuthUserDTO) -> User:
)


@router.post("/recover_password")
@router.post("/recover-password")
async def recover_password(request: Request, recover_data: RecoverPasswordDTO) -> None:
try:
url_logo = str(request.url_for("static", path="logo.png"))
Expand All @@ -36,6 +37,9 @@ async def recover_password(request: Request, recover_data: RecoverPasswordDTO) -
logo_path=url_logo,
)
await user.update(password=new_password)
except SMTPAuthenticationError:
logging.error("Authentication error while sending email", exc_info=True)
raise HTTPException(status_code=400, detail='Authentication error while sending email')
except Exception:
logging.error("User not found", exc_info=True)
raise HTTPException(status_code=404, detail="User not found")

0 comments on commit f94f4b9

Please sign in to comment.