Skip to content

Commit

Permalink
fix(backend/attendance): validate that attendance ends after it starts
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Aug 13, 2024
1 parent 0454107 commit 6c21fec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/timed/tracking/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ class AttendanceSerializer(ModelSerializer):
"user": "timed.employment.serializers.UserSerializer"
}

def validate(self, data):
"""Validate the attendance.
Ensure that attendances end after they start.
"""
instance = self.instance
from_time = data.get("from_time", instance and instance.from_time)
to_time = data.get("to_time", instance and instance.to_time)

# validate that to is not before from
if to_time is not None and to_time < from_time:
raise ValidationError(_("An attendance may not end before it starts."))

return data

class Meta:
"""Meta information for the attendance serializer."""

Expand Down
27 changes: 27 additions & 0 deletions backend/timed/tracking/tests/test_attendance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import time

import pytest
from django.urls import reverse
from rest_framework import status
Expand Down Expand Up @@ -100,3 +102,28 @@ def test_attendance_delete(internal_employee_client):

response = internal_employee_client.delete(url)
assert response.status_code == status.HTTP_204_NO_CONTENT


def test_attendance_to_before_from(internal_employee_client, attendance_factory):
"""Test that to is not before from."""
attendance = attendance_factory(
user=internal_employee_client.user, from_time=time(7, 30), to_time=time(8, 30)
)

data = {
"data": {
"type": "attendances",
"id": attendance.id,
"attributes": {"to-time": "07:00"},
}
}

url = reverse("attendance-detail", args=[attendance.id])

res = internal_employee_client.patch(url, data)

assert res.status_code == status.HTTP_400_BAD_REQUEST
json = res.json()
assert json["errors"][0]["detail"] == (
"An attendance may not end before it starts."
)

0 comments on commit 6c21fec

Please sign in to comment.