-
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 #269 from maykinmedia/feature/246-default-address
✨ [#246] Add is_standaard_adres for DigitaalAdres
- Loading branch information
Showing
5 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from vng_api_common.tests import reverse | ||
|
||
from openklant.components.klantinteracties.constants import SoortDigitaalAdres | ||
from openklant.components.klantinteracties.models import DigitaalAdres | ||
from openklant.components.klantinteracties.models.tests.factories.digitaal_adres import ( | ||
DigitaalAdresFactory, | ||
) | ||
|
@@ -73,6 +74,7 @@ def test_create_digitaal_adres(self): | |
self.assertEqual(data["verstrektDoorPartij"], None) | ||
self.assertEqual(data["adres"], "[email protected]") | ||
self.assertEqual(data["omschrijving"], "omschrijving") | ||
self.assertEqual(data["isStandaardAdres"], False) | ||
|
||
with self.subTest("with_betrokkene_and_partij"): | ||
partij = PartijFactory.create() | ||
|
@@ -160,6 +162,82 @@ def test_create_digitaal_adres_email_validation(self): | |
) | ||
self.assertEqual(digitaal_adres.adres, "0612345678") | ||
|
||
def test_create_digitaal_adres_is_standaard_adres(self): | ||
""" | ||
Creating a DigitaalAdres with isStandaardAdres=True should make other existing | ||
DigitaalAdressen no longer the default | ||
""" | ||
# Since this has a different Partij, the value of `is_standaard_adres` should stay `True` | ||
partij1, partij2 = PartijFactory.create_batch(2) | ||
existing_adres_different_partij = DigitaalAdresFactory.create( | ||
partij=partij1, is_standaard_adres=True, soort_digitaal_adres="email" | ||
) | ||
# This adres has the same `soort_digitaal_adres` and `partij`, so the value of | ||
# `is_standaard_adres` should be changed to `False` if we change another one to `True` | ||
existing_adres = DigitaalAdresFactory.create( | ||
is_standaard_adres=True, soort_digitaal_adres="email", partij=partij2 | ||
) | ||
|
||
list_url = reverse("klantinteracties:digitaaladres-list") | ||
data = { | ||
"verstrektDoorBetrokkene": None, | ||
"verstrektDoorPartij": {"uuid": str(partij2.uuid)}, | ||
"soortDigitaalAdres": "email", | ||
"adres": "[email protected]", | ||
"omschrijving": "omschrijving", | ||
"isStandaardAdres": True, | ||
} | ||
|
||
response = self.client.post(list_url, data) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
data = response.json() | ||
|
||
self.assertEqual(data["isStandaardAdres"], True) | ||
|
||
existing_adres_different_partij.refresh_from_db() | ||
existing_adres.refresh_from_db() | ||
new_adres = DigitaalAdres.objects.last() | ||
|
||
self.assertEqual(existing_adres_different_partij.is_standaard_adres, True) | ||
self.assertEqual(existing_adres.is_standaard_adres, False) | ||
self.assertEqual(new_adres.is_standaard_adres, True) | ||
|
||
def test_create_digitaal_adres_is_standaard_adres_without_partij_not_possible(self): | ||
""" | ||
Creating a DigitaalAdres with isStandaardAdres=True should not be possible with | ||
verstrektDoorPartij=None | ||
""" | ||
betrokkene = BetrokkeneFactory.create() | ||
|
||
list_url = reverse("klantinteracties:digitaaladres-list") | ||
data = { | ||
"verstrektDoorBetrokkene": {"uuid": str(betrokkene.uuid)}, | ||
"verstrektDoorPartij": None, | ||
"soortDigitaalAdres": "email", | ||
"adres": "[email protected]", | ||
"omschrijving": "omschrijving", | ||
"isStandaardAdres": True, | ||
} | ||
|
||
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": "isStandaardAdres", | ||
"code": "invalid", | ||
"reason": _( | ||
"`is_standaard_adres` kan alleen gezet worden als `verstrekt_door_partij` niet leeg is." | ||
), | ||
} | ||
], | ||
) | ||
self.assertEqual(DigitaalAdres.objects.count(), 0) | ||
|
||
def test_update_digitaal_adres(self): | ||
betrokkene, betrokkene2 = BetrokkeneFactory.create_batch(2) | ||
partij, partij2 = PartijFactory.create_batch(2) | ||
|
@@ -226,6 +304,55 @@ def test_update_digitaal_adres(self): | |
self.assertEqual(data["adres"], "0721434543") | ||
self.assertEqual(data["omschrijving"], "changed") | ||
|
||
def test_update_digitaal_adres_is_standaard_adres(self): | ||
""" | ||
Creating a DigitaalAdres with isStandaardAdres=True should make other existing | ||
DigitaalAdressen no longer the default | ||
""" | ||
partij1, partij2 = PartijFactory.create_batch(2) | ||
# Since this has a different Partij, the value of `is_standaard_adres` should stay `True` | ||
existing_adres_different_partij = DigitaalAdresFactory.create( | ||
partij=partij1, is_standaard_adres=True, soort_digitaal_adres="email" | ||
) | ||
# This adres has the same `soort_digitaal_adres` and `partij`, so the value of | ||
# `is_standaard_adres` should be changed to `False` if we change another one to `True` | ||
existing_adres = DigitaalAdresFactory.create( | ||
is_standaard_adres=True, soort_digitaal_adres="email", partij=partij2 | ||
) | ||
digitaal_adres = DigitaalAdresFactory.create( | ||
partij=partij2, | ||
soort_digitaal_adres="email", | ||
adres="adres", | ||
omschrijving="omschrijving", | ||
) | ||
detail_url = reverse( | ||
"klantinteracties:digitaaladres-detail", | ||
kwargs={"uuid": str(digitaal_adres.uuid)}, | ||
) | ||
|
||
data = { | ||
"verstrektDoorBetrokkene": {"uuid": str(digitaal_adres.betrokkene.uuid)}, | ||
"verstrektDoorPartij": {"uuid": str(partij2.uuid)}, | ||
"soortDigitaalAdres": "email", | ||
"isStandaardAdres": True, | ||
"adres": "[email protected]", | ||
"omschrijving": "changed", | ||
} | ||
|
||
response = self.client.put(detail_url, data) | ||
|
||
data = response.json() | ||
|
||
self.assertEqual(data["isStandaardAdres"], True) | ||
|
||
existing_adres_different_partij.refresh_from_db() | ||
existing_adres.refresh_from_db() | ||
digitaal_adres.refresh_from_db() | ||
|
||
self.assertEqual(existing_adres_different_partij.is_standaard_adres, True) | ||
self.assertEqual(existing_adres.is_standaard_adres, False) | ||
self.assertEqual(digitaal_adres.is_standaard_adres, True) | ||
|
||
def test_partial_update_digitaal_adres(self): | ||
betrokkene = BetrokkeneFactory.create() | ||
partij = PartijFactory.create() | ||
|
30 changes: 30 additions & 0 deletions
30
.../components/klantinteracties/migrations/0022_digitaaladres_is_standaard_adres_and_more.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Generated by Django 4.2.15 on 2024-10-31 08:27 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("klantinteracties", "0021_alter_digitaaladres_betrokkene"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="digitaaladres", | ||
name="is_standaard_adres", | ||
field=models.BooleanField( | ||
default=False, | ||
help_text="Geeft aan of dit digitaal adres het standaard adres is voor het `soort_digitaal_adres`", | ||
verbose_name="Is standaard adres", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="digitaaladres", | ||
constraint=models.UniqueConstraint( | ||
condition=models.Q(("is_standaard_adres", True)), | ||
fields=("partij", "soort_digitaal_adres"), | ||
name="unique_default_per_partij_and_soort", | ||
), | ||
), | ||
] |
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