Skip to content

Commit

Permalink
Fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joewesch committed Aug 28, 2023
1 parent 0b72362 commit d517d78
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 27 deletions.
2 changes: 2 additions & 0 deletions nautobot_golden_config/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ class ConfigPlanFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
label="Device Name",
)
feature_id = django_filters.ModelMultipleChoiceFilter(
field_name="feature__id",
queryset=models.ComplianceFeature.objects.all(),
to_field_name="id",
label="Feature ID",
)
feature = django_filters.ModelMultipleChoiceFilter(
Expand Down
25 changes: 24 additions & 1 deletion nautobot_golden_config/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
"""Params for testing."""
from datetime import datetime
import uuid
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.utils.text import slugify
from nautobot.dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Platform, Rack, RackGroup, Region, Site
from nautobot.extras.datasources.registry import get_datasource_contents
from nautobot.extras.models import GitRepository, GraphQLQuery, Status, Tag
from nautobot.extras.models import GitRepository, GraphQLQuery, Status, Tag, JobResult
from nautobot.tenancy.models import Tenant, TenantGroup
import pytz
from nautobot_golden_config.choices import ComplianceRuleConfigTypeChoice
from nautobot_golden_config.models import ComplianceFeature, ComplianceRule, ConfigCompliance


User = get_user_model()


def create_device_data():
"""Creates a Device and associated data."""
manufacturers = (
Expand Down Expand Up @@ -444,3 +451,19 @@ def create_saved_queries() -> None:
query=query,
)
saved_query_5.save()


def create_job_result() -> None:
"""Create a JobResult and return the object."""
obj_type = ContentType.objects.get(app_label="extras", model="job")
user, _ = User.objects.get_or_create(username="testuser")
result = JobResult.objects.create(
name="Test-Job-Result",
obj_type=obj_type,
user=user,
job_id=uuid.uuid4(),
)
result.status = "completed"
result.completed = datetime.now(pytz.UTC)
result.validated_save()
return result
17 changes: 14 additions & 3 deletions nautobot_golden_config/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
create_git_repos,
create_saved_queries,
create_device_data,
create_job_result,
)


Expand Down Expand Up @@ -332,7 +333,7 @@ def test_bulk_create_objects(self):
"""Skipping test due to POST method not allowed."""


# pylint: disable=too-many-ancestors
# pylint: disable=too-many-ancestors,too-many-locals
class ConfigPlanTest(APIViewTestCases.APIViewTestCase):
"""Test API for ConfigPlan."""

Expand All @@ -352,28 +353,38 @@ def setUpTestData(cls):
rule2 = create_feature_rule_json(device2, feature="Test Feature 2")
rule3 = create_feature_rule_json(device3, feature="Test Feature 3")

job_result1 = create_job_result()
job_result2 = create_job_result()
job_result3 = create_job_result()

features = [rule1.feature, rule2.feature, rule3.feature]
plan_types = ["intended", "missing", "remediation"]
job_result_ids = [job_result1.id, job_result2.id, job_result3.id]
not_approved_status = Status.objects.get(slug="not-approved")
approved_status = Status.objects.get(slug="approved")

