-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from maykinmedia/feature/234-email-validation
✨ [#234] Validate DigitaalAdres.adres if type is email
- Loading branch information
Showing
6 changed files
with
197 additions
and
23 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/openklant/components/klantinteracties/admin/digitaal_adres.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
from django import forms | ||
from django.contrib import admin | ||
|
||
from ..api.validators import OptionalEmailValidator | ||
from ..models.digitaal_adres import DigitaalAdres | ||
|
||
|
||
class DigitaalAdresAdminForm(forms.ModelForm): | ||
class Meta: | ||
model = DigitaalAdres | ||
fields = "__all__" | ||
|
||
def clean_adres(self): | ||
data = self.cleaned_data | ||
OptionalEmailValidator()(data["adres"], data.get("soort_digitaal_adres")) | ||
return data["adres"] | ||
|
||
|
||
@admin.register(DigitaalAdres) | ||
class DigitaalAdresAdmin(admin.ModelAdmin): | ||
readonly_fields = ("uuid",) | ||
search_fields = ("adres",) | ||
autocomplete_fields = ("partij",) | ||
form = DigitaalAdresAdminForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from django.test import tag | ||
from django.utils.translation import gettext as _ | ||
|
||
from rest_framework import status | ||
from vng_api_common.tests import reverse | ||
|
||
|
@@ -90,6 +93,73 @@ 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 = { | ||
"verstrektDoorBetrokkene": None, | ||
"verstrektDoorPartij": None, | ||
"soortDigitaalAdres": SoortDigitaalAdres.email, | ||
"adres": "invalid", | ||
"omschrijving": "omschrijving", | ||
} | ||
|
||
response = self.client.post(list_url, data) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
data = response.json() | ||
self.assertEqual( | ||
data["invalidParams"], | ||
[ | ||
{ | ||
"name": "adres", | ||
"code": "invalid", | ||
"reason": _("Voer een geldig e-mailadres in."), | ||
} | ||
], | ||
) | ||
|
||
digitaal_adres = DigitaalAdresFactory.create( | ||
soort_digitaal_adres=SoortDigitaalAdres.email, adres="[email protected]" | ||
) | ||
detail_url = reverse( | ||
"klantinteracties:digitaaladres-detail", | ||
kwargs={"uuid": str(digitaal_adres.uuid)}, | ||
) | ||
|
||
response = self.client.patch(detail_url, {"adres": "invalid"}) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
data = response.json() | ||
self.assertEqual( | ||
data["invalidParams"], | ||
[ | ||
{ | ||
"name": "adres", | ||
"code": "invalid", | ||
"reason": _("Voer een geldig e-mailadres in."), | ||
} | ||
], | ||
) | ||
|
||
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,36 @@ | ||
from uuid import uuid4 | ||
|
||
from django.conf import settings | ||
from django.test import override_settings | ||
from django.test import tag | ||
from django.urls import reverse | ||
from django.utils.translation import gettext as _ | ||
|
||
from django_webtest import WebTest | ||
from maykin_2fa.test import disable_admin_mfa | ||
from webtest import Form, TestResponse | ||
from webtest import TestResponse | ||
|
||
from openklant.accounts.tests.factories import SuperUserFactory | ||
from openklant.components.klantinteracties.models import DigitaalAdres | ||
from openklant.components.klantinteracties.models.tests.factories.digitaal_adres import ( | ||
DigitaalAdresFactory, | ||
) | ||
from openklant.components.klantinteracties.models.tests.factories.klantcontacten import ( | ||
BetrokkeneFactory, | ||
) | ||
from openklant.components.klantinteracties.models.tests.factories.partijen import ( | ||
PartijFactory, | ||
PersoonFactory, | ||
) | ||
from openklant.utils.tests.webtest import add_dynamic_field | ||
|
||
from ..constants import SoortDigitaalAdres | ||
|
||
class PartijAdminTests(WebTest): | ||
def _login_user(self, username: str, password: str) -> None: | ||
request = self.app.get(reverse("admin:login")) | ||
|
||
form: Form = request.forms["login-form"] | ||
form["auth-username"] = username | ||
form["auth-password"] = password | ||
redirect = form.submit() | ||
|
||
self.assertRedirects(redirect, reverse("admin:index")) | ||
|
||
@override_settings( | ||
MAYKIN_2FA_ALLOW_MFA_BYPASS_BACKENDS=settings.AUTHENTICATION_BACKENDS | ||
) | ||
@disable_admin_mfa() | ||
class PartijAdminTests(WebTest): | ||
def test_search(self): | ||
user = SuperUserFactory.create() | ||
self.app.set_user(user) | ||
|
||
nummer_persoon = PersoonFactory( | ||
partij__nummer="123456789", | ||
contactnaam_voornaam="Willem", | ||
|
@@ -56,10 +52,6 @@ def test_search(self): | |
contactnaam_achternaam="Willemse", | ||
) | ||
|
||
SuperUserFactory(username="admin", password="admin") | ||
|
||
self._login_user(username="admin", password="admin") | ||
|
||
admin_url = reverse("admin:klantinteracties_partij_changelist") | ||
|
||
# Test a nummer search query | ||
|
@@ -103,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) | ||
|
@@ -126,3 +118,50 @@ def test_digitaal_adres_inline(self): | |
self.assertEqual(adres.omschrijving, "description") | ||
self.assertEqual(adres.adres, "[email protected]") | ||
self.assertIsNone(adres.betrokkene) | ||
|
||
|
||
@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 | ||
`soort_digitaal_adres` is `email` | ||
""" | ||
user = SuperUserFactory.create() | ||
self.app.set_user(user) | ||
|
||
betrokkene = BetrokkeneFactory.create() | ||
|
||
admin_url = reverse("admin:klantinteracties_digitaaladres_add") | ||
|
||
with self.subTest("apply validation for soort_digitaal_adres == email"): | ||
response: TestResponse = self.app.get(admin_url) | ||
|
||
form = response.form | ||
form["betrokkene"] = betrokkene.pk | ||
form["soort_digitaal_adres"] = SoortDigitaalAdres.email | ||
form["adres"] = "invalid" | ||
form["omschrijving"] = "foo" | ||
|
||
response = form.submit() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertFalse(DigitaalAdres.objects.exists()) | ||
self.assertEqual( | ||
response.context["errors"], [[_("Voer een geldig e-mailadres in.")]] | ||
) | ||
|
||
with self.subTest("do not apply validation for soort_digitaal_adres != email"): | ||
response: TestResponse = self.app.get(admin_url) | ||
|
||
form = response.form | ||
form["betrokkene"] = betrokkene.pk | ||
form["soort_digitaal_adres"] = SoortDigitaalAdres.telefoonnummer | ||
form["adres"] = "0612345678" | ||
form["omschrijving"] = "foo" | ||
|
||
response = form.submit() | ||
|
||
self.assertEqual(response.status_code, 302) | ||
self.assertTrue(DigitaalAdres.objects.exists()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters