From f94f4b989d40536eddbc471128a120f921f82d04 Mon Sep 17 00:00:00 2001 From: Eduard0803 Date: Tue, 5 Dec 2023 20:43:37 -0300 Subject: [PATCH] upload tests and README --- README.md | 2 ++ gestao/tests/test_login.py | 27 +++++++++++++++++-- gestao/tests/test_user.py | 49 ++++++++++++++++++++++++++++------- gestao/web/api/login/utils.py | 2 +- gestao/web/api/login/views.py | 6 ++++- 5 files changed, 73 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 493850a..9c21a6b 100644 --- a/README.md +++ b/README.md @@ -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/ diff --git a/gestao/tests/test_login.py b/gestao/tests/test_login.py index f177d92..90ff8dc 100644 --- a/gestao/tests/test_login.py +++ b/gestao/tests/test_login.py @@ -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 @@ -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: @@ -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"], @@ -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 diff --git a/gestao/tests/test_user.py b/gestao/tests/test_user.py index 766c3df..15225e0 100644 --- a/gestao/tests/test_user.py +++ b/gestao/tests/test_user.py @@ -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 @@ -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 @@ -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 @@ -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") @@ -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( @@ -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 @@ -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) @@ -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") @@ -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())) diff --git a/gestao/web/api/login/utils.py b/gestao/web/api/login/utils.py index be36615..93a9b00 100644 --- a/gestao/web/api/login/utils.py +++ b/gestao/web/api/login/utils.py @@ -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""" diff --git a/gestao/web/api/login/views.py b/gestao/web/api/login/views.py index 08db83b..3f5cecd 100644 --- a/gestao/web/api/login/views.py +++ b/gestao/web/api/login/views.py @@ -1,5 +1,6 @@ import logging +from smtplib import SMTPAuthenticationError from fastapi import APIRouter, HTTPException, Request from gestao.db.models.user import User @@ -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")) @@ -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")