mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
Merge pull request #3199 from alphagov/admin-cancel-broadcast
Allow platform admins to cancel broadcasts.
This commit is contained in:
@@ -43,10 +43,12 @@ def _parse_nullable_datetime(dt):
|
||||
|
||||
def _update_broadcast_message(broadcast_message, new_status, updating_user):
|
||||
if updating_user not in broadcast_message.service.users:
|
||||
raise InvalidRequest(
|
||||
f'User {updating_user.id} cannot approve broadcast_message {broadcast_message.id} from other service',
|
||||
status_code=400
|
||||
)
|
||||
# we allow platform admins to cancel broadcasts
|
||||
if not (new_status == BroadcastStatusType.CANCELLED and updating_user.platform_admin):
|
||||
raise InvalidRequest(
|
||||
f'User {updating_user.id} cannot update broadcast_message {broadcast_message.id} from other service',
|
||||
status_code=400
|
||||
)
|
||||
|
||||
if new_status not in BroadcastStatusType.ALLOWED_STATUS_TRANSITIONS[broadcast_message.status]:
|
||||
raise InvalidRequest(
|
||||
|
||||
@@ -488,13 +488,20 @@ def test_update_broadcast_message_status_doesnt_let_you_update_other_things(admi
|
||||
}]
|
||||
|
||||
|
||||
def test_update_broadcast_message_status_stores_cancelled_by_and_cancelled_at(
|
||||
admin_request, sample_broadcast_service, mocker
|
||||
@pytest.mark.parametrize('user_is_platform_admin', [True, False])
|
||||
def test_update_broadcast_message_allows_service_user_and_platform_admin_to_cancel(
|
||||
admin_request, sample_broadcast_service, mocker, user_is_platform_admin
|
||||
):
|
||||
"""
|
||||
Only platform admins and users belonging to that service should be able to cancel broadcasts.
|
||||
"""
|
||||
t = create_template(sample_broadcast_service, BROADCAST_TYPE, content='emergency broadcast')
|
||||
bm = create_broadcast_message(t, status=BroadcastStatusType.BROADCASTING)
|
||||
canceller = create_user(email='canceller@gov.uk')
|
||||
sample_broadcast_service.users.append(canceller)
|
||||
if user_is_platform_admin:
|
||||
canceller.platform_admin = True
|
||||
else:
|
||||
sample_broadcast_service.users.append(canceller)
|
||||
mock_task = mocker.patch('app.celery.broadcast_message_tasks.send_broadcast_event.apply_async')
|
||||
|
||||
response = admin_request.post(
|
||||
@@ -737,7 +744,7 @@ def test_update_broadcast_message_status_rejects_approval_from_user_not_on_that_
|
||||
)
|
||||
|
||||
assert mock_task.called is False
|
||||
assert 'cannot approve broadcast' in response['message']
|
||||
assert 'cannot update broadcast' in response['message']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('current_status, new_status', [
|
||||
|
||||
Reference in New Issue
Block a user