From 5649f56260af4d0cd4618b6a7b4eb34b805e5629 Mon Sep 17 00:00:00 2001 From: SanttuA Date: Tue, 14 Nov 2023 15:29:37 +0200 Subject: [PATCH] Changed blocked reservations field requirements Changes: - staff are allowed to create type blocked reservations without filling required fields --- resources/api/reservation.py | 5 ++++ resources/tests/test_reservation_api.py | 33 ++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/resources/api/reservation.py b/resources/api/reservation.py index 5dcc011ef..21a740d9b 100644 --- a/resources/api/reservation.py +++ b/resources/api/reservation.py @@ -201,6 +201,11 @@ def __init__(self, *args, **kwargs): if is_staff_event and resource.can_create_staff_event(request.user): required = {'reserver_name', 'event_description'} + # reservations of type blocked don't require any fields + is_blocked_type = data.get('type') == Reservation.TYPE_BLOCKED + if is_blocked_type and resource.can_create_special_type_reservation(request.user): + required = [] + # we don't need to remove a field here if it isn't supported, as it will be read-only and will be more # easily removed in to_representation() for field_name in supported: diff --git a/resources/tests/test_reservation_api.py b/resources/tests/test_reservation_api.py index e75f9be65..787e38e5a 100644 --- a/resources/tests/test_reservation_api.py +++ b/resources/tests/test_reservation_api.py @@ -16,7 +16,7 @@ from resources.enums import UnitAuthorizationLevel from resources.models import ( - Period, Day, Reservation, + Period, Day, Reservation, Resource, ResourceGroup, ReservationMetadataField, ReservationMetadataSet, UnitAuthorization, ReservationReminder ) @@ -2502,6 +2502,33 @@ def test_reservation_block_type_manager(resource_in_unit, reservation_data, api_ assert reservation_obj.type == Reservation.TYPE_BLOCKED +@pytest.mark.django_db +def test_reservation_block_type_no_required_fields(resource_in_unit, reservation_data, api_client, unit_manager_user): + """ + It should be possible to create blocked type reservations without filling in any required fields + when the user has permission to create blocked reservations + """ + field_1 = ReservationMetadataField.objects.get(field_name='reserver_name') + field_2 = ReservationMetadataField.objects.get(field_name='reserver_phone_number') + field_3 = ReservationMetadataField.objects.get(field_name='reserver_email_address') + metadata_set = ReservationMetadataSet.objects.create( + name='updated_metadata', + ) + metadata_set.supported_fields.set([field_1, field_2, field_3]) + metadata_set.required_fields.set([field_1, field_2, field_3]) + resource_in_unit.reservation_metadata_set = ReservationMetadataSet.objects.get(name='updated_metadata') + resource_in_unit.save() + + api_client.force_authenticate(unit_manager_user) + list_url = reverse('reservation-list') + reservation_data['type'] = Reservation.TYPE_BLOCKED + response = api_client.post(list_url, data=reservation_data) + assert response.status_code == 201 + assert response.data['type'] == Reservation.TYPE_BLOCKED + reservation_obj = Reservation.objects.get(id=response.data['id']) + assert reservation_obj.type == Reservation.TYPE_BLOCKED + + @pytest.mark.django_db def test_reservation_cannot_add_bogus_type(resource_in_unit, reservation_data, api_client, unit_manager_user): """ User should not be able to add a non-supported type to reservation """ @@ -3277,7 +3304,7 @@ def test_reservation_not_allowed_during_maintenance_mode( @pytest.mark.django_db def test_reservation_reminder_create( - api_client, user, list_url, reservation_data, + api_client, user, list_url, reservation_data, resource_with_reservation_reminders): api_client.force_authenticate(user=user) reservation_data['resource'] = resource_with_reservation_reminders.pk @@ -3286,4 +3313,4 @@ def test_reservation_reminder_create( reservation_data['reserver_phone_number'] = '+358404040404' response = api_client.post(list_url, data=reservation_data, HTTP_ACCEPT_LANGUAGE='en') assert response.status_code == 201 - assert ReservationReminder.objects.count() == 1 \ No newline at end of file + assert ReservationReminder.objects.count() == 1