Skip to content

Commit

Permalink
adicao de testes para email
Browse files Browse the repository at this point in the history
  • Loading branch information
victorleaoo committed Jul 28, 2024
1 parent 5bd6c89 commit aa2da5c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
5 changes: 1 addition & 4 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ def test_auth_get_connections(self, setup):

assert response.status_code == 200
assert isinstance(data, list)
assert len(data) == len(enumeration.UserConnection._value2member_map_) # Garante que todos os valores da enum estão presentes
assert len(data) == len(enumeration.UserConnection._value2member_map_)

# Verifica se cada valor da enum está na resposta
for connection in enumeration.UserConnection:
assert connection.value in data

Expand Down Expand Up @@ -216,8 +215,6 @@ def test_auth_activate_account_invalid_code(self, setup):
assert response.status_code == 404
assert data['detail'] == errorMessages.INVALID_CODE



# ADMIN SETUP
def test_admin_setup(self, setup):
# Testa a tentativa com e-mail inválido
Expand Down
61 changes: 61 additions & 0 deletions tests/test_send_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from unittest.mock import patch, AsyncMock
from fastapi.testclient import TestClient
from src.utils.send_mail import send_verification_code

from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType

from src.main import app

client = TestClient(app)

class TestSendVerificationCode:
@pytest.mark.asyncio
@patch('src.utils.send_mail.fm.send_message', new_callable=AsyncMock)
@patch('os.getenv', return_value='http://testurl.com')
async def test_send_verification_code_success(self, mock_getenv, mock_send_message):
email = "testuser@email.com"
code = 123456
is_unb = False

response = await send_verification_code(email, code, is_unb)

assert response.status_code == 200
assert response.body.decode() == '{"status":"success"}'

expected_html = f"<p>Seja bem-vindo ao UnB-TV! Para confirmar a criação da sua conta, utilize o código <strong>{code}</strong></p>"
mock_send_message.assert_called_once_with(
MessageSchema(
subject="Confirme a criação da sua conta",
recipients=[email],
body=expected_html,
subtype=MessageType.html
)
)

@pytest.mark.asyncio
@patch('src.utils.send_mail.fm.send_message', new_callable=AsyncMock)
@patch('os.getenv', return_value='http://testurl.com')
async def test_send_verification_code_is_unb(self, mock_getenv, mock_send_message):
email = "testuser@unb.edu.br"
code = 123456
is_unb = True

response = await send_verification_code(email, code, is_unb)

assert response.status_code == 200
assert response.body.decode() == '{"status":"success"}'

expected_html = (f"<p>Seja bem-vindo ao UnB-TV! Para confirmar a criação da sua conta, utilize o código "
f"<strong>{code}</strong></p>"
f"<p>Como usuário da UnB, você pode configurar uma senha de administrador acessando o "
f"seguinte link após ativar sua conta: <a href='http://testurl.com/adminActivate?email={email}'>"
f"Configurar Senha de Administrador</a></p>")
mock_send_message.assert_called_once_with(
MessageSchema(
subject="Confirme a criação da sua conta",
recipients=[email],
body=expected_html,
subtype=MessageType.html
)
)

0 comments on commit aa2da5c

Please sign in to comment.