From 9f4b82f07476a1206c192535ec52225f1da33921 Mon Sep 17 00:00:00 2001 From: David McDonald Date: Fri, 5 Feb 2021 17:10:41 +0000 Subject: [PATCH] Make service a member of the broadcast organisation We will use this to easily identify all our broadcast services. There could be other ways to deal with finding and seeing all broadcast services but this is a good and easy way to start. --- app/config.py | 3 ++ app/service/rest.py | 11 +++++-- tests/app/conftest.py | 15 ++++++++-- tests/app/service/test_rest.py | 55 +++++++++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 12 deletions(-) diff --git a/app/config.py b/app/config.py index 30acf16d7..e7ff9869d 100644 --- a/app/config.py +++ b/app/config.py @@ -384,6 +384,9 @@ class Config(object): ENABLED_CBCS = {BroadcastProvider.EE, BroadcastProvider.THREE, BroadcastProvider.O2, BroadcastProvider.VODAFONE} + # as defined in api db migration 0331_add_broadcast_org.py + BROADCAST_ORGANISATION_ID = '38e4bf69-93b0-445d-acee-53ea53fe02df' + ###################### # Config overrides ### diff --git a/app/service/rest.py b/app/service/rest.py index c1f9d22ad..756828cb5 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -29,7 +29,10 @@ from app.dao.fact_notification_status_dao import ( fetch_stats_for_all_services_by_date_range, fetch_monthly_template_usage_for_service ) from app.dao.inbound_numbers_dao import dao_allocate_number_for_service -from app.dao.organisation_dao import dao_get_organisation_by_service_id +from app.dao.organisation_dao import ( + dao_get_organisation_by_service_id, + dao_add_service_to_organisation, +) from app.dao.returned_letters_dao import ( fetch_most_recent_returned_letter, fetch_recent_returned_letter_count, @@ -1094,9 +1097,9 @@ def set_as_broadcast_service(service_id): """ This route does four things - adds a service broadcast settings to define which channel broadcasts should go out on - - removes all current service permissions - - adds the broadcast service permission + - removes all current service permissions and adds the broadcast service permission - sets the services `count_as_live` to false + - adds the service to the broadcast organisation """ data = validate(request.get_json(), service_broadcast_settings_schema) service = dao_fetch_service_by_id(service_id) @@ -1111,5 +1114,7 @@ def set_as_broadcast_service(service_id): service.count_as_live = False dao_update_service(service) + dao_add_service_to_organisation(service, current_app.config['BROADCAST_ORGANISATION_ID']) + data = service_schema.dump(service).data return jsonify(data=data) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 698d59e3e..33e68b630 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -14,7 +14,7 @@ from app.dao.api_key_dao import save_model_api_key from app.dao.invited_user_dao import save_invited_user from app.dao.jobs_dao import dao_create_job from app.dao.notifications_dao import dao_create_notification -from app.dao.organisation_dao import dao_create_organisation +from app.dao.organisation_dao import dao_create_organisation, dao_add_service_to_organisation from app.dao.services_dao import (dao_create_service, dao_add_user_to_service) from app.dao.service_broadcast_settings_dao import insert_or_update_service_broadcast_settings from app.dao.templates_dao import dao_create_template @@ -151,7 +151,7 @@ def sample_service(notify_db_session): @pytest.fixture(scope='function') -def sample_broadcast_service(notify_db_session): +def sample_broadcast_service(notify_db_session, broadcast_organisation): user = create_user() service_name = 'Sample broadcast service' email_from = service_name.lower().replace(' ', '.') @@ -170,6 +170,7 @@ def sample_broadcast_service(notify_db_session): service = Service(**data) dao_create_service(service, user, service_permissions=[BROADCAST_TYPE]) insert_or_update_service_broadcast_settings(service, channel="severe") + dao_add_service_to_organisation(service, current_app.config['BROADCAST_ORGANISATION_ID']) else: if user not in service.users: dao_add_user_to_service(service, user) @@ -877,6 +878,16 @@ def sample_organisation(notify_db_session): return org +@pytest.fixture +def broadcast_organisation(notify_db_session): + org = Organisation.query.get(current_app.config['BROADCAST_ORGANISATION_ID']) + if not org: + org = Organisation(id=current_app.config['BROADCAST_ORGANISATION_ID'], name='broadcast organisation') + dao_create_organisation(org) + + return org + + @pytest.fixture def restore_provider_details(notify_db, notify_db_session): """ diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 16fa1488e..aab284edc 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -3652,7 +3652,9 @@ def test_get_returned_letter(admin_request, sample_letter_template): @pytest.mark.parametrize('channel', ["test", "severe"]) -def test_set_as_broadcast_service_sets_broadcast_channel(client, notify_db, sample_service, channel): +def test_set_as_broadcast_service_sets_broadcast_channel( + client, notify_db, sample_service, broadcast_organisation, channel +): assert sample_service.service_broadcast_settings is None data = { 'broadcast_channel': channel, @@ -3674,7 +3676,9 @@ def test_set_as_broadcast_service_sets_broadcast_channel(client, notify_db, samp assert records[0].channel == channel -def test_set_as_broadcast_service_updates_channel_for_broadcast_service(client, notify_db, sample_broadcast_service): +def test_set_as_broadcast_service_updates_channel_for_broadcast_service( + client, notify_db, sample_broadcast_service, broadcast_organisation +): assert sample_broadcast_service.broadcast_channel == "severe" resp = client.post( @@ -3696,7 +3700,9 @@ def test_set_as_broadcast_service_updates_channel_for_broadcast_service(client, @pytest.mark.parametrize('channel', ["government", "extreme", "exercise", "random", ""]) -def test_set_as_broadcast_service_rejects_unknown_channels(client, notify_db, sample_service, channel): +def test_set_as_broadcast_service_rejects_unknown_channels( + client, notify_db, sample_service, broadcast_organisation, channel +): data = { 'broadcast_channel': channel, } @@ -3709,7 +3715,7 @@ def test_set_as_broadcast_service_rejects_unknown_channels(client, notify_db, sa assert resp.status_code == 400 -def test_set_as_broadcast_service_rejects_if_no_channel(client, notify_db, sample_service): +def test_set_as_broadcast_service_rejects_if_no_channel(client, notify_db, sample_service, broadcast_organisation): data = {} resp = client.post( @@ -3720,7 +3726,9 @@ def test_set_as_broadcast_service_rejects_if_no_channel(client, notify_db, sampl assert resp.status_code == 400 -def test_set_as_broadcast_service_gives_broadcast_permission_and_removes_other_permissions(client, notify_db, sample_service): +def test_set_as_broadcast_service_gives_broadcast_permission_and_removes_other_permissions( + client, notify_db, sample_service, broadcast_organisation +): current_permissions = [p.permission for p in sample_service.permissions] assert len(current_permissions) > 0 assert current_permissions != [BROADCAST_TYPE] @@ -3741,7 +3749,7 @@ def test_set_as_broadcast_service_gives_broadcast_permission_and_removes_other_p def test_set_as_broadcast_service_maintains_broadcast_permission_for_existing_broadcast_service( - client, notify_db, sample_broadcast_service + client, notify_db, sample_broadcast_service, broadcast_organisation ): current_permissions = [p.permission for p in sample_broadcast_service.permissions] assert current_permissions == [BROADCAST_TYPE] @@ -3761,7 +3769,7 @@ def test_set_as_broadcast_service_maintains_broadcast_permission_for_existing_br assert [p.permission for p in permissions] == [BROADCAST_TYPE] -def test_set_as_broadcast_service_sets_count_as_live_to_false(client, notify_db, sample_service): +def test_set_as_broadcast_service_sets_count_as_live_to_false(client, notify_db, sample_service, broadcast_organisation): assert sample_service.count_as_live == True resp = client.post( @@ -3777,3 +3785,36 @@ def test_set_as_broadcast_service_sets_count_as_live_to_false(client, notify_db, service_from_db = Service.query.filter_by(id=sample_service.id).all()[0] assert service_from_db.count_as_live == False + + +def test_set_as_broadcast_service_sets_service_org_to_broadcast_org(client, notify_db, sample_service, broadcast_organisation): + assert sample_service.organisation_id != current_app.config['BROADCAST_ORGANISATION_ID'] + + resp = client.post( + '/service/{}/set-as-broadcast-service'.format(sample_service.id), + data=json.dumps({ + 'broadcast_channel': "severe", + }), + headers=[('Content-Type', 'application/json'), create_authorization_header()] + ) + result = resp.json + assert resp.status_code == 200 + assert result['data']['organisation'] == current_app.config['BROADCAST_ORGANISATION_ID'] + + service_from_db = Service.query.filter_by(id=sample_service.id).all()[0] + assert str(service_from_db.organisation_id) == current_app.config['BROADCAST_ORGANISATION_ID'] + + +def test_set_as_broadcast_service_does_not_error_if_run_on_a_service_that_is_already_a_broadcast_service( + client, notify_db, sample_service, broadcast_organisation +): + for _ in range(2): + resp = client.post( + '/service/{}/set-as-broadcast-service'.format(sample_service.id), + data=json.dumps({ + 'broadcast_channel': "severe", + }), + headers=[('Content-Type', 'application/json'), create_authorization_header()] + ) + result = resp.json + assert resp.status_code == 200