diff --git a/src/registrar/tests/test_views.py b/src/registrar/tests/test_views.py index b322f20b2..7a375cd11 100644 --- a/src/registrar/tests/test_views.py +++ b/src/registrar/tests/test_views.py @@ -731,7 +731,9 @@ class FinishUserProfileForOtherUsersTests(TestWithUser, WebTest): self.assertContains(save_page, "Your profile has been updated.") # We need to assert that logo is not clickable and links to manage your domain are not present - self.assertContains(save_page, "manage your domains", count=1) + # NOTE: "anage" is not a typo. It is to accomodate the fact that the "m" is uppercase in one + # instance and lowercase in the other. + self.assertContains(save_page, "anage your domains", count=2) self.assertNotContains( save_page, "Before you can manage your domains, we need you to add contact information" ) diff --git a/src/registrar/views/user_profile.py b/src/registrar/views/user_profile.py index 67a0c73ce..b3718bd20 100644 --- a/src/registrar/views/user_profile.py +++ b/src/registrar/views/user_profile.py @@ -67,8 +67,7 @@ class UserProfileView(UserProfilePermissionView, FormMixin): # Show back button conditional on user having finished setup context["show_back_button"] = False - form = self.get_form() - if hasattr(self.user, "finished_setup") and self.user.finished_setup and form.is_valid(): + if hasattr(self.user, "finished_setup") and self.user.finished_setup: context["user_finished_setup"] = True context["show_back_button"] = True @@ -120,7 +119,17 @@ class UserProfileView(UserProfilePermissionView, FormMixin): # superclass has the redirect return super().form_valid(form) + def form_invalid(self, form): + """If the form is invalid, conditionally display an additional error.""" + if hasattr(self.user, "finished_setup") and not self.user.finished_setup: + messages.error(self.request, "Before you can manage your domain, we need you to add contact information.") + logger.info("got here, now rendering response") + form.initial['redirect'] = form.data.get('redirect') + logger.info(form.initial) + return super().form_invalid(form) + def get_object(self, queryset=None): + """Override get_object to return the logged-in user's contact""" self.user = self.request.user # get the logged in user if hasattr(self.user, "contact"): # Check if the user has a contact instance @@ -144,8 +153,7 @@ class FinishProfileSetupView(UserProfileView): # Show back button conditional on user having finished setup context["show_back_button"] = False - form = self.get_form() - if hasattr(self.user, "finished_setup") and self.user.finished_setup and form.is_valid(): + if hasattr(self.user, "finished_setup") and self.user.finished_setup: if kwargs.get("redirect") == "home": context["show_back_button"] = True else: @@ -182,4 +190,4 @@ class FinishProfileSetupView(UserProfileView): return reverse(redirect_param) except NoReverseMatch as err: logger.error(f"get_redirect_url -> Could not find the specified page. Err: {err}") - return super().get_success_url() + return super().get_success_url() \ No newline at end of file