Skip to content

Commit 2ba7637

Browse files
committed
TEST: Fix community fixture email credentials and DB value in auth and healtcheck
1 parent 59e1798 commit 2ba7637

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

app/services/database/orm/community.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ async def get_community_by_username(
2020

2121
# Executa a declaração na sessão e retorna o primeiro resultado
2222
result = await session.exec(statement)
23-
community = result.first()
23+
community_result = result.first()
24+
2425
# add tratamento de descriptografia do email
25-
if community is not None:
26+
if community_result is not None:
27+
# evitar mutação direta e cache
28+
community = community_result.model_copy()
2629
community.email = decrypt_email(community.email)
27-
28-
return community
30+
else:
31+
community = None
32+
return community # add community not found treatment?
2933

3034

3135
async def create_community(

app/services/encryption.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
def encrypt_email(email: str) -> str:
1919
"""Criptografa uma string de e-mail."""
20-
var_test = cipher.encrypt(email.encode())
21-
print(f"cipher email {var_test}")
22-
var_test_de = cipher.decrypt(var_test).decode()
23-
print(f"decipher email {var_test_de}")
24-
return var_test
20+
encrypted_email = cipher.encrypt(email.encode())
21+
return encrypted_email
2522

2623

2724
def decrypt_email(encrypted_email: str) -> str:
2825
"""Descriptografa uma string de e-mail."""
26+
print("decipher email called")
2927
return cipher.decrypt(encrypted_email).decode()

tests/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ async def async_client(test_app: FastAPI) -> AsyncGenerator[AsyncClient, None]:
7575
yield client
7676

7777

78+
from app.services.encryption import encrypt_email
79+
80+
81+
class UnencryptedCommunityCredentials:
82+
username: str = "community_username_unencrypted"
83+
email: str = "[email protected]"
84+
password: str = "community_password_unencrypte"
85+
hashed_password: str = hash_password(password)
86+
87+
7888
class CommunityCredentials:
7989
username: str = "community_username"
8090
email: str = "[email protected]"
@@ -86,7 +96,7 @@ class CommunityCredentials:
8696
async def community(session: AsyncSession):
8797
community = Community(
8898
username=CommunityCredentials.username,
89-
email=CommunityCredentials.email,
99+
email=encrypt_email(CommunityCredentials.email),
90100
password=CommunityCredentials.hashed_password,
91101
)
92102
session.add(community)

tests/test_auth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def test_authentication_token_endpoint(
1717
# 1. Teste de login com credenciais válidas
1818
# O OAuth2PasswordRequestForm espera 'username' e 'password'
1919
form_data = {
20-
"username": community.username,
20+
"username": CommunityCredentials.username,
2121
"password": CommunityCredentials.password,
2222
}
2323

@@ -62,7 +62,7 @@ async def test_community_me_with_valid_token(
6262
# 1. Obter um token de acesso primeiro
6363
form_data = {
6464
"grant_type": "password",
65-
"username": community.username,
65+
"username": CommunityCredentials.username,
6666
"password": CommunityCredentials.password,
6767
}
6868
token_response = await async_client.post(
@@ -80,8 +80,8 @@ async def test_community_me_with_valid_token(
8080
# Validar a resposta
8181
assert response.status_code == status.HTTP_200_OK
8282
user_data = response.json()
83-
assert user_data["username"] == community.username
84-
assert user_data["email"] == community.email
83+
assert user_data["username"] == CommunityCredentials.username
84+
assert user_data["email"] == CommunityCredentials.email
8585
# Assegurar que a senha não é retornada na resposta
8686
assert "password" not in user_data
8787

0 commit comments

Comments
 (0)