How to create an encrypted password with mixer for Django testing?

29 views Asked by At

I'm trying to write a functional test for my Django project where I also want to test the login. For this I want to use mixer to create a user and then log in with the access data. Unfortunately this does not work. Mixer creates a user, but no real password or no encrypted password. As a result, I cannot log in to the test. If I use Django's own function make_password and create the password with it and the user with mixer, then it works. Is there nothing from mixer itself?

Code not working

# enter correct credentials
reg_user = mixer.blend(User)

driver.find_element(By.ID, "login-username").clear()
driver.find_element(By.ID, "login-username").send_keys(reg_user.username)
driver.find_element(By.ID, "login-pwd").clear()
driver.find_element(By.ID, "login-pwd").send_keys(reg_user.password)
driver.find_element(By.XPATH, "//button[@type='submit']").click()
driver.implicitly_wait(5)
# assert redirected to home
assert driver.current_url == f"{base_url}/"

Working Code

from django.contrib.auth.hashers import make_password


# enter correct credentials
password = make_password("test_3")
reg_user = mixer.blend(User, password=password)

driver.find_element(By.ID, "login-username").clear()
driver.find_element(By.ID, "login-username").send_keys(reg_user.username)
driver.find_element(By.ID, "login-pwd").clear()
driver.find_element(By.ID, "login-pwd").send_keys("test_3")
driver.find_element(By.XPATH, "//button[@type='submit']").click()
driver.implicitly_wait(5)
# assert redirected to home
assert driver.current_url == f"{base_url}/"
0

There are 0 answers