Search code examples
pythondjangodjango-testing

Django testing: RequestFactory() follow redirect


In Django, I have a view, which will redirect to the register page for certain users and I would like to write a test for this.

The standard request.client.get used for testing doesn't allow me to specify a user (it just defaults to anonymous_user?), so I can't test the behaviour.

With RequestFactory() I was able to specify request.user. However, it is not following the redirect and the test fails.

from .views import my_view
from django.test import RequestFactory()

def test_mytest(self):
    user = create_guest_user()
    self.factory = RequestFactory()
    request = self.factory.get(reverse('my_view'), follow=True)
    request.user = user
    response = my_view(request)
    self.assertContains(response, "page where redirected should contain this")

It fails on the last line with this error message:

AssertionError: 302 != 200 : Couldn't retrieve content: Response code was 302 (expected 200)

Any ideas how to do this?

EDIT: As far as I can tell this is not a duplicate as it refers to RequestFactory(), which is different from self.client.get (where follow=True will solve the problem).


Solution

  • To authenticate the default Django client, you can use the force_login method on your client:

    user = create_guest_user()
    self.client.force_login(user)
    

    If your site uses Django’s authentication system, you can use the force_login() method to simulate the effect of a user logging into the site. Use this method instead of login() when a test requires a user be logged in and the details of how a user logged in aren’t important.

    To follow the redirection (or not) using the test client, you must use the follow argument, as described here: https://docs.djangoproject.com/en/1.11/topics/testing/tools/#django.test.Client.get

    self.client.get(url, follow=True)
    

    If you set follow to True the client will follow any redirects and a redirect_chain attribute will be set in the response object containing tuples of the intermediate urls and status codes.

    So your full code should look like this:

    from django.test import TestCase
    
    class MyTestCase(TestCase):
        def test_mytest(self):
            user = create_guest_user()
            self.client.force_login(user)
            response = self.client.get(reverse('my_view'), follow=True)
            # Do your assertions on the response here