简体   繁体   English

为Django测试创建经过身份验证的用户

[英]Create authenticated user for django tests

The reason I am asking is I have the fields username , password and otp_token . 我问的原因是我otp_token usernamepasswordotp_token字段。 The otp_token is challenging to create, therefore I was wondering if there is a way create an authenticated user at the beginning of the test file to carry out the rest of the django tests as an authenticated user? otp_token创建具有挑战性,因此我想知道是否存在一种在测试文件的开头创建经过身份验证的用户的方式,以便以经过身份验证的用户身份执行其余的django测试?

For example, how to pass a logged in user to the following 例如,如何将登录用户传递给以下用户

def some_test(self):
    login = self.client.login(username='testUser', password='testPassword')

    response = self.client.get(reverse('page1:conent1'))
    self.assertEqual(response.status_code, 200)

related question 相关问题

Not sure how are you generating that token, but I think you can use some dummy data in test 不确定如何生成令牌,但我认为您可以在测试中使用一些虚拟数据

from django.test import TestCase, Client

def setUp(self):
    self.user = User.objects.create(username='<USERNAME>',
                                    email='<EMAIL>', otp_token='<YOUR_VALUE>')
    self.user.set_password(<PASSWORD>)
    self.user.save()
    self.client = Client()

def some_test(self):
    login = self.client.login(username='<USERNAME>', password='<PASSWORD>')
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM