samedi 28 octobre 2017

How to test django logout using client.post()?

I want to test if my user is logged out correctly. I can log in. I tried to logou the same way, but, the test fails. I can't figure out why.

Why this test fails?

def test_user_can_login_and_logout(self):
    response = self.client.post('/login/', {'username': 'login', 'password': 'pass'})
    user = auth.get_user(self.client)
    self.assertEqual(user.is_authenticated(), True)   # This works fine
    self.client.post('/logout/')
    self.assertEqual(user.is_authenticated(), False)  # This fails.

My urls.py

from django.conf.urls import url
from django.contrib.auth import views as auth_views

urlpatterns = [
        (...)
        url(r'^login/$', auth_views.login, {'template_name': 'home/login.html'}, name='login'),
        url(r'^logout/$', auth_views.logout, name='logout'),
        (...)
        ]

In question I skipped the code responsible for creating test user in database.

Aucun commentaire:

Enregistrer un commentaire