- Add Model Mixins: TimestampMixin, SoftDeleteMixin, AuditMixin, etc. - Add Base Services: BaseService, BulkOperationsMixin, QueryOptimizerMixin - Add Base ViewSets with bulk operations - Add BackgroundJob model for Celery task tracking - Add BaseAppCommand for management commands - Add permissions, pagination, filters, cache, logging - Migrate tests to factory_boy + faker - Add CHANGELOG.md - 297 tests passing
225 lines
9.2 KiB
Python
225 lines
9.2 KiB
Python
"""Tests for user services"""
|
|
|
|
from apps.core.exceptions import NotFoundError
|
|
from apps.user.services import ProfileService, UserService
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
from faker import Faker
|
|
from rest_framework_simplejwt.tokens import RefreshToken
|
|
|
|
from .factories import ProfileFactory, UserFactory
|
|
|
|
User = get_user_model()
|
|
fake = Faker("ru_RU")
|
|
|
|
|
|
class UserServiceTest(TestCase):
|
|
"""Tests for UserService"""
|
|
|
|
def setUp(self):
|
|
self.user = UserFactory.create_user()
|
|
self.user_data = {
|
|
"email": fake.unique.email(),
|
|
"username": fake.unique.user_name(),
|
|
"password": fake.password(length=12, special_chars=False),
|
|
}
|
|
|
|
def test_create_user_success(self):
|
|
"""Test successful user creation"""
|
|
user = UserService.create_user(**self.user_data)
|
|
|
|
self.assertIsInstance(user, User)
|
|
self.assertEqual(user.email, self.user_data["email"])
|
|
self.assertEqual(user.username, self.user_data["username"])
|
|
self.assertTrue(user.check_password(self.user_data["password"]))
|
|
self.assertFalse(user.is_verified) # Default value
|
|
|
|
def test_create_user_with_extra_fields(self):
|
|
"""Test user creation with extra fields"""
|
|
extra_data = self.user_data.copy()
|
|
extra_data["email"] = fake.unique.email()
|
|
extra_data["username"] = fake.unique.user_name()
|
|
extra_data["phone"] = f"+7{fake.numerify('##########')}"
|
|
extra_data["is_verified"] = True
|
|
|
|
user = UserService.create_user(**extra_data)
|
|
|
|
self.assertEqual(user.phone, extra_data["phone"])
|
|
self.assertTrue(user.is_verified)
|
|
|
|
def test_get_user_by_email_found(self):
|
|
"""Test getting user by existing email"""
|
|
found_user = UserService.get_user_by_email(self.user.email)
|
|
self.assertEqual(found_user, self.user)
|
|
|
|
def test_get_user_by_email_not_found(self):
|
|
"""Test getting user by non-existing email raises NotFoundError"""
|
|
nonexistent_email = fake.unique.email()
|
|
with self.assertRaises(NotFoundError) as context:
|
|
UserService.get_user_by_email(nonexistent_email)
|
|
|
|
self.assertEqual(context.exception.code, "not_found")
|
|
self.assertIn("email", context.exception.message)
|
|
|
|
def test_get_user_by_email_or_none_not_found(self):
|
|
"""Test getting user by non-existing email returns None"""
|
|
found_user = UserService.get_user_by_email_or_none(fake.unique.email())
|
|
self.assertIsNone(found_user)
|
|
|
|
def test_get_user_by_id_found(self):
|
|
"""Test getting user by existing ID"""
|
|
found_user = UserService.get_user_by_id(self.user.id)
|
|
self.assertEqual(found_user, self.user)
|
|
|
|
def test_get_user_by_id_not_found(self):
|
|
"""Test getting user by non-existing ID raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError) as context:
|
|
UserService.get_user_by_id(nonexistent_id)
|
|
|
|
self.assertEqual(context.exception.code, "not_found")
|
|
self.assertIn(str(nonexistent_id), context.exception.message)
|
|
|
|
def test_get_user_by_id_or_none_not_found(self):
|
|
"""Test getting user by non-existing ID returns None"""
|
|
found_user = UserService.get_user_by_id_or_none(
|
|
fake.pyint(min_value=900000, max_value=999999)
|
|
)
|
|
self.assertIsNone(found_user)
|
|
|
|
def test_update_user_success(self):
|
|
"""Test successful user update"""
|
|
new_data = {
|
|
"username": fake.unique.user_name(),
|
|
"phone": f"+7{fake.numerify('##########')}",
|
|
}
|
|
|
|
updated_user = UserService.update_user(self.user.id, **new_data)
|
|
|
|
self.assertIsNotNone(updated_user)
|
|
self.assertEqual(updated_user.username, new_data["username"])
|
|
self.assertEqual(updated_user.phone, new_data["phone"])
|
|
|
|
def test_update_user_not_found(self):
|
|
"""Test updating non-existing user raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError):
|
|
UserService.update_user(nonexistent_id, username=fake.user_name())
|
|
|
|
def test_delete_user_success(self):
|
|
"""Test successful user deletion"""
|
|
user_id = self.user.id
|
|
UserService.delete_user(user_id)
|
|
|
|
# Verify user is deleted
|
|
with self.assertRaises(NotFoundError):
|
|
UserService.get_user_by_id(user_id)
|
|
|
|
def test_delete_user_not_found(self):
|
|
"""Test deleting non-existing user raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError):
|
|
UserService.delete_user(nonexistent_id)
|
|
|
|
def test_get_tokens_for_user(self):
|
|
"""Test JWT token generation"""
|
|
tokens = UserService.get_tokens_for_user(self.user)
|
|
|
|
self.assertIn("refresh", tokens)
|
|
self.assertIn("access", tokens)
|
|
self.assertIsInstance(tokens["refresh"], str)
|
|
self.assertIsInstance(tokens["access"], str)
|
|
|
|
# Verify tokens are valid
|
|
refresh = RefreshToken(tokens["refresh"])
|
|
self.assertEqual(refresh["user_id"], self.user.id)
|
|
|
|
def test_verify_email_success(self):
|
|
"""Test successful email verification"""
|
|
self.user.is_verified = False
|
|
self.user.save()
|
|
|
|
user = UserService.verify_email(self.user.id)
|
|
|
|
self.assertEqual(user.id, self.user.id)
|
|
self.user.refresh_from_db()
|
|
self.assertTrue(self.user.is_verified)
|
|
|
|
def test_verify_email_not_found(self):
|
|
"""Test email verification for non-existing user raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError):
|
|
UserService.verify_email(nonexistent_id)
|
|
|
|
|
|
class ProfileServiceTest(TestCase):
|
|
"""Tests for ProfileService"""
|
|
|
|
def setUp(self):
|
|
self.user = UserFactory.create_user()
|
|
self.profile = ProfileFactory.create_profile(user=self.user)
|
|
self.profile_data = {
|
|
"first_name": fake.first_name(),
|
|
"last_name": fake.last_name(),
|
|
"bio": fake.text(max_nb_chars=200),
|
|
"date_of_birth": str(fake.date_of_birth(minimum_age=18, maximum_age=80)),
|
|
}
|
|
|
|
def test_get_profile_by_user_id_found(self):
|
|
"""Test getting profile by existing user ID"""
|
|
found_profile = ProfileService.get_profile_by_user_id(self.user.id)
|
|
self.assertEqual(found_profile, self.profile)
|
|
# Check that user is selected related
|
|
self.assertIsNotNone(found_profile.user)
|
|
|
|
def test_get_profile_by_user_id_not_found(self):
|
|
"""Test getting profile by non-existing user ID raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError) as context:
|
|
ProfileService.get_profile_by_user_id(nonexistent_id)
|
|
|
|
self.assertEqual(context.exception.code, "not_found")
|
|
|
|
def test_get_profile_by_user_id_or_none_not_found(self):
|
|
"""Test getting profile by non-existing user ID returns None"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
found_profile = ProfileService.get_profile_by_user_id_or_none(nonexistent_id)
|
|
self.assertIsNone(found_profile)
|
|
|
|
def test_update_profile_success(self):
|
|
"""Test successful profile update"""
|
|
updated_profile = ProfileService.update_profile(
|
|
self.user.id, **self.profile_data
|
|
)
|
|
|
|
self.assertIsNotNone(updated_profile)
|
|
self.assertEqual(updated_profile.first_name, self.profile_data["first_name"])
|
|
self.assertEqual(updated_profile.last_name, self.profile_data["last_name"])
|
|
self.assertEqual(updated_profile.bio, self.profile_data["bio"])
|
|
|
|
def test_update_profile_not_found(self):
|
|
"""Test updating profile for non-existing user raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError):
|
|
ProfileService.update_profile(nonexistent_id, first_name=fake.first_name())
|
|
|
|
def test_get_full_profile_data_success(self):
|
|
"""Test getting full profile data"""
|
|
profile_data = ProfileService.get_full_profile_data(self.user.id)
|
|
|
|
self.assertIsNotNone(profile_data)
|
|
self.assertEqual(profile_data["id"], self.user.id)
|
|
self.assertEqual(profile_data["email"], self.user.email)
|
|
self.assertEqual(profile_data["username"], self.user.username)
|
|
self.assertEqual(profile_data["first_name"], self.profile.first_name)
|
|
self.assertEqual(profile_data["last_name"], self.profile.last_name)
|
|
self.assertEqual(profile_data["full_name"], self.profile.full_name)
|
|
self.assertEqual(profile_data["bio"], self.profile.bio)
|
|
self.assertEqual(profile_data["is_verified"], self.user.is_verified)
|
|
|
|
def test_get_full_profile_data_not_found(self):
|
|
"""Test getting full profile data for non-existing user raises NotFoundError"""
|
|
nonexistent_id = fake.pyint(min_value=900000, max_value=999999)
|
|
with self.assertRaises(NotFoundError):
|
|
ProfileService.get_full_profile_data(nonexistent_id)
|