From a4fb43f6dea9f0df95d29b1a196b988fda56c87b Mon Sep 17 00:00:00 2001 From: purhan Date: Sat, 2 Jan 2021 15:35:18 +0530 Subject: [PATCH] [change] Add more tests to improve coverage --- openwisp_ipam/api/utils.py | 7 +++++-- openwisp_ipam/tests/test_multitenant.py | 28 ++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/openwisp_ipam/api/utils.py b/openwisp_ipam/api/utils.py index ed8f906..6042490 100644 --- a/openwisp_ipam/api/utils.py +++ b/openwisp_ipam/api/utils.py @@ -5,6 +5,7 @@ Organization = swapper.load_model('openwisp_users', 'Organization') + class FilterByOrganization: def get_queryset(self): qs = super().get_queryset() @@ -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() diff --git a/openwisp_ipam/tests/test_multitenant.py b/openwisp_ipam/tests/test_multitenant.py index cfbd727..59a560c 100644 --- a/openwisp_ipam/tests/test_multitenant.py +++ b/openwisp_ipam/tests/test_multitenant.py @@ -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() @@ -422,3 +429,22 @@ def test_browsable_api_ipaddress_list(self): ) self.assertContains(response, '10.0.0.0/24') self.assertContains(response, '10.10.0.0/24') + + 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)