Re: Problem testing a view decorated with @login_required

2010-09-26 Thread Brandon Taylor
Sorry, the second test is the one that fails.

On Sep 26, 2:01 pm, Brandon Taylor  wrote:
> Hi Everyone,
>
> I'm having an issue testing a page protected with @login_required. I'm
> loading a user via a fixture. I can call
> self.client.login(**credentials) successfully and get True back. I can
> retrieve a User object using the _auth_user_id key held in session
> from the call to self.client.login. However, my "dashboard" view keeps
> returning a 302.
>
> I'm using the built-in AuthenticationForm, and not sure what could be
> wrong at this point. I would really appreciate someone taking a look
> at this code...
>
> #views.py
> @login_required
> def dashboard(request):
>     return render_to_response('accounts/dashboard.html', {},
>         context_instance=RequestContext(request))
>
> #tests.py
> import httplib
>
> from django.contrib.auth.models import User
> from django.contrib.auth import SESSION_KEY
> from django.core.urlresolvers import reverse
> from django.test import TestCase
>
> class AccountsTestCase(TestCase):
>     def setUp(self):
>         self.dashboard_url = reverse('account_dashboard')
>
>     def tearDown(self):
>         self.client.logout()
>
>     def test_dashboard_redirects_unauthenticated_user(self):
>         dashboard_url = self.dashboard_url
>         #test that we can't get to the account dashboard if not logged
> in
>         login_url = '%s?next=%s' % (reverse('login'), dashboard_url)
>         response = self.client.get(dashboard_url)
>         self.assertRedirects(response, login_url,
> status_code=httplib.FOUND, target_status_code=httplib.OK)
>
>     def test_dashboard_allows_authenticated_user(self):
>         #test that we can login and then view the dashboard
>         logged_in = self.client.login(username='my_username',
> password='my_password')
>         self.failUnless(logged_in, 'User could not be authenticated')
>         user =
> User.objects.get(pk=self.client.session.get('_auth_user_id'))
>         response = self.client.get(self.dashboard_url)
>         self.assertEqual(response.status_code, httplib.OK)
>
> TIA,
> Brandon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Problem testing a view decorated with @login_required

2010-09-26 Thread Brandon Taylor
Hi Everyone,

I'm having an issue testing a page protected with @login_required. I'm
loading a user via a fixture. I can call
self.client.login(**credentials) successfully and get True back. I can
retrieve a User object using the _auth_user_id key held in session
from the call to self.client.login. However, my "dashboard" view keeps
returning a 302.

I'm using the built-in AuthenticationForm, and not sure what could be
wrong at this point. I would really appreciate someone taking a look
at this code...

#views.py
@login_required
def dashboard(request):
return render_to_response('accounts/dashboard.html', {},
context_instance=RequestContext(request))


#tests.py
import httplib

from django.contrib.auth.models import User
from django.contrib.auth import SESSION_KEY
from django.core.urlresolvers import reverse
from django.test import TestCase


class AccountsTestCase(TestCase):
def setUp(self):
self.dashboard_url = reverse('account_dashboard')

def tearDown(self):
self.client.logout()

def test_dashboard_redirects_unauthenticated_user(self):
dashboard_url = self.dashboard_url
#test that we can't get to the account dashboard if not logged
in
login_url = '%s?next=%s' % (reverse('login'), dashboard_url)
response = self.client.get(dashboard_url)
self.assertRedirects(response, login_url,
status_code=httplib.FOUND, target_status_code=httplib.OK)

def test_dashboard_allows_authenticated_user(self):
#test that we can login and then view the dashboard
logged_in = self.client.login(username='my_username',
password='my_password')
self.failUnless(logged_in, 'User could not be authenticated')
user =
User.objects.get(pk=self.client.session.get('_auth_user_id'))
response = self.client.get(self.dashboard_url)
self.assertEqual(response.status_code, httplib.OK)

TIA,
Brandon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.