Let users choose when to end a broadcast

Different emergencies will need broadcasts to last for a variable amount
of time. We give users some control over this by letting them stop a
broadcast early. But we should also let them set a maximum broadcast
time, for:
- when the duration of the danger is known
- when the broadcast has been live long enough to alert everyone who
  needs to know about it

This code re-uses the pattern for scheduling jobs, which has some
constraints that are probably OK for now:
- end time is limited to an hour
- longest duration is 3 whole days (eg if you start broadcasting Friday
  you have the choice of Saturday, Sunday and all of Monday, up to
  midnight)
This commit is contained in:
Chris Hill-Scott
2020-07-16 10:49:21 +01:00
parent ca292037e7
commit 83156bd16e
9 changed files with 187 additions and 55 deletions

View File

@@ -11,19 +11,19 @@ def test_form_contains_next_24h(app_):
# Friday
assert choices[0] == ('', 'Now')
assert choices[1] == ('2016-01-01T12:00:00.061258', 'Today at midday')
assert choices[13] == ('2016-01-02T00:00:00.061258', 'Today at midnight')
assert choices[1] == ('2016-01-01T12:00:00', 'Today at midday')
assert choices[13] == ('2016-01-02T00:00:00', 'Today at midnight')
# Saturday
assert choices[14] == ('2016-01-02T01:00:00.061258', 'Tomorrow at 1am')
assert choices[37] == ('2016-01-03T00:00:00.061258', 'Tomorrow at midnight')
assert choices[14] == ('2016-01-02T01:00:00', 'Tomorrow at 1am')
assert choices[37] == ('2016-01-03T00:00:00', 'Tomorrow at midnight')
# Sunday
assert choices[38] == ('2016-01-03T01:00:00.061258', 'Sunday at 1am')
assert choices[38] == ('2016-01-03T01:00:00', 'Sunday at 1am')
# Monday
assert choices[84] == ('2016-01-04T23:00:00.061258', 'Monday at 11pm')
assert choices[85] == ('2016-01-05T00:00:00.061258', 'Monday at midnight')
assert choices[84] == ('2016-01-04T23:00:00', 'Monday at 11pm')
assert choices[85] == ('2016-01-05T00:00:00', 'Monday at midnight')
with pytest.raises(IndexError):
assert choices[

View File

@@ -278,22 +278,79 @@ def test_remove_broadcast_area_page(
)
@pytest.mark.parametrize('end_time', (
# Before now
pytest.param('2020-02-02T02:00:00', marks=pytest.mark.xfail),
# End of the current hour
pytest.param('2020-02-02T03:00:00'),
# Midnight 3 days ahead
pytest.param('2020-02-06T00:00:00'),
# 1am 4 days ahead
pytest.param('2020-02-06T01:00:00', marks=pytest.mark.xfail),
))
@freeze_time('2020-02-02 02:02:02')
def test_preview_broadcast_message_page(
client_request,
service_one,
mock_get_draft_broadcast_message,
mock_get_broadcast_template,
fake_uuid,
end_time,
):
service_one['permissions'] += ['broadcast']
client_request.get(
page = client_request.get(
'.preview_broadcast_message',
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
),
)
assert [
normalize_spaces(area.text)
for area in page.select('.area-list-item.area-list-item--unremoveable')
] == [
'England',
'Scotland',
]
assert normalize_spaces(
page.select_one('.broadcast-message-wrapper').text
) == (
'This is a test'
)
form = page.select_one('form')
assert form['method'] == 'post'
assert 'action' not in form
radio_choices = [
choice['value'] for choice in form.select('input[type=radio][name=finishes_at]')
]
assert len(radio_choices) == 94
assert end_time in radio_choices
@freeze_time('2020-02-02 02:02:02.222222')
@pytest.mark.parametrize('end_time', (
# Before now
pytest.param('2020-02-02T02:00:00', marks=pytest.mark.xfail),
# End of the current hour
pytest.param('2020-02-02T03:00:00'),
# Midnight 3 days ahead
pytest.param('2020-02-06T00:00:00'),
# 1am 4 days ahead
pytest.param('2020-02-06T01:00:00', marks=pytest.mark.xfail),
))
@freeze_time('2020-02-02 02:02:02')
def test_start_broadcasting(
client_request,
service_one,
@@ -302,24 +359,54 @@ def test_start_broadcasting(
mock_update_broadcast_message,
mock_update_broadcast_message_status,
fake_uuid,
end_time,
):
service_one['permissions'] += ['broadcast']
client_request.post(
'.preview_broadcast_message',
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
_data={
'finishes_at': end_time,
}
),
mock_update_broadcast_message.assert_called_once_with(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'finishes_at': end_time,
},
)
mock_update_broadcast_message_status.assert_called_once_with(
'pending-approval',
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
)
def test_start_broadcasting_shows_validation_error(
client_request,
service_one,
mock_get_draft_broadcast_message,
mock_get_broadcast_template,
mock_update_broadcast_message,
mock_update_broadcast_message_status,
fake_uuid,
):
service_one['permissions'] += ['broadcast']
client_request.post(
page = client_request.post(
'.preview_broadcast_message',
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
),
mock_update_broadcast_message.assert_called_once_with(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'finishes_at': '2020-02-05T02:02:02.222222',
},
_data={},
_expected_status=200,
)
mock_update_broadcast_message_status.assert_called_once_with(
'pending-approval',
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
assert mock_update_broadcast_message.called is False
assert mock_update_broadcast_message_status.called is False
assert normalize_spaces(
page.select_one('form fieldset legend .error-message').text
) == (
'Select an option'
)