|
| 1 | +# docker compose -f local.yml run --rm django pytest feedback/tests.py |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.urls import reverse |
| 5 | +from rest_framework import status |
| 6 | +from rest_framework.test import APIClient |
| 7 | + |
| 8 | +from feedback.models import ContentCurationRequest, Feedback, FeedbackFormDropdown |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def api_client(): |
| 13 | + return APIClient() |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def dropdown_option(db): |
| 18 | + return FeedbackFormDropdown.objects.create(name="I need help or have a general question", display_order=1) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def feedback_data(dropdown_option): |
| 23 | + return { |
| 24 | + "name": "Test User", |
| 25 | + |
| 26 | + "subject": "Test Subject", |
| 27 | + "comments": "Test Comments", |
| 28 | + "source": "TEST", |
| 29 | + "dropdown_option": dropdown_option.id, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def content_curation_data(): |
| 35 | + return { |
| 36 | + "name": "Test User", |
| 37 | + |
| 38 | + "scientific_focus": "Biology", |
| 39 | + "data_type": "Genomics", |
| 40 | + "data_link": "https://example.com/data", |
| 41 | + "additional_info": "Extra details", |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.django_db |
| 46 | +class TestFeedbackFormDropdown: |
| 47 | + def test_dropdown_str_representation(self, dropdown_option): |
| 48 | + """Test string representation of dropdown options""" |
| 49 | + assert str(dropdown_option) == "I need help or have a general question" |
| 50 | + |
| 51 | + def test_dropdown_ordering(self): |
| 52 | + """Test that dropdown options are ordered by display_order""" |
| 53 | + dropdown1 = FeedbackFormDropdown.objects.create(name="First Option", display_order=1) |
| 54 | + dropdown2 = FeedbackFormDropdown.objects.create(name="Second Option", display_order=2) |
| 55 | + dropdowns = FeedbackFormDropdown.objects.all() |
| 56 | + assert dropdowns[0] == dropdown1 |
| 57 | + assert dropdowns[1] == dropdown2 |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.django_db |
| 61 | +class TestFeedbackAPI: |
| 62 | + def test_get_dropdown_options(self, api_client, dropdown_option): |
| 63 | + """Test retrieving dropdown options""" |
| 64 | + url = reverse("feedback:feedback-form-dropdown-options-api") |
| 65 | + response = api_client.get(url) |
| 66 | + assert response.status_code == status.HTTP_200_OK |
| 67 | + assert len(response.data["results"]) == 1 |
| 68 | + assert response.data["results"][0]["name"] == dropdown_option.name |
| 69 | + |
| 70 | + def test_create_feedback_success(self, api_client, feedback_data): |
| 71 | + """Test successful feedback creation""" |
| 72 | + url = reverse("feedback:contact-us-api") |
| 73 | + response = api_client.post(url, feedback_data, format="json") |
| 74 | + assert response.status_code == status.HTTP_201_CREATED |
| 75 | + assert Feedback.objects.count() == 1 |
| 76 | + |
| 77 | + def test_create_feedback_invalid_email(self, api_client, feedback_data): |
| 78 | + """Test feedback creation with invalid email""" |
| 79 | + url = reverse("feedback:contact-us-api") |
| 80 | + feedback_data["email"] = "invalid-email" |
| 81 | + response = api_client.post(url, feedback_data, format="json") |
| 82 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 83 | + assert "email" in response.data["error"] |
| 84 | + |
| 85 | + @pytest.mark.parametrize("field", ["name", "email", "subject", "comments"]) |
| 86 | + def test_create_feedback_missing_required_fields(self, api_client, feedback_data, field): |
| 87 | + """Test feedback creation with missing required fields""" |
| 88 | + url = reverse("feedback:contact-us-api") |
| 89 | + feedback_data.pop(field) |
| 90 | + response = api_client.post(url, feedback_data, format="json") |
| 91 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 92 | + assert field in response.data["error"] |
| 93 | + |
| 94 | + def test_create_feedback_invalid_dropdown(self, api_client, feedback_data): |
| 95 | + """Test feedback creation with non-existent dropdown option""" |
| 96 | + url = reverse("feedback:contact-us-api") |
| 97 | + feedback_data["dropdown_option"] = 999 |
| 98 | + response = api_client.post(url, feedback_data, format="json") |
| 99 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 100 | + assert "dropdown_option" in response.data["error"] |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.django_db |
| 104 | +class TestContentCurationRequestAPI: |
| 105 | + def test_create_request_success(self, api_client, content_curation_data): |
| 106 | + """Test successful content curation request creation""" |
| 107 | + url = reverse("feedback:content-curation-request-api") |
| 108 | + response = api_client.post(url, content_curation_data, format="json") |
| 109 | + assert response.status_code == status.HTTP_201_CREATED |
| 110 | + assert ContentCurationRequest.objects.count() == 1 |
| 111 | + |
| 112 | + def test_create_request_without_additional_info(self, api_client, content_curation_data): |
| 113 | + """Test request creation without optional additional info""" |
| 114 | + url = reverse("feedback:content-curation-request-api") |
| 115 | + del content_curation_data["additional_info"] |
| 116 | + response = api_client.post(url, content_curation_data, format="json") |
| 117 | + assert response.status_code == status.HTTP_201_CREATED |
| 118 | + assert ContentCurationRequest.objects.first().additional_info == "" |
| 119 | + |
| 120 | + def test_create_request_invalid_email(self, api_client, content_curation_data): |
| 121 | + """Test request creation with invalid email""" |
| 122 | + url = reverse("feedback:content-curation-request-api") |
| 123 | + content_curation_data["email"] = "invalid-email" |
| 124 | + response = api_client.post(url, content_curation_data, format="json") |
| 125 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 126 | + assert "email" in response.data["error"] |
| 127 | + |
| 128 | + @pytest.mark.parametrize("field", ["name", "email", "scientific_focus", "data_type", "data_link"]) |
| 129 | + def test_create_request_missing_required_fields(self, api_client, content_curation_data, field): |
| 130 | + """Test request creation with missing required fields""" |
| 131 | + url = reverse("feedback:content-curation-request-api") |
| 132 | + content_curation_data.pop(field) |
| 133 | + response = api_client.post(url, content_curation_data, format="json") |
| 134 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 135 | + assert field in response.data["error"] |
| 136 | + |
| 137 | + @pytest.mark.parametrize( |
| 138 | + "field,length", [("name", 151), ("data_link", 1001), ("scientific_focus", 201), ("data_type", 101)] |
| 139 | + ) |
| 140 | + def test_create_request_field_max_lengths(self, api_client, content_curation_data, field, length): |
| 141 | + """Test request creation with fields exceeding max length""" |
| 142 | + url = reverse("feedback:content-curation-request-api") |
| 143 | + content_curation_data[field] = "x" * length |
| 144 | + response = api_client.post(url, content_curation_data, format="json") |
| 145 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 146 | + assert field in response.data["error"] |
0 commit comments