-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creates correct permissions and tests them
- Loading branch information
Showing
3 changed files
with
68 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from rest_framework.permissions import DjangoModelPermissions | ||
|
||
|
||
class DjangoTenantModelPermissions(DjangoModelPermissions): | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if hasattr(obj, 'tenant'): | ||
kwargs = {'tenant': obj.tenant} | ||
elif hasattr(obj, 'tenants'): | ||
kwargs = {'tenant__in': obj.tenants.all()} | ||
else: | ||
return False | ||
|
||
return request.user.relationships.filter(**kwargs).exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import mock | ||
from django.test import RequestFactory | ||
from model_mommy import mommy | ||
|
||
from tests.utils import SharedSchemaTenantsTestCase | ||
from shared_schema_tenants.permissions import DjangoTenantModelPermissions | ||
|
||
try: | ||
from django.urls import reverse | ||
except ImportError: | ||
from django.core.urlresolvers import reverse | ||
|
||
|
||
class DjangoTenantModelPermissionsTests(SharedSchemaTenantsTestCase): | ||
|
||
def setUp(self): | ||
super(DjangoTenantModelPermissionsTests, self).setUp() | ||
factory = RequestFactory() | ||
self.request = factory.post(reverse('shared_schema_tenants:tenant_list')) | ||
self.request.user = self.user | ||
self.permission = DjangoTenantModelPermissions() | ||
|
||
def test_has_object_permission_with_created_tenant_single_tenant_object(self): | ||
obj = mock.Mock(spec=['tenant']) | ||
obj.tenant = self.tenant | ||
self.assertTrue(self.permission.has_object_permission(self.request, None, obj)) | ||
|
||
def test_has_object_permission_with_created_tenant_multi_tenant_object(self): | ||
obj = mock.Mock(spec=['tenants']) | ||
obj.tenants = mock.Mock(spec=['all']) | ||
obj.tenants.all = lambda: [self.tenant] | ||
self.assertTrue(self.permission.has_object_permission(self.request, None, obj)) | ||
|
||
def test_has_object_permission_with_new_tenant_single_tenant_object(self): | ||
obj = mock.Mock(spec=['tenant']) | ||
obj.tenant = mommy.make('shared_schema_tenants.Tenant') | ||
self.assertFalse(self.permission.has_object_permission(self.request, None, obj)) | ||
|
||
def test_has_object_permission_with_new_tenant_multi_tenant_object(self): | ||
obj = mock.Mock(spec=['tenants']) | ||
obj.tenants = mock.Mock(spec=['all']) | ||
obj.tenants.all = lambda: [mommy.make('shared_schema_tenants.Tenant')] | ||
self.assertFalse(self.permission.has_object_permission(self.request, None, obj)) | ||
|
||
def test_has_object_permission_without_tenant_attributes(self): | ||
obj = mock.Mock(spec=['test_not_tenant_attribute']) | ||
self.assertFalse(self.permission.has_object_permission(self.request, None, obj)) |