<s> from django . contrib . auth . models import User , Permission <EOL> from django . core . urlresolvers import reverse <EOL> from django . template import Template , Context <EOL> from django . test import TestCase <EOL> from django . test . client import RequestFactory <EOL> import djadmin2 <EOL> from djadmin2 import ModelAdmin2 <EOL> from djadmin2 . permissions import TemplatePermissionChecker <EOL> from blog . models import Post <EOL> class TemplatePermissionTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . factory = RequestFactory ( ) <EOL> self . user = User ( <EOL> username = '<STR_LIT>' , <EOL> is_staff = True ) <EOL> self . user . set_password ( '<STR_LIT>' ) <EOL> self . user . save ( ) <EOL> def render ( self , template , context ) : <EOL> template = Template ( template ) <EOL> context = Context ( context ) <EOL> return template . render ( context ) <EOL> def test_permission_wrapper ( self ) : <EOL> model_admin = ModelAdmin2 ( Post , djadmin2 . default ) <EOL> request = self . factory . get ( reverse ( '<STR_LIT>' ) ) <EOL> request . user = self . user <EOL> permissions = TemplatePermissionChecker ( request , model_admin ) <EOL> context = { <EOL> '<STR_LIT>' : permissions , <EOL> } <EOL> result = self . render ( <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( '<STR_LIT>' , context ) <EOL> self . assertEqual ( result , '<STR_LIT:False>' ) <EOL> post_add_permission = Permission . objects . get ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT>' , <EOL> codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( post_add_permission ) <EOL> if hasattr ( self . user , '<STR_LIT>' ) : <EOL> del self . user . _perm_cache <EOL> result = self . render ( '<STR_LIT>' , context ) <EOL> self . assertEqual ( result , '<STR_LIT:True>' ) <EOL> def test_admin_traversal_by_name ( self ) : <EOL> post_add_permission = Permission . objects . get ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT>' , <EOL> codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( post_add_permission ) <EOL> model_admin = ModelAdmin2 ( Post , djadmin2 . default ) <EOL> request = self . factory . get ( reverse ( '<STR_LIT>' ) ) <EOL> request . user = self . user <EOL> permissions = TemplatePermissionChecker ( request , model_admin ) <EOL> context = { <EOL> '<STR_LIT>' : permissions , <EOL> } <EOL> result = self . render ( '<STR_LIT>' , context ) <EOL> self . assertEqual ( result , '<STR_LIT:True>' ) <EOL> result = self . render ( '<STR_LIT>' , context ) <EOL> self . assertEqual ( result , '<STR_LIT:True>' ) <EOL> result = self . render ( '<STR_LIT>' , context ) <EOL> self . assertEqual ( result , '<STR_LIT:False>' ) <EOL> result = self . render ( '<STR_LIT>' , context ) <EOL> self . assertEqual ( result , '<STR_LIT:False>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> def test_admin_binding ( self ) : <EOL> user_admin = djadmin2 . default . get_admin_by_name ( '<STR_LIT>' ) <EOL> post_admin = djadmin2 . default . get_admin_by_name ( '<STR_LIT>' ) <EOL> request = self . factory . get ( reverse ( '<STR_LIT>' ) ) <EOL> request . user = self . user <EOL> permissions = TemplatePermissionChecker ( request , user_admin ) <EOL> post = Post . objects . create ( title = '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> context = { <EOL> '<STR_LIT>' : post , <EOL> '<STR_LIT>' : post_admin , <EOL> '<STR_LIT>' : permissions , <EOL> } <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> post_add_permission = Permission . objects . get ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT>' , <EOL> codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( post_add_permission ) <EOL> if hasattr ( self . user , '<STR_LIT>' ) : <EOL> del self . user . _perm_cache <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT:True>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> def test_view_binding ( self ) : <EOL> user_admin = djadmin2 . default . get_admin_by_name ( '<STR_LIT>' ) <EOL> post_admin = djadmin2 . default . get_admin_by_name ( '<STR_LIT>' ) <EOL> request = self . factory . get ( reverse ( '<STR_LIT>' ) ) <EOL> request . user = self . user <EOL> permissions = TemplatePermissionChecker ( request , user_admin ) <EOL> context = { <EOL> '<STR_LIT>' : post_admin , <EOL> '<STR_LIT>' : post_admin . create_view , <EOL> '<STR_LIT>' : permissions , <EOL> } <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT:False>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> post_add_permission = Permission . objects . get ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT>' , <EOL> codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( post_add_permission ) <EOL> user_change_permission = Permission . objects . get ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT:user>' , <EOL> codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( user_change_permission ) <EOL> if hasattr ( self . user , '<STR_LIT>' ) : <EOL> del self . user . _perm_cache <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> def test_object_level_permission ( self ) : <EOL> model_admin = ModelAdmin2 ( Post , djadmin2 . default ) <EOL> request = self . factory . get ( reverse ( '<STR_LIT>' ) ) <EOL> request . user = self . user <EOL> permissions = TemplatePermissionChecker ( request , model_admin ) <EOL> post = Post . objects . create ( title = '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> context = { <EOL> '<STR_LIT>' : post , <EOL> '<STR_LIT>' : permissions , <EOL> } <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT:False>' ) <EOL> post_add_permission = Permission . objects . get ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT>' , <EOL> codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( post_add_permission ) <EOL> if hasattr ( self . user , '<STR_LIT>' ) : <EOL> del self . user . _perm_cache <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> result = self . render ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> context ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> class ViewPermissionTest ( TestCase ) : <EOL> def test_view_permission_was_created ( self ) : <EOL> permissions = Permission . objects . filter ( <EOL> content_type__app_label = '<STR_LIT>' , <EOL> content_type__model = '<STR_LIT>' ) <EOL> self . assertEqual ( len ( permissions . filter ( codename = '<STR_LIT>' ) ) , <NUM_LIT:1> ) </s>
