<s> from __future__ import unicode_literals <EOL> from guardian . testapp . models import Mixed , ReverseMixed <EOL> from guardian . testapp . models import Project <EOL> from guardian . testapp . models import ProjectGroupObjectPermission <EOL> from guardian . testapp . models import ProjectUserObjectPermission <EOL> from django . contrib . auth . models import Group , Permission <EOL> from django . test import TestCase <EOL> from guardian . compat import get_user_model <EOL> from guardian . shortcuts import assign_perm <EOL> from guardian . shortcuts import get_groups_with_perms <EOL> from guardian . shortcuts import get_objects_for_group <EOL> from guardian . shortcuts import get_objects_for_user <EOL> from guardian . shortcuts import get_users_with_perms <EOL> from guardian . shortcuts import remove_perm <EOL> from guardian . testapp . tests . conf import skipUnlessTestApp <EOL> User = get_user_model ( ) <EOL> @ skipUnlessTestApp <EOL> class TestDirectUserPermissions ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . joe = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . project = Project . objects . create ( name = '<STR_LIT>' ) <EOL> def get_perm ( self , codename ) : <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : codename } <EOL> return Permission . objects . get ( ** filters ) <EOL> def test_after_perm_is_created_without_shortcut ( self ) : <EOL> perm = self . get_perm ( '<STR_LIT>' ) <EOL> ProjectUserObjectPermission . objects . create ( <EOL> user = self . joe , <EOL> permission = perm , <EOL> content_object = self . project , <EOL> ) <EOL> self . assertTrue ( self . joe . has_perm ( '<STR_LIT>' , self . project ) ) <EOL> def test_assign_perm ( self ) : <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . project ) <EOL> filters = { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : self . joe , <EOL> } <EOL> result = ProjectUserObjectPermission . objects . filter ( ** filters ) . count ( ) <EOL> self . assertEqual ( result , <NUM_LIT:1> ) <EOL> def test_remove_perm ( self ) : <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . project ) <EOL> filters = { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : self . joe , <EOL> } <EOL> result = ProjectUserObjectPermission . objects . filter ( ** filters ) . count ( ) <EOL> self . assertEqual ( result , <NUM_LIT:1> ) <EOL> remove_perm ( '<STR_LIT>' , self . joe , self . project ) <EOL> result = ProjectUserObjectPermission . objects . filter ( ** filters ) . count ( ) <EOL> self . assertEqual ( result , <NUM_LIT:0> ) <EOL> def test_get_users_with_perms ( self ) : <EOL> User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> jane = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . project ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . project ) <EOL> assign_perm ( '<STR_LIT>' , jane , self . project ) <EOL> self . assertEqual ( get_users_with_perms ( self . project , attach_perms = True ) , <EOL> { <EOL> self . joe : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> jane : [ '<STR_LIT>' ] , <EOL> } ) <EOL> def test_get_users_with_perms_plus_groups ( self ) : <EOL> User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> jane = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> self . joe . groups . add ( group ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . project ) <EOL> assign_perm ( '<STR_LIT>' , group , self . project ) <EOL> assign_perm ( '<STR_LIT>' , jane , self . project ) <EOL> self . assertEqual ( get_users_with_perms ( self . project , attach_perms = True ) , <EOL> { <EOL> self . joe : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> jane : [ '<STR_LIT>' ] , <EOL> } ) <EOL> def test_get_objects_for_user ( self ) : <EOL> foo = Project . objects . create ( name = '<STR_LIT:foo>' ) <EOL> bar = Project . objects . create ( name = '<STR_LIT:bar>' ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , foo ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , bar ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , bar ) <EOL> result = get_objects_for_user ( self . joe , '<STR_LIT>' ) <EOL> self . assertEqual ( sorted ( p . pk for p in result ) , <EOL> sorted ( [ foo . pk , bar . pk ] ) ) <EOL> def test_get_all_permissions ( self ) : <EOL> foo = Project . objects . create ( name = '<STR_LIT:foo>' ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , foo ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , foo ) <EOL> result = self . joe . get_all_permissions ( foo ) <EOL> self . assertEqual ( result , set ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> def test_get_all_permissions_no_object ( self ) : <EOL> foo = Project . objects . create ( name = '<STR_LIT:foo>' ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , foo ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , foo ) <EOL> result = self . joe . get_all_permissions ( ) <EOL> self . assertEqual ( result , set ( ) ) <EOL> @ skipUnlessTestApp <EOL> class TestDirectGroupPermissions ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . joe = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> self . joe . groups . add ( self . group ) <EOL> self . project = Project . objects . create ( name = '<STR_LIT>' ) <EOL> def get_perm ( self , codename ) : <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : codename } <EOL> return Permission . objects . get ( ** filters ) <EOL> def test_after_perm_is_created_without_shortcut ( self ) : <EOL> perm = self . get_perm ( '<STR_LIT>' ) <EOL> ProjectGroupObjectPermission . objects . create ( <EOL> group = self . group , <EOL> permission = perm , <EOL> content_object = self . project , <EOL> ) <EOL> self . assertTrue ( self . joe . has_perm ( '<STR_LIT>' , self . project ) ) <EOL> def test_assign_perm ( self ) : <EOL> assign_perm ( '<STR_LIT>' , self . group , self . project ) <EOL> filters = { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . group , <EOL> } <EOL> result = ProjectGroupObjectPermission . objects . filter ( ** filters ) . count ( ) <EOL> self . assertEqual ( result , <NUM_LIT:1> ) <EOL> def test_remove_perm ( self ) : <EOL> assign_perm ( '<STR_LIT>' , self . group , self . project ) <EOL> filters = { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . group , <EOL> } <EOL> result = ProjectGroupObjectPermission . objects . filter ( ** filters ) . count ( ) <EOL> self . assertEqual ( result , <NUM_LIT:1> ) <EOL> remove_perm ( '<STR_LIT>' , self . group , self . project ) <EOL> result = ProjectGroupObjectPermission . objects . filter ( ** filters ) . count ( ) <EOL> self . assertEqual ( result , <NUM_LIT:0> ) <EOL> def test_get_groups_with_perms ( self ) : <EOL> Group . objects . create ( name = '<STR_LIT>' ) <EOL> devs = Group . objects . create ( name = '<STR_LIT>' ) <EOL> assign_perm ( '<STR_LIT>' , self . group , self . project ) <EOL> assign_perm ( '<STR_LIT>' , self . group , self . project ) <EOL> assign_perm ( '<STR_LIT>' , devs , self . project ) <EOL> self . assertEqual ( get_groups_with_perms ( self . project , attach_perms = True ) , <EOL> { <EOL> self . group : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> devs : [ '<STR_LIT>' ] , <EOL> } ) <EOL> def test_get_objects_for_group ( self ) : <EOL> foo = Project . objects . create ( name = '<STR_LIT:foo>' ) <EOL> bar = Project . objects . create ( name = '<STR_LIT:bar>' ) <EOL> assign_perm ( '<STR_LIT>' , self . group , foo ) <EOL> assign_perm ( '<STR_LIT>' , self . group , bar ) <EOL> assign_perm ( '<STR_LIT>' , self . group , bar ) <EOL> result = get_objects_for_group ( self . group , '<STR_LIT>' ) <EOL> self . assertEqual ( sorted ( p . pk for p in result ) , <EOL> sorted ( [ foo . pk , bar . pk ] ) ) <EOL> @ skipUnlessTestApp <EOL> class TestMixedDirectAndGenericObjectPermission ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . joe = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> self . joe . groups . add ( self . group ) <EOL> self . mixed = Mixed . objects . create ( name = '<STR_LIT>' ) <EOL> self . reverse_mixed = ReverseMixed . objects . create ( name = '<STR_LIT>' ) <EOL> def test_get_users_with_perms_plus_groups ( self ) : <EOL> User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> jane = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> self . joe . groups . add ( group ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . mixed ) <EOL> assign_perm ( '<STR_LIT>' , group , self . mixed ) <EOL> assign_perm ( '<STR_LIT>' , jane , self . mixed ) <EOL> self . assertEqual ( get_users_with_perms ( self . mixed , attach_perms = True ) , <EOL> { <EOL> self . joe : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> jane : [ '<STR_LIT>' ] , <EOL> } ) <EOL> result = get_objects_for_user ( self . joe , '<STR_LIT>' ) <EOL> self . assertEqual ( sorted ( p . pk for p in result ) , <EOL> sorted ( [ self . mixed . pk ] ) ) <EOL> def test_get_users_with_perms_plus_groups_reverse_mixed ( self ) : <EOL> User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> jane = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> self . joe . groups . add ( group ) <EOL> assign_perm ( '<STR_LIT>' , self . joe , self . reverse_mixed ) <EOL> assign_perm ( '<STR_LIT>' , group , self . reverse_mixed ) <EOL> assign_perm ( '<STR_LIT>' , jane , self . reverse_mixed ) <EOL> self . assertEqual ( get_users_with_perms ( self . reverse_mixed , attach_perms = True ) , <EOL> { <EOL> self . joe : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> jane : [ '<STR_LIT>' ] , <EOL> } ) <EOL> result = get_objects_for_user ( self . joe , '<STR_LIT>' ) <EOL> self . assertEqual ( sorted ( p . pk for p in result ) , <EOL> sorted ( [ self . reverse_mixed . pk ] ) ) </s>
