1

I am trying to do a unit test for my django application. Whenever i try a get request to a particular page, it redirects me to the login page. I have logged into the web app once in setUp as shown below. I have referred to many questions such as 22208821 but cannot get it working. Below is my code.

from django.test import TestCase,Client
from django.contrib.auth.models import User
import unittest
class SimpleTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.username='testuser'
        self.email = 'test@test.com'
        self.password='12345'
        self.user =     User.objects.create_user(username=self.username,email=self.email,password=self.password)
        login = self.client.login(username=self.username,password=self.password)
        self.assertEqual(login,True)

def test_details(self):
    response = self.client.get('/dashboard/')
    print(response["location"])
    self.assertEqual(response.status_code, 200)

The output i get is

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
/admin/login/?next=/dashboard/
F
======================================================================
FAIL: test_details (interview.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path", line 22, in     test_details
self.assertEqual(response.status_code, 200)
AssertionError: 302 != 200

----------------------------------------------------------------------
Ran 1 test in 0.181s

Thanks for any help!

EDIT: Seems like the issue was my view required a staff status and i created using create_user(A user is not a staff by default). Instead, i used create_superuser and it worked fine. Thanks a lot.

Vishal Raghavan
  • 456
  • 3
  • 13

1 Answers1

2

You need to use set_password to correct save hashed password to the test DB:

def setUp(self):
        self.client = Client()
        self.username='testuser'
        self.email = 'test@test.com'
        self.password='12345'
        self.user = User(username=self.username,email=self.email)
        self.user.set_password(self.password)
        self.user.save() 
        login = self.client.login(username=self.username,password=self.password)
        self.assertEqual(login,True)

Without it you stored plain password to the password field. But client.login hashing password before credentials verification, and that's why password doesn't match.

From the docs:

Remember that if you want your test user to have a password, you can’t set the user’s password by setting the password attribute directly – you must use the set_password() function to store a correctly hashed password.

neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • Thank you so much for your quick reply. It gives me the same error. I used the exact same method above. – Vishal Raghavan Apr 23 '18 at 12:07
  • 1
    Seems like the issue was my view required a staff status and i created using create_user. Instead, i used create_superuser and it worked fine. Thanks a lot. – Vishal Raghavan Apr 23 '18 at 13:05