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.