Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[generic-auth] Added tests for generic-auth. They're not sufficient y…

…et, but it's a start.

git-svn-id: http://code.djangoproject.com/svn/django/branches/generic-auth@4027 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b69fce82b1c42d88157f45c8f5bd020468920a60 1 parent d04abf3
@jkocherhans jkocherhans authored
View
0  tests/regressiontests/generic_auth/__init__.py
No changes.
View
72 tests/regressiontests/generic_auth/models.py
@@ -0,0 +1,72 @@
+"""
+XX. Permissions
+
+"""
+
+from django.db import models
+
+class Person(models.Model):
+ name = models.CharField(maxlength=20)
+
+API_TESTS = """
+>>> from django.contrib.auth.models import User, Permission
+>>> from django.db.models.loading import get_app
+>>> from django.contrib.auth.management import create_permissions
+>>> from django.contrib.auth import has_permission
+
+>>> app = get_app('generic_auth')
+>>> create_permissions(app, [])
+Adding permission 'person | Can add person'
+Adding permission 'person | Can change person'
+Adding permission 'person | Can delete person'
+
+
+Create and register an authorization handler that acts like Django's model
+level permissions
+
+>>> def default_has_permission(user, permission, obj):
+... p_name = "%s.%s" % (permission.content_type.app_label, permission.codename)
+... return user.has_perm(p_name)
+...
+>>> has_permission.register(default_has_permission, User, Permission, Person)
+>>> has_permission.register(default_has_permission, User, Permission)
+
+
+Create a new user
+
+>>> user = User.objects.create_user('test', 'test@example.com', 'password')
+>>> user.save()
+
+
+Create a Person that we'll check from access to.
+
+>>> person = Person(name='test')
+>>> person.save()
+
+
+Get permissions from the model
+
+>>> opts = Person._meta
+>>> add_permission = opts.get_add_permission()
+>>> change_permission = opts.get_change_permission()
+>>> delete_permission = opts.get_delete_permission()
+
+
+Give the user add, change, and delete permissions for Person models, then check that permission.
+
+>>> user.user_permissions.add(add_permission)
+>>> user.user_permissions.add(change_permission)
+>>> user.user_permissions.add(delete_permission)
+>>> user.save()
+
+
+The user should have add, change, and delete permissions now. Make sure they do.
+
+>>> has_permission(user, add_permission)
+True
+>>> has_permission(user, change_permission, person)
+True
+>>> has_permission(user, delete_permission, person)
+True
+
+"""
Please sign in to comment.
Something went wrong with that request. Please try again.