add inbound sms api

two endpoints:
* get all inbound sms for a service (you can limit to the X most
  recent, or filter by user's phone number [which will be normalised])
* get a summary of inbound sms for a service - returns the count of
  inbound sms in the database, and the date that the most recent was
  sent
This commit is contained in:
Leo Hemsted
2017-05-31 14:49:14 +01:00
parent d89cb2c120
commit ef52337d85
9 changed files with 255 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
from datetime import datetime
from freezegun import freeze_time
from app.dao.inbound_sms_dao import dao_get_inbound_sms_for_service, dao_count_inbound_sms_for_service
from tests.app.db import create_inbound_sms, create_service
def test_get_all_inbound_sms(sample_service):
inbound = create_inbound_sms(sample_service)
res = dao_get_inbound_sms_for_service(sample_service.id)
assert len(res) == 1
assert res[0] == inbound
def test_get_all_inbound_sms_when_none_exist(sample_service):
res = dao_get_inbound_sms_for_service(sample_service.id)
assert len(res) == 0
def test_get_all_inbound_sms_limits_and_orders(sample_service):
with freeze_time('2017-01-01'):
one = create_inbound_sms(sample_service)
with freeze_time('2017-01-03'):
three = create_inbound_sms(sample_service)
with freeze_time('2017-01-02'):
two = create_inbound_sms(sample_service)
res = dao_get_inbound_sms_for_service(sample_service.id, limit=2)
assert len(res) == 2
assert res[0] == three
assert res[0].created_at == datetime(2017, 1, 3)
assert res[1] == two
assert res[1].created_at == datetime(2017, 1, 2)
def test_get_all_inbound_sms_filters_on_service(notify_db_session):
service_one = create_service(service_name='one')
service_two = create_service(service_name='two')
sms_one = create_inbound_sms(service_one)
sms_two = create_inbound_sms(service_two)
res = dao_get_inbound_sms_for_service(service_one.id)
assert len(res) == 1
assert res[0] == sms_one
def test_count_inbound_sms_for_service(notify_db_session):
service_one = create_service(service_name='one')
service_two = create_service(service_name='two')
create_inbound_sms(service_one)
create_inbound_sms(service_one)
create_inbound_sms(service_two)
assert dao_count_inbound_sms_for_service(service_one.id) == 2

View File

@@ -4,6 +4,7 @@ import uuid
from app.dao.jobs_dao import dao_create_job
from app.models import (
InboundSms,
Service,
User,
Template,
@@ -20,6 +21,7 @@ from app.dao.notifications_dao import dao_create_notification, dao_created_sched
from app.dao.templates_dao import dao_create_template
from app.dao.services_dao import dao_create_service
from app.dao.service_permissions_dao import dao_add_service_permission
from app.dao.inbound_sms_dao import dao_create_inbound_sms
def create_user(mobile_number="+447700900986", email="notify@digital.cabinet-office.gov.uk", state='active'):
@@ -183,3 +185,24 @@ def create_service_permission(service_id, permission=EMAIL_TYPE):
service_permissions = ServicePermission.query.all()
return service_permissions
def create_inbound_sms(
service,
notify_number=None,
user_number='7700900111',
provider_date=None,
provider_reference=None,
content='Hello'
):
inbound = InboundSms(
service=service,
created_at=datetime.utcnow(),
notify_number=notify_number or service.sms_sender,
user_number=user_number,
provider_date=provider_date or datetime.utcnow(),
provider_reference=provider_reference or 'foo',
content=content,
)
dao_create_inbound_sms(inbound)
return inbound

View File

View File

@@ -0,0 +1,93 @@
from datetime import datetime
from freezegun import freeze_time
from tests.app.db import create_inbound_sms, create_service
def test_get_inbound_sms(admin_request, sample_service):
one = create_inbound_sms(sample_service)
two = create_inbound_sms(sample_service)
json_resp = admin_request.get(
'inbound_sms.get_inbound_sms_for_service',
endpoint_kwargs={'service_id': sample_service.id}
)
sms = json_resp['data']
assert len(sms) == 2
assert {inbound['id'] for inbound in sms} == {str(one.id), str(two.id)}
assert sms[0]['content'] == 'Hello'
assert set(sms[0].keys()) == {
'id',
'created_at',
'service_id',
'notify_number',
'user_number',
'content',
'provider_date',
'provider_reference'
}
def test_get_inbound_sms_limits(admin_request, sample_service):
with freeze_time('2017-01-01'):
one = create_inbound_sms(sample_service)
with freeze_time('2017-01-02'):
two = create_inbound_sms(sample_service)
sms = admin_request.get(
'inbound_sms.get_inbound_sms_for_service',
endpoint_kwargs={'service_id': sample_service.id, 'limit': 1}
)
assert len(sms['data']) == 1
assert sms['data'][0]['id'] == str(two.id)
def test_get_inbound_sms_filters_user_number(admin_request, sample_service):
# user_number in the db is normalised
one = create_inbound_sms(sample_service, user_number='7700900001')
two = create_inbound_sms(sample_service, user_number='7700900002')
sms = admin_request.get(
'inbound_sms.get_inbound_sms_for_service',
endpoint_kwargs={'service_id': sample_service.id, 'user_number': '(07700) 900-001'}
)
assert len(sms['data']) == 1
assert sms['data'][0]['id'] == str(one.id)
assert sms['data'][0]['user_number'] == str(one.user_number)
def test_get_inbound_sms_summary(admin_request, sample_service):
other_service = create_service(service_name='other_service')
with freeze_time('2017-01-01'):
create_inbound_sms(sample_service)
with freeze_time('2017-01-02'):
create_inbound_sms(sample_service)
with freeze_time('2017-01-03'):
create_inbound_sms(other_service)
summary = admin_request.get(
'inbound_sms.get_inbound_sms_summary_for_service',
endpoint_kwargs={'service_id': sample_service.id}
)
assert summary == {
'count': 2,
'most_recent': datetime(2017, 1, 2).isoformat()
}
def test_get_inbound_sms_summary_with_no_inbound(admin_request, sample_service):
summary = admin_request.get(
'inbound_sms.get_inbound_sms_summary_for_service',
endpoint_kwargs={'service_id': sample_service.id}
)
assert summary == {
'count': 0,
'most_recent': None
}