@@ -38,6 +38,17 @@ def setUp(self):
38
38
middleware = MessageMiddleware (lambda req : None )
39
39
middleware .process_request (self .request )
40
40
self .request .session .save ()
41
+ self .appointment = self .create_appointment_for_user1 ()
42
+
43
+ def need_staff_login (self ):
44
+ self .user1 .is_staff = True
45
+ self .user1 .save ()
46
+ self .client .force_login (self .user1 )
47
+
48
+ def clean_staff_member_objects (self ):
49
+ """Delete all AppointmentRequests and Appointments linked to the StaffMember instance of self.user1."""
50
+ AppointmentRequest .objects .filter (staff_member__user = self .user1 ).delete ()
51
+ Appointment .objects .filter (appointment_request__staff_member__user = self .user1 ).delete ()
41
52
42
53
def test_get_available_slots_ajax (self ):
43
54
"""get_available_slots_ajax view should return a JSON response with available slots for the selected date."""
@@ -142,17 +153,13 @@ def test_default_thank_you(self):
142
153
143
154
def test_staff_user_without_staff_member_instance (self ):
144
155
"""Test that a staff user without a staff member instance receives an appropriate error message."""
145
- self .user1 .is_staff = True
146
-
147
- # Delete any AppointmentRequests and Appointments linked to the StaffMember instance of self.user1
148
- AppointmentRequest .objects .filter (staff_member__user = self .user1 ).delete ()
149
- Appointment .objects .filter (appointment_request__staff_member__user = self .user1 ).delete ()
156
+ self .clean_staff_member_objects ()
150
157
151
158
# Now safely delete the StaffMember instance
152
159
StaffMember .objects .filter (user = self .user1 ).delete ()
153
160
154
161
self .user1 .save () # Save the user to the database after updating
155
- self .client . force_login ( self . user1 ) # Log in as self.user1
162
+ self .need_staff_login ()
156
163
157
164
url = reverse ('appointment:get_user_appointments' )
158
165
response = self .client .get (url )
@@ -161,5 +168,55 @@ def test_staff_user_without_staff_member_instance(self):
161
168
self .assertTrue (any (
162
169
message .message == "User doesn't have a staff member instance. Please contact the administrator." for
163
170
message in message_list ),
164
- "Expected error message not found in messages." )
171
+ "Expected error message not found in messages." )
172
+
173
+ def test_delete_appointment (self ):
174
+ self .need_staff_login ()
175
+
176
+ url = reverse ('appointment:delete_appointment' , args = [self .appointment .id ])
177
+ response = self .client .get (url )
178
+
179
+ self .assertEqual (response .status_code , 302 ) # Redirect status code
180
+ self .assertRedirects (response , reverse ('appointment:get_user_appointments' ))
181
+
182
+ # Check for success messages
183
+ messages_list = list (get_messages (response .wsgi_request ))
184
+ self .assertTrue (any (_ ("Appointment deleted successfully!" ) in str (message ) for message in messages_list ))
185
+
186
+ # Check if appointment is deleted
187
+ appointment_exists = Appointment .objects .filter (pk = self .appointment .id ).exists ()
188
+ self .assertFalse (appointment_exists , "Appointment should be deleted but still exists." )
189
+
190
+ def test_delete_appointment_ajax (self ):
191
+ self .need_staff_login ()
192
+
193
+ url = reverse ('appointment:delete_appointment_ajax' )
194
+ data = json .dumps ({'appointment_id' : self .appointment .id })
195
+ response = self .client .post (url , data , content_type = 'application/json' )
196
+
197
+ self .assertEqual (response .status_code , 200 )
198
+ # Expecting both 'message' and 'success' in the response
199
+ expected_response = {"message" : "Appointment deleted successfully." , "success" : True }
200
+ self .assertEqual (json .loads (response .content ), expected_response )
201
+
202
+ # Check if appointment is deleted
203
+ appointment_exists = Appointment .objects .filter (pk = self .appointment .id ).exists ()
204
+ self .assertFalse (appointment_exists , "Appointment should be deleted but still exists." )
205
+
206
+ def test_remove_staff_member (self ):
207
+ self .need_staff_login ()
208
+ self .clean_staff_member_objects ()
209
+
210
+ url = reverse ('appointment:remove_staff_member' , args = [self .staff_member .user_id ])
211
+ response = self .client .get (url )
212
+
213
+ self .assertEqual (response .status_code , 302 ) # Redirect status code
214
+ self .assertRedirects (response , reverse ('appointment:user_profile' ))
215
+
216
+ # Check for success messages
217
+ messages_list = list (get_messages (response .wsgi_request ))
218
+ self .assertTrue (any (_ ("Staff member deleted successfully!" ) in str (message ) for message in messages_list ))
165
219
220
+ # Check if staff member is deleted
221
+ staff_member_exists = StaffMember .objects .filter (pk = self .staff_member .id ).exists ()
222
+ self .assertFalse (staff_member_exists , "Appointment should be deleted but still exists." )
0 commit comments