<s> from django . test import TestCase <EOL> from django . contrib . auth import get_user_model <EOL> from model_mommy import mommy <EOL> from rolepermissions . exceptions import RoleDoesNotExist <EOL> from rolepermissions . roles import RolesManager , AbstractUserRole <EOL> from rolepermissions . shortcuts import ( <EOL> get_user_role , grant_permission , <EOL> revoke_permission , retrieve_role , <EOL> available_perm_status <EOL> ) <EOL> from rolepermissions . verifications import has_permission <EOL> class ShoRole1 ( AbstractUserRole ) : <EOL> available_permissions = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> class ShoRole2 ( AbstractUserRole ) : <EOL> available_permissions = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> class ShoRole3 ( AbstractUserRole ) : <EOL> role_name = '<STR_LIT>' <EOL> available_permissions = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> class AvailablePermStatusTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = mommy . make ( get_user_model ( ) ) <EOL> self . user_role = ShoRole2 . assign_role_to_user ( self . user ) <EOL> def test_permission_hash ( self ) : <EOL> perm_hash = available_perm_status ( self . user ) <EOL> self . assertTrue ( perm_hash [ '<STR_LIT>' ] ) <EOL> self . assertFalse ( perm_hash [ '<STR_LIT>' ] ) <EOL> def test_permission_hash_after_modification ( self ) : <EOL> revoke_permission ( self . user , '<STR_LIT>' ) <EOL> perm_hash = available_perm_status ( self . user ) <EOL> self . assertFalse ( perm_hash [ '<STR_LIT>' ] ) <EOL> self . assertFalse ( perm_hash [ '<STR_LIT>' ] ) <EOL> class GetUserRoleTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = mommy . make ( get_user_model ( ) ) <EOL> def test_get_user_role ( self ) : <EOL> user = self . user <EOL> user_role = ShoRole1 . assign_role_to_user ( user ) <EOL> self . assertEquals ( get_user_role ( user ) , ShoRole1 ) <EOL> def test_get_user_role_after_role_change ( self ) : <EOL> user = self . user <EOL> user_role = ShoRole1 . assign_role_to_user ( user ) <EOL> user_role = ShoRole3 . assign_role_to_user ( user ) <EOL> self . assertEquals ( get_user_role ( user ) , ShoRole3 ) <EOL> def test_user_without_role ( self ) : <EOL> user = self . user <EOL> self . assertIsNone ( get_user_role ( user ) ) <EOL> def tearDown ( self ) : <EOL> RolesManager . _roles = { } <EOL> class GrantPermissionTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = mommy . make ( get_user_model ( ) ) <EOL> self . user_role = ShoRole2 . assign_role_to_user ( self . user ) <EOL> def test_grant_permission ( self ) : <EOL> user = self . user <EOL> self . assertTrue ( grant_permission ( user , '<STR_LIT>' ) ) <EOL> self . assertTrue ( has_permission ( user , '<STR_LIT>' ) ) <EOL> def test_grat_granted_permission ( self ) : <EOL> user = self . user <EOL> self . assertTrue ( grant_permission ( user , '<STR_LIT>' ) ) <EOL> self . assertTrue ( has_permission ( user , '<STR_LIT>' ) ) <EOL> def test_not_allowed_permission ( self ) : <EOL> user = self . user <EOL> self . assertFalse ( grant_permission ( user , '<STR_LIT>' ) ) <EOL> class RevokePermissionTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = mommy . make ( get_user_model ( ) ) <EOL> self . user_role = ShoRole2 . assign_role_to_user ( self . user ) <EOL> def test_revoke_permission ( self ) : <EOL> user = self . user <EOL> self . assertTrue ( revoke_permission ( user , '<STR_LIT>' ) ) <EOL> self . assertFalse ( has_permission ( user , '<STR_LIT>' ) ) <EOL> def test_revoke_revoked_permission ( self ) : <EOL> user = self . user <EOL> self . assertTrue ( revoke_permission ( user , '<STR_LIT>' ) ) <EOL> self . assertFalse ( has_permission ( user , '<STR_LIT>' ) ) <EOL> def test_not_allowed_permission ( self ) : <EOL> user = self . user <EOL> self . assertFalse ( revoke_permission ( user , '<STR_LIT>' ) ) <EOL> class RetrieveRole ( TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def test_retrive_role1 ( self ) : <EOL> self . assertEquals ( retrieve_role ( '<STR_LIT>' ) , ShoRole1 ) <EOL> def test_retrive_role2 ( self ) : <EOL> self . assertEquals ( retrieve_role ( '<STR_LIT>' ) , ShoRole2 ) <EOL> def test_retrive_role3 ( self ) : <EOL> self . assertEquals ( retrieve_role ( '<STR_LIT>' ) , ShoRole3 ) <EOL> def test_retrieve_unknowun_role ( self ) : <EOL> role = retrieve_role ( '<STR_LIT>' ) <EOL> self . assertIsNone ( role ) </s>
