From c858d2a17697c95fa9ea5095a8415a3e845061ab Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 23 Oct 2025 09:47:29 -0400 Subject: [PATCH] Add tests for Owner & OwnerGroup --- netbox/users/graphql/types.py | 2 +- netbox/users/tests/test_api.py | 111 +++++++++++++++++++++++++- netbox/users/tests/test_filtersets.py | 105 +++++++++++++++++++++++- netbox/users/tests/test_views.py | 105 ++++++++++++++++++++++++ netbox/utilities/testing/views.py | 15 ++++ 5 files changed, 335 insertions(+), 3 deletions(-) diff --git a/netbox/users/graphql/types.py b/netbox/users/graphql/types.py index 53c1a5e11..e04fc8668 100644 --- a/netbox/users/graphql/types.py +++ b/netbox/users/graphql/types.py @@ -53,4 +53,4 @@ class OwnerGroupType(BaseObjectType): pagination=True ) class OwnerType(BaseObjectType): - group: OwnerGroupType + group: OwnerGroupType | None diff --git a/netbox/users/tests/test_api.py b/netbox/users/tests/test_api.py index 597ce77de..0e1ccebf8 100644 --- a/netbox/users/tests/test_api.py +++ b/netbox/users/tests/test_api.py @@ -3,7 +3,7 @@ from django.urls import reverse from core.models import ObjectType from users.constants import TOKEN_DEFAULT_LENGTH -from users.models import Group, ObjectPermission, Token, User +from users.models import Group, ObjectPermission, Owner, OwnerGroup, Token, User from utilities.data import deepmerge from utilities.testing import APIViewTestCases, APITestCase, create_test_user @@ -448,3 +448,112 @@ class UserConfigTest(APITestCase): self.assertDictEqual(response.data, new_data) userconfig.refresh_from_db() self.assertDictEqual(userconfig.data, new_data) + + +class OwnerGroupTest(APIViewTestCases.APIViewTestCase): + model = OwnerGroup + brief_fields = ['description', 'display', 'id', 'name', 'url'] + bulk_update_data = { + 'description': 'New description', + } + + @classmethod + def setUpTestData(cls): + owner_groups = ( + OwnerGroup(name='Owner Group 1'), + OwnerGroup(name='Owner Group 2'), + OwnerGroup(name='Owner Group 3'), + ) + OwnerGroup.objects.bulk_create(owner_groups) + + cls.create_data = [ + { + 'name': 'Owner Group 4', + 'description': 'Fourth owner group', + }, + { + 'name': 'Owner Group 5', + 'description': 'Fifth owner group', + }, + { + 'name': 'Owner Group 6', + 'description': 'Sixth owner group', + }, + ] + + +class OwnerTest(APIViewTestCases.APIViewTestCase): + model = Owner + brief_fields = ['description', 'display', 'id', 'name', 'url'] + + @classmethod + def setUpTestData(cls): + owner_groups = ( + OwnerGroup(name='Owner Group 1'), + OwnerGroup(name='Owner Group 2'), + OwnerGroup(name='Owner Group 3'), + OwnerGroup(name='Owner Group 4'), + ) + OwnerGroup.objects.bulk_create(owner_groups) + + groups = ( + Group(name='Group 1'), + Group(name='Group 2'), + Group(name='Group 3'), + Group(name='Group 4'), + ) + Group.objects.bulk_create(groups) + + users = ( + User(username='User 1'), + User(username='User 2'), + User(username='User 3'), + User(username='User 4'), + ) + User.objects.bulk_create(users) + + owners = ( + Owner(name='Owner 1'), + Owner(name='Owner 2'), + Owner(name='Owner 3'), + ) + Owner.objects.bulk_create(owners) + + # Assign users and groups to owners + owners[0].user_groups.add(groups[0]) + owners[1].user_groups.add(groups[1]) + owners[2].user_groups.add(groups[2]) + owners[0].users.add(users[0]) + owners[1].users.add(users[1]) + owners[2].users.add(users[2]) + + cls.create_data = [ + { + 'name': 'Owner 4', + 'description': 'Fourth owner', + 'group': owner_groups[3].pk, + 'user_groups': [groups[3].pk], + 'users': [users[3].pk], + }, + { + 'name': 'Owner 5', + 'description': 'Fifth owner', + 'group': owner_groups[3].pk, + 'user_groups': [groups[3].pk], + 'users': [users[3].pk], + }, + { + 'name': 'Owner 6', + 'description': 'Sixth owner', + 'group': owner_groups[3].pk, + 'user_groups': [groups[3].pk], + 'users': [users[3].pk], + }, + ] + + cls.bulk_update_data = { + 'group': owner_groups[3].pk, + 'user_groups': [groups[3].pk], + 'users': [users[3].pk], + 'description': 'New description', + } diff --git a/netbox/users/tests/test_filtersets.py b/netbox/users/tests/test_filtersets.py index 1f7336cc3..745b00126 100644 --- a/netbox/users/tests/test_filtersets.py +++ b/netbox/users/tests/test_filtersets.py @@ -5,7 +5,7 @@ from django.utils.timezone import make_aware from core.models import ObjectType from users import filtersets -from users.models import Group, ObjectPermission, Token, User +from users.models import Group, ObjectPermission, Owner, OwnerGroup, Token, User from utilities.testing import BaseFilterSetTests @@ -348,3 +348,106 @@ class TokenTestCase(TestCase, BaseFilterSetTests): def test_description(self): params = {'description': ['foobar1', 'foobar2']} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + +class OwnerGroupTestCase(TestCase, BaseFilterSetTests): + queryset = OwnerGroup.objects.all() + filterset = filtersets.OwnerGroupFilterSet + + @classmethod + def setUpTestData(cls): + + owner_groups = ( + OwnerGroup(name='Owner Group 1', description='Foo'), + OwnerGroup(name='Owner Group 2', description='Bar'), + OwnerGroup(name='Owner Group 3', description='Baz'), + ) + OwnerGroup.objects.bulk_create(owner_groups) + + def test_q(self): + params = {'q': 'foo'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + + def test_name(self): + params = {'name': ['Owner Group 1', 'Owner Group 2']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_description(self): + params = {'description': ['Foo', 'Bar']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + +class OwnerTestCase(TestCase, BaseFilterSetTests): + queryset = Owner.objects.all() + filterset = filtersets.OwnerFilterSet + + @classmethod + def setUpTestData(cls): + owner_groups = ( + OwnerGroup(name='Owner Group 1'), + OwnerGroup(name='Owner Group 2'), + OwnerGroup(name='Owner Group 3'), + ) + OwnerGroup.objects.bulk_create(owner_groups) + + groups = ( + Group(name='Group 1'), + Group(name='Group 2'), + Group(name='Group 3'), + ) + Group.objects.bulk_create(groups) + + users = ( + User(username='User 1'), + User(username='User 2'), + User(username='User 3'), + ) + User.objects.bulk_create(users) + + owners = ( + Owner(name='Owner 1', group=owner_groups[0], description='Foo'), + Owner(name='Owner 2', group=owner_groups[1], description='Bar'), + Owner(name='Owner 3', group=owner_groups[2], description='Baz'), + ) + Owner.objects.bulk_create(owners) + + # Assign users and groups to owners + owners[0].user_groups.add(groups[0]) + owners[1].user_groups.add(groups[1]) + owners[2].user_groups.add(groups[2]) + owners[0].users.add(users[0]) + owners[1].users.add(users[1]) + owners[2].users.add(users[2]) + + def test_q(self): + params = {'q': 'foo'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + + def test_name(self): + params = {'name': ['Owner 1', 'Owner 2']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_description(self): + params = {'description': ['Foo', 'Bar']} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_group(self): + owner_groups = OwnerGroup.objects.order_by('id')[:2] + params = {'group_id': [owner_groups[0].pk, owner_groups[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'group': [owner_groups[0].name, owner_groups[1].name]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_user_group(self): + group = Group.objects.order_by('id')[:2] + params = {'user_group_id': [group[0].pk, group[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'user_group': [group[0].name, group[1].name]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_user(self): + users = User.objects.order_by('id')[:2] + params = {'user_id': [users[0].pk, users[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'user': [users[0].username, users[1].username]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) diff --git a/netbox/users/tests/test_views.py b/netbox/users/tests/test_views.py index 24aec6941..1980299fd 100644 --- a/netbox/users/tests/test_views.py +++ b/netbox/users/tests/test_views.py @@ -255,3 +255,108 @@ class TokenTestCase( cls.bulk_edit_data = { 'description': 'New description', } + + +class OwnerGroupTestCase(ViewTestCases.AdminModelViewTestCase): + model = OwnerGroup + + @classmethod + def setUpTestData(cls): + owner_groups = ( + OwnerGroup(name='Owner Group 1'), + OwnerGroup(name='Owner Group 2'), + OwnerGroup(name='Owner Group 3'), + ) + OwnerGroup.objects.bulk_create(owner_groups) + + cls.form_data = { + 'name': 'Owner Group X', + 'description': 'A new owner group', + } + + cls.csv_data = ( + "name,description", + "Owner Group 4,Foo", + "Owner Group 5,Bar", + "Owner Group 6,Baz", + ) + + cls.csv_update_data = ( + "id,description", + f"{owner_groups[0].pk},Foo", + f"{owner_groups[1].pk},Bar", + f"{owner_groups[2].pk},Baz", + ) + + cls.bulk_edit_data = { + 'description': 'New description', + } + + +class OwnerTestCase(ViewTestCases.AdminModelViewTestCase): + model = Owner + + @classmethod + def setUpTestData(cls): + groups = ( + Group(name='Group 1'), + Group(name='Group 2'), + Group(name='Group 3'), + ) + Group.objects.bulk_create(groups) + + users = ( + User(username='User 1'), + User(username='User 2'), + User(username='User 3'), + ) + User.objects.bulk_create(users) + + owner_groups = ( + OwnerGroup(name='Owner Group 1'), + OwnerGroup(name='Owner Group 2'), + OwnerGroup(name='Owner Group 3'), + OwnerGroup(name='Owner Group 4'), + ) + OwnerGroup.objects.bulk_create(owner_groups) + + owners = ( + Owner(name='Owner 1'), + Owner(name='Owner 2'), + Owner(name='Owner 3'), + ) + Owner.objects.bulk_create(owners) + + # Assign users and groups to owners + owners[0].user_groups.add(groups[0]) + owners[1].user_groups.add(groups[1]) + owners[2].user_groups.add(groups[2]) + owners[0].users.add(users[0]) + owners[1].users.add(users[1]) + owners[2].users.add(users[2]) + + cls.form_data = { + 'name': 'Owner X', + 'group': owner_groups[3].pk, + 'user_groups': [groups[0].pk, groups[1].pk], + 'users': [users[0].pk, users[1].pk], + 'description': 'A new owner', + } + + cls.csv_data = ( + "name,group,description", + "Owner 4,Owner Group 4,Foo", + "Owner 5,Owner Group 4,Bar", + "Owner 6,Owner Group 4,Baz", + ) + + cls.csv_update_data = ( + "id,description", + f"{owners[0].pk},Foo", + f"{owners[1].pk},Bar", + f"{owners[2].pk},Baz", + ) + + cls.bulk_edit_data = { + 'description': 'New description', + } diff --git a/netbox/utilities/testing/views.py b/netbox/utilities/testing/views.py index f00b21d08..c4ef28e26 100644 --- a/netbox/utilities/testing/views.py +++ b/netbox/utilities/testing/views.py @@ -1113,6 +1113,21 @@ class ViewTestCases: """ maxDiff = None + class AdminModelViewTestCase( + GetObjectViewTestCase, + CreateObjectViewTestCase, + EditObjectViewTestCase, + DeleteObjectViewTestCase, + ListObjectsViewTestCase, + BulkImportObjectsViewTestCase, + BulkEditObjectsViewTestCase, + BulkDeleteObjectsViewTestCase, + ): + """ + TestCase suitable for testing all standard View functions for objects which inherit from AdminModel. + """ + maxDiff = None + class DeviceComponentTemplateViewTestCase( EditObjectViewTestCase, DeleteObjectViewTestCase,