diff --git a/app/main/views/send.py b/app/main/views/send.py index 10625d9ed..b26e70829 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -1025,7 +1025,9 @@ def get_template_error_dict(exception): @main.route("/services//template//notification/check", methods=['POST']) @user_has_permissions('send_messages', restrict_admin_usage=True) def send_notification(service_id, template_id): - if {'recipient', 'placeholders'} - set(session.keys()): + recipient = get_recipient() + + if not recipient: return redirect(url_for( '.send_one_off', service_id=service_id, @@ -1038,9 +1040,9 @@ def send_notification(service_id, template_id): noti = notification_api_client.send_notification( service_id, template_id=db_template['id'], - recipient=session['recipient'] or InsensitiveDict(session['placeholders'])['address line 1'], + recipient=recipient, personalisation=session['placeholders'], - sender_id=session['sender_id'] if 'sender_id' in session else None + sender_id=session.get('sender_id', None), ) except HTTPError as exception: current_app.logger.info('Service {} could not send notification: "{}"'.format( @@ -1096,3 +1098,13 @@ def get_spreadsheet_column_headings_from_template(template): column_headings.append(column_heading) return column_headings + + +def get_recipient(): + if {'recipient', 'placeholders'} - set(session.keys()): + return None + + return ( + session['recipient'] or + InsensitiveDict(session['placeholders']).get('address line 1') + ) diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 46bc0f0fe..3dda1a696 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -3969,12 +3969,18 @@ def test_send_notification_clears_session( assert 'placeholders' not in session +@pytest.mark.parametrize('session_data', [ + {'placeholders': {'a': 'b'}}, # missing recipient + {'recipient': '123'}, # missing placeholders + {'placeholders': {}, 'recipient': ''}, # missing address +]) def test_send_notification_redirects_if_missing_data( client_request, fake_uuid, + session_data, ): with client_request.session_transaction() as session: - session['placeholders'] = {'a': 'b'} + session.update(session_data) client_request.post( 'main.send_notification',