<s> """<STR_LIT>""" <EOL> import json <EOL> from django . test import TestCase <EOL> from django . http import HttpResponse , HttpResponseForbidden <EOL> from seed . lib . superperms . orgs . models import ( <EOL> ROLE_VIEWER , <EOL> ROLE_MEMBER , <EOL> ROLE_OWNER , <EOL> Organization , <EOL> OrganizationUser , <EOL> ) <EOL> from seed . landing . models import SEEDUser as User <EOL> from seed . lib . superperms . orgs import decorators <EOL> class FakeRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> __name__ = '<STR_LIT>' <EOL> method = '<STR_LIT:POST>' <EOL> META = { '<STR_LIT>' : '<STR_LIT:127.0.0.1>' } <EOL> path = '<STR_LIT>' <EOL> body = None <EOL> def __init__ ( self , headers = None ) : <EOL> if headers : <EOL> self . META . update ( headers ) <EOL> class FakeClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def _gen_req ( self , view_func , data , headers , method = '<STR_LIT:POST>' , ** kwargs ) : <EOL> request = FakeRequest ( headers ) <EOL> if '<STR_LIT:user>' in kwargs : <EOL> request . user = kwargs . get ( '<STR_LIT:user>' ) <EOL> if callable ( view_func ) : <EOL> setattr ( request , '<STR_LIT:GET>' , { } ) <EOL> setattr ( request , method , data ) <EOL> request . body = json . dumps ( data ) <EOL> return view_func ( request ) <EOL> return request <EOL> def get ( self , view_func , data , headers = None , ** kwargs ) : <EOL> return self . _gen_req ( view_func , data , headers , method = '<STR_LIT:GET>' , ** kwargs ) <EOL> def post ( self , view_func , data , headers = None , ** kwargs ) : <EOL> return self . _gen_req ( view_func , data , headers , ** kwargs ) <EOL> @ decorators . has_perm ( '<STR_LIT>' ) <EOL> def _fake_view_no_perm_name ( request ) : <EOL> return HttpResponse ( ) <EOL> @ decorators . has_perm ( '<STR_LIT>' ) <EOL> def _fake_invite_user ( request ) : <EOL> return HttpResponse ( ) <EOL> class TestDecorators ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestDecorators , self ) . setUp ( ) <EOL> self . client = FakeClient ( ) <EOL> self . fake_org = Organization . objects . create ( name = '<STR_LIT>' ) <EOL> self . fake_member = User . objects . create ( <EOL> username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' <EOL> ) <EOL> self . fake_superuser = User . objects . create_superuser ( <EOL> username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> email = '<STR_LIT>' <EOL> ) <EOL> self . fake_owner = User . objects . create ( <EOL> username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' <EOL> ) <EOL> self . fake_viewer = User . objects . create ( <EOL> username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' <EOL> ) <EOL> self . owner_org_user = OrganizationUser . objects . create ( <EOL> user = self . fake_owner , <EOL> organization = self . fake_org , <EOL> role_level = ROLE_OWNER <EOL> ) <EOL> self . member_org_user = OrganizationUser . objects . create ( <EOL> user = self . fake_member , <EOL> organization = self . fake_org , <EOL> role_level = ROLE_MEMBER <EOL> ) <EOL> self . viewer_org_user = OrganizationUser . objects . create ( <EOL> user = self . fake_viewer , <EOL> organization = self . fake_org , <EOL> role_level = ROLE_VIEWER <EOL> ) <EOL> self . superuser_org_user = OrganizationUser . objects . create ( <EOL> user = self . fake_superuser , <EOL> organization = self . fake_org , <EOL> role_level = ROLE_VIEWER <EOL> ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> User . objects . all ( ) . delete ( ) <EOL> Organization . objects . all ( ) . delete ( ) <EOL> OrganizationUser . objects . all ( ) . delete ( ) <EOL> super ( TestDecorators , self ) . tearDown ( ) <EOL> def test_has_perm_w_no_org ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . user = User . objects . create ( username = '<STR_LIT:f>' , email = '<STR_LIT>' ) <EOL> resp = self . client . post ( <EOL> _fake_view_no_perm_name , <EOL> { '<STR_LIT>' : <NUM_LIT:0> } , <EOL> user = self . client . user <EOL> ) <EOL> error_msg = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . __class__ , HttpResponseForbidden ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , error_msg ) <EOL> def test_has_perm_user_not_in_org ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . user = User . objects . create ( username = '<STR_LIT:f>' , email = '<STR_LIT>' ) <EOL> resp = self . client . post ( <EOL> _fake_view_no_perm_name , <EOL> { '<STR_LIT>' : self . fake_org . pk } , <EOL> user = self . client . user <EOL> ) <EOL> error_msg = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , error_msg ) <EOL> def test_has_perm_w_no_perm_name ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . user = self . fake_member <EOL> resp = self . client . post ( <EOL> _fake_view_no_perm_name , <EOL> { '<STR_LIT>' : self . fake_org . pk } , <EOL> user = self . fake_member <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> def test_has_perm_w_super_user ( self ) : <EOL> """<STR_LIT>""" <EOL> super_user = User . objects . create ( username = '<STR_LIT>' ) <EOL> resp = self . client . post ( <EOL> _fake_invite_user , <EOL> { '<STR_LIT>' : self . fake_org . pk } , <EOL> user = self . fake_member <EOL> ) <EOL> error_msg = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , error_msg ) <EOL> super_user . is_superuser = True <EOL> super_user . save ( ) <EOL> self . client . user = super_user <EOL> resp = self . client . post ( <EOL> _fake_invite_user , <EOL> { '<STR_LIT>' : self . fake_org . pk } , <EOL> user = super_user <EOL> ) <EOL> self . assertEqual ( resp . __class__ , HttpResponse ) <EOL> def test_has_perm_good_case ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . user = self . fake_owner <EOL> resp = self . client . post ( <EOL> _fake_invite_user , <EOL> { '<STR_LIT>' : self . fake_org . pk } , <EOL> user = self . fake_owner <EOL> ) <EOL> self . assertEqual ( resp . __class__ , HttpResponse ) <EOL> def test_requires_parent_org_owner ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . requires_parent_org_owner ( <EOL> self . owner_org_user <EOL> ) ) <EOL> self . assertFalse ( decorators . requires_parent_org_owner ( <EOL> self . member_org_user <EOL> ) ) <EOL> baby_org = Organization . objects . create ( name = '<STR_LIT>' ) <EOL> baby_ou = OrganizationUser . objects . create ( <EOL> user = self . fake_viewer , organization = baby_org <EOL> ) <EOL> baby_org . parent_org = self . fake_org <EOL> baby_org . save ( ) <EOL> self . assertFalse ( decorators . requires_parent_org_owner ( baby_ou ) ) <EOL> def test_can_create_sub_org ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . can_create_sub_org ( self . owner_org_user ) ) <EOL> self . assertFalse ( decorators . can_create_sub_org ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . can_create_sub_org ( self . viewer_org_user ) ) <EOL> def test_can_remove_org ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . can_remove_org ( self . owner_org_user ) ) <EOL> self . assertFalse ( decorators . can_remove_org ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . can_remove_org ( self . viewer_org_user ) ) <EOL> def test_can_invite_member ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . can_invite_member ( self . owner_org_user ) ) <EOL> self . assertFalse ( decorators . can_invite_member ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . can_invite_member ( self . viewer_org_user ) ) <EOL> def test_can_remove_member ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . can_remove_member ( self . owner_org_user ) ) <EOL> self . assertFalse ( decorators . can_remove_member ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . can_remove_member ( self . viewer_org_user ) ) <EOL> def test_can_modify_query_thresh ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( <EOL> decorators . can_modify_query_thresh ( self . owner_org_user ) <EOL> ) <EOL> self . assertFalse ( decorators . can_modify_query_thresh ( <EOL> self . member_org_user <EOL> ) ) <EOL> self . assertFalse ( decorators . can_modify_query_thresh ( <EOL> self . viewer_org_user <EOL> ) ) <EOL> def test_can_view_sub_org_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( <EOL> decorators . can_view_sub_org_settings ( self . owner_org_user ) <EOL> ) <EOL> self . assertFalse ( <EOL> decorators . can_view_sub_org_settings ( self . member_org_user ) <EOL> ) <EOL> self . assertFalse ( <EOL> decorators . can_view_sub_org_settings ( self . viewer_org_user ) <EOL> ) <EOL> def test_can_view_sub_org_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( <EOL> decorators . can_view_sub_org_fields ( self . owner_org_user ) <EOL> ) <EOL> self . assertFalse ( <EOL> decorators . can_view_sub_org_fields ( self . member_org_user ) <EOL> ) <EOL> self . assertFalse ( <EOL> decorators . can_view_sub_org_fields ( self . viewer_org_user ) <EOL> ) <EOL> def test_requires_owner ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . requires_owner ( self . owner_org_user ) ) <EOL> self . assertFalse ( decorators . requires_owner ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . requires_owner ( self . viewer_org_user ) ) <EOL> def test_requires_owner_w_child_org_and_parent_owner ( self ) : <EOL> """<STR_LIT>""" <EOL> baby_org = Organization . objects . create ( name = '<STR_LIT>' ) <EOL> baby_org . parent_org = self . fake_org <EOL> baby_org . save ( ) <EOL> self . assertTrue ( decorators . requires_owner ( self . owner_org_user ) ) <EOL> def test_requires_member ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . requires_member ( self . member_org_user ) ) <EOL> self . assertTrue ( decorators . requires_member ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . requires_member ( self . viewer_org_user ) ) <EOL> def test_requires_viewer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( decorators . requires_viewer ( self . owner_org_user ) ) <EOL> self . assertTrue ( decorators . requires_viewer ( self . member_org_user ) ) <EOL> self . assertTrue ( decorators . requires_viewer ( self . viewer_org_user ) ) <EOL> def test_requires_superuser ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertFalse ( decorators . requires_superuser ( self . owner_org_user ) ) <EOL> self . assertFalse ( decorators . requires_superuser ( self . member_org_user ) ) <EOL> self . assertFalse ( decorators . requires_superuser ( self . viewer_org_user ) ) <EOL> self . assertTrue ( decorators . requires_superuser ( self . superuser_org_user ) ) </s>
