【问题标题】:Why is my test function not activating the user?为什么我的测试功能没有激活用户?
【发布时间】:2020-11-08 17:54:09
【问题描述】:

我有一个 django 电子邮件验证应用程序,它发送带有包含编码用户 pk 和令牌的激活 url 的电子邮件,然后当应用程序在 url 中收到正确的数据时,它将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')

在我的测试中,我创建了一个带有发布请求的新用户并检查电子邮件是否存在,然后我从发件箱中唯一的电子邮件中获取令牌(我确定这不是最好的方法,但它工作正常现在),获取 user_id 并将其编码为 url 并向视图发送 get 请求,该视图应该找到具有 user_id 的用户,查看令牌是否与用户匹配并将 user.is_active 设置为 True 并重定向到 login页面,但是我的user.is_active 值在所有这些完成后仍然保持False。这是测试:

tests.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/')

我已经尝试使用不带'http://testserver' 部分的activation_url

打印语句的结果在这里:

Hello test1 please use this link to verify your account
http://testserver/activate/Mg/ad1ap1-887a3ff4466d2e1d098603ddad24355e/
http://testserver/activate/Mg/ad1ap1-887a3ff4466d2e1d098603ddad24355e/
200
True
False

如您所见,网址匹配,但 is_active 值仍为 False

【问题讨论】:

  • 您的 VerificationView 中可能发生了一些异常,但异常中只有 pass。尝试打印出异常(使用 print(ex)),看看为什么代码没有进入验证用户的步骤。
  • 似乎没有 exepton,因为我在控制台中什么也没有得到

标签: python django django-testing


【解决方案1】:

发生这种情况的原因是,当is_activeFalse 时,您的测试类中的user 对象是从数据库中加载的。当视图设置is_active=True并保存用户时,测试方法无法知道实例已修改。

所以,现在测试方法中的user 实例仍然具有旧值。

如果要测试修改的字段,保存后必须要求Django刷新对象:

user.refresh_from_db()
print(user.is_active)

【讨论】:

  • 它确实有帮助,非常感谢您提供的信息,我在哪里可以阅读更多关于测试的内容?
  • @SLDem 嗨,我对这个问题的解释是错误的。我现在用正确的信息修改了答案。它与测试没有具体关系。这就是 Python(或更确切地说是任何其他编程语言)的工作原理。
猜你喜欢
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
相关资源
最近更新 更多