Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛[#226] make digitalAdres betrokkene optional in admin #273

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.15 on 2024-10-22 13:43

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


class Migration(migrations.Migration):

dependencies = [
("klantinteracties", "0020_alter_digitaaladres_soort_digitaal_adres"),
]

operations = [
migrations.AlterField(
model_name="digitaaladres",
name="betrokkene",
field=models.ForeignKey(
blank=True,
help_text="'Digitaal Adres' had 'Betrokkene bij klantcontact'",
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="klantinteracties.betrokkene",
verbose_name="betrokkene",
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DigitaalAdres(APIMixin, models.Model):
on_delete=models.CASCADE,
verbose_name=_("betrokkene"),
help_text=_("'Digitaal Adres' had 'Betrokkene bij klantcontact'"),
blank=True,
null=True,
)
soort_digitaal_adres = models.CharField(
Expand Down
36 changes: 36 additions & 0 deletions src/openklant/components/klantinteracties/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
from django.urls import reverse

from django_webtest import WebTest
from maykin_2fa.test import disable_admin_mfa
from webtest import Form, 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.partijen import (
PartijFactory,
PersoonFactory,
)
from openklant.utils.tests.webtest import add_dynamic_field


class PartijAdminTests(WebTest):
Expand Down Expand Up @@ -90,3 +94,35 @@ def test_search(self):
self.assertContains(search_response, digitaal_adres_persoon.get_full_name())
self.assertNotContains(search_response, nummer_persoon.get_full_name())
self.assertNotContains(search_response, uuid_persoon.get_full_name())

@disable_admin_mfa()
def test_digitaal_adres_inline(self):
"""
Regression test for #226

betrokkene should be optional
"""

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

partij = PartijFactory(soort_partij="persoon", voorkeurs_digitaal_adres=None)
PersoonFactory(partij=partij)
url = reverse("admin:klantinteracties_partij_change", args=[partij.pk])

response = self.app.get(url)

form = response.form
form["digitaaladres_set-TOTAL_FORMS"] = 1
add_dynamic_field(form, "digitaaladres_set-0-omschrijving", "description")
add_dynamic_field(form, "digitaaladres_set-0-soort_digitaal_adres", "email")
add_dynamic_field(form, "digitaaladres_set-0-adres", "[email protected]")
Comment on lines +117 to +119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative to having to add these fields, you could maybe create a DigitaalAdres with a factory to ensure the form is there, and then try to save it again without a betrokkene, but either way is fine to me


response = form.submit()
self.assertEqual(response.status_code, 302)

adres = DigitaalAdres.objects.get()

self.assertEqual(adres.omschrijving, "description")
self.assertEqual(adres.adres, "[email protected]")
self.assertIsNone(adres.betrokkene)
10 changes: 10 additions & 0 deletions src/openklant/utils/tests/webtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from webtest import forms


def add_dynamic_field(form: forms.Form, name: str, value: str) -> None:
"""
Helper to add fields to a webtest form that is typically done in JS otherwise.
"""
field = forms.Text(form, "input", name, pos=999, value=value)
form.fields[name] = [field]
form.field_order.append((name, field))
Loading