Мой AccountActivationView ожидает GET-запроса к path('email/confirm/', если ключ существует, AccountActivationView вызывает функцию активации и переключает is_active в профиле пользователя. Я пытаюсь реализовать тест для этой функции с помощью Django TestCase, но он не дает ожидаемых результатов. Класс представления перенаправляет клиента в нужное место, но состояние is_active учетной записи пользователя не меняется. Может ли кто-нибудь указать мне в правильном направлении, пожалуйста?
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = '[email protected]'
self.password = 'test123test'
# Creating user
User.objects.create_user(
email=self.email, username=self.username, password=self.password)
def test_activating_user(self):
'''Activating user account using link in the email'''
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
activation_key = EmailActivation.objects.get(
email=self.email).key
# The initial state of account and email should be inactive
self.assertEqual(user_email_activation_status, False)
self.assertEqual(user, False)
# Activating the account with get request to email/confirm/<key>
activation = self.client.get(
reverse('accounts:email-activate', kwargs = {'key': activation_key}))
print(activation)
# Checking if activation was successful
self.assertEqual(user_email_activation_status, True)
self.assertEqual(user, True)
Вам просто нужно повторно запустить запрос, чтобы проверить статус пользователя после того, как вы нажмете URL-адрес, чтобы активировать пользователя.
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = '[email protected]'
self.password = 'test123test'
# Creating user
User.objects.create_user(
email=self.email, username=self.username, password=self.password)
def test_activating_user(self):
'''Activating user account using link in the email'''
activation_key = EmailActivation.objects.get(
email=self.email).key
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
# The initial state of account and email should be inactive
self.assertEqual(user_email_activation_status, False)
self.assertEqual(user, False)
# Activating the account with get request to email/confirm/<key>
activation = self.client.get(
reverse('accounts:email-activate', kwargs = {'key': activation_key}))
print(activation)
# Checking if activation was successful
# get the value again after calling the route to activate the user
user_email_activation_status = EmailActivation.objects.get(
email=self.email).activated
user = User.objects.get(email=self.email).is_active
self.assertEqual(user_email_activation_status, True)
self.assertEqual(user, True)
Не могу поверить, что я потратил полчаса, пытаясь понять, что я делаю неправильно. Я был уверен, что запрос попадает в базу данных дважды. Спасибо, ashishmohite.