From ab1f99863b4602762706a95a1b80578d963c2052 Mon Sep 17 00:00:00 2001
From: Tore Brede <Tore.Brede@uib.no>
Date: Thu, 24 Mar 2022 11:24:34 +0100
Subject: [PATCH] GREG-243: Adding possibility to set date in consent endpoint.
 Showing ID for person-consent connection in endpoints

---
 greg/api/serializers/consent.py |  2 +-
 greg/api/views/person.py        |  3 +-
 greg/tests/api/test_consent.py  | 96 +++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 greg/tests/api/test_consent.py

diff --git a/greg/api/serializers/consent.py b/greg/api/serializers/consent.py
index ef3dff43..aab005ea 100644
--- a/greg/api/serializers/consent.py
+++ b/greg/api/serializers/consent.py
@@ -11,4 +11,4 @@ class ConsentSerializerBrief(serializers.ModelSerializer):
     class Meta:
         model = Consent
         queryset = Consent.objects.all().select_related("choice")
-        fields = ["type", "consent_given_at", "choice"]
+        fields = ["id", "type", "consent_given_at", "choice"]
diff --git a/greg/api/views/person.py b/greg/api/views/person.py
index 12cff657..6a574d27 100644
--- a/greg/api/views/person.py
+++ b/greg/api/views/person.py
@@ -238,4 +238,5 @@ class PersonConsentSerializer(serializers.ModelSerializer):
     # Not much logic defined here, the validation is done implicitly in the view
     class Meta:
         model = Consent
-        fields = ["person", "type", "choice"]
+        read_only_fields = ["id"]
+        fields = ["id", "person", "type", "choice", "consent_given_at"]
diff --git a/greg/tests/api/test_consent.py b/greg/tests/api/test_consent.py
new file mode 100644
index 00000000..e6a19465
--- /dev/null
+++ b/greg/tests/api/test_consent.py
@@ -0,0 +1,96 @@
+import datetime
+
+import pytest
+from rest_framework import status
+from rest_framework.reverse import reverse
+
+from greg.models import ConsentType
+
+
+@pytest.mark.django_db
+def test_get_consent_for_person_through_person_endpoint(
+    client, person, consent_type_foo, consent_fixture_choice_yes
+):
+    response = client.get(reverse("v1:person-detail", kwargs={"id": person.id}))
+
+    assert response.status_code == status.HTTP_200_OK
+    response_body = response.json()
+    consents = response_body["consents"]
+
+    assert len(consents) == 1
+    # Check that the ID shows up in the consent list from the person endpoint as well
+    assert consents == [
+        {
+            "id": 1,
+            "type": {"identifier": "foo", "mandatory": False},
+            "consent_given_at": "2021-06-20",
+            "choice": "yes",
+        }
+    ]
+
+
+@pytest.mark.django_db
+def test_get_consent_for_person(
+    client, person, consent_type_foo, consent_fixture_choice_yes
+):
+    response = client.get(
+        reverse("v1:person_consent-list", kwargs={"person_id": person.id})
+    )
+
+    assert response.status_code == status.HTTP_200_OK
+    response_body = response.json()["results"]
+
+    assert response_body == [
+        {
+            "id": 1,
+            "type": {"identifier": "foo", "mandatory": False},
+            "consent_given_at": "2021-06-20",
+            "choice": "yes",
+        }
+    ]
+
+
+@pytest.mark.django_db
+def test_add_consent_to_person(client, person, consent_type_foo: ConsentType):
+    data = {
+        "type": consent_type_foo.identifier,
+        "choice": "yes",
+        "consent_given_at": "2021-02-25",
+    }
+
+    assert person.consents.count() == 0
+    response = client.post(
+        reverse("v1:person_consent-list", kwargs={"person_id": person.id}), data=data
+    )
+    assert response.status_code == status.HTTP_201_CREATED
+
+    consent = person.consents.get()
+    assert consent.type == consent_type_foo
+    assert consent.choice == consent_type_foo.choices.get(value="yes")
+    assert consent.consent_given_at == datetime.date(2021, 2, 25)
+
+
+@pytest.mark.django_db
+def test_patch_consent(client, person, consent_type_foo, consent_fixture_choice_yes):
+    data = {
+        "type": consent_type_foo.identifier,
+        "choice": "no",
+        "consent_given_at": "2021-02-26",
+    }
+
+    consent = person.consents.get()
+    response = client.patch(
+        reverse(
+            "v1:person_consent-detail",
+            kwargs={"person_id": person.id, "id": consent.id},
+        ),
+        data=data,
+    )
+
+    assert response.status_code == status.HTTP_200_OK
+
+    # Reload data from database and check that it has been updated
+    consent.refresh_from_db()
+    assert consent.type == consent_type_foo
+    assert consent.choice == consent_type_foo.choices.get(value="no")
+    assert consent.consent_given_at == datetime.date(2021, 2, 26)
-- 
GitLab