add POST letter schema

similar to sms/email, however, for consistency with response values
and internal storage, rather than supplying an "email_address" field
or a "phone_number" field, supply an "address_line_1" and "postcode"
field within the personalisation object.
This commit is contained in:
Leo Hemsted
2017-07-07 10:46:26 +01:00
parent 49bbd1a29b
commit 7f4eec79e4
5 changed files with 243 additions and 38 deletions

View File

@@ -20,6 +20,9 @@ personalisation = {
}
letter_personalisation = dict(personalisation, required=["address_line_1", "postcode"])
https_url = {
"type": "string",
"format": "uri",

View File

@@ -1,5 +1,5 @@
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
from app.schema_validation.definitions import (uuid, personalisation)
from app.schema_validation.definitions import (uuid, personalisation, letter_personalisation)
template = {
@@ -192,40 +192,89 @@ post_email_response = {
}
def create_post_sms_response_from_notification(notification, body, from_number, url_root, service_id, scheduled_for):
return {"id": notification.id,
"reference": notification.client_reference,
"content": {'body': body,
'from_number': from_number},
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
"template": __create_template_from_notification(notification=notification,
url_root=url_root,
service_id=service_id),
"scheduled_for": scheduled_for if scheduled_for else None
}
post_letter_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST letter notification schema",
"type": "object",
"title": "POST v2/notifications/letter",
"properties": {
"reference": {"type": "string"},
"template_id": uuid,
"personalisation": letter_personalisation
},
"required": ["template_id", "personalisation"]
}
letter_content = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Letter content for POST letter notification",
"type": "object",
"title": "notification letter content",
"properties": {
"body": {"type": "string"},
"subject": {"type": "string"}
},
"required": ["body", "subject"]
}
post_letter_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST sms notification response schema",
"type": "object",
"title": "response v2/notifications/letter",
"properties": {
"id": uuid,
"reference": {"type": ["string", "null"]},
"content": letter_content,
"uri": {"type": "string", "format": "uri"},
"template": template,
"scheduled_for": {"type": ["string", "null"]}
},
"required": ["id", "content", "uri", "template"]
}
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, service_id,
scheduled_for):
def create_post_sms_response_from_notification(notification, body, from_number, url_root, scheduled_for):
noti = __create_notification_response(notification, url_root, scheduled_for)
noti['content'] = {
'from_number': from_number,
'body': body
}
return noti
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, scheduled_for):
noti = __create_notification_response(notification, url_root, scheduled_for)
noti['content'] = {
"from_email": email_from,
"body": content,
"subject": subject
}
return noti
def create_post_letter_response_from_notification(notification, content, subject, url_root, scheduled_for):
noti = __create_notification_response(notification, url_root, scheduled_for)
noti['content'] = {
"body": content,
"subject": subject
}
return noti
def __create_notification_response(notification, url_root, scheduled_for):
return {
"id": notification.id,
"reference": notification.client_reference,
"content": {
"from_email": email_from,
"body": content,
"subject": subject
},
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
"template": __create_template_from_notification(notification=notification,
url_root=url_root,
service_id=service_id),
'template': {
"id": notification.template_id,
"version": notification.template_version,
"uri": "{}services/{}/templates/{}".format(
url_root,
str(notification.service_id),
str(notification.template_id)
)
},
"scheduled_for": scheduled_for if scheduled_for else None
}
def __create_template_from_notification(notification, url_root, service_id):
return {
"id": notification.template_id,
"version": notification.template_version,
"uri": "{}services/{}/templates/{}".format(url_root, str(service_id), str(notification.template_id))
}

View File

@@ -2,7 +2,7 @@ from flask import request, jsonify, current_app
from app import api_user, authenticated_service
from app.config import QueueNames
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY, SCHEDULE_NOTIFICATIONS
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue,
@@ -11,20 +11,17 @@ from app.notifications.process_notifications import (
from app.notifications.validators import (
validate_and_format_recipient,
check_rate_limiting,
service_has_permission,
check_service_can_schedule_notification,
check_service_has_permission,
validate_template
)
from app.schema_validation import validate
from app.utils import get_public_notify_type_text
from app.v2.notifications import v2_notification_blueprint
from app.v2.notifications.notification_schemas import (
post_sms_request,
create_post_sms_response_from_notification,
post_email_request,
create_post_email_response_from_notification)
from app.v2.errors import BadRequestError
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
@@ -87,7 +84,6 @@ def post_notification(notification_type):
body=str(template_with_content),
from_number=sms_sender,
url_root=request.url_root,
service_id=authenticated_service.id,
scheduled_for=scheduled_for)
else:
resp = create_post_email_response_from_notification(notification=notification,
@@ -95,6 +91,5 @@ def post_notification(notification_type):
subject=template_with_content.subject,
email_from=authenticated_service.email_from,
url_root=request.url_root,
service_id=authenticated_service.id,
scheduled_for=scheduled_for)
return jsonify(resp), 201

View File

