Skip to content

Commit a1cd1d5

Browse files
committed
Added tests
1 parent aa08882 commit a1cd1d5

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

appointment/tests/test_views.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,41 @@ def test_delete_appointment_ajax(self):
224224
appointment_exists = Appointment.objects.filter(pk=self.appointment.id).exists()
225225
self.assertFalse(appointment_exists, "Appointment should be deleted but still exists.")
226226

227+
def test_delete_appointment_without_permission(self):
228+
"""Test that deleting an appointment without permission fails."""
229+
self.need_staff_login() # Login as a regular staff user
230+
231+
# Try to delete an appointment belonging to a different staff member
232+
different_appointment = self.create_appointment_for_user2()
233+
url = reverse('appointment:delete_appointment', args=[different_appointment.id])
234+
235+
response = self.client.post(url)
236+
237+
# Check that the user is redirected due to lack of permissions
238+
self.assertEqual(response.status_code, 403)
239+
240+
# Verify that the appointment still exists in the database
241+
self.assertTrue(Appointment.objects.filter(id=different_appointment.id).exists())
242+
243+
def test_delete_appointment_ajax_without_permission(self):
244+
"""Test that deleting an appointment via AJAX without permission fails."""
245+
self.need_staff_login() # Login as a regular staff user
246+
247+
# Try to delete an appointment belonging to a different staff member
248+
different_appointment = self.create_appointment_for_user2()
249+
url = reverse('appointment:delete_appointment_ajax')
250+
251+
response = self.client.post(url, {'appointment_id': different_appointment.id}, content_type='application/json')
252+
253+
# Check that the response indicates failure due to lack of permissions
254+
self.assertEqual(response.status_code, 403)
255+
response_data = response.json()
256+
self.assertEqual(response_data['message'], _("You can only delete your own appointments."))
257+
self.assertFalse(response_data['success'])
258+
259+
# Verify that the appointment still exists in the database
260+
self.assertTrue(Appointment.objects.filter(id=different_appointment.id).exists())
261+
227262
def test_remove_staff_member(self):
228263
self.need_superuser_login()
229264
self.clean_staff_member_objects()

appointment/utils/permissions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ def check_permissions(staff_user_id, user):
2727
if (staff_user_id and int(staff_user_id) == user.pk) or user.is_superuser:
2828
return True
2929
return False
30+
31+
32+
def has_permission_to_delete_appointment(user, appointment):
33+
"""
34+
Check if the user has permission to delete the given appointment.
35+
Returns True if the user has permission, False otherwise.
36+
"""
37+
return check_extensive_permissions(appointment.get_staff_member().user_id, user, appointment)

appointment/views_admin.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from appointment.utils.json_context import (
3434
convert_appointment_to_json, get_generic_context, get_generic_context_with_extra, handle_unauthorized_response,
3535
json_response)
36-
from appointment.utils.permissions import check_extensive_permissions, check_permissions
36+
from appointment.utils.permissions import check_extensive_permissions, check_permissions, \
37+
has_permission_to_delete_appointment
3738

3839

3940
###############################################################
@@ -454,6 +455,8 @@ def delete_service(request, service_id):
454455

455456
###############################################################
456457
# Remove staff member
458+
@require_user_authenticated
459+
@require_superuser
457460
def remove_staff_member(request, staff_user_id):
458461
staff_member = get_object_or_404(StaffMember, user_id=staff_user_id)
459462
staff_member.delete()
@@ -491,7 +494,7 @@ def get_service_list(request, response_type='html'):
491494
@require_staff_or_superuser
492495
def delete_appointment(request, appointment_id):
493496
appointment = get_object_or_404(Appointment, pk=appointment_id)
494-
if not check_extensive_permissions(appointment.get_staff_member().user_id, request.user, appointment):
497+
if not has_permission_to_delete_appointment(request.user, appointment):
495498
message = _("You can only delete your own appointments.")
496499
return handle_unauthorized_response(request, message, 'html')
497500
appointment.delete()
@@ -505,7 +508,7 @@ def delete_appointment_ajax(request):
505508
data = json.loads(request.body)
506509
appointment_id = data.get("appointment_id")
507510
appointment = get_object_or_404(Appointment, pk=appointment_id)
508-
if not check_extensive_permissions(appointment.get_staff_member().user_id, request.user, appointment):
511+
if not has_permission_to_delete_appointment(request.user, appointment):
509512
message = _("You can only delete your own appointments.")
510513
return json_response(message, status=403, success=False, error_code=ErrorCode.NOT_AUTHORIZED)
511514
appointment.delete()

0 commit comments

Comments
 (0)