From b8e6689f624335142ebfbe1a4cb24aef28ee4182 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 15 Jul 2020 17:31:59 +0100 Subject: [PATCH] allow platform admins to approve their own messages makes development a lot easier. we should remove this when we go live --- app/broadcast_message/rest.py | 11 ++++++++-- tests/app/broadcast_message/test_rest.py | 27 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/broadcast_message/rest.py b/app/broadcast_message/rest.py index f0c10a3e2..ba4858a1d 100644 --- a/app/broadcast_message/rest.py +++ b/app/broadcast_message/rest.py @@ -41,8 +41,15 @@ def _update_broadcast_message(broadcast_message, new_status, updating_user): # TODO: Restrict status transitions # TODO: validate that the user belongs to the same service, isn't the creator, has permissions, etc if new_status == BroadcastStatusType.BROADCASTING: - broadcast_message.approved_at = datetime.utcnow() - broadcast_message.approved_by = updating_user + # TODO: Remove this platform admin shortcut when the feature goes live + if updating_user == broadcast_message.created_by and not updating_user.platform_admin: + abort( + 400, + f'User {updating_user.id} cannot approve their own broadcast {broadcast_message.id}' + ) + else: + broadcast_message.approved_at = datetime.utcnow() + broadcast_message.approved_by = updating_user if new_status == BroadcastStatusType.CANCELLED: broadcast_message.cancelled_at = datetime.utcnow() diff --git a/tests/app/broadcast_message/test_rest.py b/tests/app/broadcast_message/test_rest.py index 7826fdc7e..0cbfb7a13 100644 --- a/tests/app/broadcast_message/test_rest.py +++ b/tests/app/broadcast_message/test_rest.py @@ -326,3 +326,30 @@ def test_update_broadcast_message_status_rejects_approval_from_creator( assert mock_task.called is False assert f'cannot approve their own broadcast' in response['message'] + + +def test_update_broadcast_message_status_allows_platform_admin_to_approve_own_message( + notify_db, + admin_request, + sample_service, + mocker +): + user = sample_service.created_by + user.platform_admin = True + t = create_template(sample_service, BROADCAST_TYPE) + bm = create_broadcast_message(t, status=BroadcastStatusType.PENDING_APPROVAL) + mock_task = mocker.patch('app.celery.broadcast_message_tasks.send_broadcast_message.apply_async') + + response = admin_request.post( + 'broadcast_message.update_broadcast_message_status', + _data={'status': BroadcastStatusType.BROADCASTING, 'created_by': str(user.id)}, + service_id=t.service_id, + broadcast_message_id=bm.id, + _expected_status=200 + ) + + assert response['status'] == BroadcastStatusType.BROADCASTING + assert response['approved_at'] is not None + assert response['created_by_id'] == str(user.id) + assert response['approved_by_id'] == str(user.id) + mock_task.assert_called_once_with(kwargs={'broadcast_message_id': str(bm.id)}, queue='notify-internal-tasks')