Skip to content

Commit

Permalink
Changed blocked reservations field requirements
Browse files Browse the repository at this point in the history
Changes:
- staff are allowed to create type blocked reservations without filling required fields
  • Loading branch information
SanttuA committed Nov 14, 2023
1 parent 8828b63 commit 5649f56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions resources/api/reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 30 additions & 3 deletions resources/tests/test_reservation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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
Expand All @@ -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
assert ReservationReminder.objects.count() == 1

0 comments on commit 5649f56

Please sign in to comment.