diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index d0ab58f9c5..ebf4b990d6 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -2,8 +2,8 @@ NOTIFICATION_STATUS_TYPES, NOTIFICATION_STATUS_LETTER_ACCEPTED, NOTIFICATION_STATUS_LETTER_RECEIVED, - TEMPLATE_TYPES -) + TEMPLATE_TYPES, + VA_IDENTIFIER_TYPES) from app.schema_validation.definitions import (uuid, personalisation, letter_personalisation) @@ -136,7 +136,8 @@ "phone_number": {"type": "string", "format": "phone_number"}, "va_identifier": {"type": "object", "properties": { "id_type": { - "type": "string" + "type": "string", + "enum": VA_IDENTIFIER_TYPES }, "value": { "type": "string" diff --git a/tests/app/v2/notifications/test_notification_schemas.py b/tests/app/v2/notifications/test_notification_schemas.py index e8f1b09d2c..75a9186d37 100644 --- a/tests/app/v2/notifications/test_notification_schemas.py +++ b/tests/app/v2/notifications/test_notification_schemas.py @@ -5,7 +5,7 @@ from freezegun import freeze_time from jsonschema import ValidationError -from app.models import NOTIFICATION_CREATED, EMAIL_TYPE +from app.models import NOTIFICATION_CREATED, EMAIL_TYPE, VA_IDENTIFIER_TYPES, VA_PROFILE_ID from app.schema_validation import validate from app.v2.notifications.notification_schemas import ( get_notifications_request, @@ -108,7 +108,7 @@ def test_get_notifications_request_invalid_statuses_and_template_types(): } valid_va_identifier_json = { "va_identifier": { - "id_type": "foo", + "id_type": VA_PROFILE_ID, "value": "bar" }, "template_id": str(uuid.uuid4()) @@ -116,7 +116,7 @@ def test_get_notifications_request_invalid_statuses_and_template_types(): valid_phone_number_and_va_identifier_json = { "phone_number": "6502532222", "va_identifier": { - "id_type": "foo", + "id_type": VA_PROFILE_ID, "value": "bar" }, "template_id": str(uuid.uuid4()) @@ -135,6 +135,18 @@ def test_post_sms_schema_is_valid(input): assert validate(input, post_sms_request_schema) == input +@pytest.mark.parametrize("va_identifier_type", VA_IDENTIFIER_TYPES) +def test_post_sms_schema_id_type_should_only_use_enum_values(va_identifier_type): + id_type_as_enum_json = { + "va_identifier": { + "id_type": va_identifier_type, + "value": "bar" + }, + "template_id": str(uuid.uuid4()) + } + assert validate(id_type_as_enum_json, post_sms_request_schema) == id_type_as_enum_json + + @pytest.mark.parametrize("template_id", ['2ebe4da8-17be-49fe-b02f-dff2760261a0' + "\n", '2ebe4da8-17be-49fe-b02f-dff2760261a0' + " ", @@ -160,7 +172,7 @@ def test_post_sms_json_schema_bad_uuid(template_id): missing_id_type_json = {"value": "bar"} -missing_value_json = {"id_type": "foo"} +missing_value_json = {"id_type": VA_PROFILE_ID} missing_id_type_and_value_json = {"invalid_param": "invalid"} @@ -169,7 +181,7 @@ def test_post_sms_json_schema_bad_uuid(template_id): (missing_value_json, ["value"]), (missing_id_type_and_value_json, ["id_type", "value"]) ]) -def test_post_sms_json_schema_invalid_va_identifier(va_identifier, missing_key_name): +def test_post_sms_json_schema_missing_va_identifier_required_fields(va_identifier, missing_key_name): j = { "va_identifier": va_identifier, "template_id": str(uuid.uuid4()) @@ -185,6 +197,24 @@ def test_post_sms_json_schema_invalid_va_identifier(va_identifier, missing_key_n 'message': "va_identifier " + key_name + " is a required property"} in error['errors'] +def test_post_sms_json_schema_invalid_va_identifier_id_type(): + id_type_not_an_accepted_va_identifier_json = { + "va_identifier": { + "id_type": "foo", + "value": "bar", + }, + "template_id": str(uuid.uuid4()) + } + with pytest.raises(ValidationError) as e: + validate(id_type_not_an_accepted_va_identifier_json, post_sms_request_schema) + error = json.loads(str(e.value)) + assert len(error.keys()) == 2 + assert error.get('status_code') == 400 + assert len(error.get('errors')) == 1 + assert {'error': 'ValidationError', + 'message': "va_identifier foo is not one of [va_profile_id, pid, icn]"} in error['errors'] + + def test_post_sms_json_schema_bad_uuid_and_missing_phone_number_and_va_identifier(): j = {"template_id": "notUUID"} with pytest.raises(ValidationError) as e: