My AccountActivationView expecting a GET request to path('email/confirm/', if the key exists, AccountActivationView invokes activation function and toggles is_active in user profile. I'm trying to implement a test for this feature using Django TestCase, but it does not produce the results I'm expecting. The view class redirects Client to the right location, but is_active state of the user account does not change. Can anyone point me in the right direction, please?
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = 'test@test.com'
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)
You just need to rerun the query to check the status of user after you hit the URL to activate the user
class TestUserAccounts(TestCase):
def setUp(self):
self.client = Client()
# Initial user data
self.username = 'TestTest'
self.email = 'test@test.com'
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)