|
2 | 2 | from django.urls import reverse |
3 | 3 | from pytest_django.asserts import assertRedirects |
4 | 4 |
|
| 5 | +from manage_breast_screening.core.tests.factories import UserFactory |
| 6 | +from manage_breast_screening.manual_images.tests.factories import StudyFactory |
5 | 7 | from manage_breast_screening.participants.models import AppointmentNote |
| 8 | +from manage_breast_screening.participants.models.appointment import ( |
| 9 | + AppointmentStatusNames, |
| 10 | +) |
6 | 11 | from manage_breast_screening.participants.tests.factories import AppointmentFactory |
7 | 12 |
|
8 | 13 |
|
@@ -101,6 +106,204 @@ def test_users_can_update_note(self, request, client_fixture): |
101 | 106 | assert AppointmentNote.objects.count() == 1 |
102 | 107 |
|
103 | 108 |
|
| 109 | +@pytest.mark.django_db |
| 110 | +class TestAppointmentNoteReviewView: |
| 111 | + def test_delete_link_not_shown_when_note_does_not_exist( |
| 112 | + self, clinical_user_client, clinical_user |
| 113 | + ): |
| 114 | + appointment = AppointmentFactory.create( |
| 115 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 116 | + current_status_params={ |
| 117 | + "name": AppointmentStatusNames.IN_PROGRESS, |
| 118 | + "created_by": clinical_user, |
| 119 | + }, |
| 120 | + ) |
| 121 | + response = clinical_user_client.http.get( |
| 122 | + reverse( |
| 123 | + "mammograms:appointment_note_review", |
| 124 | + kwargs={"pk": appointment.pk}, |
| 125 | + ) |
| 126 | + ) |
| 127 | + assert response.status_code == 200 |
| 128 | + assert "Appointment note" in response.content.decode() |
| 129 | + assert "app-status-bar" in response.content.decode() |
| 130 | + assert "Delete appointment note" not in response.content.decode() |
| 131 | + |
| 132 | + def test_delete_link_shown_when_note_exists(self, clinical_user_client): |
| 133 | + appointment = AppointmentFactory.create( |
| 134 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 135 | + current_status=AppointmentStatusNames.SCHEDULED, |
| 136 | + ) |
| 137 | + AppointmentNote.objects.create(appointment=appointment, content="Existing note") |
| 138 | + response = clinical_user_client.http.get( |
| 139 | + reverse( |
| 140 | + "mammograms:appointment_note_review", |
| 141 | + kwargs={"pk": appointment.pk}, |
| 142 | + ) |
| 143 | + ) |
| 144 | + assert response.status_code == 200 |
| 145 | + assert "Delete appointment note" in response.content.decode() |
| 146 | + |
| 147 | + def test_users_can_save_note(self, clinical_user_client, clinical_user): |
| 148 | + appointment = AppointmentFactory.create( |
| 149 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 150 | + current_status_params={ |
| 151 | + "name": AppointmentStatusNames.IN_PROGRESS, |
| 152 | + "created_by": clinical_user, |
| 153 | + }, |
| 154 | + ) |
| 155 | + StudyFactory.create(appointment=appointment) |
| 156 | + |
| 157 | + note_content = "Participant prefers left arm blood pressure readings." |
| 158 | + response = clinical_user_client.http.post( |
| 159 | + reverse( |
| 160 | + "mammograms:appointment_note_review", |
| 161 | + kwargs={"pk": appointment.pk}, |
| 162 | + ), |
| 163 | + {"content": note_content}, |
| 164 | + ) |
| 165 | + |
| 166 | + assertRedirects( |
| 167 | + response, |
| 168 | + reverse("mammograms:check_information", kwargs={"pk": appointment.pk}), |
| 169 | + ) |
| 170 | + saved_note = AppointmentNote.objects.get(appointment=appointment) |
| 171 | + assert saved_note.content == note_content |
| 172 | + |
| 173 | + def test_save_redirects_to_return_url(self, clinical_user_client, clinical_user): |
| 174 | + appointment = AppointmentFactory.create( |
| 175 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 176 | + current_status_params={ |
| 177 | + "name": AppointmentStatusNames.IN_PROGRESS, |
| 178 | + "created_by": clinical_user, |
| 179 | + }, |
| 180 | + ) |
| 181 | + check_info_url = reverse( |
| 182 | + "mammograms:check_information", kwargs={"pk": appointment.pk} |
| 183 | + ) |
| 184 | + response = clinical_user_client.http.post( |
| 185 | + reverse("mammograms:appointment_note_review", kwargs={"pk": appointment.pk}) |
| 186 | + + f"?return_url={check_info_url}", |
| 187 | + {"content": "Test note content"}, |
| 188 | + ) |
| 189 | + assertRedirects(response, check_info_url, fetch_redirect_response=False) |
| 190 | + |
| 191 | + def test_users_can_update_note(self, clinical_user_client, clinical_user): |
| 192 | + appointment = AppointmentFactory.create( |
| 193 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 194 | + current_status_params={ |
| 195 | + "name": AppointmentStatusNames.IN_PROGRESS, |
| 196 | + "created_by": clinical_user, |
| 197 | + }, |
| 198 | + ) |
| 199 | + StudyFactory.create(appointment=appointment) |
| 200 | + note = AppointmentNote.objects.create( |
| 201 | + appointment=appointment, content="Original note" |
| 202 | + ) |
| 203 | + |
| 204 | + updated_content = "Updated note content" |
| 205 | + response = clinical_user_client.http.post( |
| 206 | + reverse( |
| 207 | + "mammograms:appointment_note_review", kwargs={"pk": appointment.pk} |
| 208 | + ), |
| 209 | + {"content": updated_content}, |
| 210 | + ) |
| 211 | + |
| 212 | + assertRedirects( |
| 213 | + response, |
| 214 | + reverse("mammograms:check_information", kwargs={"pk": appointment.pk}), |
| 215 | + ) |
| 216 | + updated_note = AppointmentNote.objects.get(pk=note.pk) |
| 217 | + assert updated_note.content == updated_content |
| 218 | + assert AppointmentNote.objects.count() == 1 |
| 219 | + |
| 220 | + def test_access_denied_for_administrative_users( |
| 221 | + self, administrative_user_client, clinical_user |
| 222 | + ): |
| 223 | + appointment = AppointmentFactory.create( |
| 224 | + clinic_slot__clinic__setting__provider=administrative_user_client.current_provider, |
| 225 | + current_status_params={ |
| 226 | + "name": AppointmentStatusNames.IN_PROGRESS, |
| 227 | + "created_by": clinical_user, |
| 228 | + }, |
| 229 | + ) |
| 230 | + note = AppointmentNote.objects.create( |
| 231 | + appointment=appointment, content="Original note" |
| 232 | + ) |
| 233 | + |
| 234 | + response = administrative_user_client.http.post( |
| 235 | + reverse( |
| 236 | + "mammograms:appointment_note_review", kwargs={"pk": appointment.pk} |
| 237 | + ), |
| 238 | + {"content": "Updated note content"}, |
| 239 | + ) |
| 240 | + |
| 241 | + assert response.status_code == 403 |
| 242 | + updated_note = AppointmentNote.objects.get(pk=note.pk) |
| 243 | + assert updated_note.content == "Original note" |
| 244 | + assert AppointmentNote.objects.count() == 1 |
| 245 | + |
| 246 | + def test_access_denied_when_not_in_progress( |
| 247 | + self, clinical_user_client, clinical_user |
| 248 | + ): |
| 249 | + appointment = AppointmentFactory.create( |
| 250 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 251 | + current_status_params={ |
| 252 | + "name": AppointmentStatusNames.SCREENED, |
| 253 | + "created_by": UserFactory.create(), |
| 254 | + }, |
| 255 | + ) |
| 256 | + note = AppointmentNote.objects.create( |
| 257 | + appointment=appointment, content="Original note" |
| 258 | + ) |
| 259 | + StudyFactory.create(appointment=appointment) |
| 260 | + |
| 261 | + response = clinical_user_client.http.post( |
| 262 | + reverse( |
| 263 | + "mammograms:appointment_note_review", kwargs={"pk": appointment.pk} |
| 264 | + ), |
| 265 | + {"content": "Updated note content"}, |
| 266 | + ) |
| 267 | + assertRedirects( |
| 268 | + response, |
| 269 | + reverse("mammograms:show_appointment", kwargs={"pk": appointment.pk}), |
| 270 | + ) |
| 271 | + |
| 272 | + updated_note = AppointmentNote.objects.get(pk=note.pk) |
| 273 | + assert updated_note.content == "Original note" |
| 274 | + assert AppointmentNote.objects.count() == 1 |
| 275 | + |
| 276 | + def test_access_denied_when_in_progress_with_another_user( |
| 277 | + self, clinical_user_client, clinical_user |
| 278 | + ): |
| 279 | + appointment = AppointmentFactory.create( |
| 280 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider, |
| 281 | + current_status_params={ |
| 282 | + "name": AppointmentStatusNames.IN_PROGRESS, |
| 283 | + "created_by": UserFactory.create(), |
| 284 | + }, |
| 285 | + ) |
| 286 | + note = AppointmentNote.objects.create( |
| 287 | + appointment=appointment, content="Original note" |
| 288 | + ) |
| 289 | + StudyFactory.create(appointment=appointment) |
| 290 | + |
| 291 | + response = clinical_user_client.http.post( |
| 292 | + reverse( |
| 293 | + "mammograms:appointment_note_review", kwargs={"pk": appointment.pk} |
| 294 | + ), |
| 295 | + {"content": "Updated note content"}, |
| 296 | + ) |
| 297 | + assertRedirects( |
| 298 | + response, |
| 299 | + reverse("mammograms:check_information", kwargs={"pk": appointment.pk}), |
| 300 | + ) |
| 301 | + |
| 302 | + updated_note = AppointmentNote.objects.get(pk=note.pk) |
| 303 | + assert updated_note.content == "Updated note content" |
| 304 | + assert AppointmentNote.objects.count() == 1 |
| 305 | + |
| 306 | + |
104 | 307 | @pytest.mark.django_db |
105 | 308 | class TestDeleteAppointmentNoteView: |
106 | 309 | def test_get_redirects_when_note_does_not_exist(self, clinical_user_client): |
|
0 commit comments