Skip to content

Commit

Permalink
[#3688] Add a objecttypes_service field, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 31, 2024
1 parent a0698d7 commit 780de80
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.23 on 2024-01-31 14:16

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


class Migration(migrations.Migration):

dependencies = [
("zgw_consumers", "0019_alter_service_uuid"),
(
"registrations_objects_api",
"0009_objectsapiconfig_payment_status_update_json",
),
]

operations = [
migrations.AddField(
model_name="objectsapiconfig",
name="objecttypes_service",
field=models.OneToOneField(
limit_choices_to={"api_type": "orc"},
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="+",
to="zgw_consumers.service",
verbose_name="Objecttypes API",
),
),
]
28 changes: 28 additions & 0 deletions src/openforms/registrations/contrib/objects_api/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _

from furl import furl
from solo.models import SingletonModel
from zgw_consumers.constants import APITypes

Expand Down Expand Up @@ -34,6 +36,14 @@ class ObjectsAPIConfig(SingletonModel):
null=True,
related_name="+",
)
objecttypes_service = models.OneToOneField(
"zgw_consumers.Service",
verbose_name=_("Objecttypes API"),
on_delete=models.PROTECT,
limit_choices_to={"api_type": APITypes.orc},
null=True,
related_name="+",
)
drc_service = models.OneToOneField(
"zgw_consumers.Service",
verbose_name=_("Documenten API"),
Expand Down Expand Up @@ -143,6 +153,24 @@ class ObjectsAPIConfig(SingletonModel):
class Meta:
verbose_name = _("Objects API configuration")

def clean(self) -> None:
super().clean()

if (
self.objecttypes_service
and self.objecttype
and not furl(self.objecttype).host
== furl(self.objects_service.api_root).host
):
raise ValidationError(
{
"objecttype": _(
"The provided Objecttype is not part of the configured Objecttypes API."
)
},
code="invalid",
)

def apply_defaults_to(self, options):
options.setdefault("objecttype", self.objecttype)
options.setdefault("objecttype_version", self.objecttype_version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from zgw_consumers.constants import APITypes

from zgw_consumers_ext.tests.factories import ServiceFactory

from ..models import ObjectsAPIConfig


class ObjectsAPIConfigTests(TestCase):

def test_invalid_objecttypes_url(self):
config = ObjectsAPIConfig(
objecttypes_service=ServiceFactory.build(
api_root="https://objecttypen.nl/api/v1/",
api_type=APITypes.orc,
),
objecttype="https://example.com/api/dummy/",
)

self.assertRaises(ValidationError, config.clean)

def test_valid_objecttypes_url(self):
config = ObjectsAPIConfig(
objecttypes_service=ServiceFactory.build(
api_root="https://objecttypen.nl/api/v1/",
api_type=APITypes.orc,
),
objecttype="https://objecttypen.nl/api/v1/objecttypes/1",
)

try:
config.clean()
except ValidationError as e:
self.fail(f"Unexpected exception : {e}")

0 comments on commit 780de80

Please sign in to comment.