From 5eb02a45a56e0b94189fe629523fb21468164f3b Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Fri, 12 May 2017 14:07:06 +0100 Subject: [PATCH] Send go live email when service goes live --- app/service/rest.py | 16 +++++++++++++ tests/app/db.py | 4 ++-- tests/app/service/test_rest.py | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 4122db82a..6624c8979 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -46,6 +46,7 @@ from app.errors import ( InvalidRequest, register_errors) from app.service import statistics from app.service.utils import get_whitelist_objects +from app.service.sender import send_notification_to_service_users from app.schemas import ( service_schema, api_key_schema, @@ -117,10 +118,25 @@ def create_service(): @service_blueprint.route('/', methods=['POST']) def update_service(service_id): fetched_service = dao_fetch_service_by_id(service_id) + # Capture the status change here as Marshmallow changes this later + service_going_live = fetched_service.restricted and not request.get_json().get('restricted') + current_data = dict(service_schema.dump(fetched_service).data.items()) current_data.update(request.get_json()) update_dict = service_schema.load(current_data).data dao_update_service(update_dict) + + if service_going_live: + send_notification_to_service_users( + service_id=service_id, + template_id=current_app.config['SERVICE_NOW_LIVE_TEMPLATE_ID'], + personalisation={ + 'service_name': current_data['name'], + 'message_limit': current_data['message_limit'] + }, + include_user_fields=['name'] + ) + return jsonify(data=service_schema.dump(fetched_service).data), 200 diff --git a/tests/app/db.py b/tests/app/db.py index 8bbe2bd3b..f1dba8ffa 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -25,11 +25,11 @@ def create_user(mobile_number="+447700900986", email="notify@digital.cabinet-off return user -def create_service(user=None, service_name="Sample service", service_id=None): +def create_service(user=None, service_name="Sample service", service_id=None, restricted=False): service = Service( name=service_name, message_limit=1000, - restricted=False, + restricted=restricted, email_from=service_name.lower().replace(' ', '.'), created_by=user or create_user() ) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 91c928751..8fb5e3fc7 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1658,3 +1658,47 @@ def test_search_for_notification_by_to_field_return_multiple_matches( assert str(notification1.id) in [n["id"] for n in result["notifications"]] assert str(notification2.id) in [n["id"] for n in result["notifications"]] assert str(notification3.id) in [n["id"] for n in result["notifications"]] + + +def test_update_service_calls_send_notification_as_service_becomes_live(notify_db, notify_db_session, client, mocker): + send_notification_mock = mocker.patch('app.service.rest.send_notification_to_service_users') + + restricted_service = create_service( + notify_db, + notify_db_session, + restricted=True + ) + + data = { + "restricted": False + } + + auth_header = create_authorization_header() + resp = client.post( + 'service/{}'.format(restricted_service.id), + data=json.dumps(data), + headers=[auth_header], + content_type='application/json' + ) + + assert resp.status_code == 200 + assert send_notification_mock.called + + +def test_update_service_does_not_call_send_notification_for_live_service(sample_service, client, mocker): + send_notification_mock = mocker.patch('app.service.rest.send_notification_to_service_users') + + data = { + "restricted": True + } + + auth_header = create_authorization_header() + resp = client.post( + 'service/{}'.format(sample_service.id), + data=json.dumps(data), + headers=[auth_header], + content_type='application/json' + ) + + assert resp.status_code == 200 + assert not send_notification_mock.called