Updated test_validators to test the contents of the error messages.

Added some tests to the test_post_notifications.
Added a errorhandler for AuthErrors.

This endpoint is not being used anywhere, however there is some common code being used in the v1 post endpoint. The only thing that may be affected is the error response, hopefully they are the same.
This commit is contained in:
Rebecca Law
2016-10-31 12:22:26 +00:00
parent 8cf2fc72a8
commit fc298367c5
7 changed files with 120 additions and 42 deletions

View File

@@ -1,3 +1,5 @@
import uuid
from flask import json
from tests import create_authorization_header
@@ -27,14 +29,14 @@ def test_post_sms_notification_returns_201(notify_api, sample_template, mocker):
assert mocked.called
def test_post_sms_notification_returns_404_when_template_is_wrong_type(notify_api, sample_email_template):
def test_post_sms_notification_returns_404_and_missing_template(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'phone_number': '+447700900855',
'template_id': str(sample_email_template.id)
'template_id': str(uuid.uuid4())
}
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path='/v2/notifications/sms',
@@ -42,8 +44,31 @@ def test_post_sms_notification_returns_404_when_template_is_wrong_type(notify_ap
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400
resp_text = json.loads(response.get_data(as_text=True))
assert resp_text['code'] == '10400'
assert resp_text['message'] == '{0} template is not suitable for {1} notification'.format('email', 'sms')
assert resp_text['link'] == 'link to documentation'
assert resp_text.get('fields', None) is None
assert response.headers['Content-type'] == 'application/json'
error_json = json.loads(response.get_data(as_text=True))
assert error_json['code'] == 10400
assert error_json['message'] == 'Template not found'
assert error_json['fields'] == [{'template': 'Template not found'}]
assert error_json['link'] == 'link to documentation'
def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'phone_number': '+447700900855',
'template_id': str(sample_template.id)
}
response = client.post(
path='/v2/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
assert response.status_code == 401
assert response.headers['Content-type'] == 'application/json'
error_resp = json.loads(response.get_data(as_text=True))
assert error_resp['code'] == 401
assert error_resp['message'] == {'token': ['Unauthorized, authentication token must be provided']}