diff --git a/frontend/src/interfaces/index.ts b/frontend/src/interfaces/index.ts index 558d8b5f69025d9fd9dde40028a9ac0faafda750..fe1309b2c195f83a21dbdee9dbc209db49237616 100644 --- a/frontend/src/interfaces/index.ts +++ b/frontend/src/interfaces/index.ts @@ -92,7 +92,7 @@ export type Consent = { id: string type: ConsentType choice: ConsentChoice - consent_given_at: Date + consent_given_at: Date | null } export interface User { diff --git a/frontend/src/routes/landing/index.test.tsx b/frontend/src/routes/landing/index.test.tsx new file mode 100644 index 0000000000000000000000000000000000000000..fcd5045da7221c754f433a24c907013fa72ae935 --- /dev/null +++ b/frontend/src/routes/landing/index.test.tsx @@ -0,0 +1,107 @@ +import { LocalizationProvider } from '@mui/lab' +import AdapterDateFns from '@mui/lab/AdapterDateFns' +import React from 'react' +import _ from 'lodash' +import GuestPage from './index' +import { UserContext } from '../../contexts' +import { render, screen, waitFor } from '../../test-utils' + +const userData = { + user: { + auth: true, + auth_type: 'oidc', + fetched: true, + first_name: 'Frank Foreleser', + last_name: 'Føllesen', + feide_id: 'frank_foreleser@spusers.feide.no', + person_id: '20622', + sponsor_id: '', + email: 'frank@example.org', + mobile_phone: '', + fnr: '190663*****', + passport: '', + roles: [ + { + id: '20319', + ou_nb: 'Det humanistiske fakultet, sekretariatet', + ou_en: 'The Faculty of Humanities, faculty administration', + name_nb: 'Konsulent', + name_en: 'Consultant', + start_date: '2022-03-24', + end_date: '2022-03-31', + max_days: 100, + contact_person_at_unit: 'None', + comments: 'Test', + }, + ], + consents: [ + { + id: '8656', + type: { name_en: 'Search', name_nb: 'Søk', name_nn: 'Søk' }, + choice: { text_en: 'Yes', text_nb: 'Yes', text_nn: 'Yes' }, + consent_given_at: '2022-03-24', + }, + ], + registration_completed_date: '2022-03-23T23:00:00.000Z', + }, + fetchUserInfo: () => {}, + clearUserInfo: () => {}, + getUserInfo: () => {}, +} + +test('Guest values showing', async () => { + render( + // @ts-ignore + <UserContext.Provider value={userData}> + <LocalizationProvider dateAdapter={AdapterDateFns}> + <GuestPage /> + </LocalizationProvider> + </UserContext.Provider> + ) + + await waitFor( + () => { + screen.findByDisplayValue( + `${userData.user.first_name} ${userData.user.last_name}` + ) + screen.findByDisplayValue(userData.user.feide_id) + screen.findByDisplayValue(userData.user.email) + screen.findByDisplayValue(userData.user.mobile_phone) + screen.findByDisplayValue(userData.user.roles[0].name_en) + screen.findByDisplayValue(userData.user.roles[0].ou_en) + screen.findByDisplayValue( + `${userData.user.roles[0].start_date} - ${userData.user.roles[0].end_date}` + ) + + screen.findByDisplayValue(userData.user.consents[0].type.name_en) + screen.findByDisplayValue(userData.user.consents[0].choice.text_en) + screen.findByDisplayValue(userData.user.consents[0].consent_given_at) + }, + { timeout: 5000 } + ) +}) + +test('Guest values showing when consent date is not set', async () => { + const userDataCopy = _.cloneDeep(userData) + // @ts-ignore + userDataCopy.user.consents[0].consent_given_at = null + + render( + // @ts-ignore + <UserContext.Provider value={userData}> + <LocalizationProvider dateAdapter={AdapterDateFns}> + <GuestPage /> + </LocalizationProvider> + </UserContext.Provider> + ) + + // Just check that the page is showing + await waitFor( + () => { + screen.findByDisplayValue( + `${userData.user.first_name} ${userData.user.last_name}` + ) + }, + { timeout: 5000 } + ) +}) diff --git a/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx b/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx index aca738605e837e47aa7089daf95608dfd257fce0..b95000f16a9012baaccc49051fd0b9c6c803420a 100644 --- a/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx +++ b/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx @@ -22,6 +22,7 @@ const guest: Guest = { active: true, registered: true, verified: true, + invitation_status: 'active', roles: [ { id: '200', diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 237976bcfb29d2db9c06a0a2f72aa41ee7bc7aed..d89b241898eac2e8c4bac4a11d4a9f73a6c1b251 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -158,7 +158,9 @@ export function parseConsent(cons: FetchedConsent): Consent { id: cons.id, type: cons.type, choice: cons.choice, - consent_given_at: parseISO(cons.consent_given_at), + consent_given_at: cons.consent_given_at + ? parseISO(cons.consent_given_at) + : null, } } diff --git a/greg/api/serializers/consent.py b/greg/api/serializers/consent.py index ef3dff43184eba72e70b78b56b7319407891528d..aab005eab2ea2984065fa93ce0d6140664da30fa 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 12cff657d6719d5e77520ea7a541190d1ab87262..6a574d27eec2778c7df2c4e25a718e881c4f4c5f 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 0000000000000000000000000000000000000000..e6a19465f7cd79038af671fb568b97f1874db5b7 --- /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) diff --git a/greg/tests/api/test_person.py b/greg/tests/api/test_person.py index 0d2b3200fcdd421568a6a1b30c461623dd10f347..9f5d148d1b43446a69e0e976f4719f23e56fd2e5 100644 --- a/greg/tests/api/test_person.py +++ b/greg/tests/api/test_person.py @@ -77,6 +77,7 @@ def test_get_person(client, person_foo, consent_type_foo): assert data.get("last_name") == person_foo.last_name assert data.get("consents") == [ { + "id": 1, "consent_given_at": None, "type": {"identifier": "foo", "mandatory": False}, "choice": "yes",