Django: test failing on a view with @login_required

Posted by Esteban Feldman on Stack Overflow See other posts from Stack Overflow or by Esteban Feldman
Published on 2010-04-24T16:32:05Z Indexed on 2010/04/24 16:33 UTC
Read the original article Hit count: 378

Filed under:
|
|
|

Hi all,

I'm trying to build a test for a view that's decorated with @login_required, since I failed to make it work, I did a simple test and still can't make it pass.

Here is the code for the simple test and the view:

def test_login(self):
    user = self._create_new_user()
    self.assertTrue(user.is_active)
    login = self.client.login(username=user.username,
password=self.data['password1'])
    self.failUnless(login, 'Could not log in')
    response = self.client.get('/accounts/testlogin/')
    self.assertEqual(response.status_code, 200)

@login_required
def testlogin(request):
    print 'testlogin !! '
    return HttpResponse('OK')

_create_new_user() is saving the user and there is a test inside that method to see that is working.

The test fails in the response.status_code, returning 302 and the response instance is of a HttpResponseRedirect, is redirecting it as if not logged in.

Any clue? I'm missing something?

Regards Esteban

© Stack Overflow or respective owner

Related posts about django

Related posts about TDD