2008-08-08 18:07:33 +00:00
|
|
|
# coding: utf-8
|
2008-07-18 23:54:34 +00:00
|
|
|
|
2009-03-10 11:19:26 +00:00
|
|
|
import re
|
2009-03-29 18:52:38 +00:00
|
|
|
import datetime
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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
|
2008-09-02 21:10:00 +00:00
|
|
|
from django.contrib.admin.sites import LOGIN_FORM_KEY
|
2008-07-18 23:54:34 +00:00
|
|
|
from django.contrib.admin.util import quote
|
2009-03-23 20:22:56 +00:00
|
|
|
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
2008-07-18 23:54:34 +00:00
|
|
|
from django.utils.html import escape
|
|
|
|
|
|
|
|
# local test models
|
2009-04-01 03:11:58 +00:00
|
|
|
from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast, EmptyModel
|
2009-03-10 11:19:26 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set
|
2008-07-18 23:54:34 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
class AdminViewBasicTest(TestCase):
|
2009-04-01 14:14:20 +00:00
|
|
|
fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
|
2009-03-10 11:19:26 +00:00
|
|
|
|
|
|
|
# Store the bit of the URL where the admin is registered as a class
|
2009-01-14 20:22:25 +00:00
|
|
|
# variable. That way we can test a second AdminSite just by subclassing
|
|
|
|
# this test case and changing urlbit.
|
|
|
|
urlbit = 'admin'
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
def testTrailingSlashRequired(self):
|
|
|
|
"""
|
|
|
|
If you leave off the trailing slash, app should redirect and add it.
|
|
|
|
"""
|
2009-01-14 20:22:25 +00:00
|
|
|
request = self.client.get('/test_admin/%s/admin_views/article/add' % self.urlbit)
|
2008-08-09 20:52:40 +00:00
|
|
|
self.assertRedirects(request,
|
2009-01-14 20:22:25 +00:00
|
|
|
'/test_admin/%s/admin_views/article/add/' % self.urlbit, status_code=301
|
2008-08-09 20:52:40 +00:00
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
def testBasicAddGet(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure GET on the add_view works.
|
|
|
|
"""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit)
|
2008-08-09 20:52:40 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-29 16:09:29 +00:00
|
|
|
def testAddWithGETArgs(self):
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'})
|
2008-08-29 16:09:29 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
2009-03-10 11:19:26 +00:00
|
|
|
'value="My Section"' in response.content,
|
2008-08-29 16:09:29 +00:00
|
|
|
"Couldn't find an input with the right value in the response."
|
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
def testBasicEditGet(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensureGET on the change_view works.
|
|
|
|
"""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/section/1/' % self.urlbit)
|
2008-08-09 20:52:40 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
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",
|
|
|
|
}
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.post('/test_admin/%s/admin_views/section/add/' % self.urlbit, post_data)
|
2008-08-09 20:52:40 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2009-04-02 00:01:15 +00:00
|
|
|
# Post data for edit inline
|
|
|
|
inline_post_data = {
|
|
|
|
"name": u"Test section",
|
|
|
|
# inline data
|
|
|
|
"article_set-TOTAL_FORMS": u"6",
|
|
|
|
"article_set-INITIAL_FORMS": u"3",
|
|
|
|
"article_set-0-id": u"1",
|
|
|
|
# there is no title in database, give one here or formset will fail.
|
|
|
|
"article_set-0-title": u"Norske bostaver æøå skaper problemer",
|
|
|
|
"article_set-0-content": u"<p>Middle content</p>",
|
|
|
|
"article_set-0-date_0": u"2008-03-18",
|
|
|
|
"article_set-0-date_1": u"11:54:58",
|
|
|
|
"article_set-0-section": u"1",
|
|
|
|
"article_set-1-id": u"2",
|
|
|
|
"article_set-1-title": u"Need a title.",
|
|
|
|
"article_set-1-content": u"<p>Oldest content</p>",
|
|
|
|
"article_set-1-date_0": u"2000-03-18",
|
|
|
|
"article_set-1-date_1": u"11:54:58",
|
|
|
|
"article_set-2-id": u"3",
|
|
|
|
"article_set-2-title": u"Need a title.",
|
|
|
|
"article_set-2-content": u"<p>Newest content</p>",
|
|
|
|
"article_set-2-date_0": u"2009-03-18",
|
|
|
|
"article_set-2-date_1": u"11:54:58",
|
|
|
|
"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"",
|
|
|
|
"article_set-4-id": u"",
|
|
|
|
"article_set-4-title": u"",
|
|
|
|
"article_set-4-content": u"",
|
|
|
|
"article_set-4-date_0": u"",
|
|
|
|
"article_set-4-date_1": u"",
|
|
|
|
"article_set-5-id": u"",
|
|
|
|
"article_set-5-title": u"",
|
|
|
|
"article_set-5-content": u"",
|
|
|
|
"article_set-5-date_0": u"",
|
|
|
|
"article_set-5-date_1": u"",
|
|
|
|
}
|
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
def testBasicEditPost(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on edit_view works.
|
|
|
|
"""
|
2009-04-02 00:01:15 +00:00
|
|
|
response = self.client.post('/test_admin/%s/admin_views/section/1/' % self.urlbit, self.inline_post_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
|
|
|
|
|
|
|
def testEditSaveAs(self):
|
|
|
|
"""
|
|
|
|
Test "save as".
|
|
|
|
"""
|
|
|
|
post_data = self.inline_post_data.copy()
|
|
|
|
post_data.update({
|
|
|
|
'_saveasnew': u'Save+as+new',
|
|
|
|
"article_set-1-section": u"1",
|
|
|
|
"article_set-2-section": u"1",
|
|
|
|
"article_set-3-section": u"1",
|
|
|
|
"article_set-4-section": u"1",
|
|
|
|
"article_set-5-section": u"1",
|
|
|
|
})
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.post('/test_admin/%s/admin_views/section/1/' % self.urlbit, post_data)
|
2008-08-09 20:52:40 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
|
|
|
|
2008-10-08 14:47:01 +00:00
|
|
|
def testChangeListSortingCallable(self):
|
|
|
|
"""
|
2009-03-10 11:19:26 +00:00
|
|
|
Ensure we can sort on a list_display field that is a callable
|
2008-10-08 14:47:01 +00:00
|
|
|
(column 2 is callable_year in ArticleAdmin)
|
|
|
|
"""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'ot': 'asc', 'o': 2})
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
|
|
|
response.content.index('Oldest content') < response.content.index('Middle content') and
|
|
|
|
response.content.index('Middle content') < response.content.index('Newest content'),
|
|
|
|
"Results of sorting on callable are out of order."
|
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-10-08 14:47:01 +00:00
|
|
|
def testChangeListSortingModel(self):
|
|
|
|
"""
|
2009-03-10 11:19:26 +00:00
|
|
|
Ensure we can sort on a list_display field that is a Model method
|
2008-10-08 14:47:01 +00:00
|
|
|
(colunn 3 is 'model_year' in ArticleAdmin)
|
|
|
|
"""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'ot': 'dsc', 'o': 3})
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
|
|
|
response.content.index('Newest content') < response.content.index('Middle content') and
|
|
|
|
response.content.index('Middle content') < response.content.index('Oldest content'),
|
|
|
|
"Results of sorting on Model method are out of order."
|
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-10-08 14:47:01 +00:00
|
|
|
def testChangeListSortingModelAdmin(self):
|
|
|
|
"""
|
2009-03-10 11:19:26 +00:00
|
|
|
Ensure we can sort on a list_display field that is a ModelAdmin method
|
2008-10-08 14:47:01 +00:00
|
|
|
(colunn 4 is 'modeladmin_year' in ArticleAdmin)
|
|
|
|
"""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'ot': 'asc', 'o': 4})
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
2009-03-10 11:19:26 +00:00
|
|
|
response.content.index('Oldest content') < response.content.index('Middle content') and
|
2008-10-08 14:47:01 +00:00
|
|
|
response.content.index('Middle content') < response.content.index('Newest content'),
|
|
|
|
"Results of sorting on ModelAdmin method are out of order."
|
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-10-21 19:03:21 +00:00
|
|
|
def testLimitedFilter(self):
|
|
|
|
"""Ensure admin changelist filters do not contain objects excluded via limit_choices_to."""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit)
|
2008-10-21 19:03:21 +00:00
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
2009-03-10 11:19:26 +00:00
|
|
|
'<div id="changelist-filter">' in response.content,
|
2008-10-21 19:03:21 +00:00
|
|
|
"Expected filter not found in changelist view."
|
|
|
|
)
|
|
|
|
self.failIf(
|
|
|
|
'<a href="?color__id__exact=3">Blue</a>' in response.content,
|
|
|
|
"Changelist filter not correctly limited by limit_choices_to."
|
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-10-22 23:09:35 +00:00
|
|
|
def testIncorrectLookupParameters(self):
|
|
|
|
"""Ensure incorrect lookup parameters are handled gracefully."""
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'notarealfield': '5'})
|
2009-03-10 11:19:26 +00:00
|
|
|
self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
|
2009-01-14 20:22:25 +00:00
|
|
|
response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
|
|
|
|
self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
|
|
|
|
|
2009-04-01 14:14:20 +00:00
|
|
|
def testNamedGroupFieldChoicesChangeList(self):
|
|
|
|
"""
|
|
|
|
Ensures the admin changelist shows correct values in the relevant column
|
|
|
|
for rows corresponding to instances of a model in which a named group
|
|
|
|
has been used in the choices option of a field.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
|
|
|
'<a href="1/">Horizontal</a>' in response.content and
|
|
|
|
'<a href="2/">Vertical</a>' in response.content,
|
|
|
|
"Changelist table isn't showing the right human-readable values set by a model field 'choices' option named group."
|
|
|
|
)
|
|
|
|
|
|
|
|
def testNamedGroupFieldChoicesFilter(self):
|
|
|
|
"""
|
|
|
|
Ensures the filter UI shows correctly when at least one named group has
|
|
|
|
been used in the choices option of a model field.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
self.failUnless(
|
|
|
|
'<div id="changelist-filter">' in response.content,
|
|
|
|
"Expected filter not found in changelist view."
|
|
|
|
)
|
|
|
|
self.failUnless(
|
|
|
|
'<a href="?surface__exact=x">Horizontal</a>' in response.content and
|
|
|
|
'<a href="?surface__exact=y">Vertical</a>' in response.content,
|
|
|
|
"Changelist filter isn't showing options contained inside a model field 'choices' option named group."
|
|
|
|
)
|
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
class CustomModelAdminTest(AdminViewBasicTest):
|
|
|
|
urlbit = "admin2"
|
|
|
|
|
|
|
|
def testCustomAdminSiteLoginTemplate(self):
|
|
|
|
self.client.logout()
|
|
|
|
request = self.client.get('/test_admin/admin2/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/login.html')
|
|
|
|
self.assert_('Hello from a custom login template' in request.content)
|
|
|
|
|
|
|
|
def testCustomAdminSiteIndexViewAndTemplate(self):
|
|
|
|
request = self.client.get('/test_admin/admin2/')
|
|
|
|
self.assertTemplateUsed(request, 'custom_admin/index.html')
|
|
|
|
self.assert_('Hello from a custom index template *bar*' in request.content)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
def testCustomAdminSiteView(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
response = self.client.get('/test_admin/%s/my_view/' % self.urlbit)
|
|
|
|
self.assert_(response.content == "Django is a magical pony!", response.content)
|
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def get_perm(Model, perm):
|
|
|
|
"""Return the permission object, for the Model"""
|
|
|
|
ct = ContentType.objects.get_for_model(Model)
|
2008-08-09 20:52:40 +00:00
|
|
|
return Permission.objects.get(content_type=ct, codename=perm)
|
2008-07-18 23:54:34 +00:00
|
|
|
|
|
|
|
class AdminViewPermissionsTest(TestCase):
|
|
|
|
"""Tests for Admin Views Permissions."""
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
fixtures = ['admin-views-users.xml']
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""Test setup."""
|
2008-08-08 18:07:33 +00:00
|
|
|
# Setup permissions, for our users who can add, change, and delete.
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
opts = Article._meta
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
delete_user.user_permissions.add(get_perm(Section,
|
|
|
|
Section._meta.get_delete_permission()))
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
# login POST dicts
|
2008-09-02 21:10:00 +00:00
|
|
|
self.super_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.super_email_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.super_email_bad_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'notsecret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.adduser_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'adduser',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.changeuser_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'changeuser',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.deleteuser_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'deleteuser',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.joepublic_login = {
|
2008-07-18 23:54:34 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'joepublic',
|
|
|
|
'password': 'secret'}
|
|
|
|
|
|
|
|
def testLogin(self):
|
|
|
|
"""
|
|
|
|
Make sure only staff members can log in.
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
Successful posts to the login page will redirect to the orignal url.
|
2008-08-08 18:07:33 +00:00
|
|
|
Unsuccessfull attempts will continue to render the login page with
|
2008-07-18 23:54:34 +00: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-22 19:48:36 +00:00
|
|
|
self.failIf(login.context)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-22 19:48:36 +00:00
|
|
|
self.failIf(login.context)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-22 19:48:36 +00:00
|
|
|
self.failIf(login.context)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-22 19:48:36 +00:00
|
|
|
self.failIf(login.context)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-09 17:35:19 +00: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-18 23:54:34 +00:00
|
|
|
def testAddView(self):
|
|
|
|
"""Test add view restricts access and actually adds items."""
|
2008-08-08 18:07:33 +00:00
|
|
|
|
|
|
|
add_dict = {'title' : 'Døm ikke',
|
|
|
|
'content': '<p>great article</p>',
|
2008-07-18 23:54:34 +00:00
|
|
|
'date_0': '2008-03-18', 'date_1': '10:54:39',
|
|
|
|
'section': 1}
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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 14:12:58 +00:00
|
|
|
# make sure the view removes test cookie
|
|
|
|
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
|
2008-07-18 23:54:34 +00: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)
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 3)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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)
|
2008-10-24 19:14:24 +00:00
|
|
|
addpage = self.client.get('/test_admin/admin/admin_views/article/add/')
|
|
|
|
self.failUnlessEqual(addpage.status_code, 200)
|
|
|
|
change_list_link = '<a href="../">Articles</a> ›'
|
|
|
|
self.failIf(change_list_link in addpage.content,
|
|
|
|
'User restricted to add permission is given link to change list view in breadcrumbs.')
|
2008-07-18 23:54:34 +00:00
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/')
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 4)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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)
|
2008-10-24 19:14:24 +00:00
|
|
|
addpage = self.client.get('/test_admin/admin/admin_views/article/add/')
|
|
|
|
self.failUnlessEqual(addpage.status_code, 200)
|
|
|
|
self.failIf(change_list_link not in addpage.content,
|
|
|
|
'Unrestricted user is not given link to change list view in breadcrumbs.')
|
2008-07-18 23:54:34 +00:00
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
|
|
|
|
self.assertRedirects(post, '/test_admin/admin/admin_views/article/')
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 5)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-08-24 06:34:18 +00:00
|
|
|
# 8509 - if a normal user is already logged in, it is possible
|
|
|
|
# to change user into the superuser without error
|
|
|
|
login = self.client.login(username='joepublic', password='secret')
|
|
|
|
# Check and make sure that if user expires, data still persists
|
|
|
|
self.client.get('/test_admin/admin/')
|
|
|
|
self.client.post('/test_admin/admin/', self.super_login)
|
|
|
|
# make sure the view removes test cookie
|
|
|
|
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
|
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def testChangeView(self):
|
|
|
|
"""Change view should restrict access and allow users to edit items."""
|
2008-08-08 18:07:33 +00: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-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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>')
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-11-09 18:51:28 +00:00
|
|
|
# one error in form should produce singular error message, multiple errors plural
|
|
|
|
change_dict['title'] = ''
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/1/', change_dict)
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
self.failUnless('Please correct the error below.' in post.content,
|
|
|
|
'Singular error message not found in response to post with one error.')
|
|
|
|
change_dict['content'] = ''
|
|
|
|
post = self.client.post('/test_admin/admin/admin_views/article/1/', change_dict)
|
|
|
|
self.failUnlessEqual(request.status_code, 200)
|
|
|
|
self.failUnless('Please correct the errors below.' in post.content,
|
2009-03-10 11:19:26 +00:00
|
|
|
'Plural error message not found in response to post with multiple errors.')
|
2008-07-18 23:54:34 +00:00
|
|
|
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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
def testDeleteView(self):
|
|
|
|
"""Delete view should restrict access and actually delete items."""
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
delete_dict = {'post': 'yes'}
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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)
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 3)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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/')
|
2008-10-08 14:47:01 +00:00
|
|
|
self.failUnlessEqual(Article.objects.all().count(), 2)
|
2008-07-18 23:54:34 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
|
|
|
|
|
|
|
class AdminViewStringPrimaryKeyTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def __init__(self, *args):
|
|
|
|
super(AdminViewStringPrimaryKeyTest, self).__init__(*args)
|
|
|
|
self.pk = """abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 -_.!~*'() ;/?:@&=+$, <>#%" {}|\^[]`"""
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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/')
|
2009-03-23 20:22:56 +00:00
|
|
|
should_contain = """<th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
|
2008-07-18 23:54:34 +00:00
|
|
|
self.assertContains(response, should_contain)
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-29 16:46:46 +00:00
|
|
|
def test_url_conflicts_with_add(self):
|
|
|
|
"A model with a primary key that ends with add should be visible"
|
|
|
|
add_model = ModelWithStringPrimaryKey(id="i have something to add")
|
|
|
|
add_model.save()
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(add_model.pk))
|
|
|
|
should_contain = """<h1>Change model with string primary key</h1>"""
|
|
|
|
self.assertContains(response, should_contain)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-29 16:46:46 +00:00
|
|
|
def test_url_conflicts_with_delete(self):
|
|
|
|
"A model with a primary key that ends with delete should be visible"
|
|
|
|
delete_model = ModelWithStringPrimaryKey(id="delete")
|
|
|
|
delete_model.save()
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(delete_model.pk))
|
|
|
|
should_contain = """<h1>Change model with string primary key</h1>"""
|
|
|
|
self.assertContains(response, should_contain)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-29 16:46:46 +00:00
|
|
|
def test_url_conflicts_with_history(self):
|
|
|
|
"A model with a primary key that ends with history should be visible"
|
|
|
|
history_model = ModelWithStringPrimaryKey(id="history")
|
|
|
|
history_model.save()
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(history_model.pk))
|
|
|
|
should_contain = """<h1>Change model with string primary key</h1>"""
|
|
|
|
self.assertContains(response, should_contain)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 17:35:19 +00:00
|
|
|
|
|
|
|
class SecureViewTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# login POST dicts
|
2008-09-02 21:10:00 +00:00
|
|
|
self.super_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.super_email_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.super_email_bad_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'super@example.com',
|
|
|
|
'password': 'notsecret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.adduser_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'adduser',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.changeuser_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'changeuser',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.deleteuser_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'deleteuser',
|
|
|
|
'password': 'secret'}
|
2008-09-02 21:10:00 +00:00
|
|
|
self.joepublic_login = {
|
2008-08-09 17:35:19 +00:00
|
|
|
LOGIN_FORM_KEY: 1,
|
|
|
|
'username': 'joepublic',
|
|
|
|
'password': 'secret'}
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 17:35:19 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 17:35:19 +00:00
|
|
|
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')
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 17:35:19 +00:00
|
|
|
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)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-08-09 17:35:19 +00:00
|
|
|
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.
|
2009-03-10 11:19:26 +00:00
|
|
|
Unsuccessfull attempts will continue to render the login page with
|
2008-08-09 17:35:19 +00:00
|
|
|
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 14:58:47 +00:00
|
|
|
self.failIf(login.context)
|
2008-08-09 17:35:19 +00:00
|
|
|
self.client.get('/test_admin/admin/logout/')
|
2008-08-24 06:34:18 +00:00
|
|
|
# make sure the view removes test cookie
|
|
|
|
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
|
2008-08-09 17:35:19 +00:00
|
|
|
|
|
|
|
# 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 14:58:47 +00:00
|
|
|
self.failIf(login.context)
|
2008-08-09 17:35:19 +00: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 14:58:47 +00:00
|
|
|
self.failIf(login.context)
|
2008-08-09 17:35:19 +00: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 14:58:47 +00:00
|
|
|
self.failIf(login.context)
|
2008-08-09 17:35:19 +00: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'))
|
2008-08-24 06:34:18 +00:00
|
|
|
|
|
|
|
# 8509 - if a normal user is already logged in, it is possible
|
|
|
|
# to change user into the superuser without error
|
|
|
|
login = self.client.login(username='joepublic', password='secret')
|
|
|
|
# Check and make sure that if user expires, data still persists
|
|
|
|
self.client.get('/test_admin/admin/secure-view/')
|
|
|
|
self.client.post('/test_admin/admin/secure-view/', self.super_login)
|
|
|
|
# make sure the view removes test cookie
|
|
|
|
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
|
2008-12-16 15:04:47 +00:00
|
|
|
|
|
|
|
class AdminViewUnicodeTest(TestCase):
|
|
|
|
fixtures = ['admin-views-unicode.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def testUnicodeEdit(self):
|
|
|
|
"""
|
|
|
|
A test to ensure that POST on edit_view handles non-ascii characters.
|
|
|
|
"""
|
|
|
|
post_data = {
|
|
|
|
"name": u"Test lærdommer",
|
|
|
|
# inline data
|
|
|
|
"chapter_set-TOTAL_FORMS": u"6",
|
|
|
|
"chapter_set-INITIAL_FORMS": u"3",
|
|
|
|
"chapter_set-0-id": u"1",
|
|
|
|
"chapter_set-0-title": u"Norske bostaver æøå skaper problemer",
|
|
|
|
"chapter_set-0-content": u"<p>Svært frustrerende med UnicodeDecodeError</p>",
|
|
|
|
"chapter_set-1-id": u"2",
|
|
|
|
"chapter_set-1-title": u"Kjærlighet.",
|
|
|
|
"chapter_set-1-content": u"<p>La kjærligheten til de lidende seire.</p>",
|
|
|
|
"chapter_set-2-id": u"3",
|
|
|
|
"chapter_set-2-title": u"Need a title.",
|
|
|
|
"chapter_set-2-content": u"<p>Newest content</p>",
|
|
|
|
"chapter_set-3-id": u"",
|
|
|
|
"chapter_set-3-title": u"",
|
|
|
|
"chapter_set-3-content": u"",
|
|
|
|
"chapter_set-4-id": u"",
|
|
|
|
"chapter_set-4-title": u"",
|
|
|
|
"chapter_set-4-content": u"",
|
|
|
|
"chapter_set-5-id": u"",
|
|
|
|
"chapter_set-5-title": u"",
|
|
|
|
"chapter_set-5-content": u"",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/book/1/', post_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
|
|
|
|
|
|
|
def testUnicodeDelete(self):
|
|
|
|
"""
|
|
|
|
Ensure that the delete_view handles non-ascii characters
|
|
|
|
"""
|
|
|
|
delete_dict = {'post': 'yes'}
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/book/1/delete/')
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/book/1/delete/', delete_dict)
|
|
|
|
self.assertRedirects(response, '/test_admin/admin/admin_views/book/')
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
|
|
|
|
class AdminViewListEditable(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml', 'admin-views-person.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-29 18:52:38 +00:00
|
|
|
def test_inheritance(self):
|
|
|
|
Podcast.objects.create(name="This Week in Django",
|
|
|
|
release_date=datetime.date.today())
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/podcast/')
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
def test_changelist_input_html(self):
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/person/')
|
|
|
|
# 2 inputs per object(the field and the hidden id field) = 6
|
|
|
|
# 2 management hidden fields = 2
|
2009-03-23 20:22:56 +00:00
|
|
|
# 4 action inputs (3 regular checkboxes, 1 checkbox to select all)
|
2009-03-17 20:51:47 +00:00
|
|
|
# main form submit button = 1
|
|
|
|
# search field and search submit button = 2
|
|
|
|
# 6 + 2 + 1 + 2 = 11 inputs
|
2009-03-23 20:22:56 +00:00
|
|
|
self.failUnlessEqual(response.content.count("<input"), 15)
|
2009-03-17 20:51:47 +00:00
|
|
|
# 1 select per object = 3 selects
|
2009-03-23 20:22:56 +00:00
|
|
|
self.failUnlessEqual(response.content.count("<select"), 4)
|
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
def test_post_submission(self):
|
|
|
|
data = {
|
|
|
|
"form-TOTAL_FORMS": "3",
|
|
|
|
"form-INITIAL_FORMS": "3",
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
"form-0-gender": "1",
|
|
|
|
"form-0-id": "1",
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
"form-1-gender": "2",
|
|
|
|
"form-1-id": "2",
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
"form-2-alive": "checked",
|
|
|
|
"form-2-gender": "1",
|
|
|
|
"form-2-id": "3",
|
|
|
|
}
|
|
|
|
self.client.post('/test_admin/admin/admin_views/person/', data)
|
|
|
|
|
|
|
|
self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, False)
|
|
|
|
self.failUnlessEqual(Person.objects.get(name="Grace Hooper").gender, 2)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
# test a filtered page
|
|
|
|
data = {
|
|
|
|
"form-TOTAL_FORMS": "2",
|
|
|
|
"form-INITIAL_FORMS": "2",
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
"form-0-id": "1",
|
|
|
|
"form-0-gender": "1",
|
|
|
|
"form-0-alive": "checked",
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
"form-1-id": "3",
|
|
|
|
"form-1-gender": "1",
|
|
|
|
"form-1-alive": "checked",
|
|
|
|
}
|
|
|
|
self.client.post('/test_admin/admin/admin_views/person/?gender__exact=1', data)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, True)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
# test a searched page
|
|
|
|
data = {
|
|
|
|
"form-TOTAL_FORMS": "1",
|
|
|
|
"form-INITIAL_FORMS": "1",
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
"form-0-id": "1",
|
|
|
|
"form-0-gender": "1"
|
|
|
|
}
|
|
|
|
self.client.post('/test_admin/admin/admin_views/person/?q=mauchly', data)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-03-17 20:51:47 +00:00
|
|
|
self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, False)
|
|
|
|
|
2009-03-10 11:19:26 +00:00
|
|
|
class AdminInheritedInlinesTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml',]
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def testInline(self):
|
|
|
|
"Ensure that inline models which inherit from a common parent are correctly handled by admin."
|
|
|
|
|
|
|
|
foo_user = u"foo username"
|
|
|
|
bar_user = u"bar username"
|
|
|
|
|
|
|
|
name_re = re.compile('name="(.*?)"')
|
|
|
|
|
|
|
|
# test the add case
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/persona/add/')
|
|
|
|
names = name_re.findall(response.content)
|
|
|
|
# make sure we have no duplicate HTML names
|
|
|
|
self.failUnlessEqual(len(names), len(set(names)))
|
|
|
|
|
|
|
|
# test the add case
|
|
|
|
post_data = {
|
|
|
|
"name": u"Test Name",
|
|
|
|
# inline data
|
|
|
|
"accounts-TOTAL_FORMS": u"1",
|
|
|
|
"accounts-INITIAL_FORMS": u"0",
|
|
|
|
"accounts-0-username": foo_user,
|
|
|
|
"accounts-2-TOTAL_FORMS": u"1",
|
|
|
|
"accounts-2-INITIAL_FORMS": u"0",
|
|
|
|
"accounts-2-0-username": bar_user,
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/persona/add/', post_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302) # redirect somewhere
|
|
|
|
self.failUnlessEqual(Persona.objects.count(), 1)
|
|
|
|
self.failUnlessEqual(FooAccount.objects.count(), 1)
|
|
|
|
self.failUnlessEqual(BarAccount.objects.count(), 1)
|
|
|
|
self.failUnlessEqual(FooAccount.objects.all()[0].username, foo_user)
|
|
|
|
self.failUnlessEqual(BarAccount.objects.all()[0].username, bar_user)
|
|
|
|
self.failUnlessEqual(Persona.objects.all()[0].accounts.count(), 2)
|
|
|
|
|
|
|
|
# test the edit case
|
|
|
|
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/persona/1/')
|
|
|
|
names = name_re.findall(response.content)
|
|
|
|
# make sure we have no duplicate HTML names
|
|
|
|
self.failUnlessEqual(len(names), len(set(names)))
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
"name": u"Test Name",
|
|
|
|
|
|
|
|
"accounts-TOTAL_FORMS": "2",
|
|
|
|
"accounts-INITIAL_FORMS": u"1",
|
|
|
|
|
|
|
|
"accounts-0-username": "%s-1" % foo_user,
|
|
|
|
"accounts-0-account_ptr": "1",
|
|
|
|
"accounts-0-persona": "1",
|
|
|
|
|
|
|
|
"accounts-2-TOTAL_FORMS": u"2",
|
|
|
|
"accounts-2-INITIAL_FORMS": u"1",
|
|
|
|
|
|
|
|
"accounts-2-0-username": "%s-1" % bar_user,
|
|
|
|
"accounts-2-0-account_ptr": "2",
|
|
|
|
"accounts-2-0-persona": "1",
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/persona/1/', post_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302)
|
|
|
|
self.failUnlessEqual(Persona.objects.count(), 1)
|
|
|
|
self.failUnlessEqual(FooAccount.objects.count(), 1)
|
|
|
|
self.failUnlessEqual(BarAccount.objects.count(), 1)
|
|
|
|
self.failUnlessEqual(FooAccount.objects.all()[0].username, "%s-1" % foo_user)
|
|
|
|
self.failUnlessEqual(BarAccount.objects.all()[0].username, "%s-1" % bar_user)
|
|
|
|
self.failUnlessEqual(Persona.objects.all()[0].accounts.count(), 2)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
from django.core import mail
|
|
|
|
|
|
|
|
class AdminActionsTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_model_admin_custom_action(self):
|
|
|
|
"Tests a custom action defined in a ModelAdmin method"
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [1],
|
|
|
|
'action' : 'mail_admin',
|
|
|
|
'index': 0,
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data)
|
|
|
|
self.assertEquals(len(mail.outbox), 1)
|
|
|
|
self.assertEquals(mail.outbox[0].subject, 'Greetings from a ModelAdmin action')
|
|
|
|
|
|
|
|
def test_model_admin_default_delete_action(self):
|
|
|
|
"Tests the default delete action defined as a ModelAdmin method"
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [1, 2],
|
|
|
|
'action' : 'delete_selected',
|
|
|
|
'index': 0,
|
|
|
|
}
|
|
|
|
delete_confirmation_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [1, 2],
|
|
|
|
'action' : 'delete_selected',
|
|
|
|
'index': 0,
|
|
|
|
'post': 'yes',
|
|
|
|
}
|
|
|
|
confirmation = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data)
|
|
|
|
self.assertContains(confirmation, "Are you sure you want to delete the selected subscriber objects")
|
|
|
|
self.failUnless(confirmation.content.count(ACTION_CHECKBOX_NAME) == 2)
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/subscriber/', delete_confirmation_data)
|
|
|
|
self.failUnlessEqual(Subscriber.objects.count(), 0)
|
|
|
|
|
|
|
|
def test_custom_function_mail_action(self):
|
|
|
|
"Tests a custom action defined in a function"
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [1],
|
|
|
|
'action' : 'external_mail',
|
|
|
|
'index': 0,
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data)
|
|
|
|
self.assertEquals(len(mail.outbox), 1)
|
|
|
|
self.assertEquals(mail.outbox[0].subject, 'Greetings from a function action')
|
|
|
|
|
|
|
|
def test_custom_function_action_with_redirect(self):
|
|
|
|
"Tests a custom action defined in a function"
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [1],
|
|
|
|
'action' : 'redirect_to',
|
|
|
|
'index': 0,
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data)
|
|
|
|
self.failUnlessEqual(response.status_code, 302)
|
2009-03-31 00:03:34 +00:00
|
|
|
|
|
|
|
class TestInlineNotEditable(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
result = self.client.login(username='super', password='secret')
|
|
|
|
self.failUnlessEqual(result, True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
"""
|
|
|
|
InlineModelAdmin broken?
|
|
|
|
"""
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/parent/add/')
|
|
|
|
self.failUnlessEqual(response.status_code, 200)
|
2009-04-01 03:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminCustomQuerysetTest(TestCase):
|
|
|
|
fixtures = ['admin-views-users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
self.pks = [EmptyModel.objects.create().id for i in range(3)]
|
|
|
|
|
|
|
|
def test_changelist_view(self):
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
|
|
|
|
for i in self.pks:
|
|
|
|
if i > 1:
|
|
|
|
self.assertContains(response, 'Primary key = %s' % i)
|
|
|
|
else:
|
|
|
|
self.assertNotContains(response, 'Primary key = %s' % i)
|
|
|
|
|
|
|
|
def test_change_view(self):
|
|
|
|
for i in self.pks:
|
|
|
|
response = self.client.get('/test_admin/admin/admin_views/emptymodel/%s/' % i)
|
|
|
|
if i > 1:
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
else:
|
|
|
|
self.assertEqual(response.status_code, 404)
|