diff --git a/legal-api/flags.json b/legal-api/flags.json index b6a663155d..c3ccc9a4ac 100644 --- a/legal-api/flags.json +++ b/legal-api/flags.json @@ -12,6 +12,7 @@ "enable-new-feature": [ "reviewImportedData", "draftRebaseChecks", + "migratedCourtOrder", "authV2Business", "authV2Filing", "authV2Tasks", diff --git a/legal-api/pyproject.toml b/legal-api/pyproject.toml index f156c8b410..91717e5175 100644 --- a/legal-api/pyproject.toml +++ b/legal-api/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "legal-api" -version = "3.1.76" +version = "3.1.77" description = "" authors = [ {name = "thor",email = "1042854+thorwolpert@users.noreply.github.com"} diff --git a/legal-api/src/legal_api/services/authz.py b/legal-api/src/legal_api/services/authz.py index c1cbe62b9a..fc39e65813 100644 --- a/legal-api/src/legal_api/services/authz.py +++ b/legal-api/src/legal_api/services/authz.py @@ -55,6 +55,7 @@ FILE_OVER_EXISTING_DRAFT_PERMISSION = "FILE_OVER_EXISTING_DRAFT" STAFF_DRAFT_FILING_BYPASS_FLAG = "enable-staff-draft-filing-bypass" DRAFT_REBASE_CHECKS_FEATURE = "draftRebaseChecks" # a value inside the enable-new-feature flag +MIGRATED_COURT_ORDER_FEATURE = "migratedCourtOrder" # a value inside the enable-new-feature flag class BusinessBlocker(str, Enum): diff --git a/legal-api/src/legal_api/services/filings/validations/common_validations.py b/legal-api/src/legal_api/services/filings/validations/common_validations.py index 6ba37737f3..ee3b50f8f4 100644 --- a/legal-api/src/legal_api/services/filings/validations/common_validations.py +++ b/legal-api/src/legal_api/services/filings/validations/common_validations.py @@ -93,6 +93,8 @@ CoreFiling.FilingTypes.RESTORATION, } DRS_KEY_PATTERN = re.compile(r"^([A-Z]+)-(DS\d+)$") +MAX_FILE_SIZE: Final = 30 * 1024 * 1024 # 30 MB +MAX_COURT_ORDER_FILE_SIZE: Final = 50 * 1024 * 1024 # 50 MB FILING_TYPES_WITH_NR = [ CoreFiling.FilingTypes.INCORPORATIONAPPLICATION, @@ -562,7 +564,8 @@ def validate_share_currency(filing_json, filing_type, business=None): def validate_court_order( court_order_path: str, court_order: dict, - is_file_or_details_required: bool = False + is_file_or_details_required: bool = False, + is_file_and_details_required: bool = False ) -> list[dict]: """Validate the courtOrder data of the filing.""" msg = [] @@ -575,8 +578,8 @@ def validate_court_order( if court_order_date.timestamp() > datetime.now(UTC).timestamp(): msg.append({"error": "Court order date cannot be in the future.", "path": f"{court_order_path}/orderDate"}) - if is_file_or_details_required: - msg.extend(_validate_court_order_documents(court_order_path, court_order)) + if is_file_or_details_required or is_file_and_details_required: + msg.extend(_validate_court_order_documents(court_order_path, court_order, is_file_and_details_required)) if flags.is_on("enabled-deeper-permission-action"): required_permission = ListActionsPermissionsAllowed.COURT_ORDER_POA.value @@ -588,7 +591,7 @@ def validate_court_order( return msg -def _validate_court_order_documents(court_order_path, court_order): +def _validate_court_order_documents(court_order_path, court_order, is_file_and_details_required=False): msg = [] file_key = court_order.get("fileKey") files = court_order.get("files", []) @@ -598,11 +601,17 @@ def _validate_court_order_documents(court_order_path, court_order): filing = Filing.find_by_id(filing_id) existing_file_keys = [document.file_key for document in filing.documents.all()] - if not court_order.get("orderDetails") and not file_key and not files: + has_details = bool((court_order.get("orderDetails") or "").strip()) + if is_file_and_details_required: + if not has_details: + msg.append({"error": _("Court Order details are required."), "path": f"{court_order_path}/orderDetails"}) + if not file_key and not files: + msg.append({"error": _("Court Order document is required."), "path": f"{court_order_path}/files"}) + elif not has_details and not file_key and not files: msg.append({"error": _("Court Order is required (in orderDetails/fileKey/files)."), "path": court_order_path}) if file_key: - msg.extend(validate_pdf(file_key, f"{court_order_path}/fileKey")) + msg.extend(validate_pdf(file_key, f"{court_order_path}/fileKey", max_file_size=MAX_COURT_ORDER_FILE_SIZE)) elif files: msg.extend(_validate_court_order_documents_list(f"{court_order_path}/files", files, existing_file_keys)) @@ -620,7 +629,9 @@ def _validate_court_order_documents_list(files_path, files, existing_file_keys): if file.get("fileKey") in existing_file_keys: continue # don't validate existing file keys - msg.extend(validate_pdf(file.get("fileKey"), f"{files_path}/{file_index}/fileKey")) + msg.extend(validate_pdf(file.get("fileKey"), + f"{files_path}/{file_index}/fileKey", + max_file_size=MAX_COURT_ORDER_FILE_SIZE)) if court_order_document_count == 0: # if only supporting documents and no court order documents were found msg.append({"error": _("At least one Court Order document is required."), "path": files_path}) @@ -643,7 +654,10 @@ def check_good_standing_permission(business: Business) -> Error | None: return PermissionService.check_user_permission(required_permission, message=message) -def validate_pdf(file_key: str, file_key_path: str, verify_paper_size: bool = True) -> list: +def validate_pdf(file_key: str, + file_key_path: str, + verify_paper_size: bool = True, + max_file_size: int = MAX_FILE_SIZE) -> list: """Validate the PDF file.""" msg = [] try: @@ -661,7 +675,6 @@ def validate_pdf(file_key: str, file_key_path: str, verify_paper_size: bool = Tr msg.append({"error": _("Document must be set to fit onto 8.5” x 11” letter-size paper."), "path": file_key_path}) - max_file_size: Final = 30000000 if file_size > max_file_size: msg.append({"error": _("File exceeds maximum size."), "path": file_key_path}) diff --git a/legal-api/src/legal_api/services/filings/validations/court_order.py b/legal-api/src/legal_api/services/filings/validations/court_order.py index 3345027e65..ffa33bad1c 100644 --- a/legal-api/src/legal_api/services/filings/validations/court_order.py +++ b/legal-api/src/legal_api/services/filings/validations/court_order.py @@ -18,6 +18,7 @@ from business_model.models import Business from legal_api.errors import Error +from legal_api.services.authz import MIGRATED_COURT_ORDER_FEATURE, is_new_feature_enabled from legal_api.services.filings.validations.common_validations import validate_court_order @@ -26,10 +27,13 @@ def validate(business: Business, court_order: dict) -> Error | None: if not business or not court_order: return Error(HTTPStatus.BAD_REQUEST, [{"error": babel("A valid business and filing are required.")}]) + # the migrated court order page requires details and a document, the legacy page details or a document + is_migrated = is_new_feature_enabled(MIGRATED_COURT_ORDER_FEATURE) msg = validate_court_order( "/filing/courtOrder", court_order["filing"]["courtOrder"], - is_file_or_details_required=True + is_file_or_details_required=not is_migrated, + is_file_and_details_required=is_migrated ) if msg: diff --git a/legal-api/tests/unit/services/filings/validations/test_court_order.py b/legal-api/tests/unit/services/filings/validations/test_court_order.py index 92449c8985..67d859aba8 100644 --- a/legal-api/tests/unit/services/filings/validations/test_court_order.py +++ b/legal-api/tests/unit/services/filings/validations/test_court_order.py @@ -19,39 +19,123 @@ from registry_schemas.example_data import COURT_ORDER_FILING_TEMPLATE from reportlab.lib.pagesizes import letter +from legal_api.services.authz import MIGRATED_COURT_ORDER_FEATURE +from legal_api.services.filings.validations import common_validations +from legal_api.services.filings.validations.common_validations import ( + MAX_COURT_ORDER_FILE_SIZE, + MAX_FILE_SIZE, + validate_pdf, +) from legal_api.services.filings.validations.court_order import validate from tests.unit.models import factory_business -from tests.unit.services.filings.test_utils import _upload_file, mock_drs_get_document +from tests.unit.services.filings.test_utils import _create_pdf_file, _upload_file, mock_drs_get_document from tests.unit.services.filings.validations import lists_are_equal +from tests.unit.services.utils import mock_new_feature + + +def _mock_file_size(monkeypatch, file_size): + """Serve a valid letter-size PDF that reports the given size.""" + pdf_data = _create_pdf_file(letter, invalid=False).read() + monkeypatch.setattr(common_validations, '_get_file_data', lambda _file_key: (pdf_data, file_size)) + + +DETAILS_REQUIRED = {'error': 'Court Order details are required.', 'path': '/filing/courtOrder/orderDetails'} +DOCUMENT_REQUIRED = {'error': 'Court Order document is required.', 'path': '/filing/courtOrder/files'} +DETAILS_OR_DOCUMENT_REQUIRED = {'error': 'Court Order is required (in orderDetails/fileKey/files).', + 'path': '/filing/courtOrder'} + + +def _court_order_filing(order_details, document): + """Return a court order filing with only the given details and document (fileKey / files / None).""" + filing = copy.deepcopy(COURT_ORDER_FILING_TEMPLATE) + court_order = filing['filing']['courtOrder'] + file_key = court_order.pop('fileKey') + del court_order['orderDetails'] + if order_details is not None: + court_order['orderDetails'] = order_details + if document == 'fileKey': + court_order['fileKey'] = file_key + elif document == 'files': + court_order['files'] = [{'fileName': 'test_file.pdf', 'fileKey': file_key, 'documentType': 'court_order'}] + return filing @pytest.mark.parametrize( - 'test_status, expected_code, expected_msg', + 'test_name, order_details, document, expected_msg', [ - ('FAIL', HTTPStatus.BAD_REQUEST, [ - {'error': 'Invalid effectOfOrder.', 'path': '/filing/courtOrder/effectOfOrder'}, - {'error': 'Court Order is required (in orderDetails/fileKey/files).', 'path': '/filing/courtOrder'}]), - ('SUCCESS', None, None) + ('details_and_file_key', 'A note about order', 'fileKey', None), + ('details_and_files', 'A note about order', 'files', None), + ('no_details', None, 'files', [DETAILS_REQUIRED]), + ('empty_details', '', 'files', [DETAILS_REQUIRED]), + ('blank_details', ' ', 'fileKey', [DETAILS_REQUIRED]), + ('no_document', 'A note about order', None, [DOCUMENT_REQUIRED]), + ('no_details_or_document', None, None, [DETAILS_REQUIRED, DOCUMENT_REQUIRED]), ] ) -def test_court_orders(session, test_status, expected_code, expected_msg): - """Assert valid court order.""" +def test_court_order_migrated_requires_details_and_document(session, monkeypatch, test_name, order_details, + document, expected_msg): + """Assert the court order filing requires the order details and a document when migratedCourtOrder is on.""" + mock_new_feature(monkeypatch, MIGRATED_COURT_ORDER_FEATURE, True) + _mock_file_size(monkeypatch, MAX_FILE_SIZE) business = factory_business('BC1234567') - filing = copy.deepcopy(COURT_ORDER_FILING_TEMPLATE) - del filing['filing']['courtOrder']['fileKey'] - if test_status == 'FAIL': - del filing['filing']['courtOrder']['orderDetails'] - filing['filing']['courtOrder']['effectOfOrder'] = 'invalid' - err = validate(business, filing) - if expected_code: - assert err.code == expected_code + err = validate(business, _court_order_filing(order_details, document)) + + if expected_msg: + assert err.code == HTTPStatus.BAD_REQUEST assert lists_are_equal(err.msg, expected_msg) else: assert err is None +@pytest.mark.parametrize( + 'test_name, order_details, document, expected_msg', + [ + ('details_and_files', 'A note about order', 'files', None), + ('details_only', 'A note about order', None, None), + ('file_key_only', None, 'fileKey', None), + ('files_only', None, 'files', None), + ('no_details_or_document', None, None, [DETAILS_OR_DOCUMENT_REQUIRED]), + ('blank_details_no_document', ' ', None, [DETAILS_OR_DOCUMENT_REQUIRED]), + ] +) +def test_court_order_legacy_requires_details_or_document(session, monkeypatch, test_name, order_details, + document, expected_msg): + """Assert the court order filing requires the order details or a document when migratedCourtOrder is off.""" + mock_new_feature(monkeypatch, MIGRATED_COURT_ORDER_FEATURE, False) + _mock_file_size(monkeypatch, MAX_FILE_SIZE) + business = factory_business('BC1234567') + + err = validate(business, _court_order_filing(order_details, document)) + + if expected_msg: + assert err.code == HTTPStatus.BAD_REQUEST + assert lists_are_equal(err.msg, expected_msg) + else: + assert err is None + + +@pytest.mark.parametrize('is_migrated, expected_required', [ + (True, [DETAILS_REQUIRED, DOCUMENT_REQUIRED]), + (False, [DETAILS_OR_DOCUMENT_REQUIRED]), +]) +def test_court_order_invalid_effect_of_order(session, monkeypatch, is_migrated, expected_required): + """Assert an invalid effectOfOrder is reported with the required errors.""" + mock_new_feature(monkeypatch, MIGRATED_COURT_ORDER_FEATURE, is_migrated) + business = factory_business('BC1234567') + filing = _court_order_filing(None, None) + filing['filing']['courtOrder']['effectOfOrder'] = 'invalid' + + err = validate(business, filing) + + assert err.code == HTTPStatus.BAD_REQUEST + assert lists_are_equal(err.msg, [ + {'error': 'Invalid effectOfOrder.', 'path': '/filing/courtOrder/effectOfOrder'}, + *expected_required + ]) + + file_key_path = '/filing/courtOrder/fileKey' @@ -103,3 +187,48 @@ def test_court_order_file(session, monkeypatch, test_name, expected_code, expect assert lists_are_equal(err.msg, expected_msg) else: assert err is None + + +@pytest.mark.parametrize('is_file_key', [True, False]) +@pytest.mark.parametrize( + 'file_size, expected_code', + [ + (MAX_FILE_SIZE + 1, None), + (MAX_COURT_ORDER_FILE_SIZE, None), + (MAX_COURT_ORDER_FILE_SIZE + 1, HTTPStatus.BAD_REQUEST), + ]) +def test_court_order_file_size(session, monkeypatch, file_size, expected_code, is_file_key): + """Assert court order files are accepted up to the court order size limit.""" + _mock_file_size(monkeypatch, file_size) + business = factory_business('BC1234567') + filing = copy.deepcopy(COURT_ORDER_FILING_TEMPLATE) + expected_path = file_key_path + + if not is_file_key: + file_key = filing['filing']['courtOrder'].pop('fileKey') + filing['filing']['courtOrder']['files'] = [ + {'fileName': 'test_file.pdf', 'fileKey': file_key, 'documentType': 'court_order'} + ] + expected_path = '/filing/courtOrder/files/0/fileKey' + + err = validate(business, filing) + + if expected_code: + assert err.code == expected_code + assert lists_are_equal(err.msg, [{'error': 'File exceeds maximum size.', 'path': expected_path}]) + else: + assert err is None + + +@pytest.mark.parametrize( + 'file_size, expected_msg', + [ + (MAX_FILE_SIZE, []), + (MAX_FILE_SIZE + 1, [{'error': 'File exceeds maximum size.', 'path': file_key_path}]), + ]) +def test_validate_pdf_default_file_size(app, monkeypatch, file_size, expected_msg): + """Assert the default size limit still applies to other uploads.""" + _mock_file_size(monkeypatch, file_size) + + with app.app_context(): + assert validate_pdf('COOP-DS1234567890', file_key_path) == expected_msg