mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Capture the fire text callbacks.
Parse the form data, and stop the message
This commit is contained in:
@@ -86,7 +86,34 @@ def create_inbound_mmg_sms_object(service, json):
|
|||||||
@receive_notifications_blueprint.route('/notifications/sms/receive/firetext', methods=['POST'])
|
@receive_notifications_blueprint.route('/notifications/sms/receive/firetext', methods=['POST'])
|
||||||
def receive_firetext_sms():
|
def receive_firetext_sms():
|
||||||
post_data = request.form
|
post_data = request.form
|
||||||
current_app.logger.info("Received Firetext notification form data: {}".format(post_data))
|
|
||||||
|
potential_services = dao_fetch_services_by_sms_sender(post_data['destination'])
|
||||||
|
if len(potential_services) != 1:
|
||||||
|
current_app.logger.error('Inbound number "{}" not associated with exactly one service'.format(
|
||||||
|
post_data['source']
|
||||||
|
))
|
||||||
|
statsd_client.incr('inbound.firetext.failed')
|
||||||
|
return jsonify({
|
||||||
|
"status": "ok"
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
service = potential_services[0]
|
||||||
|
|
||||||
|
user_number = normalise_phone_number(post_data['source'])
|
||||||
|
message = post_data['message']
|
||||||
|
timestamp = post_data['time']
|
||||||
|
|
||||||
|
dao_create_inbound_sms(
|
||||||
|
InboundSms(
|
||||||
|
service=service,
|
||||||
|
notify_number=service.sms_sender,
|
||||||
|
user_number=user_number,
|
||||||
|
provider_date=timestamp,
|
||||||
|
content=message
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
statsd_client.incr('inbound.firetext.successful')
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"status": "ok"
|
"status": "ok"
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from unittest.mock import call
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from flask import json
|
from flask import json
|
||||||
@@ -10,6 +11,7 @@ from app.notifications.receive_notifications import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from app.models import InboundSms
|
from app.models import InboundSms
|
||||||
|
from tests.app.conftest import sample_service
|
||||||
from tests.app.db import create_service
|
from tests.app.db import create_service
|
||||||
|
|
||||||
|
|
||||||
@@ -92,7 +94,11 @@ def test_receive_notification_error_if_not_single_matching_service(client, notif
|
|||||||
assert InboundSms.query.count() == 0
|
assert InboundSms.query.count() == 0
|
||||||
|
|
||||||
|
|
||||||
def test_receive_notification_returns_received_to_firetext(client):
|
def test_receive_notification_returns_received_to_firetext(notify_db_session, client, mocker):
|
||||||
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
||||||
|
|
||||||
|
create_service(service_name='b', sms_sender='07111111111')
|
||||||
|
|
||||||
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
||||||
|
|
||||||
response = client.post(
|
response = client.post(
|
||||||
@@ -103,4 +109,73 @@ def test_receive_notification_returns_received_to_firetext(client):
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
result = json.loads(response.get_data(as_text=True))
|
result = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
|
mock.assert_has_calls([call('inbound.firetext.successful')])
|
||||||
|
|
||||||
assert result['status'] == 'ok'
|
assert result['status'] == 'ok'
|
||||||
|
|
||||||
|
|
||||||
|
def test_receive_notification_from_firetext_persists_message(notify_db_session, client, mocker):
|
||||||
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
||||||
|
|
||||||
|
service = create_service(service_name='b', sms_sender='07111111111')
|
||||||
|
|
||||||
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
||||||
|
|
||||||
|
response = client.post(
|
||||||
|
path='/notifications/sms/receive/firetext',
|
||||||
|
data=data,
|
||||||
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
result = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
|
persisted = InboundSms.query.first()
|
||||||
|
|
||||||
|
assert result['status'] == 'ok'
|
||||||
|
assert persisted.notify_number == '07111111111'
|
||||||
|
assert persisted.user_number == '7999999999'
|
||||||
|
assert persisted.service == service
|
||||||
|
assert persisted.content == 'this is a message'
|
||||||
|
assert persisted.provider_date == datetime(2017, 1, 1, 12, 0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_receive_notification_from_firetext_persists_message_with_normalized_phone(notify_db_session, client, mocker):
|
||||||
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
||||||
|
|
||||||
|
create_service(service_name='b', sms_sender='07111111111')
|
||||||
|
|
||||||
|
data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
||||||
|
|
||||||
|
response = client.post(
|
||||||
|
path='/notifications/sms/receive/firetext',
|
||||||
|
data=data,
|
||||||
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
result = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
|
persisted = InboundSms.query.first()
|
||||||
|
|
||||||
|
assert result['status'] == 'ok'
|
||||||
|
assert persisted.user_number == '447999999999'
|
||||||
|
|
||||||
|
|
||||||
|
def test_returns_ok_to_firetext_if_mismatched_sms_sender(notify_db_session, client, mocker):
|
||||||
|
|
||||||
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
||||||
|
|
||||||
|
create_service(service_name='b', sms_sender='07111111199')
|
||||||
|
|
||||||
|
data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
||||||
|
|
||||||
|
response = client.post(
|
||||||
|
path='/notifications/sms/receive/firetext',
|
||||||
|
data=data,
|
||||||
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
result = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
|
assert not InboundSms.query.all()
|
||||||
|
assert result['status'] == 'ok'
|
||||||
|
mock.assert_has_calls([call('inbound.firetext.failed')])
|
||||||
|
|||||||
Reference in New Issue
Block a user