From e905a36fb2be1a2d4867d96fc7c44b83002d2fba Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 12 Jun 2020 11:33:23 -0400 Subject: [PATCH] Update tests for tag changes --- netbox/circuits/tests/test_views.py | 5 +- netbox/dcim/tests/test_views.py | 70 ++++++++++++++--------- netbox/ipam/tests/test_views.py | 12 ++-- netbox/tenancy/tests/test_views.py | 2 +- netbox/utilities/testing/utils.py | 9 ++- netbox/utilities/testing/views.py | 19 +++++- netbox/virtualization/tests/test_views.py | 10 ++-- 7 files changed, 84 insertions(+), 43 deletions(-) diff --git a/netbox/circuits/tests/test_views.py b/netbox/circuits/tests/test_views.py index 9cc7af6ae..38365521a 100644 --- a/netbox/circuits/tests/test_views.py +++ b/netbox/circuits/tests/test_views.py @@ -26,7 +26,7 @@ class ProviderTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'noc_contact': 'noc@example.com', 'admin_contact': 'admin@example.com', 'comments': 'Another provider', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -106,7 +106,7 @@ class CircuitTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'commit_rate': 1000, 'description': 'A new circuit', 'comments': 'Some comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -124,5 +124,4 @@ class CircuitTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'commit_rate': 2000, 'description': 'New description', 'comments': 'New comments', - } diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index e9201e8fa..49f6f67b0 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -94,7 +94,7 @@ class SiteTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'contact_phone': '123-555-9999', 'contact_email': 'hank@stricklandpropane.com', 'comments': 'Test site', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -202,7 +202,7 @@ class RackReservationTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'user': user3.pk, 'tenant': None, 'description': 'Rack reservation', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -268,7 +268,7 @@ class RackTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'outer_depth': 500, 'outer_unit': RackDimensionUnitChoices.UNIT_MILLIMETER, 'comments': 'Some comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -359,7 +359,7 @@ class DeviceTypeTestCase( 'is_full_depth': True, 'subdevice_role': '', # CharField 'comments': 'Some comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.bulk_edit_data = { @@ -967,7 +967,7 @@ class DeviceTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'vc_position': None, 'vc_priority': None, 'comments': 'A new device', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), 'local_context_data': None, } @@ -1001,12 +1001,14 @@ class ConsolePortTestCase(ViewTestCases.DeviceComponentViewTestCase): ConsolePort(device=device, name='Console Port 3'), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Console Port X', 'type': ConsolePortTypeChoices.TYPE_RJ45, 'description': 'A console port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1016,7 +1018,7 @@ class ConsolePortTestCase(ViewTestCases.DeviceComponentViewTestCase): 'label_pattern': 'Serial[3-5]', 'type': ConsolePortTypeChoices.TYPE_RJ45, 'description': 'A console port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1045,12 +1047,14 @@ class ConsoleServerPortTestCase(ViewTestCases.DeviceComponentViewTestCase): ConsoleServerPort(device=device, name='Console Server Port 3'), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Console Server Port X', 'type': ConsolePortTypeChoices.TYPE_RJ45, 'description': 'A console server port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1058,7 +1062,7 @@ class ConsoleServerPortTestCase(ViewTestCases.DeviceComponentViewTestCase): 'name_pattern': 'Console Server Port [4-6]', 'type': ConsolePortTypeChoices.TYPE_RJ45, 'description': 'A console server port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1087,6 +1091,8 @@ class PowerPortTestCase(ViewTestCases.DeviceComponentViewTestCase): PowerPort(device=device, name='Power Port 3'), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Power Port X', @@ -1094,7 +1100,7 @@ class PowerPortTestCase(ViewTestCases.DeviceComponentViewTestCase): 'maximum_draw': 100, 'allocated_draw': 50, 'description': 'A power port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1104,7 +1110,7 @@ class PowerPortTestCase(ViewTestCases.DeviceComponentViewTestCase): 'maximum_draw': 100, 'allocated_draw': 50, 'description': 'A power port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1141,6 +1147,8 @@ class PowerOutletTestCase(ViewTestCases.DeviceComponentViewTestCase): PowerOutlet(device=device, name='Power Outlet 3', power_port=powerports[0]), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Power Outlet X', @@ -1148,7 +1156,7 @@ class PowerOutletTestCase(ViewTestCases.DeviceComponentViewTestCase): 'power_port': powerports[1].pk, 'feed_leg': PowerOutletFeedLegChoices.FEED_LEG_B, 'description': 'A power outlet', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1158,7 +1166,7 @@ class PowerOutletTestCase(ViewTestCases.DeviceComponentViewTestCase): 'power_port': powerports[1].pk, 'feed_leg': PowerOutletFeedLegChoices.FEED_LEG_B, 'description': 'A power outlet', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1202,6 +1210,8 @@ class InterfaceTestCase( ) VLAN.objects.bulk_create(vlans) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'virtual_machine': None, @@ -1216,7 +1226,7 @@ class InterfaceTestCase( 'mode': InterfaceModeChoices.MODE_TAGGED, 'untagged_vlan': vlans[0].pk, 'tagged_vlans': [v.pk for v in vlans[1:4]], - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1232,7 +1242,7 @@ class InterfaceTestCase( 'mode': InterfaceModeChoices.MODE_TAGGED, 'untagged_vlan': vlans[0].pk, 'tagged_vlans': [v.pk for v in vlans[1:4]], - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1279,6 +1289,8 @@ class FrontPortTestCase(ViewTestCases.DeviceComponentViewTestCase): FrontPort(device=device, name='Front Port 3', rear_port=rearports[2]), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Front Port X', @@ -1286,7 +1298,7 @@ class FrontPortTestCase(ViewTestCases.DeviceComponentViewTestCase): 'rear_port': rearports[3].pk, 'rear_port_position': 1, 'description': 'New description', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1297,7 +1309,7 @@ class FrontPortTestCase(ViewTestCases.DeviceComponentViewTestCase): '{}:1'.format(rp.pk) for rp in rearports[3:6] ], 'description': 'New description', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1326,13 +1338,15 @@ class RearPortTestCase(ViewTestCases.DeviceComponentViewTestCase): RearPort(device=device, name='Rear Port 3'), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Rear Port X', 'type': PortTypeChoices.TYPE_8P8C, 'positions': 3, 'description': 'A rear port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1341,7 +1355,7 @@ class RearPortTestCase(ViewTestCases.DeviceComponentViewTestCase): 'type': PortTypeChoices.TYPE_8P8C, 'positions': 3, 'description': 'A rear port', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1373,18 +1387,20 @@ class DeviceBayTestCase(ViewTestCases.DeviceComponentViewTestCase): DeviceBay(device=device, name='Device Bay 3'), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'name': 'Device Bay X', 'description': 'A device bay', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { 'device': device.pk, 'name_pattern': 'Device Bay [4-6]', 'description': 'A device bay', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1413,6 +1429,8 @@ class InventoryItemTestCase(ViewTestCases.DeviceComponentViewTestCase): InventoryItem(device=device, name='Inventory Item 3'), ]) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'device': device.pk, 'manufacturer': manufacturer.pk, @@ -1423,7 +1441,7 @@ class InventoryItemTestCase(ViewTestCases.DeviceComponentViewTestCase): 'serial': '123ABC', 'asset_tag': 'ABC123', 'description': 'An inventory item', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -1435,7 +1453,7 @@ class InventoryItemTestCase(ViewTestCases.DeviceComponentViewTestCase): 'part_id': '123456', 'serial': '123ABC', 'description': 'An inventory item', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = { @@ -1513,7 +1531,7 @@ class CableTestCase( 'color': 'c0c0c0', 'length': 100, 'length_unit': CableLengthUnitChoices.UNIT_FOOT, - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -1626,7 +1644,7 @@ class PowerPanelTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'site': sites[1].pk, 'rack_group': rackgroups[1].pk, 'name': 'Power Panel X', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -1680,7 +1698,7 @@ class PowerFeedTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'amperage': 100, 'max_utilization': 50, 'comments': 'New comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), # Connection 'cable': None, diff --git a/netbox/ipam/tests/test_views.py b/netbox/ipam/tests/test_views.py index d4d5a857c..b704664c1 100644 --- a/netbox/ipam/tests/test_views.py +++ b/netbox/ipam/tests/test_views.py @@ -33,7 +33,7 @@ class VRFTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'tenant': tenants[0].pk, 'enforce_unique': True, 'description': 'A new VRF', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -100,7 +100,7 @@ class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'rir': rirs[1].pk, 'date_added': datetime.date(2020, 1, 1), 'description': 'A new aggregate', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -183,7 +183,7 @@ class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'role': roles[1].pk, 'is_pool': True, 'description': 'A new prefix', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -232,7 +232,7 @@ class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'nat_inside': None, 'dns_name': 'example', 'description': 'A new IP address', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -320,7 +320,7 @@ class VLANTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'status': VLANStatusChoices.STATUS_RESERVED, 'role': roles[1].pk, 'description': 'A new VLAN', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -376,7 +376,7 @@ class ServiceTestCase( 'port': 999, 'ipaddresses': [], 'description': 'A new service', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( diff --git a/netbox/tenancy/tests/test_views.py b/netbox/tenancy/tests/test_views.py index ca2c2633f..4e00b648c 100644 --- a/netbox/tenancy/tests/test_views.py +++ b/netbox/tenancy/tests/test_views.py @@ -55,7 +55,7 @@ class TenantTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'group': tenant_groups[1].pk, 'description': 'A new tenant', 'comments': 'Some comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( diff --git a/netbox/utilities/testing/utils.py b/netbox/utilities/testing/utils.py index fd8c70f05..d763012f0 100644 --- a/netbox/utilities/testing/utils.py +++ b/netbox/utilities/testing/utils.py @@ -14,7 +14,14 @@ def post_data(data): if value is None: ret[key] = '' elif type(value) in (list, tuple): - ret[key] = value + if value and hasattr(value[0], 'pk'): + # Value is a list of instances + ret[key] = [v.pk for v in value] + else: + ret[key] = value + elif hasattr(value, 'pk'): + # Value is an instance + ret[key] = value.pk else: ret[key] = str(value) diff --git a/netbox/utilities/testing/views.py b/netbox/utilities/testing/views.py index 62a982eba..7c9631686 100644 --- a/netbox/utilities/testing/views.py +++ b/netbox/utilities/testing/views.py @@ -5,8 +5,10 @@ from django.db.models import ForeignKey, ManyToManyField from django.forms.models import model_to_dict from django.test import Client, TestCase as _TestCase, override_settings from django.urls import reverse, NoReverseMatch +from django.utils.text import slugify from netaddr import IPNetwork +from extras.models import Tag from users.models import ObjectPermission from utilities.permissions import resolve_permission_ct from .utils import disable_warnings, post_data @@ -48,7 +50,7 @@ class TestCase(_TestCase): obj_perm.object_types.add(ct) # - # Convenience methods + # Custom assertions # def assertHttpStatus(self, response, expected_status): @@ -75,7 +77,7 @@ class TestCase(_TestCase): # TODO: Differentiate between tags assigned to the instance and a M2M field for tags (ex: ConfigContext) if key == 'tags': - model_dict[key] = ','.join(sorted([tag.name for tag in value])) + model_dict[key] = sorted(value) # Convert ManyToManyField to list of instance PKs elif model_dict[key] and type(value) in (list, tuple) and hasattr(value[0], 'pk'): @@ -102,6 +104,19 @@ class TestCase(_TestCase): self.assertDictEqual(model_dict, relevant_data) + # + # Convenience methods + # + + @classmethod + def create_tags(cls, *names): + """ + Create and return a Tag instance for each name given. + """ + tags = [Tag(name=name, slug=slugify(name)) for name in names] + Tag.objects.bulk_create(tags) + return tags + # # UI Tests diff --git a/netbox/virtualization/tests/test_views.py b/netbox/virtualization/tests/test_views.py index b6f5be8b2..0ccf8a9b1 100644 --- a/netbox/virtualization/tests/test_views.py +++ b/netbox/virtualization/tests/test_views.py @@ -97,7 +97,7 @@ class ClusterTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'tenant': None, 'site': sites[1].pk, 'comments': 'Some comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), } cls.csv_data = ( @@ -161,7 +161,7 @@ class VirtualMachineTestCase(ViewTestCases.PrimaryObjectViewTestCase): 'memory': 32768, 'disk': 4000, 'comments': 'Some comments', - 'tags': 'Alpha,Bravo,Charlie', + 'tags': cls.create_tags('Alpha', 'Bravo', 'Charlie'), 'local_context_data': None, } @@ -228,6 +228,8 @@ class InterfaceTestCase( ) VLAN.objects.bulk_create(vlans) + tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') + cls.form_data = { 'virtual_machine': virtualmachines[1].pk, 'name': 'Interface X', @@ -240,7 +242,7 @@ class InterfaceTestCase( 'mode': InterfaceModeChoices.MODE_TAGGED, 'untagged_vlan': vlans[0].pk, 'tagged_vlans': [v.pk for v in vlans[1:4]], - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_create_data = { @@ -255,7 +257,7 @@ class InterfaceTestCase( 'mode': InterfaceModeChoices.MODE_TAGGED, 'untagged_vlan': vlans[0].pk, 'tagged_vlans': [v.pk for v in vlans[1:4]], - 'tags': 'Alpha,Bravo,Charlie', + 'tags': tags, } cls.bulk_edit_data = {