Python 为什么我的测试功能没有激活用户?
我有一个django电子邮件验证应用程序,它发送包含编码用户pk和令牌的激活url的电子邮件,然后当应用程序收到url中的正确数据时,它将Python 为什么我的测试功能没有激活用户?,python,django,django-testing,Python,Django,Django Testing,我有一个django电子邮件验证应用程序,它发送包含编码用户pk和令牌的激活url的电子邮件,然后当应用程序收到url中的正确数据时,它将user.is_active布尔值设置为True。我已经编写了一个测试,它应该创建一个用户并发送一个带有编码pk和令牌的get请求,但是即使url是正确的,它也无法激活我的用户(如下所示) views.py包含注册函数和基于类的验证视图 def signup(request): if request.method == 'POST':
user.is_active
布尔值设置为True
。我已经编写了一个测试,它应该创建一个用户并发送一个带有编码pk和令牌的get请求,但是即使url是正确的,它也无法激活我的用户(如下所示)
views.py包含注册函数和基于类的验证视图
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
# ... process form data
user.save()
# Verification
email_subject = 'Activate your followerr account'
domain = get_current_site(request).domain
user_id = urlsafe_base64_encode(force_bytes(user.pk))
link = reverse('activate', kwargs={
'user_id': user_id,
'token': token_generator.make_token(user),
})
activate_url = 'http://' + domain + link
email_body = 'Hello ' + user.name + \
' please use this link to verify your account\n' + activate_url
email = EmailMessage(
email_subject,
email_body,
'noreply@domain.com',
[user.email],
)
email.send()
return redirect('login')
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})
class VerificationView(View):
def get(self, request, user_id, token):
try:
id = urlsafe_base64_decode(force_text(user_id))
user = User.objects.get(pk=id)
if not token_generator.check_token(user, token):
return HttpResponse('login failed')
if user.is_active:
return redirect('login')
user.is_active = True
user.save()
return redirect('login')
except Exception as ex:
pass
return redirect('login')
在我的测试中,我使用post请求创建一个新用户,并检查电子邮件是否存在,然后我从发件箱中的唯一电子邮件中获取令牌(我确定这不是最好的方法,但它现在可以工作),获取用户id,并将其编码为url,然后向视图发送get请求,该视图应找到具有用户id的用户,查看令牌是否与用户匹配,并将user.is\u active
设置为True
并重定向到login
页面,但是我的user.is\u active
值在所有这些操作完成后仍然保持False
。以下是测试:
测试.py
def test_signup_email_confirmation(self):
response = self.client.post('/signup/', data={'email': 'test1@gmail.com',
'name': 'test1',
'gender': True,
'password1': 'test1',
'password2': 'test1'}, follow=True)
self.assertEqual(len(mail.outbox), 1)
for i in mail.outbox:
token = i.body.split(' ')[-1]
print(i.body) # <-- the entire email body with the activation url
token = token[38:-1]
user = User.objects.get(email='test1@gmail.com')
user_id = urlsafe_base64_encode(force_bytes(user.pk))
activation_url = reverse('activate', kwargs={'user_id': user_id, 'token': token})
activation_url = 'http://testserver' + activation_url
res = self.client.get(activation_url, follow=True)
print(activation_url)
print(token_generator.check_token(user, token))
print(res.status_code)
print(user.is_active)
self.assertEqual(res.status_code, 200)
self.assertRedirects(response, '/login/')
正如您可以看到url的匹配,但是
处于活动状态
值仍然保持False
,发生这种情况的原因是当处于活动状态
为False
时,测试类中的用户
对象是从数据库加载的。当视图设置为_active=True
并保存用户时,测试方法无法知道实例已修改
因此,现在测试方法中的用户
实例仍然具有旧值
如果要测试修改后的字段,必须要求Django在保存后刷新对象:
user.refresh_from_db()
print(user.is_active)
您的VerificationView中可能发生了一些异常,但异常中只有
pass
。试着打印出异常(使用print(ex)),看看为什么代码没有达到让用户验证的步骤。看起来没有例外,因为我的控制台中什么都没有。这实际上很有帮助,非常感谢你提供的信息,我在哪里可以读到更多关于测试的内容?@SLDem Hi,我对这个问题的解释是错误的。我现在用正确的信息修改了答案。它与测试没有具体关系。这就是Python(或者更确切地说是任何其他编程语言)的工作方式。
user.refresh_from_db()
print(user.is_active)