From 3529188968c2a6da0d980564f7af35905d6f827e Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 12 Mar 2020 13:56:35 +0000 Subject: [PATCH] redirect from address_line_n to address textarea page if someone starts a new one-off flow they'll get taken to the address page. However, if someone hits the back button, they'll cycle backwards through placeholders and will end up on the individual line pages. Lets redirect them to the correct place. We'll additionally need to reconstruct the address block from the various session variables that may or may not be populated --- app/main/views/send.py | 20 +++++++++++- tests/app/main/views/test_send.py | 54 +++++++------------------------ 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/app/main/views/send.py b/app/main/views/send.py index 8db4d931e..a4bcf08cf 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -335,6 +335,18 @@ def get_notification_check_endpoint(service_id, template): )) +def get_letter_address_from_session(): + """ + Takes non-blank-string address values out of the placeholders, and returns a multi line string + """ + keys = [f'address line {i}' for i in range(1, 7)] + ['postcode'] + return '\n'.join([ + session['placeholders'][key] + for key in keys + if session['placeholders'].get(key) + ]) + + @main.route( "/services//send//one-off/address", methods=['GET', 'POST'] @@ -364,7 +376,9 @@ def send_one_off_letter_address(service_id, template_id): sms_sender=None ) - form = LetterAddressForm() + current_session_address = get_letter_address_from_session() + + form = LetterAddressForm(address=current_session_address) if form.validate_on_submit(): session['placeholders'].update(form.as_address_lines_1_to_7_with_postcode) @@ -467,6 +481,10 @@ def send_test_step(service_id, template_id, step_index): template_id=template_id, )) + # if we're in a letter, we should show address block rather than "address line #" or "postcode" + if db_template['template_type'] == 'letter' and current_placeholder in first_column_headings['letter']: + return redirect(url_for('.send_one_off_letter_address', service_id=service_id, template_id=template_id)) + optional_placeholder = (current_placeholder in optional_address_columns) form = get_placeholder_form_instance( current_placeholder, diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 9a3352184..84398c3e3 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -2015,66 +2015,34 @@ def test_send_test_caches_page_count( assert session['send_test_letter_page_count'] == 9 -def test_send_test_indicates_optional_address_columns( +def test_send_one_off_back_link_populates_address_textarea( client_request, mocker, mock_get_service_letter_template, + mock_template_preview, fake_uuid, ): - - mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=1) - with client_request.session_transaction() as session: session['recipient'] = None - session['placeholders'] = {} + session['placeholders'] = {'address line 1': 'foo', 'address line 2': 'bar', 'address line 3': ''} + # imagine someone hit the back button to go from line 3 page to line 2 page page = client_request.get( 'main.send_test_step', service_id=SERVICE_ONE_ID, template_id=fake_uuid, - step_index=3, - ) - - assert normalize_spaces(page.select('label')[0].text) == ( - 'address line 4 ' - 'Optional' - ) - assert page.select('.govuk-back-link')[0]['href'] == url_for( - 'main.send_one_off_step', - service_id=SERVICE_ONE_ID, - template_id=fake_uuid, step_index=2, + _follow_redirects=True ) + assert page.select_one('h1').text.strip() == 'Send ‘Two week reminder’' -def test_send_test_allows_empty_optional_address_columns( - client_request, - mocker, - mock_get_service_letter_template, - fake_uuid, -): + form = page.select_one('form') + assert form.select_one('label').text.strip() == 'Address' - mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=1) - - with client_request.session_transaction() as session: - session['recipient'] = None - session['placeholders'] = {} - - client_request.post( - 'main.send_test_step', - service_id=SERVICE_ONE_ID, - template_id=fake_uuid, - step_index=3, - # no data here - _expected_status=302, - _expected_redirect=url_for( - 'main.send_test_step', - service_id=SERVICE_ONE_ID, - template_id=fake_uuid, - step_index=4, - _external=True, - ), - ) + textarea = form.select_one('textarea') + assert textarea.attrs['name'] == 'address' + assert textarea.text == 'foo\nbar' def test_send_test_sms_message_puts_submitted_data_in_session(