Skip to content

Commit

Permalink
✨ [#246] Only allow is_standaard_adres if partij is set
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Nov 21, 2024
1 parent 1360921 commit 8076b6a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ def validate_adres(self, adres):
OptionalEmailValidator()(adres, soort_digitaal_adres)
return adres

def validate(self, attrs):
partij = get_field_value(self, attrs, "partij")
is_standaard_adres = get_field_value(self, attrs, "is_standaard_adres")
if is_standaard_adres and not partij:
raise serializers.ValidationError(
{
"is_standaard_adres": _(
"`is_standaard_adres` kan alleen gezet worden "
"als `verstrekt_door_partij` niet leeg is."
)
}
)

return super().validate(attrs)

@transaction.atomic
def update(self, instance, validated_data):
if "partij" in validated_data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,41 @@ def test_create_digitaal_adres_is_standaard_adres(self):
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)
Expand Down

0 comments on commit 8076b6a

Please sign in to comment.