2008-08-09 02:07:33 +08:00
|
|
|
# coding: utf-8
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.contrib.auth.models import User, Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.admin.models import LogEntry
|
|
|
|
from django.contrib.admin.sites import LOGIN_FORM_KEY, _encode_post_data
|
|
|
|
from django.contrib.admin.util import quote
|
|
|
|
from django.utils.html import escape
|
|
|
|
|
|
|
|
# local test models
|
|
|
|
from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
|
|
|
|
|
2008-08-10 04:52:40 +08:00
|
|
|
class AdminViewBasicTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def testTrailingSlashRequired(self):
|
|
|
|
"""
|
|
|
|
If you leave off the trailing slash, app should redirect and add it.
|
|
|
|
"""
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/add')
|
|
|
|
self.assertRedirects(request,
|
|
|
|
'/test_admin/admin/admin_views/article/add/'
|
|
|
|
)
|
|
|
|
|
|
|
|
def testBasicAddGet(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure GET on the add_view works.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/section/add/')
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def testBasicEditGet(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensureGET on the change_view works.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/section/1/')
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def testBasicAddPost(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on add_view works.
|
|
|
|
"""
|
|
|
|
post_data = {
|
|
|
|
"name": u"Another Section",
|
|
|
|
# inline data
|
|
|
|
"article_set-TOTAL_FORMS": u"3",
|
|
|
|
"article_set-INITIAL_FORMS": u"0",
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/section/add/', post_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
|
|
|
|
|
|
|
def testBasicEditPost(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on edit_view works.
|
|
|
|
"""
|
|
|
|
post_data = {
|
|
|
|
"name": u"Test section",
|
|
|
|
# inline data
|
|
|
|
"article_set-TOTAL_FORMS": u"4",
|
|
|
|
"article_set-INITIAL_FORMS": u"1",
|
|
|
|
"article_set-0-id": u"1",
|
|
|
|
# there is no title in database, give one here or formset
|
|
|
|
# will fail.
|
|
|
|
"article_set-0-title": u"Need a title.",
|
|
|
|
"article_set-0-content": u"<p>test content</p>",
|
|
|
|
"article_set-0-date_0": u"2008-03-18",
|
|
|
|
"article_set-0-date_1": u"11:54:58",
|
|
|
|
"article_set-1-id": u"",
|
|
|
|
"article_set-1-title": u"",
|
|
|
|
"article_set-1-content": u"",
|
|
|
|
"article_set-1-date_0": u"",
|
|
|
|
"article_set-1-date_1": u"",
|
|
|
|
"article_set-2-id": u"",
|
|
|
|
"article_set-2-title": u"",
|
|
|
|
"article_set-2-content": u"",
|
|
|
|
"article_set-2-date_0": u"",
|
|
|
|
"article_set-2-date_1": u"",
|
|
|
|
"article_set-3-id": u"",
|
|
|
|
"article_set-3-title": u"",
|
|
|
|
"article_set-3-content": u"",
|
|
|
|
"article_set-3-date_0": u"",
|
|
|
|
"article_set-3-date_1": u"",
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/section/1/', post_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def get_perm(Model, perm):
|
|
|
|
"""Return the permission object, for the Model"""
|
|
|
|
ct = ContentType.objects.get_for_model(Model)
|
2008-08-10 04:52:40 +08:00
|
|
|
return Permission.objects.get(content_type=ct, codename=perm)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
class AdminViewPermissionsTest(TestCase):
|
|
|
|
"""Tests for Admin Views Permissions."""
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
fixtures = ['admin-views-users.xml']
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def setUp(self):
|
|
|
|
"""Test setup."""
|
2008-08-09 02:07:33 +08:00
|
|
|
# Setup permissions, for our users who can add, change, and delete.
|
2008-07-19 07:54:34 +08:00
|
|
|
# We can't put this into the fixture, because the content type id
|
|
|
|
# and the permission id could be different on each run of the test.
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
opts = Article._meta
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# User who can add Articles
|
|
|
|
add_user = User.objects.get(username='adduser')
|
|
|
|
add_user.user_permissions.add(get_perm(Article,
|
|
|
|
opts.get_add_permission()))
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# User who can change Articles
|
|
|
|
change_user = User.objects.get(username='changeuser')
|
|
|
|
change_user.user_permissions.add(get_perm(Article,
|
|
|
|
opts.get_change_permission()))
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# User who can delete Articles
|
|
|
|
delete_user = User.objects.get(username='deleteuser')
|
|
|
|
delete_user.user_permissions.add(get_perm(Article,
|
|
|
|
opts.get_delete_permission()))
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
delete_user.user_permissions.add(get_perm(Section,
|
|
|
|
Section._meta.get_delete_permission()))
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# login POST dicts
|
|
|
|
self.super_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.super_email_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.super_email_bad_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'notsecret'}
|
|
|
|
self.adduser_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'adduser',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.changeuser_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'changeuser',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.deleteuser_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'deleteuser',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.joepublic_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'joepublic',
|
|
|
|
'password': 'secret'}
|
|
|
|
|
|
|
|
def testLogin(self):
|
|
|
|
"""
|
|
|
|
Make sure only staff members can log in.
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
Successful posts to the login page will redirect to the orignal url.
|
2008-08-09 02:07:33 +08:00
|
|
|
Unsuccessfull attempts will continue to render the login page with
|
2008-07-19 07:54:34 +08:00
|
|
|
a 200 status code.
|
|
|
|
"""
|
|
|
|
# Super User
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/', self.super_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/')
|
2008-07-23 03:48:36 +08:00
|
|
|
self.failIf(login.context)
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Test if user enters e-mail address
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/', self.super_email_login)
|
|
|
|
self.assertContains(login, "Your e-mail address is not your username")
|
|
|
|
# only correct passwords get a username hint
|
|
|
|
login = self.client.post('/test_admin/admin/', self.super_email_bad_login)
|
|
|
|
self.assertContains(login, "Usernames cannot contain the '@' character")
|
|
|
|
new_user = User(username='jondoe', password='secret', email='super@example.com')
|
|
|
|
new_user.save()
|
|
|
|
# check to ensure if there are multiple e-mail addresses a user doesn't get a 500
|
|
|
|
login = self.client.post('/test_admin/admin/', self.super_email_login)
|
|
|
|
self.assertContains(login, "Usernames cannot contain the '@' character")
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Add User
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/', self.adduser_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/')
|
2008-07-23 03:48:36 +08:00
|
|
|
self.failIf(login.context)
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Change User
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/', self.changeuser_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/')
|
2008-07-23 03:48:36 +08:00
|
|
|
self.failIf(login.context)
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Delete User
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/', self.deleteuser_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/')
|
2008-07-23 03:48:36 +08:00
|
|
|
self.failIf(login.context)
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Regular User should not be able to login.
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/', self.joepublic_login)
|
|
|
|
self.failUnlessEqual(login.status_code, 200)
|
|
|
|
# Login.context is a list of context dicts we just need to check the first one.
|
|
|
|
self.assert_(login.context[0].get('error_message'))
|
|
|
|
|
2008-08-10 01:35:19 +08:00
|
|
|
def testLoginSuccessfullyRedirectsToOriginalUrl(self):
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
query_string = "the-answer=42"
|
|
|
|
login = self.client.post('/test_admin/admin/', self.super_login, QUERY_STRING = query_string )
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/?%s' % query_string)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def testAddView(self):
|
|
|
|
"""Test add view restricts access and actually adds items."""
|
2008-08-09 02:07:33 +08:00
|
|
|
|
|
|
|
add_dict = {'title' : 'Døm ikke',
|
|
|
|
'content': '<p>great article</p>',
|
2008-07-19 07:54:34 +08:00
|
|
|
'date_0': '2008-03-18', 'date_1': '10:54:39',
|
|
|
|
'section': 1}
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Change User should not have access to add articles
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.changeuser_login)
|
2008-08-23 22:12:58 +08:00
|
|
|
# make sure the view removes test cookie
|
|
|
|
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
|
2008-07-19 07:54:34 +08:00
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/add/')
|
|
|
|
self.failUnlessEqual(request.status_code, 403)
|
|
|
|
# Try POST just to make sure
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
|
|
|
|
self.failUnlessEqual(post.status_code, 403)
|
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 1)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Add user may login and POST to add view, then redirect to admin root
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.adduser_login)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 2)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Super can add too, but is redirected to the change list view
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.super_login)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/admin_views/article/')
|
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 3)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Check and make sure that if user expires, data still persists
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
|
|
|
|
self.assertContains(post, 'Please log in again, because your session has expired.')
|
|
|
|
self.super_login['post_data'] = _encode_post_data(add_dict)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', self.super_login)
|
2008-08-23 22:12:58 +08:00
|
|
|
# make sure the view removes test cookie
|
|
|
|
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
|
2008-07-19 07:54:34 +08:00
|
|
|
self.assertRedirects(post, '/test_admin/admin/admin_views/article/')
|
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 4)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
def testChangeView(self):
|
|
|
|
"""Change view should restrict access and allow users to edit items."""
|
2008-08-09 02:07:33 +08:00
|
|
|
|
|
|
|
change_dict = {'title' : 'Ikke fordømt',
|
|
|
|
'content': '<p>edited article</p>',
|
|
|
|
'date_0': '2008-03-18', 'date_1': '10:54:39',
|
|
|
|
'section': 1}
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# add user shoud not be able to view the list of article or change any of them
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.adduser_login)
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/')
|
|
|
|
self.failUnlessEqual(request.status_code, 403)
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/1/')
|
|
|
|
self.failUnlessEqual(request.status_code, 403)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/1/', change_dict)
|
|
|
|
self.failUnlessEqual(post.status_code, 403)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# change user can view all items and edit them
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.changeuser_login)
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/1/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/1/', change_dict)
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/admin_views/article/')
|
|
|
|
self.failUnlessEqual(Article.objects.get(pk=1).content, '<p>edited article</p>')
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
def testCustomModelAdminTemplates(self):
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.super_login)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Test custom change list template with custom extra context
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/customarticle/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
self.assert_("var hello = 'Hello!';" in request.content)
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/change_list.html')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Test custom change form template
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/customarticle/add/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/change_form.html')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Add an article so we can test delete and history views
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', {
|
|
|
|
'content': '<p>great article</p>',
|
|
|
|
'date_0': '2008-03-18',
|
|
|
|
'date_1': '10:54:39'
|
|
|
|
})
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/')
|
|
|
|
self.failUnlessEqual(CustomArticle.objects.all().count(), 1)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Test custom delete and object history templates
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/delete_confirmation.html')
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/object_history.html')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
def testCustomAdminSiteTemplates(self):
|
|
|
|
from django.contrib import admin
|
|
|
|
self.assertEqual(admin.site.index_template, None)
|
|
|
|
self.assertEqual(admin.site.login_template, None)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.assertTemplateUsed(request, 'admin/login.html')
|
|
|
|
self.client.post('/test_admin/admin/', self.changeuser_login)
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.assertTemplateUsed(request, 'admin/index.html')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
admin.site.login_template = 'custom_admin/login.html'
|
|
|
|
admin.site.index_template = 'custom_admin/index.html'
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/login.html')
|
|
|
|
self.assert_('Hello from a custom login template' in request.content)
|
|
|
|
self.client.post('/test_admin/admin/', self.changeuser_login)
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/index.html')
|
|
|
|
self.assert_('Hello from a custom index template' in request.content)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Finally, using monkey patching check we can inject custom_context arguments in to index
|
|
|
|
original_index = admin.site.index
|
|
|
|
def index(*args, **kwargs):
|
|
|
|
kwargs['extra_context'] = {'foo': '*bar*'}
|
|
|
|
return original_index(*args, **kwargs)
|
|
|
|
admin.site.index = index
|
|
|
|
request = self.client.get('/test_admin/admin/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/index.html')
|
|
|
|
self.assert_('Hello from a custom index template *bar*' in request.content)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
del admin.site.index # Resets to using the original
|
|
|
|
admin.site.login_template = None
|
|
|
|
admin.site.index_template = None
|
|
|
|
|
|
|
|
def testDeleteView(self):
|
|
|
|
"""Delete view should restrict access and actually delete items."""
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
delete_dict = {'post': 'yes'}
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# add user shoud not be able to delete articles
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.adduser_login)
|
|
|
|
request = self.client.get('/test_admin/admin/admin_views/article/1/delete/')
|
|
|
|
self.failUnlessEqual(request.status_code, 403)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict)
|
|
|
|
self.failUnlessEqual(post.status_code, 403)
|
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 1)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Delete user can delete
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.deleteuser_login)
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/section/1/delete/')
|
|
|
|
# test response contains link to related Article
|
|
|
|
self.assertContains(response, "admin_views/article/1/")
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
response = self.client.get('/test_admin/admin/admin_views/article/1/delete/')
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict)
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/')
|
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 0)
|
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
class AdminViewStringPrimaryKeyTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, *args):
|
|
|
|
super(AdminViewStringPrimaryKeyTest, self).__init__(*args)
|
|
|
|
self.pk = """abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 -_.!~*'() ;/?:@&=+$, <>#%" {}|\^[]`"""
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
content_type_pk = ContentType.objects.get_for_model(ModelWithStringPrimaryKey).pk
|
|
|
|
LogEntry.objects.log_action(100, content_type_pk, self.pk, self.pk, 2, change_message='')
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def test_get_change_view(self):
|
|
|
|
"Retrieving the object using urlencoded form of primary key should work"
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(self.pk))
|
|
|
|
self.assertContains(response, escape(self.pk))
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def test_changelist_to_changeform_link(self):
|
|
|
|
"The link from the changelist referring to the changeform of the object should be quoted"
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/')
|
|
|
|
should_contain = """<tr class="row1"><th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
|
|
|
|
self.assertContains(response, should_contain)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def test_recentactions_link(self):
|
|
|
|
"The link from the recent actions list referring to the changeform of the object should be quoted"
|
|
|
|
response = self.client.get('/test_admin/admin/')
|
|
|
|
should_contain = """<a href="admin_views/modelwithstringprimarykey/%s/">%s</a>""" % (quote(self.pk), escape(self.pk))
|
|
|
|
self.assertContains(response, should_contain)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def test_deleteconfirmation_link(self):
|
|
|
|
"The link from the delete confirmation page referring back to the changeform of the object should be quoted"
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/delete/' % quote(self.pk))
|
|
|
|
should_contain = """<a href="../../%s/">%s</a>""" % (quote(self.pk), escape(self.pk))
|
|
|
|
self.assertContains(response, should_contain)
|
2008-08-10 01:35:19 +08:00
|
|
|
|
|
|
|
class SecureViewTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# login POST dicts
|
|
|
|
self.super_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.super_email_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.super_email_bad_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'notsecret'}
|
|
|
|
self.adduser_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'adduser',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.changeuser_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'changeuser',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.deleteuser_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'deleteuser',
|
|
|
|
'password': 'secret'}
|
|
|
|
self.joepublic_login = {'post_data': _encode_post_data({}),
|
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'joepublic',
|
|
|
|
'password': 'secret'}
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_secure_view_shows_login_if_not_logged_in(self):
|
|
|
|
"Ensure that we see the login form"
|
|
|
|
response = self.client.get('/test_admin/admin/secure-view/' )
|
|
|
|
self.assertTemplateUsed(response, 'admin/login.html')
|
|
|
|
|
|
|
|
def test_secure_view_login_successfully_redirects_to_original_url(self):
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
query_string = "the-answer=42"
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.super_login, QUERY_STRING = query_string )
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/secure-view/?%s' % query_string)
|
|
|
|
|
|
|
|
def test_staff_member_required_decorator_works_as_per_admin_login(self):
|
|
|
|
"""
|
|
|
|
Make sure only staff members can log in.
|
|
|
|
|
|
|
|
Successful posts to the login page will redirect to the orignal url.
|
|
|
|
Unsuccessfull attempts will continue to render the login page with
|
|
|
|
a 200 status code.
|
|
|
|
"""
|
|
|
|
# Super User
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.super_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/secure-view/')
|
2008-08-15 22:58:47 +08:00
|
|
|
self.failIf(login.context)
|
2008-08-10 01:35:19 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
# Test if user enters e-mail address
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.super_email_login)
|
|
|
|
self.assertContains(login, "Your e-mail address is not your username")
|
|
|
|
# only correct passwords get a username hint
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.super_email_bad_login)
|
|
|
|
self.assertContains(login, "Usernames cannot contain the '@' character")
|
|
|
|
new_user = User(username='jondoe', password='secret', email='super@example.com')
|
|
|
|
new_user.save()
|
|
|
|
# check to ensure if there are multiple e-mail addresses a user doesn't get a 500
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.super_email_login)
|
|
|
|
self.assertContains(login, "Usernames cannot contain the '@' character")
|
|
|
|
|
|
|
|
# Add User
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.adduser_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/secure-view/')
|
2008-08-15 22:58:47 +08:00
|
|
|
self.failIf(login.context)
|
2008-08-10 01:35:19 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
# Change User
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.changeuser_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/secure-view/')
|
2008-08-15 22:58:47 +08:00
|
|
|
self.failIf(login.context)
|
2008-08-10 01:35:19 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
# Delete User
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.deleteuser_login)
|
|
|
|
self.assertRedirects(login, '/test_admin/admin/secure-view/')
|
2008-08-15 22:58:47 +08:00
|
|
|
self.failIf(login.context)
|
2008-08-10 01:35:19 +08:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
# Regular User should not be able to login.
|
|
|
|
request = self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
login = self.client.post('/test_admin/admin/secure-view/', self.joepublic_login)
|
|
|
|
self.failUnlessEqual(login.status_code, 200)
|
|
|
|
# Login.context is a list of context dicts we just need to check the first one.
|
|
|
|
self.assert_(login.context[0].get('error_message'))
|