inital
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Unit tests package."""
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Unit tests for models."""
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from app.models import User, Organization
|
||||
from app.utils.constants import UserStatus
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestUserModel:
|
||||
"""Tests for User model."""
|
||||
|
||||
def test_create_user(self, db):
|
||||
"""Test creating a user."""
|
||||
user = User(
|
||||
email="test@example.com",
|
||||
full_name="Test User",
|
||||
status=UserStatus.ACTIVE,
|
||||
)
|
||||
user.save()
|
||||
|
||||
assert user.id is not None
|
||||
assert user.email == "test@example.com"
|
||||
assert user.full_name == "Test User"
|
||||
assert user.status == UserStatus.ACTIVE
|
||||
assert user.created_at is not None
|
||||
assert user.deleted_at is None
|
||||
|
||||
def test_user_to_dict(self, test_user):
|
||||
"""Test user to_dict method."""
|
||||
user_dict = test_user.to_dict()
|
||||
|
||||
assert "id" in user_dict
|
||||
assert "email" in user_dict
|
||||
assert user_dict["email"] == test_user.email
|
||||
assert "created_at" in user_dict
|
||||
|
||||
def test_user_soft_delete(self, test_user):
|
||||
"""Test soft deleting a user."""
|
||||
test_user.delete(soft=True)
|
||||
|
||||
assert test_user.deleted_at is not None
|
||||
assert isinstance(test_user.deleted_at, datetime)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestOrganizationModel:
|
||||
"""Tests for Organization model."""
|
||||
|
||||
def test_create_organization(self, db):
|
||||
"""Test creating an organization."""
|
||||
org = Organization(
|
||||
name="Test Org",
|
||||
slug="test-org",
|
||||
description="Test organization",
|
||||
)
|
||||
org.save()
|
||||
|
||||
assert org.id is not None
|
||||
assert org.name == "Test Org"
|
||||
assert org.slug == "test-org"
|
||||
assert org.is_active is True
|
||||
assert org.created_at is not None
|
||||
|
||||
def test_organization_to_dict(self, test_organization):
|
||||
"""Test organization to_dict method."""
|
||||
org_dict = test_organization.to_dict()
|
||||
|
||||
assert "id" in org_dict
|
||||
assert "name" in org_dict
|
||||
assert org_dict["name"] == test_organization.name
|
||||
assert "slug" in org_dict
|
||||
|
||||
def test_get_member_count(self, test_organization):
|
||||
"""Test getting member count."""
|
||||
count = test_organization.get_member_count()
|
||||
assert count == 1 # Only the owner
|
||||
@@ -0,0 +1 @@
|
||||
"""Services unit tests package."""
|
||||
@@ -0,0 +1,102 @@
|
||||
"""Unit tests for AuthService."""
|
||||
import pytest
|
||||
from app.services.auth_service import AuthService
|
||||
from app.exceptions.auth_exceptions import InvalidCredentialsError
|
||||
from app.exceptions.validation_exceptions import EmailAlreadyExistsError
|
||||
from app.utils.constants import UserStatus, AuthMethodType
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestAuthService:
|
||||
"""Tests for AuthService."""
|
||||
|
||||
def test_register_user(self, db):
|
||||
"""Test user registration."""
|
||||
email = "newuser@example.com"
|
||||
password = "SecurePassword123!"
|
||||
full_name = "New User"
|
||||
|
||||
user = AuthService.register_user(
|
||||
email=email,
|
||||
password=password,
|
||||
full_name=full_name,
|
||||
)
|
||||
|
||||
assert user.id is not None
|
||||
assert user.email == email.lower()
|
||||
assert user.full_name == full_name
|
||||
assert user.status == UserStatus.ACTIVE
|
||||
assert user.has_password_auth()
|
||||
|
||||
def test_register_duplicate_email(self, db, test_user):
|
||||
"""Test registering with duplicate email."""
|
||||
with pytest.raises(EmailAlreadyExistsError):
|
||||
AuthService.register_user(
|
||||
email=test_user.email,
|
||||
password="SomePassword123!",
|
||||
)
|
||||
|
||||
def test_authenticate_success(self, db, test_user):
|
||||
"""Test successful authentication."""
|
||||
user = AuthService.authenticate(
|
||||
email=test_user.email,
|
||||
password=test_user._test_password,
|
||||
)
|
||||
|
||||
assert user.id == test_user.id
|
||||
assert user.last_login_at is not None
|
||||
|
||||
def test_authenticate_wrong_password(self, db, test_user):
|
||||
"""Test authentication with wrong password."""
|
||||
with pytest.raises(InvalidCredentialsError):
|
||||
AuthService.authenticate(
|
||||
email=test_user.email,
|
||||
password="WrongPassword123!",
|
||||
)
|
||||
|
||||
def test_authenticate_nonexistent_user(self, db):
|
||||
"""Test authentication with non-existent email."""
|
||||
with pytest.raises(InvalidCredentialsError):
|
||||
AuthService.authenticate(
|
||||
email="nonexistent@example.com",
|
||||
password="SomePassword123!",
|
||||
)
|
||||
|
||||
def test_create_session(self, app, db, test_user):
|
||||
"""Test creating a session."""
|
||||
with app.test_request_context():
|
||||
session = AuthService.create_session(test_user)
|
||||
|
||||
assert session.id is not None
|
||||
assert session.user_id == test_user.id
|
||||
assert session.token is not None
|
||||
assert session.is_active()
|
||||
|
||||
def test_change_password(self, app, db, test_user):
|
||||
"""Test changing password."""
|
||||
with app.test_request_context():
|
||||
new_password = "NewPassword456!"
|
||||
|
||||
AuthService.change_password(
|
||||
user=test_user,
|
||||
current_password=test_user._test_password,
|
||||
new_password=new_password,
|
||||
)
|
||||
|
||||
# Verify can login with new password
|
||||
user = AuthService.authenticate(
|
||||
email=test_user.email,
|
||||
password=new_password,
|
||||
)
|
||||
|
||||
assert user.id == test_user.id
|
||||
|
||||
def test_change_password_wrong_current(self, app, db, test_user):
|
||||
"""Test changing password with wrong current password."""
|
||||
with app.test_request_context():
|
||||
with pytest.raises(InvalidCredentialsError):
|
||||
AuthService.change_password(
|
||||
user=test_user,
|
||||
current_password="WrongPassword123!",
|
||||
new_password="NewPassword456!",
|
||||
)
|
||||
Reference in New Issue
Block a user