Fixed #10521 -- Modified the Remote User tests so that it isn't dependent on particular deployed URLs. Thanks to Kegan and Ramiro Morales for their contributions.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10674 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2009-05-04 07:05:44 +00:00
parent 6acfa88687
commit e91deca779
2 changed files with 20 additions and 10 deletions

View File

@ -8,6 +8,7 @@ from django.test import TestCase
class RemoteUserTest(TestCase): class RemoteUserTest(TestCase):
urls = 'django.contrib.auth.tests.urls'
middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware' middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
backend = 'django.contrib.auth.backends.RemoteUserBackend' backend = 'django.contrib.auth.backends.RemoteUserBackend'
@ -28,15 +29,15 @@ class RemoteUserTest(TestCase):
""" """
num_users = User.objects.count() num_users = User.objects.count()
response = self.client.get('/') response = self.client.get('/remote_user/')
self.assert_(isinstance(response.context['user'], AnonymousUser)) self.assert_(isinstance(response.context['user'], AnonymousUser))
self.assertEqual(User.objects.count(), num_users) self.assertEqual(User.objects.count(), num_users)
response = self.client.get('/', REMOTE_USER=None) response = self.client.get('/remote_user/', REMOTE_USER=None)
self.assert_(isinstance(response.context['user'], AnonymousUser)) self.assert_(isinstance(response.context['user'], AnonymousUser))
self.assertEqual(User.objects.count(), num_users) self.assertEqual(User.objects.count(), num_users)
response = self.client.get('/', REMOTE_USER='') response = self.client.get('/remote_user/', REMOTE_USER='')
self.assert_(isinstance(response.context['user'], AnonymousUser)) self.assert_(isinstance(response.context['user'], AnonymousUser))
self.assertEqual(User.objects.count(), num_users) self.assertEqual(User.objects.count(), num_users)
@ -46,13 +47,13 @@ class RemoteUserTest(TestCase):
as a User. as a User.
""" """
num_users = User.objects.count() num_users = User.objects.count()
response = self.client.get('/', REMOTE_USER='newuser') response = self.client.get('/remote_user/', REMOTE_USER='newuser')
self.assertEqual(response.context['user'].username, 'newuser') self.assertEqual(response.context['user'].username, 'newuser')
self.assertEqual(User.objects.count(), num_users + 1) self.assertEqual(User.objects.count(), num_users + 1)
User.objects.get(username='newuser') User.objects.get(username='newuser')
# Another request with same user should not create any new users. # Another request with same user should not create any new users.
response = self.client.get('/', REMOTE_USER='newuser') response = self.client.get('/remote_user/', REMOTE_USER='newuser')
self.assertEqual(User.objects.count(), num_users + 1) self.assertEqual(User.objects.count(), num_users + 1)
def test_known_user(self): def test_known_user(self):
@ -62,12 +63,12 @@ class RemoteUserTest(TestCase):
User.objects.create(username='knownuser') User.objects.create(username='knownuser')
User.objects.create(username='knownuser2') User.objects.create(username='knownuser2')
num_users = User.objects.count() num_users = User.objects.count()
response = self.client.get('/', REMOTE_USER=self.known_user) response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
self.assertEqual(response.context['user'].username, 'knownuser') self.assertEqual(response.context['user'].username, 'knownuser')
self.assertEqual(User.objects.count(), num_users) self.assertEqual(User.objects.count(), num_users)
# Test that a different user passed in the headers causes the new user # Test that a different user passed in the headers causes the new user
# to be logged in. # to be logged in.
response = self.client.get('/', REMOTE_USER=self.known_user2) response = self.client.get('/remote_user/', REMOTE_USER=self.known_user2)
self.assertEqual(response.context['user'].username, 'knownuser2') self.assertEqual(response.context['user'].username, 'knownuser2')
self.assertEqual(User.objects.count(), num_users) self.assertEqual(User.objects.count(), num_users)
@ -82,13 +83,13 @@ class RemoteUserTest(TestCase):
user.last_login = default_login user.last_login = default_login
user.save() user.save()
response = self.client.get('/', REMOTE_USER=self.known_user) response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
self.assertNotEqual(default_login, response.context['user'].last_login) self.assertNotEqual(default_login, response.context['user'].last_login)
user = User.objects.get(username='knownuser') user = User.objects.get(username='knownuser')
user.last_login = default_login user.last_login = default_login
user.save() user.save()
response = self.client.get('/', REMOTE_USER=self.known_user) response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
self.assertEqual(default_login, response.context['user'].last_login) self.assertEqual(default_login, response.context['user'].last_login)
def tearDown(self): def tearDown(self):
@ -113,7 +114,7 @@ class RemoteUserNoCreateTest(RemoteUserTest):
def test_unknown_user(self): def test_unknown_user(self):
num_users = User.objects.count() num_users = User.objects.count()
response = self.client.get('/', REMOTE_USER='newuser') response = self.client.get('/remote_user/', REMOTE_USER='newuser')
self.assert_(isinstance(response.context['user'], AnonymousUser)) self.assert_(isinstance(response.context['user'], AnonymousUser))
self.assertEqual(User.objects.count(), num_users) self.assertEqual(User.objects.count(), num_users)

View File

@ -1,9 +1,18 @@
from django.conf.urls.defaults import patterns from django.conf.urls.defaults import patterns
from django.contrib.auth.urls import urlpatterns from django.contrib.auth.urls import urlpatterns
from django.http import HttpResponse
from django.template import Template, RequestContext
def remote_user_auth_view(request):
"Dummy view for remote user tests"
t = Template("Username is {{ user }}.")
c = RequestContext(request, {})
return HttpResponse(t.render(c))
# special urls for auth test cases # special urls for auth test cases
urlpatterns += patterns('', urlpatterns += patterns('',
(r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')), (r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
(r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')), (r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')),
(r'^remote_user/$', remote_user_auth_view),
) )