diff --git a/changelog.d/2989.fixed.md b/changelog.d/2989.fixed.md new file mode 100644 index 0000000000..be2d1a7100 --- /dev/null +++ b/changelog.d/2989.fixed.md @@ -0,0 +1 @@ +Fix IPAM API crash bug that caused unnecessary error reports sent as e-mail to site admins diff --git a/python/nav/web/ipam/prefix_tree.py b/python/nav/web/ipam/prefix_tree.py index b3d5e4c1d3..014ba843de 100644 --- a/python/nav/web/ipam/prefix_tree.py +++ b/python/nav/web/ipam/prefix_tree.py @@ -467,10 +467,10 @@ def make_tree(prefixes, family=None, root_ip=None, show_all=None, sort_by="ip"): init = [] if root_ip is not None and root_ip: - scope = Prefix.objects.get(net_address=root_ip) - if scope is not None: + try: + scope = Prefix.objects.get(net_address=root_ip) node = PrefixNode(scope) - else: + except Prefix.DoesNotExist: node = FauxNode(root_ip, "scope", "scope") init.append(node) diff --git a/tests/integration/web/ipam/api_test.py b/tests/integration/web/ipam/api_test.py new file mode 100644 index 0000000000..b398aacd41 --- /dev/null +++ b/tests/integration/web/ipam/api_test.py @@ -0,0 +1,12 @@ +class TestPrefixViewSet: + def test_when_prefix_address_is_unknown_it_should_not_crash(self, client): + response = client.get( + "/ipam/api/", + follow=True, + data={ + "net_type": "all", + "within": "192.168.42.0/24", + "show_all": "True", + }, + ) + assert response.status_code == 200