|
| 1 | +import unittest |
| 2 | +import unittest.mock as mock |
| 3 | +from utilities.testing.base import TestCase |
| 4 | + |
| 5 | +from django.urls import reverse |
| 6 | +from django.test import override_settings |
| 7 | +from rest_framework import status |
| 8 | + |
| 9 | +from ipam.models import VLANGroup, VLAN, Prefix, IPAddress |
| 10 | +from netbox_scripthelper.api.views import filter_results |
| 11 | + |
| 12 | + |
| 13 | +class TestFilterResults(unittest.TestCase): |
| 14 | + |
| 15 | + def create_request(self, **kwargs): |
| 16 | + mm = mock.MagicMock() |
| 17 | + mm.query_params = kwargs |
| 18 | + return mm |
| 19 | + |
| 20 | + def test(self): |
| 21 | + |
| 22 | + cases = [ |
| 23 | + ("empty", self.create_request(), [], []), |
| 24 | + ("only limit", self.create_request(limit=2), [1, 2, 3, 4, 5], [1, 2]), |
| 25 | + ("only limit: all", self.create_request(limit=20), [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), |
| 26 | + ("only q: matched", self.create_request(q=2), [1, 2, 3, 20, 5], [2, 20]), |
| 27 | + ("only q: not matched", self.create_request(q=7), [1, 2, 3, 20, 5], []), |
| 28 | + ("q and limit", self.create_request(q=2, limit=2), [1, 2, 3, 20, 21], [2, 20]), |
| 29 | + ("q and limit: all", self.create_request(q=2, limit=10), [1, 2, 3, 20, 21], [2, 20, 21]), |
| 30 | + ] |
| 31 | + |
| 32 | + for case in cases: |
| 33 | + got = filter_results(case[1], case[2]) |
| 34 | + self.assertListEqual(got, case[-1], case[0]) |
| 35 | + |
| 36 | + |
| 37 | +class TestAvailablesVLANS(TestCase): |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def setUpTestData(cls): |
| 41 | + VLANGroup.objects.create(name='TestVG1', slug='testvg1', min_vid=100, max_vid=1000) |
| 42 | + vg2 = VLANGroup.objects.create(name='TestVG2', slug='testvg2', min_vid=10, max_vid=15) |
| 43 | + VLAN.objects.create(name='TestVlan2', vid=12, group=vg2) |
| 44 | + |
| 45 | + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], LOGIN_REQUIRED=False) |
| 46 | + def test(self): |
| 47 | + cases = [ |
| 48 | + ("no_limit_no_filter", "", 5), |
| 49 | + ("with_limit_no_filter", "?limit=10", 5), |
| 50 | + ("with_limit_with_filter_not_match", "?limit=10&q=2", 0), |
| 51 | + ("with_limit_with_filter_match", "?limit=2&q=1", 2), |
| 52 | + ("no_limit_with_filter", "?q=1", 5), |
| 53 | + ] |
| 54 | + vg = VLANGroup.objects.get(name='TestVG2') |
| 55 | + url = reverse('plugins-api:netbox_scripthelper-api:vlangroup-available-vlans', kwargs={'pk': vg.pk}) |
| 56 | + for case in cases: |
| 57 | + response = self.client.get(f'{url}{case[1]}') |
| 58 | + self.assertEqual(response.status_code, status.HTTP_200_OK, case[0]) |
| 59 | + self.assertEqual(len(response.data['results']), case[-1], case[0]) |
| 60 | + |
| 61 | + |
| 62 | +class TestAvailablesPrefixes(TestCase): |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def setUpTestData(cls): |
| 66 | + Prefix.objects.create(prefix='172.20.0.0/24') |
| 67 | + Prefix.objects.create(prefix='172.20.0.128/27') |
| 68 | + Prefix.objects.create(prefix='172.20.0.192/27') |
| 69 | + |
| 70 | + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], LOGIN_REQUIRED=False) |
| 71 | + def test_fixed_mask(self): |
| 72 | + cases = [ |
| 73 | + ("no_limit_no_filter", "?prefixlen=27", 6), |
| 74 | + ("with_limit_no_filter", "?prefixlen=27&limit=3", 3), |
| 75 | + ("with_limit_with_filter", "?prefixlen=27&limit=3&q=172.20.0.1", 1), |
| 76 | + ("no_limit_with_filter", "?prefixlen=27&q=172.20.0.1", 1), |
| 77 | + ] |
| 78 | + p = Prefix.objects.get(prefix='172.20.0.0/24') |
| 79 | + url = reverse('plugins-api:netbox_scripthelper-api:prefix-available-prefixes', kwargs={'pk': p.pk}) |
| 80 | + for case in cases: |
| 81 | + response = self.client.get(f'{url}{case[1]}') |
| 82 | + self.assertEqual(response.status_code, status.HTTP_200_OK, case[0]) |
| 83 | + self.assertEqual(len(response.data['results']), case[-1], case[0]) |
| 84 | + |
| 85 | + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], LOGIN_REQUIRED=False) |
| 86 | + def test(self): |
| 87 | + cases = [ |
| 88 | + ("no_limit_no_filter", "", 3), |
| 89 | + ("with_limit_no_filter", "?limit=2", 2), |
| 90 | + ("with_limit_with_filter", "?limit=3&q=172.20.0.1", 1), |
| 91 | + ("empty", "?limit=3&q=172.30.0.1", 0), |
| 92 | + ] |
| 93 | + p = Prefix.objects.get(prefix='172.20.0.0/24') |
| 94 | + url = reverse('plugins-api:netbox_scripthelper-api:prefix-available-prefixes', kwargs={'pk': p.pk}) |
| 95 | + for case in cases: |
| 96 | + response = self.client.get(f'{url}{case[1]}') |
| 97 | + self.assertEqual(response.status_code, status.HTTP_200_OK, case[0]) |
| 98 | + self.assertEqual(len(response.data['results']), case[-1], case[0]) |
| 99 | + |
| 100 | + |
| 101 | +class TestAvailablesIPAddresses(TestCase): |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def setUpTestData(cls): |
| 105 | + Prefix.objects.create(prefix='192.168.0.0/28') |
| 106 | + IPAddress.objects.bulk_create([ |
| 107 | + IPAddress(address='192.168.0.2/28'), |
| 108 | + IPAddress(address='192.168.0.5/28') |
| 109 | + ]) |
| 110 | + |
| 111 | + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], LOGIN_REQUIRED=False) |
| 112 | + def test(self): |
| 113 | + cases = [ |
| 114 | + ("no_limit_no_filter", "", 12), |
| 115 | + ("with_limit_no_filter", "?limit=5", 5), |
| 116 | + ("with_limit_with_filter", "?limit=3&q=192.168.0.1", 3), |
| 117 | + ("empty", "?limit=3&q=172.30.0.1", 0), |
| 118 | + ] |
| 119 | + p = Prefix.objects.get(prefix='192.168.0.0/28') |
| 120 | + url = reverse('plugins-api:netbox_scripthelper-api:prefix-available-ips', kwargs={'pk': p.pk}) |
| 121 | + for case in cases: |
| 122 | + response = self.client.get(f'{url}{case[1]}') |
| 123 | + self.assertEqual(response.status_code, status.HTTP_200_OK, case[0]) |
| 124 | + self.assertEqual(len(response.data['results']), case[-1], case[0]) |
0 commit comments