From 714c349efd361021434e8d188c7c33079b0c0c21 Mon Sep 17 00:00:00 2001 From: David McDonald Date: Fri, 25 Oct 2019 16:13:22 +0100 Subject: [PATCH] Use existing postage format for validation Also adds explicit test for checking of postage --- app/service/send_pdf_letter_schema.py | 3 +-- tests/app/service/test_rest.py | 30 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/service/send_pdf_letter_schema.py b/app/service/send_pdf_letter_schema.py index be9c26167..8ce062a3c 100644 --- a/app/service/send_pdf_letter_schema.py +++ b/app/service/send_pdf_letter_schema.py @@ -4,11 +4,10 @@ send_pdf_letter_request = { "type": "object", "title": "Send an uploaded pdf letter", "properties": { - "postage": {"enum": ["first", "second"]}, + "postage": {"type": "string", "format": "postage"}, "filename": {"type": "string"}, "created_by": {"type": "string"}, "file_id": {"type": "string"}, }, - "required": ["postage", "filename", "created_by", "file_id"] } diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 073d01a42..4632f4d43 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -2297,21 +2297,35 @@ def test_create_pdf_letter(mocker, sample_service_full_permissions, client, fake assert json_resp == {'id': fake_uuid} -def test_create_pdf_letter_validates_against_json_schema(sample_service_full_permissions, client): +@pytest.mark.parametrize('post_data, expected_errors', [ + ( + {}, + [ + {'error': 'ValidationError', 'message': 'postage is a required property'}, + {'error': 'ValidationError', 'message': 'filename is a required property'}, + {'error': 'ValidationError', 'message': 'created_by is a required property'}, + {'error': 'ValidationError', 'message': 'file_id is a required property'} + ] + ), + ( + {"postage": "third", "filename": "string", "created_by": "string", "file_id": "string"}, + [ + {'error': 'ValidationError', 'message': 'postage invalid. It must be either first or second.'} + ] + ) +]) +def test_create_pdf_letter_validates_against_json_schema( + sample_service_full_permissions, client, post_data, expected_errors +): response = client.post( url_for('service.create_pdf_letter', service_id=sample_service_full_permissions.id), - data=json.dumps({}), + data=json.dumps(post_data), headers=[('Content-Type', 'application/json'), create_authorization_header()] ) json_resp = json.loads(response.get_data(as_text=True)) assert response.status_code == 400 - assert json_resp['errors'] == [ - {'error': 'ValidationError', 'message': 'postage is a required property'}, - {'error': 'ValidationError', 'message': 'filename is a required property'}, - {'error': 'ValidationError', 'message': 'created_by is a required property'}, - {'error': 'ValidationError', 'message': 'file_id is a required property'} - ] + assert json_resp['errors'] == expected_errors def test_get_notification_for_service_includes_template_redacted(admin_request, sample_notification):