<s> from __future__ import with_statement <EOL> from django . contrib . auth . models import AnonymousUser , User <EOL> from django . core import cache <EOL> from django . test . testcases import TestCase <EOL> from rulez . exceptions import RulesException <EOL> from rulez . rolez . base import AbstractRole <EOL> from rulez . rolez . cache_helper import get_counter , increment_counter , get_roles , get_user_pk , roles_key <EOL> from rulez . rolez . models import ModelRoleMixin <EOL> class Mock ( ) : <EOL> pk = <NUM_LIT> <EOL> class MockUser ( ) : <EOL> def __init__ ( self ) : <EOL> self . pk = <NUM_LIT> <EOL> def is_anonymous ( self ) : <EOL> return False <EOL> class Tester ( AbstractRole ) : <EOL> @ classmethod <EOL> def is_member ( cls , user , obj ) : <EOL> return getattr ( user , '<STR_LIT>' , False ) <EOL> class TestModel ( ModelRoleMixin ) : <EOL> pk = <NUM_LIT:1> <EOL> roles = [ Tester ] <EOL> class RolesCacheHelperTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . cache . clear ( ) <EOL> def test_incrementing_counter_works ( self ) : <EOL> obj = Mock ( ) <EOL> first = get_counter ( obj ) <EOL> self . assertEqual ( first , <NUM_LIT:0> ) <EOL> increment_counter ( obj ) <EOL> second = get_counter ( obj ) <EOL> self . assertNotEqual ( second , first ) <EOL> def test_incrementing_counter_works_for_none ( self ) : <EOL> increment_counter ( None ) <EOL> def test_get_roles_for_None_raises ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> res = get_counter ( None ) <EOL> self . assertEqual ( res , None ) <EOL> def test_rulez_invalidate_works ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> first = get_counter ( model ) <EOL> self . assertEqual ( first , <NUM_LIT:0> ) <EOL> model . rulez_invalidate ( ) <EOL> second = get_counter ( model ) <EOL> self . assertNotEqual ( second , first ) <EOL> def test_get_empty_roles_works ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> res = get_roles ( user , model ) <EOL> self . assertEqual ( res , [ ] ) <EOL> def test_user_with_role_works ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> setattr ( user , '<STR_LIT>' , True ) <EOL> res = get_roles ( user , model ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) <EOL> def test_get_roles_cache_works ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> setattr ( user , '<STR_LIT>' , True ) <EOL> res = get_roles ( user , model ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) <EOL> res2 = get_roles ( user , model ) <EOL> self . assertEqual ( len ( res2 ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( res , res2 ) <EOL> def test_has_role_works ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> setattr ( user , '<STR_LIT>' , True ) <EOL> res = model . has_role ( user , Tester ) <EOL> self . assertEqual ( res , True ) <EOL> def test_has_role_caches_on_instance ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> setattr ( user , '<STR_LIT>' , True ) <EOL> self . assertFalse ( hasattr ( model , "<STR_LIT>" ) ) <EOL> res = model . has_role ( user , Tester ) <EOL> self . assertEqual ( res , True ) <EOL> self . assertTrue ( hasattr ( model , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( model . _rolez ) ) <EOL> res = model . has_role ( user , Tester ) <EOL> self . assertEqual ( res , True ) <EOL> self . assertTrue ( hasattr ( model , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( model . _rolez ) ) <EOL> def test_doesnt_have_role_works ( self ) : <EOL> model = TestModel ( ) <EOL> user = MockUser ( ) <EOL> res = model . has_role ( user , Tester ) <EOL> self . assertEqual ( res , False ) <EOL> def test_get_anonymous_user_works ( self ) : <EOL> anon = AnonymousUser ( ) <EOL> res = get_user_pk ( anon ) <EOL> self . assertEqual ( res , '<STR_LIT>' ) <EOL> def test_get_roles_works_for_anonymous ( self ) : <EOL> model = TestModel ( ) <EOL> user = AnonymousUser ( ) <EOL> res = model . has_role ( user , Tester ) <EOL> self . assertEqual ( res , False ) <EOL> def test_get_counter_does_not_return_spaces ( self ) : <EOL> obj = Mock ( ) <EOL> user = MockUser ( ) <EOL> roles_key ( user , obj ) <EOL> increment_counter ( obj ) <EOL> res = roles_key ( user , obj ) <EOL> self . assertTrue ( '<STR_LIT:U+0020>' not in res ) <EOL> def test_roles_for_users_on_users_raises_without_relevant_roles ( self ) : <EOL> django_user = User . objects . create ( username = "<STR_LIT:test>" , <EOL> email = "<STR_LIT>" , <EOL> first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> user = MockUser ( ) <EOL> setattr ( user , '<STR_LIT>' , True ) <EOL> with self . assertRaises ( RulesException ) : <EOL> res = get_roles ( user , django_user ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) <EOL> def test_roles_for_users_on_users_works_with_relevant_roles ( self ) : <EOL> django_user = User . objects . create ( username = "<STR_LIT:test>" , <EOL> email = "<STR_LIT>" , <EOL> first_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" ) <EOL> setattr ( django_user , '<STR_LIT>' , lambda : [ Tester ] ) <EOL> user = MockUser ( ) <EOL> setattr ( user , '<STR_LIT>' , True ) <EOL> res = get_roles ( user , django_user ) <EOL> self . assertEqual ( len ( res ) , <NUM_LIT:1> ) </s>
