Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented some unit tests for the
ParamRoleBackend
class.
Also declared a few dummy models in order to setup a self-contained testing environment.
- Loading branch information
Showing
2 changed files
with
328 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
##-------------------------------------------------## | ||
|
Oops, something went wrong.