Ticket #5775: test_staffmemberrequired.patch

File test_staffmemberrequired.patch, 2.8 KB (added by jdetaeye, 8 years ago)

Additional test for this decorator

  • django/contrib/admin/tests/__init__.py

     
    1 from django.contrib.admin.tests import widgets
     1from django.contrib.admin.tests import widgets, decorators
    22
    33__test__ = {
    44    'WIDGET_TESTS': widgets,
     5    'DECORATORS_TESTS': decorators,
    56}
  • django/contrib/admin/tests/decorators.py

     
     1"""
     2>>> from django.contrib.auth.models import User
     3>>> from django.test import Client
     4
     5Created a regular user and an admin user.
     6
     7>>> adminuser = User.objects.create_user('staff', 'staff@mysite', 'staffpassword')
     8>>> normaluser = User.objects.create_user('user', 'user@mysite', 'userpassword')
     9>>> adminuser.is_staff = True
     10>>> adminuser.save()
     11
     12Login as a staff user and access a protected view.
     13
     14>>> c = Client()
     15>>> c.login(username='staff', password='staffpassword')
     16True
     17>>> response = c.get('/admin/staff_members_only/', {'param':1})
     18>>> response.status_code
     19200
     20>>> response.content
     21'param:1'
     22
     23Login as a normal user and access a protected view.
     24
     25>>> c = Client()
     26>>> c.login(username='user', password='userpassword')
     27True
     28>>> response = c.get('/admin/staff_members_only/', {'param':1})
     29>>> response.status_code
     30200
     31>>> response.content.find('action="/admin/staff_members_only/?param=1"') > 0
     32True
     33
     34"""
     35
     36from django.contrib.admin.views.decorators import staff_member_required
     37from django.http import HttpResponse
     38
     39def test_staff_member_required( req ):
     40  return HttpResponse( '\n'.join( u'%s:%s' % (item,req.GET[item]) for item in req.GET) )
     41test_staff_member_required = staff_member_required( test_staff_member_required )
  • tests/urls.py

    Property changes on: django\contrib\admin\tests\decorators.py
    ___________________________________________________________________
    Name: svn:executable
       + *
    Name: svn:mime-type
       + text/plain
    Name: svn:keywords
       + URL LastChangedRevision LastChangedBy LastChangedDate
    Name: svn:eol-style
       + native
    
     
    1111
    1212    # test urlconf for {% url %} template tag
    1313    (r'^url_tag/', include('regressiontests.templates.urls')),
    14    
     14
    1515    # django built-in views
    1616    (r'^views/', include('regressiontests.views.urls')),
     17
     18    # test urlconf for the staff_member_required admin decorator
     19    ('^admin/staff_members_only/$', 'django.contrib.admin.tests.decorators.test_staff_member_required'),
    1720)
Back to Top