diff --git a/doctor/tests.py b/doctor/tests.py index ddca107..2dd4bc2 100644 --- a/doctor/tests.py +++ b/doctor/tests.py @@ -435,6 +435,33 @@ def test_get_document_number(self): self.assertEqual(doc_num, document_number) +class TestDocumentNumberExtraction(unittest.TestCase): + def test_get_document_number_invalid_form(self): + """Test that we get a proper response when form validation fails""" + # Test with empty request + response = requests.post( + "http://doctor:5050/utils/document-number/pdf/", + ) + self.assertEqual(response.status_code, 400, msg="Wrong status code") + self.assertIn( + "File is missing", + response.text, + msg="Wrong validation error message", + ) + + # Test with non-PDF file + with NamedTemporaryFile(suffix=".txt") as tmp: + tmp.write(b"This is not a PDF file") + tmp.flush() + tmp.seek(0) + files = {"file": (tmp.name, tmp.read())} + response = requests.post( + "http://doctor:5050/utils/document-number/pdf/", + files=files, + ) + self.assertEqual(response.status_code, 400, msg="Wrong status code") + + class RedactionTest(unittest.TestCase): def test_xray_no_pdf(self): """Are we able to discover bad redacts?""" diff --git a/doctor/views.py b/doctor/views.py index 795248d..e9c685e 100644 --- a/doctor/views.py +++ b/doctor/views.py @@ -437,11 +437,20 @@ def get_document_number(request) -> HttpResponse: :return: PACER document number """ - form = BaseFileForm(request.GET, request.FILES) + form = BaseFileForm(request.POST, request.FILES) if not form.is_valid(): - validation_message = form.errors.get_json_data()["__all__"][0][ - "message" - ] + error_data = form.errors.get_json_data() + if "__all__" in error_data: + validation_message = error_data["__all__"][0]["message"] + elif "file" in error_data: + validation_message = error_data["file"][0]["message"] + else: + for field, errors in error_data.items(): + if errors and errors[0]: + validation_message = errors[0]["message"] + break + else: + validation_message = "Form validation failed." return HttpResponse(validation_message, status=BAD_REQUEST) fp = form.cleaned_data["fp"] document_number = get_document_number_from_pdf(fp)