title: Why does Django Redirect Test Fail?
body: <p>I have a view unit test that is failing and I can't figure out the reason why.  I believe it has something to do with the test database.  The view in question is the default Django login view, django.contrib.auth.views.login.  In my project, after the user logs in, they are redirected to a page that show which members are logged in.  I've only stubbed out that page.</p>
  
  <p>Here is the unit test:</p>
  
  <pre><code>from django.test import TestCase
  from django.contrib.auth.models import User
  from django.test.client import Client, RequestFactory
  from django.core.urlresolvers import reverse
  from utils.factories import UserFactory
  
  class TestSignInView(TestCase):
      def setUp(self):
          self.client = Client()
          # self.user = UserFactory()
          self.user = User.objects.create_user(username='jdoe', password='jdoepass')
  
      def tearDown(self):
          self.user.delete()
  
      def test_user_enters_valid_data(self):
          response = self.client.post(reverse('login'), {'username': self.user.username, 'password': self.user.password}, follow=True)
          print response.context['form'].errors
          self.assertRedirects(response, reverse('show-members-online'))
  </code></pre>
  
  <p>Here is the error I get:</p>
  
  <pre><code>File "/Users/me/.virtualenvs/sp/lib/python2.7/site-packages/django/test/testcases.py", line 576, in assertRedirects
  (response.status_code, status_code))
  AssertionError: Response didn't redirect as expected: Response code was 200 (expected 302)
  &lt;ul class="errorlist"&gt;&lt;li&gt;__all__&lt;ul class="errorlist"&gt;&lt;li&gt;Please enter a correct username and password. Note that both fields may be case-sensitive.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
  </code></pre>
  
  <p>The test fails with the same error whether I create the user manually with the create_user function or if I use this factory_boy factory:</p>
  
  <pre><code>from django.contrib.auth.models import User
  class UserFactory(factory.DjangoModelFactory):
      FACTORY_FOR = User
      username = 'jdoe'
      # password = 'jdoepass'
      password = factory.PostGenerationMethodCall('set_password', 'jdoepass')
      email = 'jdoe@example.com'  
  </code></pre>
  
  <p>Here's the view I'm redirecting the user to after they log in successfully:</p>
  
  <pre><code>from django.shortcuts import render
  def show_members_online(request, template):
      return render(request, template)
  </code></pre>
  
  <p>I printed out the error which shows that the test isn't recognizing the username/password pair.  I've also printed out the username and password inside the test to confirm that they're the same values as what I initialize them to in setUp.  At first, when I was utilizing the User factory, I thought it was because I wasn't encrypting the password when I created the user.  That's when I did some research and learned that I needed to use the PostGenerationMethodCall to set the password.</p>
  
  <p>I also looked at Django's testcases.py file.  I don't understand everything that it's doing but it prompted me to try setting 'follow=True' when I do the post but that didn't make a difference.  Can anyone tell me what I'm doing wrong?  By the way, I'm using nosetests as my test runner.</p>
  
  <p>Thanks!</p>