Skip to content

Fixes #47 [Housekeeping]: Develop Plugin Tests - Models #257

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

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
- TODO: ACL Form Bubble/ICON Extended/Standard
- TODO: Add an Access List to an Interface Custom Fields after comments - DONE
- TODO: ACL rules, look at last number and increment to next 10
- TODO: Clone for ACL Interface should include device
- TODO: Inconsistent errors for add/edit (where model is using a generic page)
- TODO: Check Constants across codebase for consistency.
- TODO: Test API, Forms, & Models - https://github.com/k01ek/netbox-bgp/tree/main/netbox_bgp/tests , https://github.com/DanSheps/netbox-secretstore/tree/develop/netbox_secretstore/tests & https://github.com/FlxPeters/netbox-plugin-prometheus-sd/tree/main/netbox_prometheus_sd/tests
- TODO: Test API, & Forms - https://github.com/k01ek/netbox-bgp/tree/main/netbox_bgp/tests , https://github.com/DanSheps/netbox-secretstore/tree/develop/netbox_secretstore/tests & https://github.com/FlxPeters/netbox-plugin-prometheus-sd/tree/main/netbox_prometheus_sd/tests
78 changes: 0 additions & 78 deletions netbox_acls/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@
# Sets a standard help_text value to be used by the various classes for acl index
help_text_acl_rule_index = "Determines the order of the rule in the ACL processing. AKA Sequence Number."

# Sets a standard error message for ACL rules with an action of remark, but no remark set.
error_message_no_remark = "Action is set to remark, you MUST add a remark."
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
error_message_action_remark_source_prefix_set = "Action is set to remark, Source Prefix CANNOT be set."
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."


class AccessListForm(NetBoxModelForm):
"""
Expand Down Expand Up @@ -545,35 +538,6 @@ class Meta:
),
}

def clean(self):
"""
Validates form inputs before submitting:
- Check if action set to remark, but no remark set.
- Check if action set to remark, but source_prefix set.
- Check remark set, but action not set to remark.
"""
super().clean()
cleaned_data = self.cleaned_data
error_message = {}

action = cleaned_data.get("action")
remark = cleaned_data.get("remark")
source_prefix = cleaned_data.get("source_prefix")

if action == "remark":
# Check if action set to remark, but no remark set.
if not remark:
error_message["remark"] = [error_message_no_remark]
# Check if action set to remark, but source_prefix set.
if source_prefix:
error_message["source_prefix"] = [error_message_action_remark_source_prefix_set]
# Check remark set, but action not set to remark.
elif remark:
error_message["remark"] = [error_message_remark_without_action_remark]

if error_message:
raise ValidationError(error_message)


class ACLExtendedRuleForm(NetBoxModelForm):
"""
Expand Down Expand Up @@ -651,45 +615,3 @@ class Meta:
),
"source_ports": help_text_acl_rule_logic,
}

def clean(self):
"""
Validates form inputs before submitting:
- Check if action set to remark, but no remark set.
- Check if action set to remark, but source_prefix set.
- Check if action set to remark, but source_ports set.
- Check if action set to remark, but destination_prefix set.
- Check if action set to remark, but destination_ports set.
- Check if action set to remark, but protocol set.
- Check remark set, but action not set to remark.
"""
super().clean()
cleaned_data = self.cleaned_data
error_message = {}

action = cleaned_data.get("action")
remark = cleaned_data.get("remark")
source_prefix = cleaned_data.get("source_prefix")
source_ports = cleaned_data.get("source_ports")
destination_prefix = cleaned_data.get("destination_prefix")
destination_ports = cleaned_data.get("destination_ports")
protocol = cleaned_data.get("protocol")

if action == "remark":
if not remark:
error_message["remark"] = [error_message_no_remark]
if source_prefix:
error_message["source_prefix"] = [error_message_action_remark_source_prefix_set]
if source_ports:
error_message["source_ports"] = ["Action is set to remark, Source Ports CANNOT be set."]
if destination_prefix:
error_message["destination_prefix"] = ["Action is set to remark, Destination Prefix CANNOT be set."]
if destination_ports:
error_message["destination_ports"] = ["Action is set to remark, Destination Ports CANNOT be set."]
if protocol:
error_message["protocol"] = ["Action is set to remark, Protocol CANNOT be set."]
elif remark:
error_message["remark"] = [error_message_remark_without_action_remark]

if error_message:
raise ValidationError(error_message)
62 changes: 62 additions & 0 deletions netbox_acls/migrations/0005_alter_accesslist_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Generated by Django 5.1.8 on 2025-04-20 22:35

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("netbox_acls", "0004_netbox_acls"),
]

operations = [
migrations.AlterModelOptions(
name="accesslist",
options={"ordering": ("assigned_object_type", "assigned_object_id", "name")},
),
migrations.AlterModelOptions(
name="aclextendedrule",
options={"ordering": ("access_list", "index")},
),
migrations.AlterModelOptions(
name="aclinterfaceassignment",
options={"ordering": ("assigned_object_type", "assigned_object_id", "access_list", "direction")},
),
migrations.AlterModelOptions(
name="aclstandardrule",
options={"ordering": ("access_list", "index")},
),
migrations.AlterField(
model_name="accesslist",
name="assigned_object_type",
field=models.ForeignKey(
limit_choices_to=models.Q(
models.Q(
models.Q(("app_label", "dcim"), ("model", "device")),
models.Q(("app_label", "dcim"), ("model", "virtualchassis")),
models.Q(("app_label", "virtualization"), ("model", "virtualmachine")),
_connector="OR",
),
),
on_delete=django.db.models.deletion.PROTECT,
related_name="+",
to="contenttypes.contenttype",
),
),
migrations.AlterField(
model_name="aclinterfaceassignment",
name="assigned_object_type",
field=models.ForeignKey(
limit_choices_to=models.Q(
models.Q(
models.Q(("app_label", "dcim"), ("model", "interface")),
models.Q(("app_label", "virtualization"), ("model", "vminterface")),
_connector="OR",
),
),
on_delete=django.db.models.deletion.PROTECT,
related_name="+",
to="contenttypes.contenttype",
),
),
]
Loading