diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index ebf4b990d6..e5d0c2293d 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -195,7 +195,8 @@ "email_address": {"type": "string", "format": "email_address"}, "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 75a9186d37..23651b9f15 100644 --- a/tests/app/v2/notifications/test_notification_schemas.py +++ b/tests/app/v2/notifications/test_notification_schemas.py @@ -282,7 +282,7 @@ def test_post_sms_request_schema_invalid_phone_number_and_missing_template(): } valid_va_identifier_json = { "va_identifier": { - "id_type": "foo", + "id_type": VA_PROFILE_ID, "value": "bar" }, "template_id": str(uuid.uuid4()) @@ -290,7 +290,7 @@ def test_post_sms_request_schema_invalid_phone_number_and_missing_template(): valid_email_and_va_identifier_json = { "email_address": "test@example.gov.uk", "va_identifier": { - "id_type": "foo", + "id_type": VA_PROFILE_ID, "value": "bar" }, "template_id": str(uuid.uuid4()) @@ -336,7 +336,7 @@ def test_post_email_schema_invalid_email_address(email_address, err_msg): 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"} @@ -345,7 +345,7 @@ def test_post_email_schema_invalid_email_address(email_address, err_msg): (missing_value_json, ["value"]), (missing_id_type_and_value_json, ["id_type", "value"]) ]) -def test_post_email_json_schema_invalid_va_identifier(va_identifier, missing_key_name): +def test_post_email_json_schema_missing_va_identifier_required_fields(va_identifier, missing_key_name): j = { "va_identifier": va_identifier, "template_id": str(uuid.uuid4()) @@ -361,6 +361,36 @@ def test_post_email_json_schema_invalid_va_identifier(va_identifier, missing_key 'message': "va_identifier " + key_name + " is a required property"} in error['errors'] +def test_post_email_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_email_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'] + + +@pytest.mark.parametrize("va_identifier_type", VA_IDENTIFIER_TYPES) +def test_post_email_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_email_request_schema) == id_type_as_enum_json + + def valid_email_response(): return { "id": str(uuid.uuid4()),