<s> from __future__ import unicode_literals <EOL> import mock <EOL> import unittest <EOL> from django . contrib . auth . models import AnonymousUser <EOL> from django . contrib . auth . models import Group <EOL> from django . contrib . auth . models import Permission <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import ValidationError <EOL> from django . test import TestCase <EOL> import guardian <EOL> from guardian . backends import ObjectPermissionBackend <EOL> from guardian . compat import get_user_model <EOL> from guardian . compat import get_user_model_path <EOL> from guardian . compat import get_user_permission_codename <EOL> from guardian . compat import basestring <EOL> from guardian . compat import unicode <EOL> from guardian . exceptions import GuardianError <EOL> from guardian . exceptions import NotUserNorGroup <EOL> from guardian . exceptions import ObjectNotPersisted <EOL> from guardian . exceptions import WrongAppError <EOL> from guardian . models import GroupObjectPermission <EOL> from guardian . models import UserObjectPermission <EOL> from guardian . testapp . tests . conf import TestDataMixin <EOL> User = get_user_model ( ) <EOL> user_model_path = get_user_model_path ( ) <EOL> class UserPermissionTests ( TestDataMixin , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( UserPermissionTests , self ) . setUp ( ) <EOL> self . user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . ctype = ContentType . objects . create ( <EOL> model = '<STR_LIT:bar>' , app_label = '<STR_LIT>' ) <EOL> self . obj1 = ContentType . objects . create ( <EOL> model = '<STR_LIT:foo>' , app_label = '<STR_LIT>' ) <EOL> self . obj2 = ContentType . objects . create ( <EOL> model = '<STR_LIT:bar>' , app_label = '<STR_LIT>' ) <EOL> def test_assignement ( self ) : <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> UserObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . user , <EOL> self . ctype ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , <EOL> self . ctype ) ) <EOL> def test_assignement_and_remove ( self ) : <EOL> UserObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . user , <EOL> self . ctype ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> UserObjectPermission . objects . remove_perm ( '<STR_LIT>' , <EOL> self . user , self . ctype ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> def test_ctypes ( self ) : <EOL> UserObjectPermission . objects . assign_perm ( <EOL> '<STR_LIT>' , self . user , self . obj1 ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> UserObjectPermission . objects . remove_perm ( <EOL> '<STR_LIT>' , self . user , self . obj1 ) <EOL> UserObjectPermission . objects . assign_perm ( <EOL> '<STR_LIT>' , self . user , self . obj2 ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> UserObjectPermission . objects . assign_perm ( <EOL> '<STR_LIT>' , self . user , self . obj1 ) <EOL> UserObjectPermission . objects . assign_perm ( <EOL> '<STR_LIT>' , self . user , self . obj2 ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> UserObjectPermission . objects . remove_perm ( <EOL> '<STR_LIT>' , self . user , self . obj1 ) <EOL> UserObjectPermission . objects . remove_perm ( <EOL> '<STR_LIT>' , self . user , self . obj2 ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> def test_assign_perm_validation ( self ) : <EOL> self . assertRaises ( Permission . DoesNotExist , <EOL> UserObjectPermission . objects . assign_perm , '<STR_LIT>' , self . user , <EOL> self . user ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> ctype = ContentType . objects . get_for_model ( group ) <EOL> user_ctype = ContentType . objects . get_for_model ( self . user ) <EOL> codename = get_user_permission_codename ( '<STR_LIT>' ) <EOL> perm = Permission . objects . get ( <EOL> codename = codename , content_type = user_ctype ) <EOL> create_info = dict ( <EOL> permission = perm , <EOL> user = self . user , <EOL> content_type = ctype , <EOL> object_pk = group . pk <EOL> ) <EOL> self . assertRaises ( ValidationError , UserObjectPermission . objects . create , <EOL> ** create_info ) <EOL> def test_unicode ( self ) : <EOL> codename = get_user_permission_codename ( '<STR_LIT>' ) <EOL> obj_perm = UserObjectPermission . objects . assign_perm ( codename , <EOL> self . user , self . user ) <EOL> self . assertTrue ( isinstance ( obj_perm . __unicode__ ( ) , unicode ) ) <EOL> def test_errors ( self ) : <EOL> not_saved_user = User ( username = '<STR_LIT>' ) <EOL> codename = get_user_permission_codename ( '<STR_LIT>' ) <EOL> self . assertRaises ( ObjectNotPersisted , <EOL> UserObjectPermission . objects . assign_perm , <EOL> codename , self . user , not_saved_user ) <EOL> self . assertRaises ( ObjectNotPersisted , <EOL> UserObjectPermission . objects . remove_perm , <EOL> codename , self . user , not_saved_user ) <EOL> class GroupPermissionTests ( TestDataMixin , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( GroupPermissionTests , self ) . setUp ( ) <EOL> self . user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . group , created = Group . objects . get_or_create ( name = '<STR_LIT>' ) <EOL> self . user . groups . add ( self . group ) <EOL> self . ctype = ContentType . objects . create ( <EOL> model = '<STR_LIT:bar>' , app_label = '<STR_LIT>' ) <EOL> self . obj1 = ContentType . objects . create ( <EOL> model = '<STR_LIT:foo>' , app_label = '<STR_LIT>' ) <EOL> self . obj2 = ContentType . objects . create ( <EOL> model = '<STR_LIT:bar>' , app_label = '<STR_LIT>' ) <EOL> def test_assignement ( self ) : <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , <EOL> self . ctype ) ) <EOL> GroupObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . group , <EOL> self . ctype ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , <EOL> self . ctype ) ) <EOL> def test_assignement_and_remove ( self ) : <EOL> GroupObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . group , <EOL> self . ctype ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> GroupObjectPermission . objects . remove_perm ( '<STR_LIT>' , <EOL> self . group , self . ctype ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . ctype ) ) <EOL> def test_ctypes ( self ) : <EOL> GroupObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . group , <EOL> self . obj1 ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> GroupObjectPermission . objects . remove_perm ( '<STR_LIT>' , <EOL> self . group , self . obj1 ) <EOL> GroupObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . group , <EOL> self . obj2 ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> GroupObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . group , <EOL> self . obj1 ) <EOL> GroupObjectPermission . objects . assign_perm ( '<STR_LIT>' , self . group , <EOL> self . obj2 ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> self . assertTrue ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> GroupObjectPermission . objects . remove_perm ( '<STR_LIT>' , <EOL> self . group , self . obj1 ) <EOL> GroupObjectPermission . objects . remove_perm ( '<STR_LIT>' , <EOL> self . group , self . obj2 ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj2 ) ) <EOL> self . assertFalse ( self . user . has_perm ( '<STR_LIT>' , self . obj1 ) ) <EOL> def test_assign_perm_validation ( self ) : <EOL> self . assertRaises ( Permission . DoesNotExist , <EOL> GroupObjectPermission . objects . assign_perm , '<STR_LIT>' , self . group , <EOL> self . group ) <EOL> user = User . objects . create ( username = '<STR_LIT>' ) <EOL> ctype = ContentType . objects . get_for_model ( user ) <EOL> perm = Permission . objects . get ( codename = '<STR_LIT>' ) <EOL> create_info = dict ( <EOL> permission = perm , <EOL> group = self . group , <EOL> content_type = ctype , <EOL> object_pk = user . pk <EOL> ) <EOL> self . assertRaises ( ValidationError , GroupObjectPermission . objects . create , <EOL> ** create_info ) <EOL> def test_unicode ( self ) : <EOL> obj_perm = GroupObjectPermission . objects . assign_perm ( "<STR_LIT>" , <EOL> self . group , self . group ) <EOL> self . assertTrue ( isinstance ( obj_perm . __unicode__ ( ) , unicode ) ) <EOL> def test_errors ( self ) : <EOL> not_saved_group = Group ( name = '<STR_LIT>' ) <EOL> self . assertRaises ( ObjectNotPersisted , <EOL> GroupObjectPermission . objects . assign_perm , <EOL> "<STR_LIT>" , self . group , not_saved_group ) <EOL> self . assertRaises ( ObjectNotPersisted , <EOL> GroupObjectPermission . objects . remove_perm , <EOL> "<STR_LIT>" , self . group , not_saved_group ) <EOL> class ObjectPermissionBackendTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = User . objects . create ( username = '<STR_LIT>' ) <EOL> self . backend = ObjectPermissionBackend ( ) <EOL> def test_attrs ( self ) : <EOL> self . assertTrue ( self . backend . supports_anonymous_user ) <EOL> self . assertTrue ( self . backend . supports_object_permissions ) <EOL> self . assertTrue ( self . backend . supports_inactive_user ) <EOL> def test_authenticate ( self ) : <EOL> self . assertEqual ( self . backend . authenticate ( <EOL> self . user . username , self . user . password ) , None ) <EOL> def test_has_perm_noobj ( self ) : <EOL> result = self . backend . has_perm ( self . user , "<STR_LIT>" ) <EOL> self . assertFalse ( result ) <EOL> def test_has_perm_notauthed ( self ) : <EOL> user = AnonymousUser ( ) <EOL> self . assertFalse ( self . backend . has_perm ( user , "<STR_LIT>" , self . user ) ) <EOL> def test_has_perm_wrong_app ( self ) : <EOL> self . assertRaises ( WrongAppError , self . backend . has_perm , <EOL> self . user , "<STR_LIT>" , self . user ) <EOL> def test_obj_is_not_model ( self ) : <EOL> for obj in ( Group , <NUM_LIT> , "<STR_LIT>" , [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:7> ] , { } ) : <EOL> self . assertFalse ( self . backend . has_perm ( self . user , <EOL> "<STR_LIT>" , obj ) ) <EOL> def test_not_active_user ( self ) : <EOL> user = User . objects . create ( username = '<STR_LIT>' ) <EOL> ctype = ContentType . objects . create ( <EOL> model = '<STR_LIT:bar>' , app_label = '<STR_LIT>' ) <EOL> perm = '<STR_LIT>' <EOL> UserObjectPermission . objects . assign_perm ( perm , user , ctype ) <EOL> self . assertTrue ( self . backend . has_perm ( user , perm , ctype ) ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . assertFalse ( self . backend . has_perm ( user , perm , ctype ) ) <EOL> class GuardianBaseTests ( TestCase ) : <EOL> def has_attrs ( self ) : <EOL> self . assertTrue ( hasattr ( guardian , '<STR_LIT>' ) ) <EOL> def test_version ( self ) : <EOL> for x in guardian . VERSION : <EOL> self . assertTrue ( isinstance ( x , ( int , basestring ) ) ) <EOL> def test_get_version ( self ) : <EOL> self . assertTrue ( isinstance ( guardian . get_version ( ) , basestring ) ) <EOL> class TestExceptions ( TestCase ) : <EOL> def _test_error_class ( self , exc_cls ) : <EOL> self . assertTrue ( isinstance ( exc_cls , GuardianError ) ) <EOL> def test_error_classes ( self ) : <EOL> self . assertTrue ( isinstance ( GuardianError ( ) , Exception ) ) <EOL> guardian_errors = [ NotUserNorGroup ] <EOL> for err in guardian_errors : <EOL> self . _test_error_class ( err ( ) ) <EOL> @ unittest . skip ( "<STR_LIT>" ) <EOL> class TestMonkeyPatch ( TestCase ) : <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_monkey_patch ( self , mocked_get_user_model ) : <EOL> from django . contrib . auth . models import AbstractUser <EOL> class CustomUserTestClass ( AbstractUser ) : <EOL> pass <EOL> mocked_get_user_model . return_value = CustomUserTestClass <EOL> self . assertFalse ( getattr ( CustomUserTestClass , '<STR_LIT>' , False ) ) <EOL> self . assertFalse ( getattr ( CustomUserTestClass , '<STR_LIT>' , False ) ) <EOL> self . assertFalse ( getattr ( CustomUserTestClass , '<STR_LIT>' , False ) ) <EOL> guardian . monkey_patch_user ( ) <EOL> self . assertTrue ( getattr ( CustomUserTestClass , '<STR_LIT>' , False ) ) <EOL> self . assertTrue ( getattr ( CustomUserTestClass , '<STR_LIT>' , False ) ) <EOL> self . assertTrue ( getattr ( CustomUserTestClass , '<STR_LIT>' , False ) ) </s>
