Skip to content

Commit bbe6840

Browse files
committed
FEAT: Add ADMIN_EMAIL environment variable, update admin creation logic to use it and apply to test_admin and conftest
1 parent 090c991 commit bbe6840

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

app/routers/admin/routes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
async def create_admin(session: AsyncSession):
1919
ADMIN_USER = os.getenv("ADMIN_USER")
2020
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
21+
ADMIN_EMAIL = os.getenv("ADMIN_EMAIL")
2122
password = ADMIN_PASSWORD
2223
hashed_password = auth.hash_password(password)
2324
community = DBCommunity(
2425
username=ADMIN_USER,
25-
26+
email=ADMIN_EMAIL,
2627
password=hashed_password,
2728
role="admin",
2829
)

docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ services:
1717
- ENCRYPTION_KEY=r0-QKv5qACJNFRqy2cNZCsfZ_zVvehlC-v8zDJb--EI=
1818
- ADMIN_USER=admin
1919
- ADMIN_PASSWORD=admin
20+
- ADMIN_EMAIL = [email protected]
2021
- ALGORITHM=HS256
2122
- ACCESS_TOKEN_EXPIRE_MINUTES=20
2223
restart: unless-stopped

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from collections.abc import AsyncGenerator
23

34
import pytest
@@ -22,6 +23,10 @@
2223
# Usamos engine e AsyncSessionLocal apenas para os testes.
2324
# Isso garante que os testes são isolados e usam o banco de dados em memória.
2425
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
26+
os.environ["ADMIN_USER"] = "ADMIN_USER"
27+
os.environ["ADMIN_PASSWORD"] = "ADMIN_PASSWORD"
28+
os.environ["ADMIN_EMAIL"] = "ADMIN_EMAIL"
29+
2530

2631
test_engine = create_async_engine(TEST_DATABASE_URL, echo=False, future=True)
2732

tests/test_admin.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
from sqlmodel import select
3+
from sqlmodel.ext.asyncio.session import AsyncSession
4+
5+
from app.routers.admin.routes import create_admin
6+
from app.services.auth import verify_password
7+
from app.services.database.models import Community
8+
from app.services.encryption import decrypt_email
9+
10+
# Dados de teste
11+
TEST_USERNAME = "test_user_crypto"
12+
TEST_EMAIL = "[email protected]"
13+
TEST_PASSWORD = "@SafePassword123"
14+
import os
15+
16+
17+
@pytest.mark.asyncio
18+
async def test_insert_admin(session: AsyncSession):
19+
ADMIN_USER = os.getenv("ADMIN_USER")
20+
ADMIN_EMAIL = os.getenv("ADMIN_EMAIL")
21+
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
22+
23+
community = Community(
24+
username=ADMIN_USER,
25+
)
26+
27+
await create_admin(session)
28+
statement = select(Community).where(community.username == ADMIN_USER)
29+
result = await session.exec(statement)
30+
found = result.first()
31+
32+
assert found is not None
33+
assert found.username == ADMIN_USER
34+
assert decrypt_email(found.email) == ADMIN_EMAIL
35+
assert verify_password(ADMIN_PASSWORD, found.password)

0 commit comments

Comments
 (0)