for cont in range(1, 4):
ConfigPlan.objects.create(
plan = ConfigPlan.objects.create(
device=Device.objects.get(name=f"Device {cont}"),
plan_type=plan_types[cont - 1],
feature=features[cont - 1],
config_set=f"Test Config Set {cont}",
change_control_id=f"Test Change Control ID {cont}",
change_control_url=f"https://{cont}.example.com/",
status=not_approved_status,
job_result_id=job_result_ids[cont - 1],
)
plan.feature.add(features[cont - 1])
plan.validated_save()

cls.update_data = {
"change_control_id": "Test Change Control ID 4",
"change_control_url": "https://4.example.com/",
"status": approved_status.slug,
}

cls.bulk_update_data = {
"change_control_id": "Test Change Control ID 5",
"change_control_url": "https://5.example.com/",
"status": approved_status.slug,
}

Expand Down
33 changes: 24 additions & 9 deletions nautobot_golden_config/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from nautobot.utilities.testing import FilterTestCases
from nautobot_golden_config import filters, models

from .conftest import create_feature_rule_json, create_device_data, create_feature_rule_cli
from .conftest import create_feature_rule_json, create_device_data, create_feature_rule_cli, create_job_result


class ConfigComplianceModelTestCase(TestCase):
Expand Down Expand Up @@ -305,7 +305,7 @@ def test_search(self):

# pylint: disable=too-many-ancestors
# pylint: disable=too-many-instance-attributes
class ConfigPlanModelTestCase(FilterTestCases.FilterTestCase):
class ConfigPlanFilterTestCase(FilterTestCases.FilterTestCase):
"""Test filtering operations for ConfigPlan Model."""

queryset = models.ConfigPlan.objects.all()
Expand All @@ -324,49 +324,55 @@ def setUp(self):
self.status2 = Status.objects.get(name="Approved")
self.tag1, _ = Tag.objects.get_or_create(name="Tag 1")
self.tag2, _ = Tag.objects.get_or_create(name="Tag 2")
self.job_result1 = create_job_result()
self.job_result2 = create_job_result()
self.config_plan1 = models.ConfigPlan.objects.create(
device=self.device1,
plan_type="intended",
created="2020-01-01",
config_set="intended test",
feature=self.feature1,
change_control_id="12345",
status=self.status2,
job_result_id=self.job_result1.id,
)
self.config_plan1.tags.add(self.tag1)
self.config_plan1.save()
self.config_plan1.feature.add(self.feature1)
self.config_plan1.validated_save()
self.config_plan2 = models.ConfigPlan.objects.create(
device=self.device1,
plan_type="missing",
created="2020-01-02",
config_set="missing test",
feature=self.feature2,
change_control_id="23456",
status=self.status1,
job_result_id=self.job_result1.id,
)
self.config_plan2.tags.add(self.tag2)
self.config_plan2.save()
self.config_plan2.feature.add(self.feature2)
self.config_plan2.validated_save()
self.config_plan3 = models.ConfigPlan.objects.create(
device=self.device2,
plan_type="remediation",
created="2020-01-03",
config_set="remediation test",
feature=self.feature1,
change_control_id="34567",
status=self.status2,
job_result_id=self.job_result2.id,
)
self.config_plan3.tags.add(self.tag2)
self.config_plan3.save()
self.config_plan3.feature.add(self.feature1)
self.config_plan3.validated_save()
self.config_plan4 = models.ConfigPlan.objects.create(
device=self.device2,
plan_type="manual",
created="2020-01-04",
config_set="manual test",
change_control_id="45678",
status=self.status1,
job_result_id=self.job_result1.id,
)
self.config_plan4.tags.add(self.tag1)
self.config_plan4.save()
self.config_plan4.validated_save()

def test_full(self):
"""Test without filtering to ensure all have been added."""
Expand Down Expand Up @@ -454,3 +460,12 @@ def test_filter_tag(self):
filterset = self.filterset(params, self.queryset)
self.assertEqual(filterset.qs.count(), 2)
self.assertQuerysetEqualAndNotEmpty(filterset.qs, self.queryset.filter(tags__name=self.tag1.name).distinct())

def test_job_result_id(self):
"""Test filtering by Job Result ID."""
params = {"job_result_id": [self.job_result1.pk]}
filterset = self.filterset(params, self.queryset)
self.assertEqual(filterset.qs.count(), 3)
self.assertQuerysetEqualAndNotEmpty(
filterset.qs, self.queryset.filter(job_result_id=self.job_result1.id).distinct()
)
31 changes: 24 additions & 7 deletions nautobot_golden_config/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
)
from nautobot_golden_config.tests.conftest import create_git_repos

from .conftest import create_config_compliance, create_device, create_feature_rule_json, create_saved_queries
from .conftest import (
create_config_compliance,
create_device,
create_feature_rule_json,
create_saved_queries,
create_job_result,
)


class ConfigComplianceModelTestCase(TestCase):
Expand Down Expand Up @@ -295,19 +301,23 @@ def setUp(self):
self.rule = create_feature_rule_json(self.device)
self.feature = self.rule.feature
self.status = Status.objects.get(slug="not-approved")
self.job_result = create_job_result()

