Skip to content

Commit

Permalink
[bug] Fix exception in api openwisp#71
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Nov 11, 2020
1 parent a4f3c93 commit b6dbf25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion openwisp_ipam/api/views.py
Expand Up @@ -133,7 +133,7 @@ def __len__(self):
def index_of(self, address):
index = int(self.subnet.subnet._address_class(address)) - self.network - 1
if index < 0 or index >= self.count(): # pragma: no cover
raise IndexError
raise serializers.ValidationError({'detail': 'Invalid Address'})
return index


Expand Down
18 changes: 18 additions & 0 deletions openwisp_ipam/tests/test_api.py
Expand Up @@ -5,6 +5,7 @@
from django.test import TestCase
from django.urls import reverse
from openwisp_users.tests.utils import TestMultitenantAdminMixin
from rest_framework.serializers import ValidationError
from swapper import load_model

from . import CreateModelsMixin, PostDataMixin
Expand Down Expand Up @@ -45,6 +46,23 @@ def test_unavailable_ip(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, None)

def test_ipv4_invalid_host(self):
subnet = self._create_subnet(subnet='10.0.0.0/16')
response = self.client.get(
reverse('ipam:hosts', args=(subnet.id,)), {'start': '10.255.0.0'}
)
self.assertEqual(str(response.data['detail']), 'Invalid Address')
self.assertEqual(response.status_code, 400)

def test_ipv6_invalid_host(self):
subnet = self._create_subnet(subnet='fdb6:21b:a477::/64')
response = self.client.get(
reverse('ipam:hosts', args=(subnet.id,)),
{'start': 'fdb6:21b:a477:1:fff::fff'},
)
self.assertEqual(str(response.data['detail']), 'Invalid Address')
self.assertEqual(response.status_code, 400)

def test_ipv4_request_api(self):
subnet = self._create_subnet(subnet='10.0.0.0/24')
self._create_ipaddress(ip_address='10.0.0.1', subnet=subnet)
Expand Down

0 comments on commit b6dbf25

Please sign in to comment.