|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from django.urls import reverse |
| 3 | +from rest_framework import status |
| 4 | +from rest_framework.authtoken.models import Token |
| 5 | +from rest_framework.test import APIClient, APITestCase |
| 6 | + |
| 7 | +from momconnect.models import Clinic |
| 8 | + |
| 9 | + |
| 10 | +class ClinicDetailsViewTests(APITestCase): |
| 11 | + def setUp(self): |
| 12 | + self.api_client = APIClient() |
| 13 | + |
| 14 | + self.user = User.objects.create_user( |
| 15 | + "username", "[email protected]", "password" |
| 16 | + ) |
| 17 | + token = Token.objects.get(user=self.user) |
| 18 | + self.api_client.credentials(HTTP_AUTHORIZATION="Token " + token.key) |
| 19 | + |
| 20 | + self.clinic = Clinic.objects.create(value="123456", name="Test Clinic") |
| 21 | + self.url = reverse("clinic-check") |
| 22 | + |
| 23 | + def test_unauthenticated_client(self): |
| 24 | + """ |
| 25 | + Test that an unauthenticated client receives a 401 Unauthorized response |
| 26 | + when attempting to access the clinic details endpoint. |
| 27 | + """ |
| 28 | + response = self.client.get(self.url, {"clinic_code": self.clinic.value}) |
| 29 | + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 30 | + |
| 31 | + def test_missing_clinic_code(self): |
| 32 | + """ |
| 33 | + Test that the endpoint returns a 400 Bad Request response |
| 34 | + when the clinic_code parameter is missing. |
| 35 | + """ |
| 36 | + response = self.api_client.get(self.url) |
| 37 | + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 38 | + self.assertEqual(response.data, {"error": "missing clinic code"}) |
| 39 | + |
| 40 | + def test_invalid_clinic_code(self): |
| 41 | + """ |
| 42 | + Test that the endpoint returns a 400 Bad Request response |
| 43 | + when the clinic_code parameter is invalid. |
| 44 | + """ |
| 45 | + response = self.api_client.get(self.url, {"clinic_code": "invalid"}) |
| 46 | + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 47 | + self.assertEqual(response.data, {"error": "invalid"}) |
| 48 | + |
| 49 | + def test_clinic_not_found(self): |
| 50 | + """ |
| 51 | + Test that the endpoint returns a 404 Not Found response |
| 52 | + when the clinic with the provided clinic_code does not exist. |
| 53 | + """ |
| 54 | + response = self.api_client.get(self.url, {"clinic_code": "999999"}) |
| 55 | + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 56 | + self.assertEqual(response.data, {"error": "clinic not found"}) |
| 57 | + |
| 58 | + def test_successful_clinic_retrieval(self): |
| 59 | + """ |
| 60 | + Test that the endpoint successfully retrieves clinic details |
| 61 | + when a valid clinic_code is provided. |
| 62 | + """ |
| 63 | + response = self.api_client.get(self.url, {"clinic_code": self.clinic.value}) |
| 64 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 65 | + self.assertEqual(response.data["code"], self.clinic.value) |
| 66 | + self.assertEqual(response.data["name"], self.clinic.name) |
0 commit comments