def test_create_config_plan_intended(self):
"""Test Create Object."""
config_plan = ConfigPlan.objects.create(
device=self.device,
plan_type="intended",
feature=self.feature,
config_set="test intended config",
change_control_id="1234",
change_control_url="https://1234.example.com/",
status=self.status,
job_result_id=self.job_result.id,
)
config_plan.feature.add(self.feature)
config_plan.validated_save()
self.assertEqual(config_plan.device, self.device)
self.assertEqual(config_plan.feature, self.feature)
self.assertEqual(config_plan.feature.first(), self.feature)
self.assertEqual(config_plan.config_set, "test intended config")
self.assertEqual(config_plan.change_control_id, "1234")
self.assertEqual(config_plan.status, self.status)
Expand All @@ -318,13 +328,16 @@ def test_create_config_plan_missing(self):
config_plan = ConfigPlan.objects.create(
device=self.device,
plan_type="missing",
feature=self.feature,
config_set="test missing config",
change_control_id="2345",
change_control_url="https://2345.example.com/",
status=self.status,
job_result_id=self.job_result.id,
)
config_plan.feature.add(self.feature)
config_plan.validated_save()
self.assertEqual(config_plan.device, self.device)
self.assertEqual(config_plan.feature, self.feature)
self.assertEqual(config_plan.feature.first(), self.feature)
self.assertEqual(config_plan.config_set, "test missing config")
self.assertEqual(config_plan.change_control_id, "2345")
self.assertEqual(config_plan.status, self.status)
Expand All @@ -335,13 +348,16 @@ def test_create_config_plan_remediation(self):
config_plan = ConfigPlan.objects.create(
device=self.device,
plan_type="remediation",
feature=self.feature,
config_set="test remediation config",
change_control_id="3456",
change_control_url="https://3456.example.com/",
status=self.status,
job_result_id=self.job_result.id,
)
config_plan.feature.add(self.feature)
config_plan.validated_save()
self.assertEqual(config_plan.device, self.device)
self.assertEqual(config_plan.feature, self.feature)
self.assertEqual(config_plan.feature.first(), self.feature)
self.assertEqual(config_plan.config_set, "test remediation config")
self.assertEqual(config_plan.change_control_id, "3456")
self.assertEqual(config_plan.status, self.status)
Expand All @@ -353,6 +369,7 @@ def test_create_config_plan_manual(self):
device=self.device,
plan_type="manual",
config_set="test manual config",
job_result_id=self.job_result.id,
)
self.assertEqual(config_plan.device, self.device)
self.assertEqual(config_plan.config_set, "test manual config")
Expand Down
28 changes: 21 additions & 7 deletions nautobot_golden_config/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from nautobot.utilities.testing import ViewTestCases
from nautobot_golden_config import views, models

from .conftest import create_feature_rule_json, create_device_data
from .conftest import create_feature_rule_json, create_device_data, create_job_result


User = get_user_model()
Expand Down Expand Up @@ -322,37 +322,51 @@ def setUpTestData(cls):
rule2 = create_feature_rule_json(device2, feature="Test Feature 2")
rule3 = create_feature_rule_json(device3, feature="Test Feature 3")

job_result1 = create_job_result()
job_result2 = create_job_result()
job_result3 = create_job_result()

not_approved_status = Status.objects.get(slug="not-approved")
approved_status = Status.objects.get(slug="approved")

models.ConfigPlan.objects.create(
plan1 = models.ConfigPlan.objects.create(
device=device1,
plan_type="intended",
feature=rule1.feature,
config_set="Test Config Set 1",
change_control_id="Test Change Control ID 1",
change_control_url="https://1.example.com/",
status=not_approved_status,
job_result_id=job_result1.id,
)
models.ConfigPlan.objects.create(
plan1.feature.add(rule1.feature)
plan1.validated_save()
plan2 = models.ConfigPlan.objects.create(
device=device2,
plan_type="missing",
feature=rule2.feature,
config_set="Test Config Set 2",
change_control_id="Test Change Control ID 2",
change_control_url="https://2.example.com/",
status=not_approved_status,
job_result_id=job_result2.id,
)
models.ConfigPlan.objects.create(
plan2.feature.add(rule2.feature)
plan2.validated_save()
plan3 = models.ConfigPlan.objects.create(
device=device3,
plan_type="remediation",
feature=rule3.feature,
config_set="Test Config Set 3",
change_control_id="Test Change Control ID 3",
change_control_url="https://3.example.com/",
status=not_approved_status,
job_result_id=job_result3.id,
)
plan3.feature.add(rule3.feature)
plan3.validated_save()

# Used for EditObjectViewTestCase
cls.form_data = {
"change_control_id": "Test Change Control ID 4",
"change_control_url": "https://4.example.com/",
"status": approved_status.pk,
}

Expand Down

0 comments on commit d517d78

Please sign in to comment.