Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed failing tests in Nautobot v2.3.13 #849

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/849.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed failing tests in Nautobot v2.3.13.
63 changes: 61 additions & 2 deletions nautobot_golden_config/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from nautobot.core.testing import APITestCase, APIViewTestCases
from nautobot.apps.testing import APITestCase, APIViewTestCases
from nautobot.dcim.models import Device, Platform
from nautobot.extras.models import DynamicGroup, GitRepository, GraphQLQuery, Status
from rest_framework import status

from nautobot_golden_config.choices import RemediationTypeChoice
from nautobot_golden_config.models import ConfigPlan, GoldenConfigSetting, RemediationSetting
from nautobot_golden_config.models import ConfigPlan, ConfigReplace, GoldenConfigSetting, RemediationSetting
from nautobot_golden_config.tests.conftest import (
create_config_compliance,
create_device,
Expand Down Expand Up @@ -409,6 +409,65 @@ def setUpTestData(cls):
}


class ConfigReplaceAPITestCase( # pylint: disable=too-many-ancestors
APIViewTestCases.CreateObjectViewTestCase,
APIViewTestCases.GetObjectViewTestCase,
APIViewTestCases.ListObjectsViewTestCase,
APIViewTestCases.UpdateObjectViewTestCase,
APIViewTestCases.DeleteObjectViewTestCase,
APIViewTestCases.NotesURLViewTestCase,
):
"""Test API for ConfigReplace."""

model = ConfigReplace

@classmethod
def setUpTestData(cls):
create_device_data()
platform = Device.objects.first().platform
for num in range(3):
ConfigReplace.objects.create(
name=f"test configreplace {num}",
platform=platform,
description="test description",
regex="^(.*)$",
replace="xyz",
)
cls.update_data = {
"name": "new name",
"platform": platform.pk,
"description": "new description",
"regex": "^NEW (.*)$",
"replace": "NEW replaced text",
}
cls.create_data = [
{
"name": "test configreplace 4",
"platform": platform.pk,
"description": "test description",
"regex": "^(.*)$",
"replace": "xyz",
},
{
"name": "test configreplace 5",
"platform": platform.pk,
"description": "test description",
"regex": "^(.*)$",
"replace": "xyz",
},
{
"name": "test configreplace 6",
"platform": platform.pk,
"description": "test description",
"regex": "^(.*)$",
"replace": "xyz",
},
]
cls.bulk_update_data = {
"description": "new description",
}


class GenerateIntendedConfigViewAPITestCase(APITestCase):
"""Test API for GenerateIntendedConfigView."""

Expand Down
76 changes: 31 additions & 45 deletions nautobot_golden_config/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from django.test import RequestFactory, override_settings
from django.urls import reverse
from lxml import html
from nautobot.core.models.querysets import RestrictedQuerySet
from nautobot.core.testing import TestCase, ViewTestCases
from nautobot.apps.models import RestrictedQuerySet
from nautobot.apps.testing import TestCase, ViewTestCases
from nautobot.dcim.models import Device
from nautobot.extras.models import Relationship, RelationshipAssociation, Status

Expand Down Expand Up @@ -102,57 +102,43 @@ def test_config_compliance_details_sotagg_no_error(
mock_graph_ql_query.assert_called()


@override_settings(EXEMPT_VIEW_PERMISSIONS=["*"])
class ConfigReplaceListViewTestCase(TestCase):
"""Test ConfigReplaceListView."""
class ConfigReplaceUIViewSetTestCase(ViewTestCases.PrimaryObjectViewTestCase): # pylint: disable=too-many-ancestors
"""Test ConfigReplaceUIViewSet."""

model = models.ConfigReplace

_csv_headers = "name,platform,description,regex,replace"
_entry_name = "test name"
_entry_description = "test description"
_entry_regex = "^startswiththeend$"
_entry_replace = "<dontlookatme>"
bulk_edit_data = {
"description": "new description",
}

@classmethod
def setUpTestData(cls):
"""Set up base objects."""
create_device_data()
cls._delete_test_entry()
models.ConfigReplace.objects.create(
name=cls._entry_name,
platform=Device.objects.first().platform,
description=cls._entry_description,
regex=cls._entry_regex,
replace=cls._entry_replace,
)

@property
def _url(self):
return reverse("plugins:nautobot_golden_config:configreplace_list")

@classmethod
def _delete_test_entry(cls):
try:
entry = models.ConfigReplace.objects.get(name=cls._entry_name)
entry.delete()
except models.ConfigReplace.DoesNotExist:
pass

def test_configreplace_import(self):
self._delete_test_entry()
self.add_permissions("nautobot_golden_config.add_configreplace")
platform = Device.objects.first().platform
import_entry = (
f"{self._entry_name},{platform.id},{self._entry_description},{self._entry_regex},{self._entry_replace}"
for num in range(3):
models.ConfigReplace.objects.create(
name=f"test configreplace {num}",
platform=platform,
description="test description",
regex="^(.*)$",
replace="xyz",
)
cls.form_data = {
"name": "new name",
"platform": platform.pk,
"description": "new description",
"regex": "^NEW (.*)$",
"replace": "NEW replaced text",
}

# For compatibility with Nautobot lower than v2.2.0
cls.csv_data = (
"name,regex,replace,platform",
f"test configreplace 4,^(.*)$,xyz,{platform.pk}",
f"test configreplace 5,^(.*)$,xyz,{platform.pk}",
f"test configreplace 6,^(.*)$,xyz,{platform.pk}",
)
form_data = {"csv_data": f"{self._csv_headers}\n{import_entry}"}
response = self.client.post(f"{self._url}import/", data=form_data, follow=True)
last_entry = models.ConfigReplace.objects.last()
self.assertEqual(response.status_code, 200)
self.assertEqual(last_entry.name, self._entry_name)
self.assertEqual(last_entry.platform, platform)
self.assertEqual(last_entry.description, self._entry_description)
self.assertEqual(last_entry.regex, self._entry_regex)
self.assertEqual(last_entry.replace, self._entry_replace)


class GoldenConfigListViewTestCase(TestCase):
Expand Down