Skip to content

Commit

Permalink
👌 [#234] PR feedback
Browse files Browse the repository at this point in the history
* add explicit API test to check if no validation is applied if soort != email
* remove re-raising of validationerrors for email validation in case of API context
* move retrieval of soort_digitaal_adres to caller code (admin/serializer.validate)
  • Loading branch information
stevenbal committed Nov 8, 2024
1 parent 6d795b0 commit 5e1ee41
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Meta:
model = DigitaalAdres
fields = "__all__"

def clean(self):
def clean_adres(self):
data = self.cleaned_data
OptionalEmailValidator()(data, None)
return data
OptionalEmailValidator()(data["adres"], data.get("soort_digitaal_adres"))
return data["adres"]


@admin.register(DigitaalAdres)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from openklant.components.klantinteracties.models.digitaal_adres import DigitaalAdres
from openklant.components.klantinteracties.models.klantcontacten import Betrokkene
from openklant.components.klantinteracties.models.partijen import Partij
from openklant.utils.serializers import get_field_value


class DigitaalAdresForeignKeySerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -88,7 +89,17 @@ class Meta:
"help_text": _("De unieke URL van dit digitaal adres binnen deze API."),
},
}
validators = [OptionalEmailValidator()]

def validate_adres(self, adres):
"""
Define the validator here, to avoid DRF spectacular marking the format for
`adres` as `email`
"""
soort_digitaal_adres = get_field_value(
self, self.initial_data, "soort_digitaal_adres"
)
OptionalEmailValidator()(adres, soort_digitaal_adres)
return adres

@transaction.atomic
def update(self, instance, validated_data):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.test import tag
from django.utils.translation import gettext as _

from rest_framework import status
Expand Down Expand Up @@ -92,6 +93,7 @@ def test_create_digitaal_adres(self):
self.assertEqual(data["adres"], "[email protected]")
self.assertEqual(data["omschrijving"], "omschrijving")

@tag("gh-234")
def test_create_digitaal_adres_email_validation(self):
list_url = reverse("klantinteracties:digitaaladres-list")
data = {
Expand Down Expand Up @@ -140,6 +142,24 @@ def test_create_digitaal_adres_email_validation(self):
],
)

with self.subTest("no validation applied if soort is not email"):
response = self.client.patch(
detail_url,
{
"soortDigitaalAdres": SoortDigitaalAdres.telefoonnummer,
"adres": "0612345678",
},
)

self.assertEqual(response.status_code, status.HTTP_200_OK)

digitaal_adres.refresh_from_db()

self.assertEqual(
digitaal_adres.soort_digitaal_adres, SoortDigitaalAdres.telefoonnummer
)
self.assertEqual(digitaal_adres.adres, "0612345678")

def test_update_digitaal_adres(self):
betrokkene, betrokkene2 = BetrokkeneFactory.create_batch(2)
partij, partij2 = PartijFactory.create_batch(2)
Expand Down
24 changes: 5 additions & 19 deletions src/openklant/components/klantinteracties/api/validators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.core.exceptions import ValidationError
from django.core.validators import EmailValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

from rest_framework import exceptions, serializers
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator, qs_filter

from openklant.components.klantinteracties.constants import SoortDigitaalAdres
Expand All @@ -26,7 +25,6 @@
PartijIdentificator,
)
from openklant.components.klantinteracties.models.rekeningnummers import Rekeningnummer
from openklant.utils.serializers import get_field_value


class FKUniqueTogetherValidator(UniqueTogetherValidator):
Expand Down Expand Up @@ -175,21 +173,9 @@ def Rekeningnummer_exists(value):
class OptionalEmailValidator(EmailValidator):
"""
EmailValidator for SoortDigitaalAdres that only attempts to validate if
`SoortDigitaalAdres` is `email
`SoortDigitaalAdres` is `email`
"""

requires_context = True

def __call__(self, attrs: dict, serializer):
if (
get_field_value(serializer, attrs, "soort_digitaal_adres")
== SoortDigitaalAdres.email
):
try:
super().__call__(get_field_value(serializer, attrs, "adres"))
except ValidationError as e:
# re-raise as field error
if serializer:
raise exceptions.ValidationError({"adres": e.message})
else:
raise ValidationError({"adres": e})
def __call__(self, value: str, soort_digitaal_adres: str):
if soort_digitaal_adres == SoortDigitaalAdres.email:
super().__call__(value)
6 changes: 4 additions & 2 deletions src/openklant/components/klantinteracties/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from uuid import uuid4

from django.test import tag
from django.urls import reverse
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -94,8 +95,8 @@ def test_digitaal_adres_inline(self):
betrokkene should be optional
"""

SuperUserFactory(username="admin", password="admin")
self._login_user(username="admin", password="admin")
user = SuperUserFactory(username="admin", password="admin")
self.app.set_user(user)

partij = PartijFactory(soort_partij="persoon", voorkeurs_digitaal_adres=None)
PersoonFactory(partij=partij)
Expand All @@ -121,6 +122,7 @@ def test_digitaal_adres_inline(self):

@disable_admin_mfa()
class DigitaalAdresAdminTests(WebTest):
@tag("gh-234")
def test_email_validation(self):
"""
Check that the admin applies conditional email validation on `adres`, only if
Expand Down

0 comments on commit 5e1ee41

Please sign in to comment.