Source code for tests.users.test_services
from django.test import TestCase
from apps.users.models import ActivationCode, Profile, User
from services.users import FollowersService, UsersService
from services.users.exceptions import AlreadyFollowingUserException, NotFollowingUserException
from services.users.unauthorized import UnauthorizedUserService
from tests.users.factories import ActivationCodeFactory, CategoryFactory, UserFactory # type: ignore
[docs]
class TestUsersService(TestCase):
[docs]
def setUp(self):
self.user = UserFactory(is_active=False, email="original_email@example.com")
self.service = UsersService(self.user)
[docs]
def test_activate_user(self):
self.service.activate()
self.user.refresh_from_db()
self.assertTrue(self.user.is_active)
[docs]
def test_set_password(self):
new_password = "newPassword123"
self.service.set_password(new_password)
self.assertTrue(self.user.check_password(new_password))
[docs]
def test_set_email(self):
new_email = "new_email@example.com"
self.service.set_email(new_email)
self.user.refresh_from_db()
self.assertEqual(self.user.email, new_email)
[docs]
def test_delete(self):
self.service.delete()
self.user.refresh_from_db()
self.assertTrue(self.user.is_deleted)
self.assertFalse(self.user.is_active)
# Assuming you have a related_name="posts" in the User model
self.assertTrue(all(post.is_deleted for post in self.user.posts.all()))
[docs]
def test_confirm_password(self):
password = "newPassword123"
activation_code = ActivationCodeFactory(user=self.user)
uid = activation_code.uid
token = activation_code.code
self.service.confirm_password(password, uid, token)
self.assertTrue(self.user.check_password(password))
with self.assertRaises(ActivationCode.DoesNotExist):
ActivationCode.objects.get(user=self.user, uid=uid, code=token)
[docs]
def test_confirm_email(self):
new_email = "new_email@example.com"
activation_code = ActivationCodeFactory(user=self.user, email=new_email)
uid = activation_code.uid
token = activation_code.code
self.service.confirm_email(uid, token)
self.user.refresh_from_db()
self.assertEqual(self.user.email, new_email)
with self.assertRaises(ActivationCode.DoesNotExist):
ActivationCode.objects.get(user=self.user, uid=uid, code=token)
[docs]
class TestUnauthorizedUserService(TestCase):
[docs]
def test_create_user(self):
data = {
"email": "test@example.com",
"username": "testuser",
}
profile_data = {
"birthdate": "1999-10-10",
}
interests = [CategoryFactory().pk, CategoryFactory().pk]
password = "testpassword"
user = UnauthorizedUserService.create_user(data, profile_data, interests, password)
self.assertTrue(User.objects.filter(email="test@example.com").exists())
self.assertTrue(user.check_password(password))
self.assertEqual(user.interests.count(), 2)
self.assertTrue(Profile.objects.filter(user=user).exists())
[docs]
class FollowersServiceTestCase(TestCase):
[docs]
def test_follow(self):
"""Checking that user1 is now following user2"""
service = FollowersService(self.user1)
service.follow(self.user2)
self.assertIn(self.user2, self.user1.following.all())
[docs]
def test_follow_already_following(self):
"""Checking for AlreadyFollowingUserException"""
service = FollowersService(self.user1)
self.user1.following.add(self.user2)
with self.assertRaises(AlreadyFollowingUserException):
service.follow(self.user2)
[docs]
def test_unfollow(self):
"""Checking that user1 is no longer following user2"""
service = FollowersService(self.user1)
self.user1.following.add(self.user2)
service.unfollow(self.user2)
self.assertNotIn(self.user2, self.user1.following.all())
[docs]
def test_unfollow_not_following(self):
"""Checking for NotFollowingUserException"""
service = FollowersService(self.user1)
with self.assertRaises(NotFollowingUserException):
service.unfollow(self.user2)