Skip to content
Snippets Groups Projects
Commit 82070cd2 authored by Tore.Brede's avatar Tore.Brede
Browse files

Merge branch 'GREG-243_fix_consent_api_issues' into 'master'

GREG-243: Adding possibility to set date in consent endpoint. Showing ID for...

See merge request !307
parents d53ccafe a85848d9
No related branches found
No related tags found
1 merge request!307GREG-243: Adding possibility to set date in consent endpoint. Showing ID for...
Pipeline #124550 passed
......@@ -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 {
......
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 }
)
})
......@@ -22,6 +22,7 @@ const guest: Guest = {
active: true,
registered: true,
verified: true,
invitation_status: 'active',
roles: [
{
id: '200',
......
......@@ -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,
}
}
......
......@@ -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"]
......@@ -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"]
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)
......@@ -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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment