Skip to content

Commit

Permalink
[change] Add more tests to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jan 2, 2021
1 parent c000861 commit a4fb43f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 5 additions & 2 deletions openwisp_ipam/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

Organization = swapper.load_model('openwisp_users', 'Organization')


class FilterByOrganization:
def get_queryset(self):
qs = super().get_queryset()
Expand Down Expand Up @@ -64,11 +65,13 @@ def assert_organization_permissions(self, request):
try:
organization = self.get_csv_organization()
if str(organization.pk) in self.get_user_organizations():
return
return
except Organization.DoesNotExist:
# if organization in CSV doesn't exist, then check if
# user can create new organizations
permission = Permission.objects.filter(user=request.user).filter(codename='add_organization')
permission = Permission.objects.filter(user=request.user).filter(
codename='add_organization'
)
if permission.exists():
return
raise PermissionDenied()
Expand Down
28 changes: 27 additions & 1 deletion openwisp_ipam/tests/test_multitenant.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth.models import Permission
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.urls import reverse
from openwisp_users.tests.utils import TestMultitenantAdminMixin
from swapper import load_model

from openwisp_ipam.api.utils import (
AuthorizeCSVImport,
FilterByOrganization,
FilterByParent,
FilterSerializerByOrganization,
)

from . import CreateModelsMixin, PostDataMixin

User = get_user_model()
Expand Down Expand Up @@ -422,3 +429,22 @@ def test_browsable_api_ipaddress_list(self):
)
self.assertContains(response, '10.0.0.0/24</option>')
self.assertContains(response, '10.10.0.0/24</option>')

def test_not_implemented_error(self):
with self.assertRaises(NotImplementedError):
FilterByOrganization.get_organization_queryset(self)

with self.assertRaises(NotImplementedError):
FilterByParent.get_parent_queryset(self)

with self.assertRaises(NotImplementedError):
FilterByParent.get_organization_queryset(self)

with self.assertRaises(NotImplementedError):
AuthorizeCSVImport.get_csv_organization(self)

with self.assertRaises(NotImplementedError):
AuthorizeCSVImport.get_user_organizations(self)

with self.assertRaises(NotImplementedError):
FilterSerializerByOrganization.filter_fields(self)

0 comments on commit a4fb43f

Please sign in to comment.