Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Django redirect from view class

I'm learning Django atm and I am stuck with creating a simple test case.

How I can test the following view? If the user is already logged in it redirects him to list view.

from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm
from django.views.generic import TemplateView
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

class LoginView(TemplateView):
    """
    Base class for client area login.
    """
    def get(self, request, *args, **kwargs):
        """
        Display login form, only if user is not yet authenticated.
        Otherwise redirect to tickets index.
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        if request.user.is_authenticated():
            return redirect(reverse('tickets:index'))

        form = AuthenticationForm()

        return render(request, 'login.html', {'form': form})

So far the test I have created looks like this:

from django.test import TestCase, RequestFactory
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User, AnonymousUser
from app.account.views import LoginView


class TestLoginForm(TestCase):

    def setUp(self):
        """
        Set up environment
        :return:
        """
        self.user = User.objects.create_user(username="test", password="test123", email="[email protected]")
        self.factory = RequestFactory()

    def tearDown(self):
        """
        Clean created user object
        :return:
        """
        self.user.delete()
        self.factory = None

    def test_login_form_redirect(self):
        """
        Test login redirect
        :return:
        """


        request = self.factory.get(reverse('account:login'))
        request.user = AnonymousUser()

        view = LoginView()
        response = view.get(request=request)
        self.assertEqual(response.status_code, 200)

        request.user = self.user
        response = view.get(request=request)
        self.assertRedirects(response, expected_url=reverse('tickets:index'), status_code=302, target_status_code=200)

The first assertation passed, but the second where the user should be redirected if already has logged in throws this error

AttributeError: 'HttpResponseRedirect' object has no attribute 'client'

What am I doing wrong?

like image 419
Sh1d0w Avatar asked Jan 16 '15 13:01

Sh1d0w


Video Answer


1 Answers

Ok I have managed to make successful test case, with following code

def test_login_form_redirect(self):
    """
    Test login redirect
    :return:
    """

    self.client.logout()

    response = self.client.get(reverse('account:login'))
    self.assertEqual(response.status_code, 200)

    self.assertTrue(self.client.login(username='test', password='test123'))
    response = self.client.get(reverse('account:login'))
    self.assertRedirects(response, expected_url=reverse('tickets:index'), status_code=302, target_status_code=200)
like image 116
Sh1d0w Avatar answered Oct 06 '22 00:10

Sh1d0w