Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Unit Testing - Client login does not seem to work

I have been referring to the tutorial in https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing when working on my own unit testing. My unit test codes are below:

from django.core.urlresolvers import reverse
from django.test import Client, TestCase
from django.contrib.auth.models import User

from companies.models import Company

class ContactViewTest(TestCase):

    def setUp(self):
        self.client = Client(HTTP_HOST='localhost:8000')

        company = Company(name='Test')
        company.save()

        user = User.objects.create(username='test_user1', email='[email protected]')
        user.set_password('password')
        user.save()

        self.user = user
        self.company = company

    def test_logged_in_all_contacts_correct_template(self):
        c = self.client
        user_login = c.login(username=self.user.username, password=self.user.password)
        self.assertTrue(user_login)

        resp = c.get(reverse('all-contacts'), follow=True)

        # Check if user is logged in
        self.assertEquals(str(resp.context['user']), 'test_user1')
        # Check if response is "success"
        self.assertEqual(resp.status_code, 200)

        self.assertTemplateUsed(resp, 'all_contacts.html')

Upon running python manage.py test contacts.tests, I ran into a few errors.

The first being self.assertTrue(user_login) which returns AssertionError: False is not true

The second being self.assertEquals(str(resp.context['user']), 'test_user1') which returns AssertionError: 'AnonymousUser' != 'test_user1' when I removed self.assertTrue(user_login)

I am not really sure what went wrong other than the client.login() did not work as I expected so thanks in advance to whoever can help.

like image 850
chronox Avatar asked Nov 02 '25 23:11

chronox


2 Answers

The problem is that self.user.password is the hashed password.

You can either do,

user_login = c.login(username=self.user.username, password='password')

or you could use force_login instead, which doesn't require a password.

c.force_login(self.user)
like image 69
Alasdair Avatar answered Nov 05 '25 16:11

Alasdair


Any case from the following can be possible:

Make sure you are using the correct DJANGO_SETTINGS_MODULE.

Following apps should be listed in INSTALLED_APPS:

INSTALLED_APPS = (
    ...,
    'django.contrib.auth',
    'django.contrib.sessions'
    ..,
    )

Following middlewares should be listed in MIDDLEWARE:

MIDDLEWARE = (
    ...,
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    ...,
    )

If you have swapped the django.contrib.auth.models.User with a custom User my_app.User then tests require you have my_app listed in INSTALLED_APPSand also AUTH_USER_MODEL i.e

INSTALLED_APPS = (
...,
    'my_app',
..,
)
AUTH_USER_MODEL = 'my_app.User'
like image 20
Abdullah Avatar answered Nov 05 '25 16:11

Abdullah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!