From 3fdd180515c3acf25ef3c2b799baa06c718a2a11 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 13 Jun 2017 18:00:25 +0100 Subject: [PATCH] New endpoint to set ServiceInboundApi data. --- app/service/rest.py | 12 ++++++++- .../app/dao/test_save_service_inbound_api.py | 16 ++++++++++++ tests/app/service/test_rest.py | 25 ++++++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 5c0dfae6a..d1f60d56f 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -20,6 +20,7 @@ from app.dao.api_key_dao import ( expire_api_key) from app.dao.date_util import get_financial_year from app.dao.notification_usage_dao import get_total_billable_units_for_sent_sms_notifications_in_date_range +from app.dao.service_inbound_api_dao import save_service_inbound_api from app.dao.services_dao import ( dao_fetch_service_by_id, dao_fetch_all_services, @@ -49,7 +50,7 @@ from app.errors import ( InvalidRequest, register_errors ) -from app.models import Service +from app.models import Service, ServiceInboundApi from app.service import statistics from app.service.utils import get_whitelist_objects from app.service.sender import send_notification_to_service_users @@ -531,3 +532,12 @@ def get_yearly_monthly_usage(service_id): return json.dumps(json_results) except TypeError: return jsonify(result='error', message='No valid year provided'), 400 + + +@service_blueprint.route('//inbound-api', methods=['POST']) +def set_service_inbound_api(service_id): + data = request.get_json() + data["service_id"] = service_id + save_service_inbound_api(ServiceInboundApi(**data)) + + return jsonify(data="Service inbound api data saved"), 201 diff --git a/tests/app/dao/test_save_service_inbound_api.py b/tests/app/dao/test_save_service_inbound_api.py index f24222fe9..3c6f03b9e 100644 --- a/tests/app/dao/test_save_service_inbound_api.py +++ b/tests/app/dao/test_save_service_inbound_api.py @@ -1,3 +1,8 @@ +import uuid + +import pytest +from sqlalchemy.exc import SQLAlchemyError + from app.dao.service_inbound_api_dao import save_service_inbound_api from app.models import ServiceInboundApi @@ -18,3 +23,14 @@ def test_save_service_inbound_api(sample_service): assert results[0].url == "https::/some_service/inbound_messages" assert results[0].unsigned_bearer_token == "some_unique_string" assert results[0].bearer_token != "some_unique_string" + + +def test_save_service_inbound_api_fails_if_service_doesnot_exist(notify_db, notify_db_session): + service_inbound_api = ServiceInboundApi( + service_id=uuid.uuid4(), + url="https::/some_service/inbound_messages", + bearer_token="some_unique_string" + ) + + with pytest.raises(SQLAlchemyError): + save_service_inbound_api(service_inbound_api) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 70622bac1..8b7f6f575 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -14,8 +14,8 @@ from app.models import ( Organisation, Rate, Service, ServicePermission, User, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, INTERNATIONAL_SMS_TYPE, INBOUND_SMS_TYPE, - DVLA_ORG_LAND_REGISTRY -) + DVLA_ORG_LAND_REGISTRY, + ServiceInboundApi) from tests import create_authorization_header from tests.app.db import create_template from tests.app.conftest import ( @@ -27,7 +27,6 @@ from tests.app.conftest import ( ) from tests.app.db import create_user -from tests.conftest import set_config_values def test_get_service_list(client, service_factory): @@ -2149,3 +2148,23 @@ def test_search_for_notification_by_to_field_returns_content( assert notifications[0]['id'] == str(notification.id) assert notifications[0]['to'] == '+447700900855' assert notifications[0]['body'] == 'Hello Foo\nYour thing is due soon' + + +def test_set_service_inbound_api(client, sample_service): + data = { + "url": "https://some_service/inbound-sms", + "bearer_token": "some-unique-string" + } + response = client.post( + '/service/{}/inbound-api'.format(sample_service.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), create_authorization_header()] + ) + assert response.status_code == 201 + + api_data = ServiceInboundApi.query.all() + assert len(api_data) == 1 + assert api_data[0].service_id == sample_service.id + assert api_data[0].url == "https://some_service/inbound-sms" + assert api_data[0].unsigned_bearer_token == "some-unique-string" + assert api_data[0].bearer_token != "some-unique-string"