Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit f6ee6220 authored by Vincent SIMONIN's avatar Vincent SIMONIN
Browse files

:twisted_rightwards_arrows: Merge branch 'rps_check_source_not_eq_target_url' into 'main'

:bug: Fixed source URL and target URL can be equal

See merge request !56
parents 25553db0 bdd71cfe
No related branches found
No related tags found
1 merge request!56🐛 Fixed source URL and target URL can be equal
Pipeline #118675 passed
"""Migration File"""
# pylint: disable=C0103
import django.contrib.postgres.fields
from django.db import migrations, models
import netbox_rps_plugin.models
class Migration(migrations.Migration):
"""Migration Class"""
dependencies = [
(
"netbox_rps_plugin",
"0010_alter_httpheader_options_alter_mapping_options_and_more",
),
]
operations = [
migrations.AlterField(
model_name="mapping",
name="extra_protocols",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(max_length=32),
blank=True,
default=netbox_rps_plugin.models.default_protocol,
size=None,
),
),
migrations.AddConstraint(
model_name="mapping",
constraint=models.CheckConstraint(
check=models.Q(("source__exact", models.F("target")), _negated=True),
name="netbox_rps_plugin_mapping_check_target_source_url",
),
),
]
"""Models definitions"""
from django.core.exceptions import ValidationError
from django.conf import settings
from django.db import models
from django.urls import reverse
......@@ -120,6 +121,12 @@ class Mapping(NetBoxModel):
class Meta:
ordering = ("source", "target")
constraints = [
models.CheckConstraint(
check=~models.Q(source__exact=models.F("target")),
name="%(app_label)s_%(class)s_check_target_source_url",
)
]
def __str__(self):
return f"{self.source}"
......@@ -128,6 +135,15 @@ class Mapping(NetBoxModel):
"""override"""
return reverse("plugins:netbox_rps_plugin:mapping", args=[self.pk])
def clean(self):
"""Clean model method for validation"""
super().clean()
if self.source == self.target:
raise ValidationError(
{"target": "Target URL cannot be equal to source URL."}
)
class SamlConfig(NetBoxModel):
"""SAML config definition class"""
......
......@@ -50,6 +50,27 @@ class TestMappingUnique(Base):
response.content, b'{"source":["mapping with this Source already exists."]}'
)
def test_that_source_url_cannot_be_equal_to_destination_url(self) -> None:
"""Test that source ULR cannot be equal to destination URL"""
response = requests.post(
url=f"http://{HOST}:{PORT}/api/plugins/rps/mapping/",
json={
"source": "https://truc8.com/api",
"target": "https://truc8.com/api",
"authentication": "none",
"testingpage": None,
},
headers={"Authorization": f"Token {API_KEY}"},
timeout=5,
)
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.content,
b'{"target":["Target URL cannot be equal to source URL."]}',
)
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment