|
3 | 3 | from sqlmodel.ext.asyncio.session import AsyncSession |
4 | 4 |
|
5 | 5 | from app.services.database.models import Community |
| 6 | +from app.services.database.orm.community import ( |
| 7 | + create_community, |
| 8 | + get_community_by_username, |
| 9 | +) |
| 10 | + |
| 11 | +# Dados de teste |
| 12 | +TEST_USERNAME = "test_user_crypto" |
| 13 | +TEST_EMAIL = "crypto@test.com" |
| 14 | +TEST_PASSWORD = "@SafePassword123" |
6 | 15 |
|
7 | 16 |
|
8 | 17 | @pytest.mark.asyncio |
9 | 18 | async def test_insert_communities(session: AsyncSession): |
10 | 19 | community = Community( |
11 | | - username="admin", |
12 | | - email="teste@teste.com", |
13 | | - password="@teste123", |
| 20 | + username=TEST_USERNAME, |
| 21 | + email=TEST_EMAIL, |
| 22 | + password=TEST_PASSWORD, |
14 | 23 | ) |
15 | 24 | session.add(community) |
16 | 25 | await session.commit() |
17 | 26 | await session.refresh(community) |
18 | 27 |
|
19 | | - statement = select(Community).where(Community.username == "admin") |
| 28 | + statement = select(Community).where(Community.username == TEST_USERNAME) |
20 | 29 | result = await session.exec(statement) |
21 | 30 | found = result.first() |
22 | 31 |
|
23 | 32 | assert found is not None |
24 | | - assert found.username == "admin" |
25 | | - assert found.email == "teste@teste.com" |
26 | | - assert found.password == "@teste123" |
| 33 | + assert found.username == TEST_USERNAME |
| 34 | + assert found.email == TEST_EMAIL |
| 35 | + assert found.password == TEST_PASSWORD |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_community_orm_flow_with_encryption_transparency( |
| 40 | + session: AsyncSession, |
| 41 | +): |
| 42 | + """ |
| 43 | + Testa a criação e a leitura de uma comunidade, validando |
| 44 | + que o ORM (propriedades) garante a transparência da criptografia. |
| 45 | + """ |
| 46 | + new_community = Community( |
| 47 | + username=TEST_USERNAME, |
| 48 | + email=TEST_EMAIL, |
| 49 | + password=TEST_PASSWORD, |
| 50 | + ) |
| 51 | + |
| 52 | + # 2. Ação: Use a função ORM para criar a comunidade |
| 53 | + created_community = await create_community( |
| 54 | + community=new_community, session=session |
| 55 | + ) |
| 56 | + |
| 57 | + # 3. Asserção de Leitura Transparente (getters) |
| 58 | + # Ao acessar 'created_community.email', ele DEVE retornar o email descriptografado |
| 59 | + assert created_community.email == TEST_EMAIL |
| 60 | + assert created_community.username == TEST_USERNAME |
| 61 | + |
| 62 | + # 4. Asserção da Criptografia (Validação do Armazenamento) |
| 63 | + # Acessar o campo interno '_email' para provar que está criptografado |
| 64 | + stored_email = created_community._email |
| 65 | + |
| 66 | + # O email armazenado não deve ser igual ao email original (em texto puro) |
| 67 | + assert stored_email != TEST_EMAIL |
| 68 | + |
| 69 | + # O email armazenado deve ser um valor válido de Fernet (a criptografia) |
| 70 | + # Usamos a função de criptografia para ter um valor esperado |
| 71 | + assert stored_email == TEST_EMAIL |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_get_community_by_username_orm(session: AsyncSession): |
| 76 | + """ |
| 77 | + Testa a função de leitura 'get_community_by_username' e a descriptografia. |
| 78 | + """ |
| 79 | + # 1. Preparação: Crie um registro diretamente no banco para garantir |
| 80 | + # que o teste não dependa da função create_community |
| 81 | + community_to_insert = Community( |
| 82 | + username="newreader_test", |
| 83 | + email=TEST_EMAIL, |
| 84 | + password=TEST_PASSWORD, |
| 85 | + ) |
| 86 | + |
| 87 | + # Fazemos a inserção no banco de forma manual para forçar a criptografia |
| 88 | + # (O setter do modelo faz a criptografia automaticamente aqui) |
| 89 | + session.add(community_to_insert) |
| 90 | + await session.commit() |
| 91 | + await session.refresh(community_to_insert) |
| 92 | + |
| 93 | + # 2. Ação: Use a função ORM para buscar o registro |
| 94 | + found_community = await get_community_by_username( |
| 95 | + username="newreader_test", session=session |
| 96 | + ) |
| 97 | + |
| 98 | + # 3. Asserções |
| 99 | + assert found_community is not None |
| 100 | + |
| 101 | + # O email lido deve ser o valor original (descriptografado pelo modelo) |
| 102 | + assert found_community.email == TEST_EMAIL |
| 103 | + assert found_community.username == "newreader_test" |
| 104 | + |
| 105 | + # Garante que a senha está correta (embora não seja o foco, é bom manter) |
| 106 | + assert found_community.password == TEST_PASSWORD |
0 commit comments