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

Add model tests #134

Draft
wants to merge 16 commits into
base: dev
Choose a base branch
from
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
"isort.args": [
"--profile=black"
],
"isort.path": "/opt/netbox/venv/bin/isort",
"isort.path": ["/opt/netbox/venv/bin/isort"],
"python.analysis.typeCheckingMode": "strict",
python.Jedi
"python.analysis.extraPaths": [
"/opt/netbox/netbox"
],
Expand Down Expand Up @@ -86,6 +85,7 @@
"aaron-bond.better-comments",
"batisteo.vscode-django",
"codezombiech.gitignore",
"eamodio.gitlens",
"esbenp.prettier-vscode",
"formulahendry.auto-rename-tag",
"mintlify.document",
Expand Down
1 change: 1 addition & 0 deletions .devcontainer/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pylint-django
wily
yapf
sourcery-analytics
coverage
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,15 @@ cython_debug/
.vscode/
# JetBrains
.idea/

# Temporary files
*.tmp
tmp/

# coverage
coverage/
htmlcov/
.coverage
.coverage.*
coverage.xml
*.cover
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ rebuild: setup makemigrations migrate collectstatic start

.PHONY: test
test: setup
${VENV_PY_PATH} ${NETBOX_MANAGE_PATH}/manage.py makemigrations ${PLUGIN_NAME} --check
${VENV_PY_PATH} ${NETBOX_MANAGE_PATH}/manage.py test ${PLUGIN_NAME}
${NETBOX_MANAGE_PATH}/manage.py makemigrations ${PLUGIN_NAME} --check
coverage run --source "netbox_acls" ${NETBOX_MANAGE_PATH}/manage.py test ${PLUGIN_NAME} -v 2

.PHONY: coverage_report
coverage_report:
coverage report

.PHONY: test_coverage
test_coverage: test coverage_report

#relpatch:
# $(eval GSTATUS := $(shell git status --porcelain))
Expand Down
2 changes: 0 additions & 2 deletions netbox_acls/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ class Meta:
}

def __init__(self, *args, **kwargs):

# Initialize helper selectors
instance = kwargs.get("instance")
initial = kwargs.get("initial", {}).copy()
Expand Down Expand Up @@ -324,7 +323,6 @@ class ACLInterfaceAssignmentForm(NetBoxModelForm):
comments = CommentField()

def __init__(self, *args, **kwargs):

# Initialize helper selectors
instance = kwargs.get("instance")
initial = kwargs.get("initial", {}).copy()
Expand Down
1 change: 1 addition & 0 deletions netbox_acls/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .types import *


class Query(ObjectType):
"""
Defines the queries available to this plugin via the graphql api.
Expand Down
1 change: 0 additions & 1 deletion netbox_acls/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,3 @@ class Meta:
model = models.ACLStandardRule
fields = "__all__"
filterset_class = filtersets.ACLStandardRuleFilterSet

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("netbox_acls", "0001_initial"),
Expand Down
1 change: 0 additions & 1 deletion netbox_acls/migrations/0003_netbox_acls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("netbox_acls", "0002_alter_accesslist_options_and_more"),
]
Expand Down
26 changes: 26 additions & 0 deletions netbox_acls/migrations/0004_alter_accesslist_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.1.5 on 2023-02-02 22:34

import django.core.validators
from django.db import migrations, models


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

operations = [
migrations.AlterField(
model_name="accesslist",
name="name",
field=models.CharField(
max_length=500,
validators=[
django.core.validators.RegexValidator(
"^[a-zA-Z0-9-_]+$",
"Only alphanumeric, hyphens, and underscores characters are allowed.",
)
],
),
),
]
19 changes: 18 additions & 1 deletion netbox_acls/models/access_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dcim.models import Device, Interface, VirtualChassis
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models
from django.urls import reverse
Expand All @@ -21,7 +22,7 @@


alphanumeric_plus = RegexValidator(
r"^[0-9a-zA-Z,-,_]*$",
r"^[a-zA-Z0-9-_]+$",
"Only alphanumeric, hyphens, and underscores characters are allowed.",
)

Expand Down Expand Up @@ -146,6 +147,22 @@ def get_absolute_url(self):
args=[self.pk],
)

def clean(self):
super().clean()

# Get the model type of the assigned interface.
if self.assigned_object_type.model_class() == VMInterface:
interface_host = self.assigned_object.virtual_machine
elif self.assigned_object_type.model_class() == Interface:
interface_host = self.assigned_object.device
# Check if the assigned interface's host is the same as the host assigned to the access list.
if interface_host != self.access_list.assigned_object:
raise ValidationError(
{
"assigned_object": "The assigned object must be the same as the device assigned to it."
}
)

@classmethod
def get_prerequisite_models(cls):
return [AccessList]
Expand Down
Loading