mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 07:21:49 -05:00
Improvements to the tests.
Update AuthError with a to_dict_v2 method.
This commit is contained in:
@@ -12,8 +12,15 @@ from app.dao.services_dao import dao_fetch_service_by_id
|
|||||||
class AuthError(Exception):
|
class AuthError(Exception):
|
||||||
def __init__(self, message, code):
|
def __init__(self, message, code):
|
||||||
self.message = {"token": [message]}
|
self.message = {"token": [message]}
|
||||||
|
self.short_message = message
|
||||||
self.code = code
|
self.code = code
|
||||||
|
|
||||||
|
def to_dict_v2(self):
|
||||||
|
return {'code': self.code,
|
||||||
|
'message': self.short_message,
|
||||||
|
'fields': self.message,
|
||||||
|
'link': 'link to docs'}
|
||||||
|
|
||||||
|
|
||||||
def get_auth_token(req):
|
def get_auth_token(req):
|
||||||
auth_header = req.headers.get('Authorization', None)
|
auth_header = req.headers.get('Authorization', None)
|
||||||
|
|||||||
@@ -51,10 +51,7 @@ def register_errors(blueprint):
|
|||||||
|
|
||||||
@blueprint.errorhandler(AuthError)
|
@blueprint.errorhandler(AuthError)
|
||||||
def auth_error(error):
|
def auth_error(error):
|
||||||
return jsonify(status_code=error.code,
|
return jsonify(error.to_dict_v2()), error.code
|
||||||
message=error.message,
|
|
||||||
code=error.code,
|
|
||||||
link='link to docs'), error.code
|
|
||||||
|
|
||||||
@blueprint.errorhandler(Exception)
|
@blueprint.errorhandler(Exception)
|
||||||
def internal_server_error(error):
|
def internal_server_error(error):
|
||||||
|
|||||||
@@ -56,12 +56,13 @@ post_sms_response = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def create_post_sms_response_from_notification(notification, content):
|
def create_post_sms_response_from_notification(notification, body, from_number, url_root):
|
||||||
return {"id": notification.id,
|
return {"id": notification.id,
|
||||||
"reference": None, # not yet implemented
|
"reference": None, # not yet implemented
|
||||||
"content": content,
|
"content": {'body': body,
|
||||||
"uri": "v2/notifications/{}".format(notification.id),
|
'from_number': from_number},
|
||||||
|
"uri": "{}/v2/notifications/{}".format(url_root, str(notification.id)),
|
||||||
"template": {"id": notification.template_id,
|
"template": {"id": notification.template_id,
|
||||||
"version": notification.template_version,
|
"version": notification.template_version,
|
||||||
"uri": "v2/templates/{}".format(notification.template_id)}
|
"uri": "{}/v2/templates/{}".format(url_root, str(notification.template_id))}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from flask import request, jsonify
|
from flask import request, jsonify, current_app
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
from app import api_user
|
from app import api_user
|
||||||
@@ -38,7 +38,8 @@ def post_sms_notification():
|
|||||||
api_key_id=api_user.id,
|
api_key_id=api_user.id,
|
||||||
key_type=api_user.key_type)
|
key_type=api_user.key_type)
|
||||||
send_notification_to_queue(notification, service.research_mode)
|
send_notification_to_queue(notification, service.research_mode)
|
||||||
resp = create_post_sms_response_from_notification(notification, content)
|
|
||||||
|
resp = create_post_sms_response_from_notification(notification, content, service.sms_sender, request.url_root)
|
||||||
return jsonify(resp), 201
|
return jsonify(resp), 201
|
||||||
|
|
||||||
|
|
||||||
@@ -67,4 +68,4 @@ def __validate_template(form, service):
|
|||||||
check_template_is_active(template)
|
check_template_is_active(template)
|
||||||
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
||||||
check_sms_content_char_count(template_with_content.replaced_content_count)
|
check_sms_content_char_count(template_with_content.replaced_content_count)
|
||||||
return template, template_with_content.replaced_content_count
|
return template, template_with_content.content
|
||||||
|
|||||||
@@ -30,8 +30,9 @@ def test_create_content_for_notification_fails_with_missing_personalisation(samp
|
|||||||
|
|
||||||
def test_create_content_for_notification_fails_with_additional_personalisation(sample_template_with_placeholders):
|
def test_create_content_for_notification_fails_with_additional_personalisation(sample_template_with_placeholders):
|
||||||
template = Template.query.get(sample_template_with_placeholders.id)
|
template = Template.query.get(sample_template_with_placeholders.id)
|
||||||
with pytest.raises(BadRequestError):
|
with pytest.raises(BadRequestError) as e:
|
||||||
create_content_for_notification(template, {'name': 'Bobbhy', 'Additional': 'Data'})
|
create_content_for_notification(template, {'name': 'Bobby', 'Additional placeholder': 'Data'})
|
||||||
|
assert e.value.message == 'Template personalisation not needed for template: Additional placeholder'
|
||||||
|
|
||||||
|
|
||||||
def test_persist_notification_creates_and_save_to_db(sample_template, sample_api_key):
|
def test_persist_notification_creates_and_save_to_db(sample_template, sample_api_key):
|
||||||
@@ -41,7 +42,7 @@ def test_persist_notification_creates_and_save_to_db(sample_template, sample_api
|
|||||||
sample_template.service.id, {}, 'sms', sample_api_key.id,
|
sample_template.service.id, {}, 'sms', sample_api_key.id,
|
||||||
sample_api_key.key_type)
|
sample_api_key.key_type)
|
||||||
assert Notification.query.count() == 1
|
assert Notification.query.count() == 1
|
||||||
assert Notification.query.get(notification.id).__eq__(notification)
|
assert Notification.query.get(notification.id) is not None
|
||||||
assert NotificationHistory.query.count() == 1
|
assert NotificationHistory.query.count() == 1
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -97,17 +97,15 @@ def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(k
|
|||||||
live_service) is None
|
live_service) is None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('key_type',
|
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(notify_db, notify_db_session,
|
||||||
['team'])
|
|
||||||
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(key_type, notify_db, notify_db_session,
|
|
||||||
sample_service):
|
sample_service):
|
||||||
sample_service_whitelist(notify_db, notify_db_session, email_address="some_other_email@test.com")
|
sample_service_whitelist(notify_db, notify_db_session, email_address="some_other_email@test.com")
|
||||||
assert service_can_send_to_recipient("some_other_email@test.com",
|
assert service_can_send_to_recipient("some_other_email@test.com",
|
||||||
key_type,
|
'team',
|
||||||
sample_service) is None
|
sample_service) is None
|
||||||
sample_service_whitelist(notify_db, notify_db_session, mobile_number='07513332413')
|
sample_service_whitelist(notify_db, notify_db_session, mobile_number='07513332413')
|
||||||
assert service_can_send_to_recipient('07513332413',
|
assert service_can_send_to_recipient('07513332413',
|
||||||
key_type,
|
'team',
|
||||||
sample_service) is None
|
sample_service) is None
|
||||||
|
|
||||||
|
|
||||||
@@ -120,9 +118,9 @@ def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recip
|
|||||||
notify_db, notify_db_session):
|
notify_db, notify_db_session):
|
||||||
trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
|
trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
|
||||||
with pytest.raises(BadRequestError) as exec_info:
|
with pytest.raises(BadRequestError) as exec_info:
|
||||||
assert service_can_send_to_recipient(recipient,
|
service_can_send_to_recipient(recipient,
|
||||||
key_type,
|
key_type,
|
||||||
trial_mode_service) is None
|
trial_mode_service)
|
||||||
assert exec_info.value.status_code == 400
|
assert exec_info.value.status_code == 400
|
||||||
assert exec_info.value.code == 10400
|
assert exec_info.value.code == 10400
|
||||||
assert exec_info.value.message == error_message
|
assert exec_info.value.message == error_message
|
||||||
@@ -133,9 +131,9 @@ def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recip
|
|||||||
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
|
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
|
||||||
live_service = create_service(notify_db, notify_db_session, service_name='live mode', restricted=False)
|
live_service = create_service(notify_db, notify_db_session, service_name='live mode', restricted=False)
|
||||||
with pytest.raises(BadRequestError) as e:
|
with pytest.raises(BadRequestError) as e:
|
||||||
assert service_can_send_to_recipient("0758964221",
|
service_can_send_to_recipient("0758964221",
|
||||||
'team',
|
'team',
|
||||||
live_service) is None
|
live_service)
|
||||||
assert e.value.status_code == 400
|
assert e.value.status_code == 400
|
||||||
assert e.value.code == 10400
|
assert e.value.code == 10400
|
||||||
assert e.value.message == 'Can’t send to this recipient using a team-only API key'
|
assert e.value.message == 'Can’t send to this recipient using a team-only API key'
|
||||||
|
|||||||
@@ -25,16 +25,15 @@ def test_post_sms_schema_is_valid(input):
|
|||||||
|
|
||||||
def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
|
def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
|
||||||
j = {"template_id": "notUUID"}
|
j = {"template_id": "notUUID"}
|
||||||
try:
|
with pytest.raises(ValidationError) as e:
|
||||||
validate(j, post_sms_request)
|
validate(j, post_sms_request)
|
||||||
except ValidationError as e:
|
error = json.loads(e.value.message)
|
||||||
error = json.loads(e.message)
|
assert "POST v2/notifications/sms" in error['message']
|
||||||
assert "POST v2/notifications/sms" in error['message']
|
assert len(error.get('fields')) == 2
|
||||||
assert len(error.get('fields')) == 2
|
assert "'phone_number' is a required property" in error['fields']
|
||||||
assert "phone_number" in e.message
|
assert "'template_id' not a valid UUID" in error['fields']
|
||||||
assert "template_id" in e.message
|
assert error.get('code') == '1001'
|
||||||
assert error.get('code') == '1001'
|
assert error.get('link', None) is not None
|
||||||
assert error.get('link', None) is not None
|
|
||||||
|
|
||||||
|
|
||||||
def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
||||||
@@ -44,15 +43,14 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
|||||||
"reference": "reference from caller",
|
"reference": "reference from caller",
|
||||||
"personalisation": "not_a_dict"
|
"personalisation": "not_a_dict"
|
||||||
}
|
}
|
||||||
try:
|
with pytest.raises(ValidationError) as e:
|
||||||
validate(j, post_sms_request)
|
validate(j, post_sms_request)
|
||||||
except ValidationError as e:
|
error = json.loads(e.value.message)
|
||||||
error = json.loads(e.message)
|
assert "POST v2/notifications/sms" in error['message']
|
||||||
assert "POST v2/notifications/sms" in error['message']
|
assert len(error.get('fields')) == 1
|
||||||
assert len(error.get('fields')) == 1
|
assert error['fields'][0] == "'personalisation' should contain key value pairs"
|
||||||
assert "personalisation" in e.message
|
assert error.get('code') == '1001'
|
||||||
assert error.get('code') == '1001'
|
assert error.get('link', None) is not None
|
||||||
assert error.get('link', None) is not None
|
|
||||||
|
|
||||||
|
|
||||||
valid_response = {
|
valid_response = {
|
||||||
@@ -85,7 +83,6 @@ def test_post_sms_response_schema_is_valid(input):
|
|||||||
def test_post_sms_response_schema_missing_uri():
|
def test_post_sms_response_schema_missing_uri():
|
||||||
j = valid_response
|
j = valid_response
|
||||||
del j["uri"]
|
del j["uri"]
|
||||||
try:
|
with pytest.raises(ValidationError) as e:
|
||||||
validate(j, post_sms_response)
|
validate(j, post_sms_response)
|
||||||
except ValidationError as e:
|
assert 'uri' in e.value.message
|
||||||
assert 'uri' in e.message
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import uuid
|
|||||||
|
|
||||||
from flask import json
|
from flask import json
|
||||||
|
|
||||||
|
from app.models import Notification
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
|
|
||||||
|
|
||||||
@@ -22,10 +23,17 @@ def test_post_sms_notification_returns_201(notify_api, sample_template, mocker):
|
|||||||
|
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
resp_json = json.loads(response.get_data(as_text=True))
|
resp_json = json.loads(response.get_data(as_text=True))
|
||||||
|
notifications = Notification.query.all()
|
||||||
|
assert len(notifications) == 1
|
||||||
|
notification_id = notifications[0].id
|
||||||
assert resp_json['id'] is not None
|
assert resp_json['id'] is not None
|
||||||
assert resp_json['reference'] is None
|
assert resp_json['reference'] is None
|
||||||
|
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']
|
||||||
assert resp_json['template']['id'] == str(sample_template.id)
|
assert resp_json['template']['id'] == str(sample_template.id)
|
||||||
assert resp_json['template']['version'] == sample_template.version
|
assert resp_json['template']['version'] == sample_template.version
|
||||||
|
assert 'v2/templates/{}'.format(sample_template.id) in resp_json['template']['uri']
|
||||||
assert mocked.called
|
assert mocked.called
|
||||||
|
|
||||||
|
|
||||||
@@ -70,7 +78,9 @@ def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api
|
|||||||
assert response.headers['Content-type'] == 'application/json'
|
assert response.headers['Content-type'] == 'application/json'
|
||||||
error_resp = json.loads(response.get_data(as_text=True))
|
error_resp = json.loads(response.get_data(as_text=True))
|
||||||
assert error_resp['code'] == 401
|
assert error_resp['code'] == 401
|
||||||
assert error_resp['message'] == {'token': ['Unauthorized, authentication token must be provided']}
|
assert error_resp['message'] == 'Unauthorized, authentication token must be provided'
|
||||||
|
assert error_resp['fields'] == {'token': ['Unauthorized, authentication token must be provided']}
|
||||||
|
assert error_resp['link'] == 'link to docs'
|
||||||
|
|
||||||
|
|
||||||
def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, sample_template):
|
def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, sample_template):
|
||||||
|
|||||||
Reference in New Issue
Block a user