From be6c83cf7b89a85c56fe9e0b32e9fad39253a7aa Mon Sep 17 00:00:00 2001 From: Dranoel Date: Fri, 28 Jun 2024 11:45:30 +0100 Subject: [PATCH] [FCL-180] Save user input when pressing previous in licence application form --- e2e_tests/test_transactional_licence_form.py | 34 +++++++++++++++ transactional_licence_form/views.py | 46 ++++++++++---------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/e2e_tests/test_transactional_licence_form.py b/e2e_tests/test_transactional_licence_form.py index d81c13d63..443fcb67d 100644 --- a/e2e_tests/test_transactional_licence_form.py +++ b/e2e_tests/test_transactional_licence_form.py @@ -300,3 +300,37 @@ def get_review_row(page, label): expect(get_review_row(page, "What is the full legal name")).to_have_text( "New Organisation name" ) + + +def test_data_saved_when_going_back(page: Page): + """ + Test the process to apply for a computational analysis licence. + We stop on the review page (short of actually submitting the form) + (which triggers an email to the licencing team): the final step will be tested + in isolation with a mocked email service.""" + + # Fill contact details in full + page.goto("/re-use-find-case-law-records/steps/contact") + page.get_by_label("Contact Full Name").fill("Full Name") + page.get_by_label("Contact Email address").fill("contact@example.com") + page.get_by_label( + "This is a different person (please enter their details below)" + ).click() + page.get_by_label("Licence holder Full Name").fill("Licence Holder") + page.get_by_label("Licence holder Email").fill("licenceholder@example.com") + + # On the next page, fill only 1 field, leaving others blank + page.get_by_text("Next").click() + page.get_by_label("What is the full legal name of your organisation?").fill( + "Organisation name" + ) + + # Check previous page has information + page.get_by_text("Previous").click() + expect(page.get_by_label("Contact Full Name")).to_have_value("Full Name") + + # Check incomplete next page has information + page.get_by_text("Next").click() + expect( + page.get_by_label("What is the full legal name of your organisation?") + ).to_have_value("Organisation name") diff --git a/transactional_licence_form/views.py b/transactional_licence_form/views.py index 6cbecc65b..4d1603d6b 100644 --- a/transactional_licence_form/views.py +++ b/transactional_licence_form/views.py @@ -34,15 +34,6 @@ def render_goto_step(self, goto_step, **kwargs): return redirect(url) def post(self, *args, **kwargs): - # We override this to ensure that the form is saved when returning to the review page - - wizard_goto_step = self.request.POST.get("wizard_goto_step", None) - if not self.in_review(): - # If we are not reviewing, we follow the default behaviour: we do not validate or save the form - # and go directly to the step. - if wizard_goto_step and wizard_goto_step in self.get_form_list(): - return self.render_goto_step(wizard_goto_step) - management_form = ManagementForm(self.request.POST, prefix=self.prefix) if not management_form.is_valid(): raise SuspiciousOperation( @@ -58,22 +49,29 @@ def post(self, *args, **kwargs): form = self.get_form(data=self.request.POST, files=self.request.FILES) - if form.is_valid(): - self.storage.set_step_data(self.steps.current, self.process_step(form)) - self.storage.set_step_files( - self.steps.current, self.process_step_files(form) - ) + wizard_goto_step = self.request.POST.get("wizard_goto_step", None) + + # If we're in review or pressing 'Next', we need to ensure the user fixes bad data + if not form.is_valid() and (self.in_review() or wizard_goto_step is None): + return self.render(form) + + self.storage.set_step_data(self.steps.current, self.process_step(form)) + self.storage.set_step_files(self.steps.current, self.process_step_files(form)) + + # Going to a step out of order takes priority + if wizard_goto_step and wizard_goto_step in self.get_form_list(): + return self.render_goto_step(wizard_goto_step) + + # If the form is invalid, re-render it with errors + if not form.is_valid(): + return self.render(form) + + # When moving forwards from the final review form, send the email + if self.steps.current == self.steps.last: + return self.render_done(form, **kwargs) - if self.steps.current == self.steps.last: - return self.render_done(form, **kwargs) - else: - if wizard_goto_step and wizard_goto_step in self.get_form_list(): - # If we are reviewing, and there is a goto step, we go to that step. - return self.render_goto_step(wizard_goto_step) - else: - # Otherwise we follow the default behaviour. - return self.render_next_step(form) - return self.render(form) + # If the user isn't jumping and their input is valid, move forwards + return self.render_next_step(form) def in_review(self): has_review_parameter = bool(