File size: 3,072 Bytes
507973e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Unit tests for authentication service."""
import pytest
from sqlalchemy.ext.asyncio import AsyncSession

from src.core.security import verify_password, decode_token
from src.models.user import UserRegister, UserLogin
from src.services.auth_service import register, login


@pytest.mark.asyncio
async def test_register_creates_user_and_token(test_session: AsyncSession):
    """Test that registration creates a user and returns a token."""
    data = UserRegister(
        email="newuser@example.com",
        password="securepass123",
        name="New User",
    )

    user, token = await register(test_session, data)

    assert user.id is not None
    assert user.email == data.email
    assert user.name == data.name
    assert user.is_active is True
    assert verify_password("securepass123", user.hashed_password)
    assert token is not None

    # Verify token contains user_id
    payload = decode_token(token)
    assert int(payload["sub"]) == user.id


@pytest.mark.asyncio
async def test_register_duplicate_email_fails(test_session: AsyncSession):
    """Test that registering with duplicate email fails."""
    data = UserRegister(
        email="duplicate@example.com",
        password="password123",
        name="User One",
    )

    await register(test_session, data)

    # Try to register again with same email
    with pytest.raises(ValueError, match="Email already registered"):
        await register(test_session, data)


@pytest.mark.asyncio
async def test_login_with_valid_credentials(test_session: AsyncSession):
    """Test login with correct credentials returns token."""
    # Register a user first
    register_data = UserRegister(
        email="login@example.com",
        password="mypassword",
        name="Login User",
    )
    await register(test_session, register_data)

    # Now login
    login_data = UserLogin(email="login@example.com", password="mypassword")
    user, token = await login(test_session, login_data)

    assert user.email == "login@example.com"
    assert token is not None

    # Verify token
    payload = decode_token(token)
    assert int(payload["sub"]) == user.id


@pytest.mark.asyncio
async def test_login_with_wrong_password_fails(test_session: AsyncSession):
    """Test login with wrong password fails."""
    # Register a user
    register_data = UserRegister(
        email="wrongpass@example.com",
        password="correctpass",
        name="Wrong Pass User",
    )
    await register(test_session, register_data)

    # Try to login with wrong password
    login_data = UserLogin(email="wrongpass@example.com", password="wrongpass")

    with pytest.raises(ValueError, match="Invalid credentials"):
        await login(test_session, login_data)


@pytest.mark.asyncio
async def test_login_with_nonexistent_email_fails(test_session: AsyncSession):
    """Test login with non-existent email fails."""
    login_data = UserLogin(email="noexist@example.com", password="password")

    with pytest.raises(ValueError, match="Invalid credentials"):
        await login(test_session, login_data)