diff --git a/app/service/callback_rest.py b/app/service/callback_rest.py index 95098e989..4c177bab4 100644 --- a/app/service/callback_rest.py +++ b/app/service/callback_rest.py @@ -5,11 +5,6 @@ from flask import ( ) from sqlalchemy.exc import SQLAlchemyError -from app.dao.service_inbound_api_dao import ( - save_service_inbound_api, - reset_service_inbound_api, - get_service_inbound_api -) from app.errors import ( register_errors ) @@ -21,6 +16,11 @@ from app.service.service_callback_api_schema import ( create_service_callback_api_schema, update_service_callback_api_schema ) +from app.dao.service_inbound_api_dao import ( + save_service_inbound_api, + get_service_inbound_api, + reset_service_inbound_api +) service_callback_blueprint = Blueprint('service_callback', __name__, url_prefix='/service/') diff --git a/tests/app/service/test_callback_rest.py b/tests/app/service/test_callback_rest.py new file mode 100644 index 000000000..0937bc9f3 --- /dev/null +++ b/tests/app/service/test_callback_rest.py @@ -0,0 +1,86 @@ +import json +import uuid + +from tests import create_authorization_header + +from tests.app.db import ( + create_service_inbound_api, +) + + +def test_create_service_inbound_api(client, sample_service): + data = { + "url": "https://some_service/inbound-sms", + "bearer_token": "some-unique-string", + "updated_by_id": str(sample_service.users[0].id) + } + 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 + + resp_json = json.loads(response.get_data(as_text=True))["data"] + assert resp_json["id"] + assert resp_json["service_id"] == str(sample_service.id) + assert resp_json["url"] == "https://some_service/inbound-sms" + assert resp_json["updated_by_id"] == str(sample_service.users[0].id) + assert resp_json["created_at"] + assert not resp_json["updated_at"] + + +def test_set_service_inbound_api_raises_404_when_service_does_not_exist(client): + data = { + "url": "https://some_service/inbound-sms", + "bearer_token": "some-unique-string", + "updated_by_id": str(uuid.uuid4()) + } + response = client.post( + '/service/{}/inbound-api'.format(uuid.uuid4()), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), create_authorization_header()] + ) + assert response.status_code == 404 + assert json.loads(response.get_data(as_text=True))['message'] == 'No result found' + + +def test_update_service_inbound_api_updates_url(client, sample_service): + service_inbound_api = create_service_inbound_api(service=sample_service, + url="https://original_url.com") + + data = { + "url": "https://another_url.com", + "updated_by_id": str(sample_service.users[0].id) + } + response = client.post("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + assert response.status_code == 200 + resp_json = json.loads(response.get_data(as_text=True))["data"] + assert resp_json["url"] == "https://another_url.com" + assert service_inbound_api.url == "https://another_url.com" + + +def test_update_service_inbound_api_updates_bearer_token(client, sample_service): + service_inbound_api = create_service_inbound_api(service=sample_service, + bearer_token="some_super_secret") + data = { + "bearer_token": "different_token", + "updated_by_id": str(sample_service.users[0].id) + } + response = client.post("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + assert response.status_code == 200 + assert service_inbound_api.bearer_token == "different_token" + + +def test_fetch_service_inbound_api(client, sample_service): + service_inbound_api = create_service_inbound_api(service=sample_service) + + response = client.get("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), + headers=[create_authorization_header()]) + + assert response.status_code == 200 + assert json.loads(response.get_data(as_text=True))["data"] == service_inbound_api.serialize() diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 109430a3c..a90278f0b 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -2225,84 +2225,6 @@ def test_search_for_notification_by_to_field_returns_content( assert notifications[0]['template']['content'] == 'Hello (( Name))\nYour thing is due soon' -def test_create_service_inbound_api(client, sample_service): - data = { - "url": "https://some_service/inbound-sms", - "bearer_token": "some-unique-string", - "updated_by_id": str(sample_service.users[0].id) - } - 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 - - resp_json = json.loads(response.get_data(as_text=True))["data"] - assert resp_json["id"] - assert resp_json["service_id"] == str(sample_service.id) - assert resp_json["url"] == "https://some_service/inbound-sms" - assert resp_json["updated_by_id"] == str(sample_service.users[0].id) - assert resp_json["created_at"] - assert not resp_json["updated_at"] - - -def test_set_service_inbound_api_raises_404_when_service_does_not_exist(client): - data = { - "url": "https://some_service/inbound-sms", - "bearer_token": "some-unique-string", - "updated_by_id": str(uuid.uuid4()) - } - response = client.post( - '/service/{}/inbound-api'.format(uuid.uuid4()), - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), create_authorization_header()] - ) - assert response.status_code == 404 - assert json.loads(response.get_data(as_text=True))['message'] == 'No result found' - - -def test_update_service_inbound_api_updates_url(client, sample_service): - service_inbound_api = create_service_inbound_api(service=sample_service, - url="https://original_url.com") - - data = { - "url": "https://another_url.com", - "updated_by_id": str(sample_service.users[0].id) - } - response = client.post("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - assert response.status_code == 200 - resp_json = json.loads(response.get_data(as_text=True))["data"] - assert resp_json["url"] == "https://another_url.com" - assert service_inbound_api.url == "https://another_url.com" - - -def test_update_service_inbound_api_updates_bearer_token(client, sample_service): - service_inbound_api = create_service_inbound_api(service=sample_service, - bearer_token="some_super_secret") - data = { - "bearer_token": "different_token", - "updated_by_id": str(sample_service.users[0].id) - } - response = client.post("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - assert response.status_code == 200 - assert service_inbound_api.bearer_token == "different_token" - - -def test_fetch_service_inbound_api(client, sample_service): - service_inbound_api = create_service_inbound_api(service=sample_service) - - response = client.get("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), - headers=[create_authorization_header()]) - - assert response.status_code == 200 - assert json.loads(response.get_data(as_text=True))["data"] == service_inbound_api.serialize() - - def test_send_one_off_notification(admin_request, mocker): service = create_service() template = create_template(service=service)