From fc30e739642783a81143e9283f8fb04b3cc03053 Mon Sep 17 00:00:00 2001 From: Severin Beauvais Date: Wed, 2 Dec 2026 23:11:42 +0000 Subject: [PATCH] fix(strr-api): keep a regular draft from being turned into a renewal outside the window (REGBACKLOG-391) The registration ID check now applies to POST as well as PUT on /applications/{number}, so an existing draft can only skip the renewal window and in-progress checks if it is already a renewal of the requested registration. Addresses review feedback on #1964. --- .../src/strr_api/resources/application.py | 4 +++- .../unit/resources/test_renewal_window_api.py | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/strr-api/src/strr_api/resources/application.py b/strr-api/src/strr_api/resources/application.py index 09c5c863c..1ed4eb2d9 100644 --- a/strr-api/src/strr_api/resources/application.py +++ b/strr-api/src/strr_api/resources/application.py @@ -191,7 +191,9 @@ def create_application(application_number: Optional[str] = None): http_status=HTTPStatus.BAD_REQUEST, ) - if request.method == "PUT" and existing_registration_id != registration_id: + # An existing application must already be a renewal of this registration, whether it's saved by PUT or + # POST; otherwise a regular draft could be turned into a renewal past the window and in-progress checks + if application is not None and existing_registration_id != registration_id: return error_response( message=ErrorMessage.REGISTRATION_ID_MISMATCH.value, http_status=HTTPStatus.BAD_REQUEST, diff --git a/strr-api/tests/unit/resources/test_renewal_window_api.py b/strr-api/tests/unit/resources/test_renewal_window_api.py index 6ffd97cf7..a7b7f59a8 100644 --- a/strr-api/tests/unit/resources/test_renewal_window_api.py +++ b/strr-api/tests/unit/resources/test_renewal_window_api.py @@ -8,6 +8,7 @@ import pytest +from strr_api.enums.enum import ApplicationType, ErrorMessage from strr_api.models import Application from tests.integration.application_seed import host_renewal_application_json from tests.shared_test_constants import MOCK_INVOICE_RESPONSE @@ -31,6 +32,29 @@ def test_new_renewal_draft_is_refused_before_window(mock_invoice, client, renewa assert Application.query.filter_by(registration_id=registration.id).count() == 0 +@pytest.mark.parametrize("days_to_expiry", [200, -(3 * 365 + 60)]) # before the window opens; after it closes +@pytest.mark.parametrize("method", ["post", "put"]) +def test_regular_draft_cannot_become_renewal_outside_window( + client, renewal_headers, make_registration, days_to_expiry, method +): + with open(HOST_REGISTRATION) as f: + base_json = json.load(f) + draft_headers = {**renewal_headers, "isDraft": True} + rv = client.post("/applications", json=base_json, headers=draft_headers) + assert rv.status_code == HTTPStatus.OK + number = rv.json["header"]["applicationNumber"] + registration = make_registration(days_to_expiry) + payload = host_renewal_application_json(registration.id, base_json=base_json) + + rv = getattr(client, method)(f"/applications/{number}", json=payload, headers=draft_headers) + + assert rv.status_code == HTTPStatus.BAD_REQUEST + assert rv.json["message"] == ErrorMessage.REGISTRATION_ID_MISMATCH.value + application = Application.find_by_application_number(number) + assert application.type == ApplicationType.REGISTRATION.value + assert application.registration_id is None + + @pytest.mark.parametrize( "expiry_shift_days, error_code", [