Skip to content

Commit

Permalink
Implemented some unit tests for the ParamRoleBackend class.
Browse files Browse the repository at this point in the history
Also declared a few dummy models in order to setup a self-contained testing environment.
  • Loading branch information
seldon committed Sep 14, 2011
1 parent 3da950f commit bc4e743
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 13 deletions.
71 changes: 71 additions & 0 deletions flexi_auth/tests/models.py
@@ -0,0 +1,71 @@
from django.db import models

from flexi_auth.models import PermissionBase

class Author(models.Model):
name = models.CharField(max_length=50)
surname = models.CharField(max_length=50)

class Article(models.Model):
title = models.CharField(max_length=50)
body = models.TextField()
author = models.ForeignKey(Author)

##-------------- authorization API----------------##
# table-level CREATE permission
@classmethod
def can_create(cls, user, context):
if context:
try:
website = context['website']
edition = context['edition']
if (website=="BarSite" or (website=="FooSite" and edition=="morning")):
return True
except KeyError:
pass
return False
# row-level VIEW permission
def can_view (self, user, context):
if context:
try:
website = context['website']
edition = context['edition']
if (website=="BarSite" or (website=="FooSite" and edition=="morning")):
return True
except KeyError:
pass
return False
##-------------------------------------------------##


class Book(models.Model, PermissionBase):
title = models.CharField(max_length=50)
content = models.TextField()
authors = models.ManyToManyField(Author)

##-------------- authorization API----------------##
# table-level CREATE permission
@classmethod
def can_create(cls, user, context):
if context:
try:
website = context['website']
edition = context['edition']
if (website=="BarSite" or (website=="FooSite" and edition=="morning")):
return True
except KeyError:
pass
return False
# row-level VIEW permission
def can_view (self, user, context):
if context:
try:
language = context['language']
cover = context['cover']
if (language=="Italian" or (language=="Dutch" and cover=="Paperback")):
return True
except KeyError:
pass
return False
##-------------------------------------------------##

0 comments on commit bc4e743

Please sign in to comment.