Merge branch 'master' into caching-with-redis

Conflicts:
	tests/app/notifications/test_process_notification.py
This commit is contained in:
Martyn Inglis
2016-11-22 13:00:37 +00:00
9 changed files with 314 additions and 45 deletions

View File

@@ -104,7 +104,7 @@ def test_cache_is_not_incremented_on_failure_to_persist_notification(sample_api_
@freeze_time("2016-01-01 11:09:00.061258")
def test_persist_notification_with_job_and_created(sample_job, sample_api_key, mocker):
def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker):
assert Notification.query.count() == 0
assert NotificationHistory.query.count() == 0
mocked_redis = mocker.patch('app.notifications.process_notifications.redis_store.incr')
@@ -119,7 +119,8 @@ def test_persist_notification_with_job_and_created(sample_job, sample_api_key, m
key_type=sample_api_key.key_type,
created_at=created_at,
job_id=sample_job.id,
job_row_number=10)
job_row_number=10,
reference="ref from client")
assert Notification.query.count() == 1
assert NotificationHistory.query.count() == 1
persisted_notification = Notification.query.all()[0]
@@ -127,6 +128,8 @@ def test_persist_notification_with_job_and_created(sample_job, sample_api_key, m
assert persisted_notification.job_row_number == 10
assert persisted_notification.created_at == created_at
mocked_redis.assert_called_once_with(str(sample_job.service_id) + "-2016-01-01-count")
assert persisted_notification.client_reference == "ref from client"
assert persisted_notification.reference is None
@pytest.mark.parametrize('research_mode, queue, notification_type, key_type',
@@ -151,7 +154,7 @@ def test_send_notification_to_queue(notify_db, notify_db_session,
def test_send_notification_to_queue_throws_exception_deletes_notification(sample_notification, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async', side_effect=Boto3Error("EXPECTED"))
with pytest.raises(SendNotificationToQueueError):
with pytest.raises(Boto3Error):
send_notification_to_queue(sample_notification, False)
mocked.assert_called_once_with([(str(sample_notification.id))], queue='send-sms')

View File

@@ -3,8 +3,10 @@ import uuid
import pytest
from flask import json
from jsonschema import ValidationError
from notifications_utils.recipients import InvalidPhoneError, InvalidEmailError
from app.v2.notifications.notification_schemas import post_sms_request, post_sms_response
from app.v2.notifications.notification_schemas import post_sms_request, post_sms_response, post_email_request, \
post_email_response
from app.schema_validation import validate
valid_json = {"phone_number": "07515111111",
@@ -54,6 +56,32 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
assert len(error.keys()) == 2
@pytest.mark.parametrize('invalid_phone_number, err_msg',
[('08515111111', 'phone_number Not a UK mobile number'),
('07515111*11', 'phone_number Must not contain letters or symbols'),
('notaphoneumber', 'phone_number Must not contain letters or symbols')])
def test_post_sms_request_invalid_phone_number(invalid_phone_number, err_msg):
j = {"phone_number": invalid_phone_number,
"template_id": str(uuid.uuid4())
}
with pytest.raises(ValidationError) as e:
validate(j, post_sms_request)
errors = json.loads(e.value.message).get('errors')
assert len(errors) == 1
assert {"error": "ValidationError", "message": err_msg} == errors[0]
def test_post_sms_request_invalid_phone_number_and_missing_template():
j = {"phone_number": '08515111111',
}
with pytest.raises(ValidationError) as e:
validate(j, post_sms_request)
errors = json.loads(e.value.message).get('errors')
assert len(errors) == 2
assert {"error": "ValidationError", "message": "phone_number Not a UK mobile number"} in errors
assert {"error": "ValidationError", "message": "template_id is a required property"} in errors
valid_response = {
"id": str(uuid.uuid4()),
"content": {"body": "contents of message",
@@ -90,3 +118,58 @@ def test_post_sms_response_schema_missing_uri():
assert error['status_code'] == 400
assert error['errors'] == [{'error': 'ValidationError',
'message': "uri is a required property"}]
valid_post_email_json = {"email_address": "test@example.gov.uk",
"template_id": str(uuid.uuid4())
}
valid_post_email_json_with_optionals = {
"email_address": "test@example.gov.uk",
"template_id": str(uuid.uuid4()),
"reference": "reference from caller",
"personalisation": {"key": "value"}
}
@pytest.mark.parametrize("input", [valid_post_email_json, valid_post_email_json_with_optionals])
def test_post_email_schema_is_valid(input):
assert validate(input, post_email_request) == input
def test_post_email_schema_bad_uuid_and_missing_email_address():
j = {"template_id": "bad_template"}
with pytest.raises(ValidationError):
validate(j, post_email_request)
def test_post_email_schema_invalid_email_address():
j = {"template_id": str(uuid.uuid4()),
"email_address": "notavalidemail@address"}
with pytest.raises(ValidationError):
validate(j, post_email_request)
valid_email_response = {"id": str(uuid.uuid4()),
"content": {"body": "the body of the message",
"subject": "subject of the message",
"from_email": "service@dig.gov.uk"},
"uri": "/v2/notifications/id",
"template": {"id": str(uuid.uuid4()),
"version": 1,
"uri": "/v2/template/id"}
}
valid_email_response_with_optionals = {"id": str(uuid.uuid4()),
"reference": "some reference",
"content": {"body": "the body of the message",
"subject": "subject of the message",
"from_email": "service@dig.gov.uk"},
"uri": "/v2/notifications/id",
"template": {"id": str(uuid.uuid4()),
"version": 1,
"uri": "/v2/template/id"}
}
@pytest.mark.parametrize("input", [valid_email_response, valid_email_response_with_optionals])
def test_post_email_response(input):
assert validate(input, post_email_response) == input

View File

@@ -1,7 +1,7 @@
import uuid
import pytest
from flask import json
from app.models import Notification
from tests import create_authorization_header
@@ -12,7 +12,8 @@ def test_post_sms_notification_returns_201(notify_api, sample_template, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'phone_number': '+447700900855',
'template_id': str(sample_template.id)
'template_id': str(sample_template.id),
'reference': 'reference_from_client'
}
auth_header = create_authorization_header(service_id=sample_template.service_id)
@@ -27,7 +28,7 @@ def test_post_sms_notification_returns_201(notify_api, sample_template, mocker):
assert len(notifications) == 1
notification_id = notifications[0].id
assert resp_json['id'] is not None
assert resp_json['reference'] is None
assert resp_json['reference'] == 'reference_from_client'
assert resp_json['content']['body'] == sample_template.content
assert resp_json['content']['from_number'] == sample_template.service.sms_sender
assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri']
@@ -102,3 +103,54 @@ def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, s
assert error_resp['errors'] == [{'error': 'ValidationError',
'message': "template_id is a required property"
}]
def test_post_email_notification_returns_201(client, sample_email_template, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = {
"reference": "reference from caller",
"email_address": sample_email_template.service.users[0].email_address,
"template_id": sample_email_template.id,
}
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
response = client.post(
path="v2/notifications/email",
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 201
resp_json = json.loads(response.get_data(as_text=True))
notification = Notification.query.first()
assert resp_json['id'] == str(notification.id)
assert resp_json['reference'] == "reference from caller"
assert notification.reference is None
assert resp_json['content']['body'] == sample_email_template.content
assert resp_json['content']['subject'] == sample_email_template.subject
assert resp_json['content']['from_email'] == sample_email_template.service.email_from
assert 'v2/notifications/{}'.format(notification.id) in resp_json['uri']
assert resp_json['template']['id'] == str(sample_email_template.id)
assert resp_json['template']['version'] == sample_email_template.version
assert 'v2/templates/{}'.format(sample_email_template.id) in resp_json['template']['uri']
assert mocked.called
def test_post_email_notification_returns_404_and_missing_template(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
"email_address": sample_service.users[0].email_address,
'template_id': str(uuid.uuid4())
}
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path='/v2/notifications/email',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400
assert response.headers['Content-type'] == 'application/json'
error_json = json.loads(response.get_data(as_text=True))
assert error_json['status_code'] == 400
assert error_json['errors'] == [{"error": "BadRequestError",
"message": 'Template not found'}]