我的AccountActivationView期望对路径的GET请求('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 = '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)
答案 0 :(得分:0)
您只需在点击URL激活用户后重新运行查询以检查用户状态
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)