@@ -0,0 +1,158 @@
import uuid
from flask import url_for, json
import pytest
from app.models import Job, Notification, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE
from app.v2.errors import RateLimitError
from tests import create_authorization_header
from tests.app.db import create_service, create_template
def letter_request(client, data, service_id, _expected_status=201):
resp = client.post(
url_for('v2_notifications.post_notification', notification_type='letter'),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=service_id)]
)
assert resp.status_code == _expected_status
json_resp = json.loads(resp.get_data(as_text=True))
return json_resp
@pytest.mark.parametrize('reference', [None, 'reference_from_client'])
def test_post_letter_notification_returns_201(client, sample_letter_template, mocker, reference):
mocked = mocker.patch('app.celery.tasks.build_dvla_file.apply_async')
data = {
'template_id': str(sample_letter_template.id),
'personalisation': {
'address_line_1': 'Her Royal Highness Queen Elizabeth II',
'address_line_2': 'Buckingham Palace',
'address_line_3': 'London',
'postcode': 'SW1 1AA',
'name': 'Lizzie'
}
}
if reference:
data.update({'reference': reference})
resp_json = letter_request(client, data, service_id=sample_letter_template.service_id)
job = Job.query.one()
notification = Notification.query.all()
notification_id = notification.id
assert resp_json['id'] == str(notification_id)
assert resp_json['reference'] == reference
assert resp_json['content']['subject'] == sample_letter_template.subject
assert resp_json['content']['body'] == sample_letter_template.content
assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri']
assert resp_json['template']['id'] == str(sample_letter_template.id)
assert resp_json['template']['version'] == sample_letter_template.version
assert (
'services/{}/templates/{}'.format(
sample_letter_template.service_id,
sample_letter_template.id
) in resp_json['template']['uri']
)
assert not resp_json['scheduled_for']
mocked.assert_called_once_with((str(job.id), ), queue='job-tasks')
def test_post_letter_notification_returns_400_and_missing_template(
client,
sample_service
):
data = {
'template_id': str(uuid.uuid4()),
'personalisation': {'address_line_1': '', 'postcode': ''}
}
error_json = letter_request(client, data, service_id=sample_service.id, _expected_status=400)
assert error_json['status_code'] == 400
assert error_json['errors'] == [{'error': 'BadRequestError', 'message': 'Template not found'}]
def test_post_notification_returns_403_and_well_formed_auth_error(
client,
sample_letter_template
):
data = {
'template_id': str(sample_letter_template.id),
'personalisation': {'address_line_1': '', 'postcode': ''}
}
error_json = letter_request(client, data, service_id=sample_letter_template.service_id, _expected_status=401)
assert error_json['status_code'] == 401
assert error_json['errors'] == [{
'error': 'AuthError',
'message': 'Unauthorized, authentication token must be provided'
}]
def test_notification_returns_400_for_schema_problems(
client,
sample_service
):
data = {
'personalisation': {'address_line_1': '', 'postcode': ''}
}
error_json = letter_request(client, data, service_id=sample_service.id, _expected_status=400)
assert error_json['status_code'] == 400
assert error_json['errors'] == [{
'error': 'ValidationError',
'message': 'template_id is a required property'
}]
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
client,
sample_letter_template,
mocker
):
persist_mock = mocker.patch('app.v2.notifications.post_notifications.persist_notification')
mocker.patch(
'app.v2.notifications.post_notifications.check_rate_limiting',
side_effect=RateLimitError('LIMIT', 'INTERVAL', 'TYPE')
)
data = {
'template_id': str(sample_letter_template.id),
'personalisation': {'address_line_1': '', 'postcode': ''}
}
error_json = letter_request(client, data, service_id=sample_letter_template.service_id, _expected_status=429)
assert error_json['status_code'] == 429
assert error_json['errors'] == [{
'error': 'RateLimitError',
'message': 'Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds'
}]
assert not persist_mock.called
def test_post_letter_notification_returns_400_if_not_allowed_to_send_notification(
client,
notify_db_session
):
service = create_service(service_permissions=[EMAIL_TYPE, SMS_TYPE])
template = create_template(service, template_type=LETTER_TYPE)
data = {
'template_id': str(template.id),
'personalisation': {'address_line_1': '', 'postcode': ''}
}
error_json = letter_request(client, data, service_id=service.id, _expected_status=400)
assert error_json['status_code'] == 400
assert error_json['errors'] == [
{'error': 'BadRequestError', 'message': 'Cannot send text letters'}
]

View File

@@ -58,8 +58,8 @@ def test_post_sms_notification_returns_201(client, sample_template_with_placehol
@pytest.mark.parametrize("notification_type, key_send_to, send_to",
[("sms", "phone_number", "+447700900855"),
("email", "email_address", "sample@email.com")])
def test_post_sms_notification_returns_400_and_missing_template(client, sample_service,
notification_type, key_send_to, send_to):
def test_post_notification_returns_400_and_missing_template(client, sample_service,
notification_type, key_send_to, send_to):
data = {
key_send_to: send_to,
'template_id': str(uuid.uuid4())