代码之家  ›  专栏  ›  技术社区  ›  Daniel Long Daniel Roseman

Django测试:用户。身份验证失败

  •  2
  • Daniel Long Daniel Roseman  · 技术社区  · 6 年前

    这个问题我已经解决了好几个小时了。我是django和自动化测试的新手,我一直在使用Selenium和django的StaticLiveServerTestCase。我一直在尝试测试我的登录表单(顺便说一下,当我使用runserver并自己测试它时,它工作得很好。)

    一切都很好,只是我似乎无法成功登录我的测试用户。我已经将断点缩小到django的用户。验证方法。

    在我的设置中成功创建了用户对象,我可以通过在测试方法中访问它的属性来确认这一点。但是,身份验证失败。

    我已经查看了以下内容以寻求帮助,但他们并没有帮我走多远:

    知道为什么身份验证失败吗?我是否需要向设置中添加一些内容?

    from django.contrib.staticfiles.testing import StaticLiveServerTestCase
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from django.contrib.auth.models import User
    
    from django.contrib.auth import authenticate
    
    
    class AccountTestCase(StaticLiveServerTestCase):
        def setUp(self):
            self.selenium = webdriver.Chrome()
            super().setUp()
            User.objects.create(username='test', email='test@test.com', password='Test1234', is_active=True)
    
        def tearDownClass(self):
            self.selenium.quit()
            super().tearDown()
    
        def test_register(self):
            user = authenticate(username='test', password='Test1234')
            if user is not None: # prints Backend login failed
                print("Backend login successful")
            else:
                print("Backend login failed")
    
        user = User.objects.get(username='test')
        print(user)
        print(user.username) # prints test
        print(user.password) # prints Test1234
    
    3 回复  |  直到 6 年前
        1
  •  4
  •   Daniel Long Daniel Roseman    6 年前

    我发现了问题。用户。authenticate()方法散列提供的密码。但是,我在创建用户时直接设置了密码,这意味着它存储为Test1234,因此在身份验证期间提供的哈希密码与“Test1234”不匹配,因此失败。

    要正确存储哈希密码,需要使用set\u password()方法。

    更新的设置代码:

    def setUp(self):
        self.selenium = webdriver.Chrome()
        super().setUp()
        user = User.objects.create(username='test', email='test@test.com', is_active=True)
        user.set_password('Test1234')
        user.save()
    
        2
  •  1
  •   kumaravel k    6 年前

    使用create\u superuser解决了此问题。下面的代码解决了这个问题。

    from django.contrib.auth.models import User
    from django.contrib.staticfiles.testing import StaticLiveServerTestCase
    from django.test import Client
    from selenium.webdriver.common.by import By
    from selenium.webdriver.firefox.webdriver import WebDriver
    import time
    from django.contrib.auth import authenticate
    
    #from apps.digital.models import User
    
    
    class MyTests(StaticLiveServerTestCase):
    
        port = 0
        host = 'my host'
    
        def setUp(self):
            super(MyTests, self).setUp()
            self.selenium = WebDriver()
            self.client = Client()
            self.user = User.objects.create_superuser(username='test', password='Test1234', email='test@test.com', is_active=True)
            self.user.save()
    
        def tearDown(self):
            self.selenium.quit()
            super(MyTests, self).tearDown()
    
        def test_login(self):
            self.user = authenticate(username='test', password='Test1234')
            if self.user is not None:  # prints Backend login failed
                self.user = User.objects.get(username='test')
                print(self.user.username)  # prints test
                print(self.user.password)  # prints Test1234
                self.login = self.client.login(username='test', password='Test1234')
                self.assertEqual(self.login, True)
                print("Backend login successful")
    
                self.selenium.get('%s%s' % (self.live_server_url, '/admin/'))
                username_input = self.selenium.find_element_by_name("username")
                username_input.send_keys(self.user.username)
                password_input = self.selenium.find_element_by_name("password")
                password_input.send_keys('Test1234')
                self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
                time.sleep(1)
    
            else:
                print("Backend login failed")
    
        3
  •  0
  •   kumaravel k    6 年前

    用户身份验证正在通过,但当我尝试使用相同的凭据登录时,登录失败。

    from django.contrib.auth.models import User
    from django.contrib.staticfiles.testing import StaticLiveServerTestCase
    from django.test import Client
    from selenium.webdriver.common.by import By
    from selenium.webdriver.firefox.webdriver import WebDriver
    import time
    from django.contrib.auth import authenticate
    
    #from apps.digital.models import User
    
    
    class MyTests(StaticLiveServerTestCase):
    
        port = 0
        host = '<my host>'
    
        def setUp(self):
            super(MyTests, self).setUp()
            self.selenium = WebDriver()
            self.client = Client()
            self.user = User.objects.create(username='test', email='test@test.com', is_active=True)
            self.user.set_password('Test1234')
            self.user.save()
    
    
    
        def tearDown(self):
            self.selenium.quit()
            super(MyTests, self).tearDown()
    
        def test_login(self):
            self.user = authenticate(username='test', password='Test1234')
            if self.user is not None:  # prints Backend login failed
                self.user = User.objects.get(username='test')
                print(self.user.username)  # prints test
                print(self.user.password)  # prints Test1234
                self.login = self.client.login(username='test', password='Test1234')
                self.assertEqual(self.login, True)
                print("Backend login successful")
    
                self.selenium.get('%s%s' % (self.live_server_url, '/admin/'))
                username_input = self.selenium.find_element_by_name("username")
                username_input.send_keys(self.user.username)
                password_input = self.selenium.find_element_by_name("password")
                password_input.send_keys('Test1234')
                self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
                time.sleep(1)
    
            else:
                print("Backend login failed")