<s> from django . contrib . auth . models import User <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . http import HttpResponse <EOL> from django . test import TestCase <EOL> from django . test . client import RequestFactory <EOL> from django . test . utils import override_settings <EOL> from organizations . models import Organization , OrganizationUser <EOL> from organizations . mixins import ( OrganizationMixin , OrganizationUserMixin , <EOL> MembershipRequiredMixin , AdminRequiredMixin , OwnerRequiredMixin ) <EOL> from . utils import request_factory_login <EOL> class ViewStub ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . kwargs = kwargs <EOL> def get_context_data ( self , ** kwargs ) : <EOL> return kwargs <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> class OrgView ( OrganizationMixin , ViewStub ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UserView ( OrganizationUserMixin , ViewStub ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ override_settings ( USE_TZ = True ) <EOL> class ObjectMixinTests ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . foo = Organization . objects . get ( name = '<STR_LIT>' ) <EOL> self . dave = OrganizationUser . objects . get ( user__username = '<STR_LIT>' , <EOL> organization = self . foo ) <EOL> def test_get_org_object ( self ) : <EOL> view = OrgView ( organization_pk = self . foo . pk ) <EOL> self . assertEqual ( view . get_object ( ) , self . foo ) <EOL> def test_get_user_object ( self ) : <EOL> view = UserView ( organization_pk = self . foo . pk , user_pk = self . dave . pk ) <EOL> self . assertEqual ( view . get_object ( ) , self . dave ) <EOL> self . assertEqual ( view . get_organization ( ) , self . foo ) <EOL> def test_get_model ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( Organization , OrganizationMixin ( ) . get_org_model ( ) ) <EOL> self . assertEqual ( Organization , OrganizationUserMixin ( ) . get_org_model ( ) ) <EOL> self . assertEqual ( OrganizationUser , <EOL> OrganizationUserMixin ( ) . get_user_model ( ) ) <EOL> @ override_settings ( USE_TZ = True ) <EOL> class AccessMixinTests ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . nirvana = Organization . objects . get ( name = "<STR_LIT>" ) <EOL> self . kurt = User . objects . get ( username = "<STR_LIT>" ) <EOL> self . krist = User . objects . get ( username = "<STR_LIT>" ) <EOL> self . dave = User . objects . get ( username = "<STR_LIT>" ) <EOL> self . dummy = User . objects . create_user ( "<STR_LIT>" , <EOL> email = "<STR_LIT>" , password = "<STR_LIT:test>" ) <EOL> self . factory = RequestFactory ( ) <EOL> self . kurt_request = request_factory_login ( self . factory , self . kurt ) <EOL> self . krist_request = request_factory_login ( self . factory , self . krist ) <EOL> self . dave_request = request_factory_login ( self . factory , self . dave ) <EOL> self . dummy_request = request_factory_login ( self . factory , self . dummy ) <EOL> def test_member_access ( self ) : <EOL> class MemberView ( MembershipRequiredMixin , OrgView ) : <EOL> pass <EOL> self . assertEqual ( <NUM_LIT:200> , MemberView ( ) . dispatch ( self . kurt_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> self . assertEqual ( <NUM_LIT:200> , MemberView ( ) . dispatch ( self . krist_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> self . assertEqual ( <NUM_LIT:200> , MemberView ( ) . dispatch ( self . dave_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> with self . assertRaises ( PermissionDenied ) : <EOL> MemberView ( ) . dispatch ( self . dummy_request , <EOL> organization_pk = self . nirvana . pk ) <EOL> def test_admin_access ( self ) : <EOL> class AdminView ( AdminRequiredMixin , OrgView ) : <EOL> pass <EOL> self . assertEqual ( <NUM_LIT:200> , AdminView ( ) . dispatch ( self . kurt_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> self . assertEqual ( <NUM_LIT:200> , AdminView ( ) . dispatch ( self . krist_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> self . assertEqual ( <NUM_LIT:200> , AdminView ( ) . dispatch ( self . dave_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> with self . assertRaises ( PermissionDenied ) : <EOL> AdminView ( ) . dispatch ( self . dummy_request , <EOL> organization_pk = self . nirvana . pk ) <EOL> def test_owner_access ( self ) : <EOL> class OwnerView ( OwnerRequiredMixin , OrgView ) : <EOL> pass <EOL> self . assertEqual ( <NUM_LIT:200> , OwnerView ( ) . dispatch ( self . kurt_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> with self . assertRaises ( PermissionDenied ) : <EOL> OwnerView ( ) . dispatch ( self . krist_request , <EOL> organization_pk = self . nirvana . pk ) <EOL> self . assertEqual ( <NUM_LIT:200> , OwnerView ( ) . dispatch ( self . dave_request , <EOL> organization_pk = self . nirvana . pk ) . status_code ) <EOL> with self . assertRaises ( PermissionDenied ) : <EOL> OwnerView ( ) . dispatch ( self . dummy_request , <EOL> organization_pk = self . nirvana . pk ) </s>
