<s> from django . test . testcases import TestCase <EOL> from rulez import registry <EOL> from rulez . backends import ObjectPermissionBackend <EOL> from rulez . exceptions import NonexistentFieldName , NotBooleanPermission <EOL> from rulez . registry import Rule <EOL> class MockModel ( ) : <EOL> pk = <NUM_LIT> <EOL> not_callable = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . attr_permission = True <EOL> self . attr_wrong_permission = "<STR_LIT>" <EOL> def mock_permission ( self , user ) : <EOL> return True <EOL> def mock_simple_permission ( self ) : <EOL> return True <EOL> def mock_non_boolean_permission ( self , user ) : <EOL> return "<STR_LIT>" <EOL> class MockUser ( ) : <EOL> def __init__ ( self , is_active = True ) : <EOL> self . pk = <NUM_LIT> <EOL> self . is_active = is_active <EOL> def is_anonymous ( self ) : <EOL> return False <EOL> class BackendTestCase ( TestCase ) : <EOL> def create_fixtures ( self ) : <EOL> self . user = MockUser ( ) <EOL> self . inactive_user = MockUser ( is_active = False ) <EOL> self . model = MockModel ( ) <EOL> def test_user_is_tested_for_rule ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> res = back . has_perm ( self . user , '<STR_LIT>' , self . model ) <EOL> self . assertEqual ( res , True ) <EOL> def test_rules_returns_False_for_None_obj ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> res = back . has_perm ( self . user , '<STR_LIT>' , None ) <EOL> self . assertEqual ( res , False ) <EOL> def test_rules_returns_False_for_inexistant_rule ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> res = back . has_perm ( self . user , '<STR_LIT>' , self . model ) <EOL> self . assertEqual ( res , False ) <EOL> def test_user_is_tested_for_simple_rule ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> res = back . has_perm ( self . user , '<STR_LIT>' , self . model ) <EOL> self . assertEqual ( res , True ) <EOL> def test_user_is_tested_for_simple_rule_by_field_name ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( <EOL> '<STR_LIT>' , MockModel , field_name = '<STR_LIT>' ) <EOL> back = ObjectPermissionBackend ( ) <EOL> res = back . has_perm ( self . user , '<STR_LIT>' , self . model ) <EOL> self . assertEqual ( res , True ) <EOL> def test_non_existant_filenames_are_caught ( self ) : <EOL> self . create_fixtures ( ) <EOL> codename = '<STR_LIT>' <EOL> rule = Rule ( codename , MockModel , field_name = '<STR_LIT>' ) <EOL> registry . registry [ MockModel ] . update ( { codename : rule } ) <EOL> back = ObjectPermissionBackend ( ) <EOL> self . assertRaises ( <EOL> NonexistentFieldName , back . has_perm , self . user , '<STR_LIT>' , <EOL> self . model ) <EOL> def test_inactive_user_can_never_have_any_permissions ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> res = back . has_perm ( self . inactive_user , '<STR_LIT>' , self . model ) <EOL> self . assertEqual ( res , False ) <EOL> def test_non_boolean_permissions_raises ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> self . assertRaises ( <EOL> NotBooleanPermission , back . has_perm , self . user , <EOL> '<STR_LIT>' , self . model ) <EOL> def test_non_callable_permission_raises ( self ) : <EOL> self . create_fixtures ( ) <EOL> registry . register ( '<STR_LIT>' , MockModel ) <EOL> back = ObjectPermissionBackend ( ) <EOL> self . assertRaises ( <EOL> NotBooleanPermission , back . has_perm , self . user , <EOL> '<STR_LIT>' , self . model ) </s>
