<s> import warnings <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import User , Group , Permission , AnonymousUser <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import connection <EOL> from django . test import TestCase <EOL> from django . utils import unittest <EOL> class BackendTest ( TestCase ) : <EOL> backend = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . curr_auth = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( self . backend , ) <EOL> User . objects . create_user ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> User . objects . create_superuser ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . curr_auth <EOL> ContentType . objects . clear_cache ( ) <EOL> def test_has_perm ( self ) : <EOL> user = User . objects . get ( username = '<STR_LIT:test>' ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , False ) <EOL> user . is_staff = True <EOL> user . save ( ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , False ) <EOL> user . is_superuser = True <EOL> user . save ( ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , True ) <EOL> user . is_staff = False <EOL> user . is_superuser = False <EOL> user . save ( ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , False ) <EOL> user . is_staff = True <EOL> user . is_superuser = True <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , False ) <EOL> def test_custom_perms ( self ) : <EOL> user = User . objects . get ( username = '<STR_LIT:test>' ) <EOL> content_type = ContentType . objects . get_for_model ( Group ) <EOL> perm = Permission . objects . create ( name = '<STR_LIT:test>' , content_type = content_type , codename = '<STR_LIT:test>' ) <EOL> user . user_permissions . add ( perm ) <EOL> user . save ( ) <EOL> user = User . objects . get ( username = '<STR_LIT:test>' ) <EOL> self . assertEqual ( user . get_all_permissions ( ) == set ( [ u'<STR_LIT>' ] ) , True ) <EOL> self . assertEqual ( user . get_group_permissions ( ) , set ( [ ] ) ) <EOL> self . assertEqual ( user . has_module_perms ( '<STR_LIT>' ) , False ) <EOL> self . assertEqual ( user . has_module_perms ( '<STR_LIT>' ) , True ) <EOL> perm = Permission . objects . create ( name = '<STR_LIT>' , content_type = content_type , codename = '<STR_LIT>' ) <EOL> user . user_permissions . add ( perm ) <EOL> user . save ( ) <EOL> perm = Permission . objects . create ( name = '<STR_LIT>' , content_type = content_type , codename = '<STR_LIT>' ) <EOL> user . user_permissions . add ( perm ) <EOL> user . save ( ) <EOL> user = User . objects . get ( username = '<STR_LIT:test>' ) <EOL> self . assertEqual ( user . get_all_permissions ( ) , set ( [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT:test>' ) , False ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , True ) <EOL> self . assertEqual ( user . has_perms ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , True ) <EOL> perm = Permission . objects . create ( name = '<STR_LIT>' , content_type = content_type , codename = '<STR_LIT>' ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> group . permissions . add ( perm ) <EOL> group . save ( ) <EOL> user . groups . add ( group ) <EOL> user = User . objects . get ( username = '<STR_LIT:test>' ) <EOL> exp = set ( [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) <EOL> self . assertEqual ( user . get_all_permissions ( ) , exp ) <EOL> self . assertEqual ( user . get_group_permissions ( ) , set ( [ u'<STR_LIT>' ] ) ) <EOL> self . assertEqual ( user . has_perms ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , True ) <EOL> user = AnonymousUser ( ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT:test>' ) , False ) <EOL> self . assertEqual ( user . has_perms ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , False ) <EOL> def test_has_no_object_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . get ( username = '<STR_LIT:test>' ) <EOL> content_type = ContentType . objects . get_for_model ( Group ) <EOL> perm = Permission . objects . create ( name = '<STR_LIT:test>' , content_type = content_type , codename = '<STR_LIT:test>' ) <EOL> user . user_permissions . add ( perm ) <EOL> user . save ( ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' , '<STR_LIT:object>' ) , False ) <EOL> self . assertEqual ( user . get_all_permissions ( '<STR_LIT:object>' ) , set ( [ ] ) ) <EOL> self . assertEqual ( user . has_perm ( '<STR_LIT>' ) , True ) <EOL> self . assertEqual ( user . get_all_permissions ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> def test_get_all_superuser_permissions ( self ) : <EOL> "<STR_LIT>" <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . assertEqual ( len ( user . get_all_permissions ( ) ) , len ( Permission . objects . all ( ) ) ) <EOL> BackendTest = unittest . skipIf ( not connection . features . supports_joins , <EOL> '<STR_LIT>' ) ( BackendTest ) <EOL> class TestObj ( object ) : <EOL> pass <EOL> class SimpleRowlevelBackend ( object ) : <EOL> supports_object_permissions = True <EOL> supports_inactive_user = False <EOL> def has_perm ( self , user , perm , obj = None ) : <EOL> if not obj : <EOL> return <EOL> if isinstance ( obj , TestObj ) : <EOL> if user . username == '<STR_LIT>' : <EOL> return True <EOL> elif user . is_anonymous ( ) and perm == '<STR_LIT>' : <EOL> return True <EOL> elif not user . is_active and perm == '<STR_LIT>' : <EOL> return True <EOL> return False <EOL> def has_module_perms ( self , user , app_label ) : <EOL> if not user . is_anonymous ( ) and not user . is_active : <EOL> return False <EOL> return app_label == "<STR_LIT>" <EOL> def get_all_permissions ( self , user , obj = None ) : <EOL> if not obj : <EOL> return [ ] <EOL> if not isinstance ( obj , TestObj ) : <EOL> return [ '<STR_LIT:none>' ] <EOL> if user . is_anonymous ( ) : <EOL> return [ '<STR_LIT>' ] <EOL> if user . username == '<STR_LIT>' : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> return [ '<STR_LIT>' ] <EOL> def get_group_permissions ( self , user , obj = None ) : <EOL> if not obj : <EOL> return <EOL> if not isinstance ( obj , TestObj ) : <EOL> return [ '<STR_LIT:none>' ] <EOL> if '<STR_LIT>' in [ group . name for group in user . groups . all ( ) ] : <EOL> return [ '<STR_LIT>' ] <EOL> else : <EOL> return [ '<STR_LIT:none>' ] <EOL> class RowlevelBackendTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> backend = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . curr_auth = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = tuple ( self . curr_auth ) + ( self . backend , ) <EOL> self . user1 = User . objects . create_user ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> self . user2 = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> self . user3 = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> self . save_warnings_state ( ) <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , category = DeprecationWarning , <EOL> module = '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . curr_auth <EOL> self . restore_warnings_state ( ) <EOL> ContentType . objects . clear_cache ( ) <EOL> def test_has_perm ( self ) : <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user2 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , True ) <EOL> self . assertEqual ( self . user2 . has_perm ( '<STR_LIT>' ) , False ) <EOL> self . assertEqual ( self . user2 . has_perms ( [ '<STR_LIT>' , '<STR_LIT>' ] , TestObj ( ) ) , True ) <EOL> self . assertEqual ( self . user3 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user3 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user3 . has_perms ( [ '<STR_LIT>' , '<STR_LIT>' ] , TestObj ( ) ) , False ) <EOL> def test_get_all_permissions ( self ) : <EOL> self . assertEqual ( self . user1 . get_all_permissions ( TestObj ( ) ) , set ( [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( self . user2 . get_all_permissions ( TestObj ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( self . user2 . get_all_permissions ( ) , set ( [ ] ) ) <EOL> def test_get_group_permissions ( self ) : <EOL> content_type = ContentType . objects . get_for_model ( Group ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> self . user3 . groups . add ( group ) <EOL> self . assertEqual ( self . user3 . get_group_permissions ( TestObj ( ) ) , set ( [ '<STR_LIT>' ] ) ) <EOL> RowlevelBackendTest = unittest . skipIf ( not connection . features . supports_joins , <EOL> '<STR_LIT>' ) ( RowlevelBackendTest ) <EOL> class AnonymousUserBackend ( SimpleRowlevelBackend ) : <EOL> supports_anonymous_user = True <EOL> supports_inactive_user = False <EOL> class NoAnonymousUserBackend ( SimpleRowlevelBackend ) : <EOL> supports_anonymous_user = False <EOL> supports_inactive_user = False <EOL> class AnonymousUserBackendTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> backend = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . curr_auth = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( self . backend , ) <EOL> self . user1 = AnonymousUser ( ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . curr_auth <EOL> def test_has_perm ( self ) : <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , True ) <EOL> def test_has_perms ( self ) : <EOL> self . assertEqual ( self . user1 . has_perms ( [ '<STR_LIT>' ] , TestObj ( ) ) , True ) <EOL> self . assertEqual ( self . user1 . has_perms ( [ '<STR_LIT>' , '<STR_LIT>' ] , TestObj ( ) ) , False ) <EOL> def test_has_module_perms ( self ) : <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , True ) <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) <EOL> def test_get_all_permissions ( self ) : <EOL> self . assertEqual ( self . user1 . get_all_permissions ( TestObj ( ) ) , set ( [ '<STR_LIT>' ] ) ) <EOL> class NoAnonymousUserBackendTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> backend = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . curr_auth = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = tuple ( self . curr_auth ) + ( self . backend , ) <EOL> self . user1 = AnonymousUser ( ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . curr_auth <EOL> def test_has_perm ( self ) : <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> def test_has_perms ( self ) : <EOL> self . assertEqual ( self . user1 . has_perms ( [ '<STR_LIT>' ] , TestObj ( ) ) , False ) <EOL> def test_has_module_perms ( self ) : <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) <EOL> def test_get_all_permissions ( self ) : <EOL> self . assertEqual ( self . user1 . get_all_permissions ( TestObj ( ) ) , set ( ) ) <EOL> class NoBackendsTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . old_AUTHENTICATION_BACKENDS = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = [ ] <EOL> self . user = User . objects . create_user ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . old_AUTHENTICATION_BACKENDS <EOL> def test_raises_exception ( self ) : <EOL> self . assertRaises ( ImproperlyConfigured , self . user . has_perm , ( '<STR_LIT>' , TestObj ( ) , ) ) <EOL> class InActiveUserBackend ( SimpleRowlevelBackend ) : <EOL> supports_anonymous_user = False <EOL> supports_inactive_user = True <EOL> class NoInActiveUserBackend ( SimpleRowlevelBackend ) : <EOL> supports_anonymous_user = False <EOL> supports_inactive_user = False <EOL> class InActiveUserBackendTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> backend = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . curr_auth = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( self . backend , ) <EOL> self . user1 = User . objects . create_user ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> self . user1 . is_active = False <EOL> self . user1 . save ( ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . curr_auth <EOL> def test_has_perm ( self ) : <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , True ) <EOL> def test_has_module_perms ( self ) : <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) <EOL> class NoInActiveUserBackendTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> backend = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . curr_auth = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = tuple ( self . curr_auth ) + ( self . backend , ) <EOL> self . user1 = User . objects . create_user ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> self . user1 . is_active = False <EOL> self . user1 . save ( ) <EOL> def tearDown ( self ) : <EOL> settings . AUTHENTICATION_BACKENDS = self . curr_auth <EOL> def test_has_perm ( self ) : <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , False ) <EOL> self . assertEqual ( self . user1 . has_perm ( '<STR_LIT>' , TestObj ( ) ) , True ) <EOL> def test_has_module_perms ( self ) : <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) <EOL> self . assertEqual ( self . user1 . has_module_perms ( "<STR_LIT>" ) , False ) </s>
