[Django]-Django Tests: User.authenticate fails

6πŸ‘

βœ…

I found the issue. The User.authenticate() method hashes the password provided. However, I set the password directly when creating the user, which means it was stored as Test1234, so the hashed password provided during authentication did not match β€˜Test1234’, hence the failure.

To properly store a hashed password, you need to use the set_password() method.

Updated setUp code:

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()
πŸ‘€Daniel Long

1πŸ‘

Using create_superuser resolved the issue. Below code resolves it.

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")
πŸ‘€kumaravel k

0πŸ‘

User Authentication is getting passed but when I try to login with the same credentials, the login fails.

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")
πŸ‘€kumaravel k

Leave a comment