diff --git a/gestao/tests/test_gestao.py b/gestao/tests/test_gestao.py index 6ed1d99..911c4b4 100644 --- a/gestao/tests/test_gestao.py +++ b/gestao/tests/test_gestao.py @@ -2,9 +2,6 @@ from fastapi import FastAPI from httpx import AsyncClient from starlette import status -from uuid import uuid4 - -from gestao.tests.utils import generate_fake_user @pytest.mark.anyio @@ -12,57 +9,3 @@ async def test_health(client: AsyncClient, fastapi_app: FastAPI) -> None: url = fastapi_app.url_path_for("health_check") response = await client.get(url) assert response.status_code == status.HTTP_200_OK - - -@pytest.mark.anyio -async def test_get_users(client: AsyncClient, fastapi_app: FastAPI) -> None: - url = fastapi_app.url_path_for("get_users") - response = await client.get(url) - assert response.status_code == 200 - - -@pytest.mark.anyio -async def test_get_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: - url = fastapi_app.url_path_for("get_user", user_id=str(uuid4())) - response = await client.get(url) - assert response.status_code == 404 - - -@pytest.mark.anyio -async def test_create_user_correct(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 - - -@pytest.mark.anyio -async def test_create_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: - url = fastapi_app.url_path_for("create_user") - user = generate_fake_user() - user.pop("name") - response = await client.post(url, json=user) - assert response.status_code == 422 - - -@pytest.mark.anyio -async def test_update_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: - url = fastapi_app.url_path_for("update_user", user_id=str(uuid4())) - response = await client.put(url, json={}) - assert response.status_code == 404 - - -@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())) - response = await client.patch(url) - assert response.status_code == 404 diff --git a/gestao/tests/test_login.py b/gestao/tests/test_login.py new file mode 100644 index 0000000..257d83f --- /dev/null +++ b/gestao/tests/test_login.py @@ -0,0 +1,57 @@ +import pytest +from fastapi import FastAPI +from httpx import AsyncClient +from starlette import status + +from gestao.tests.utils import generate_fake_user +from gestao.web.api.login.utils import generate_password, send_email + + +def test_generate_password_correct() -> None: + password = generate_password() + assert isinstance(password, str) + assert len(password) == 8 + length = 15 + password = generate_password(length) + assert isinstance(password, str) + assert len(password) == 15 + + +@pytest.mark.anyio +async def test_login_user_correct(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_credentials = { + 'registration': user_data['registration'], + 'password': user_data['password'], + } + + url = fastapi_app.url_path_for('login_user') + response = await client.post(url, json=user_credentials) + assert response.status_code == 200 + + +@pytest.mark.anyio +async def test_login_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: + user_credentials = { + 'registration': 'email@example.com', + 'password': 'password_example', + } + + url = fastapi_app.url_path_for('login_user') + response = await client.post(url, json=user_credentials) + assert response.status_code == 404 + + +@pytest.mark.anyio +async def test_recover_password_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: + user_credentials = { + 'email': 'email@example.com', + } + + url = fastapi_app.url_path_for('recover_password') + response = await client.post(url, json=user_credentials) + assert response.status_code == 404 diff --git a/gestao/tests/test_user.py b/gestao/tests/test_user.py new file mode 100644 index 0000000..3271a55 --- /dev/null +++ b/gestao/tests/test_user.py @@ -0,0 +1,101 @@ +import pytest +from fastapi import FastAPI +from httpx import AsyncClient +from starlette import status +from uuid import uuid4 + +from gestao.tests.utils import generate_fake_user + + +@pytest.mark.anyio +async def test_get_users(client: AsyncClient, fastapi_app: FastAPI) -> None: + url = fastapi_app.url_path_for("get_users") + response = await client.get(url) + assert response.status_code == 200 + user_data = response.json() + assert isinstance(user_data, list) + + +@pytest.mark.anyio +async def test_get_user_correct(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_id = user_data["id"] + + url = fastapi_app.url_path_for("get_user", user_id=user_id) + response = await client.get(url) + assert response.status_code == 200 + + +@pytest.mark.anyio +async def test_get_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: + url = fastapi_app.url_path_for("get_user", user_id=str(uuid4())) + response = await client.get(url) + assert response.status_code == 404 + + +@pytest.mark.anyio +async def test_create_user_correct(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 + + +@pytest.mark.anyio +async def test_create_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: + url = fastapi_app.url_path_for("create_user") + user = generate_fake_user() + user.pop("name") + response = await client.post(url, json=user) + assert response.status_code == 422 + + +@pytest.mark.anyio +async def test_update_user_correct(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_id = user_data["id"] + + url = fastapi_app.url_path_for("update_user", user_id=user_id) + response = await client.put( + url, + json={ + "name": "joao", + }, + ) + assert response.status_code == 200 + + +@pytest.mark.anyio +async def test_update_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: + url = fastapi_app.url_path_for("update_user", user_id=str(uuid4())) + response = await client.put(url, json={}) + assert response.status_code == 404 + + +@pytest.mark.anyio +async def test_delete_user_correct(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_id = user_data["id"] + + url = fastapi_app.url_path_for("delete_user", user_id=user_id) + response = await client.delete(url) + assert response.status_code == 200 + + +@pytest.mark.anyio +async def test_delete_user_incorrect(client: AsyncClient, fastapi_app: FastAPI) -> None: + url = fastapi_app.url_path_for("delete_user", user_id=str(uuid4())) + response = await client.delete(url) + assert response.status_code == 200 diff --git a/gestao/tests/utils.py b/gestao/tests/utils.py index 95e5c81..aa69afe 100644 --- a/gestao/tests/utils.py +++ b/gestao/tests/utils.py @@ -1,40 +1,37 @@ def generate_fake_user(): - from faker import Faker - - f = Faker("pt-br") user = { - "name": f"{f.name()}", - "address": f"{f.address()}", - "neighborhood": "Gama", - "city": f"{f.city()}", - "state": f"{f.state()}", - "zipcode": f"{f.postcode()}", - "cpf": f"{f.random_number(digits=11)}", - "rg": f"{f.random_number(digits=7)}", - "birth_date": f"{f.date_of_birth()}", - "place_of_birth": f"{f.city()}", - "blood_type": "A+", - "gender": "M", - "father_name": f"{f.name()}", - "mother_name": f"{f.name()}", + "name": "joao", + "address": "null", + "neighborhood": "null", + "city": "null", + "state": "null", + "zipcode": "null", + "cpf": "null", + "rg": "null", + "birth_date": "2023-01-01", + "place_of_birth": "null", + "blood_type": "null", + "gender": "null", + "father_name": "null", + "mother_name": "null", "position": "null", "occupancy": "null", - "admission_date": f"{f.date_of_birth()}", - "situation": "active", - "phone": f"{f.phone_number()}", - "email": f'{f.email(domain="sindpol.org.br")}', - "marital_status": "alone", - "education": "Bachelor Degree", - "registration": f"{f.random_number(digits=10)}", - "role": "cop", + "admission_date": "2023-01-01", + "situation": "null", + "phone": "null", + "email": "joao@example.com", + "marital_status": "null", + "education": "null", + "registration": "null", + "role": "null", "category": "null", "pattern": "null", "dispatcher": "null", - "dispatched_date": f"{f.date_of_birth()}", - "war_name": f"{f.name()}", - "password": "admin", - "rg_consignor": "SSP", - "rg_date": f"{f.date_of_birth()}", + "dispatched_date": "2023-01-01", + "war_name": "null", + "password": "null", + "rg_consignor": "null", + "rg_date": "2023-01-01", "situation_obs": "null", } diff --git a/gestao/web/api/login/utils.py b/gestao/web/api/login/utils.py index 30ce3c9..52714b7 100644 --- a/gestao/web/api/login/utils.py +++ b/gestao/web/api/login/utils.py @@ -5,7 +5,7 @@ import string -def generate_password(length=8): +def generate_password(length: int=8) -> str: characters = string.ascii_letters + string.digits password = "".join(secrets.choice(characters) for _ in range(length)) return password