37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
|
|
"""Authentication tests."""
|
||
|
|
import pytest
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.core.auth import get_password_hash, verify_password
|
||
|
|
from app.models.user import User
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_password_hash():
|
||
|
|
"""Test password hashing and verification."""
|
||
|
|
password = "testpassword"
|
||
|
|
hashed = get_password_hash(password)
|
||
|
|
assert verify_password(password, hashed)
|
||
|
|
assert not verify_password("wrongpassword", hashed)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_user_creation(db: AsyncSession):
|
||
|
|
"""Test user creation."""
|
||
|
|
user = User(
|
||
|
|
username="testuser",
|
||
|
|
password_hash=get_password_hash("testpass"),
|
||
|
|
role="member",
|
||
|
|
is_active=True,
|
||
|
|
)
|
||
|
|
db.add(user)
|
||
|
|
await db.commit()
|
||
|
|
await db.refresh(user)
|
||
|
|
|
||
|
|
result = await db.execute(select(User).where(User.username == "testuser"))
|
||
|
|
fetched = result.scalar_one_or_none()
|
||
|
|
assert fetched is not None
|
||
|
|
assert fetched.username == "testuser"
|
||
|
|
assert fetched.role == "member"
|