Merge pull request #1021 from alphagov/imdad-feat-get-inbound-by-id

Add endpoint to get inbound sms by id
This commit is contained in:
Imdad Ahad
2017-06-07 16:24:17 +01:00
committed by GitHub
4 changed files with 68 additions and 3 deletions

View File

@@ -47,3 +47,10 @@ def delete_inbound_sms_created_more_than_a_week_ago():
).delete(synchronize_session='fetch')
return deleted
def dao_get_inbound_sms_by_id(service_id, inbound_id):
return InboundSms.query.filter_by(
id=inbound_id,
service_id=service_id
).one()

View File

@@ -3,15 +3,20 @@ from flask import (
jsonify,
request
)
from notifications_utils.recipients import validate_and_format_phone_number
from app.dao.inbound_sms_dao import dao_get_inbound_sms_for_service, dao_count_inbound_sms_for_service
from app.dao.inbound_sms_dao import (
dao_get_inbound_sms_for_service,
dao_count_inbound_sms_for_service,
dao_get_inbound_sms_by_id
)
from app.errors import register_errors
inbound_sms = Blueprint(
'inbound_sms',
__name__,
url_prefix='/service/<service_id>/inbound-sms'
url_prefix='/service/<uuid:service_id>/inbound-sms'
)
register_errors(inbound_sms)
@@ -40,3 +45,10 @@ def get_inbound_sms_summary_for_service(service_id):
count=count,
most_recent=most_recent[0].created_at.isoformat() if most_recent else None
)
@inbound_sms.route('/<uuid:inbound_sms_id>', methods=['GET'])
def get_inbound_by_id(service_id, inbound_sms_id):
inbound_sms = dao_get_inbound_sms_by_id(service_id, inbound_sms_id)
return jsonify(inbound_sms.serialize()), 200

View File

@@ -5,7 +5,8 @@ from freezegun import freeze_time
from app.dao.inbound_sms_dao import (
dao_get_inbound_sms_for_service,
dao_count_inbound_sms_for_service,
delete_inbound_sms_created_more_than_a_week_ago
delete_inbound_sms_created_more_than_a_week_ago,
dao_get_inbound_sms_by_id
)
from tests.app.db import create_inbound_sms, create_service
@@ -86,3 +87,11 @@ def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
delete_inbound_sms_created_more_than_a_week_ago()
assert len(InboundSms.query.all()) == 2
def test_get_inbound_sms_by_id_returns(sample_service):
inbound = create_inbound_sms(sample_service)
inbound_from_db = dao_get_inbound_sms_by_id(sample_service.id, inbound.id)
assert inbound == inbound_from_db

View File

@@ -112,3 +112,40 @@ def test_get_inbound_sms_summary_with_no_inbound(admin_request, sample_service):
'count': 0,
'most_recent': None
}
def test_get_inbound_sms_by_id_returns_200(admin_request, sample_service):
inbound = create_inbound_sms(sample_service, user_number='447700900001')
response = admin_request.get(
'inbound_sms.get_inbound_by_id',
endpoint_kwargs={
'service_id': sample_service.id,
'inbound_sms_id': inbound.id
}
)
assert response['user_number'] == '447700900001'
assert response['service_id'] == str(sample_service.id)
def test_get_inbound_sms_by_id_invalid_id_returns_404(admin_request, sample_service):
assert admin_request.get(
'inbound_sms.get_inbound_by_id',
endpoint_kwargs={
'service_id': sample_service.id,
'inbound_sms_id': 'bar'
},
expected_status=404
)
def test_get_inbound_sms_by_id_with_invalid_service_id_returns_404(admin_request, sample_service):
assert admin_request.get(
'inbound_sms.get_inbound_by_id',
endpoint_kwargs={
'service_id': 'foo',
'inbound_sms_id': '2cfbd6a1-1575-4664-8969-f27be0ea40d9'
},
expected_status=404
)