Repository: pypi/warehouse

Issue:
Show an error when a pre-existing org account name is chosen
When changing the org account name, choosing a name that already exists should fail rather than showing a banner.

To recreate the issue-

- Click on 'Your organizations'
- Select an organization
- Click on 'Settings'
- Click on 'Change organization account name;
- Enter a name for an org that already exists

The confirmation box disappears and a banner appears

![test10](https://user-images.githubusercontent.com/37237726/183532975-688f1c52-1dbb-45d8-bc56-ad71d5adc6cb.png)




Discussion:

Resolving patch:
diff --git a/warehouse/manage/views.py b/warehouse/manage/views.py
--- a/warehouse/manage/views.py
+++ b/warehouse/manage/views.py
@@ -1386,6 +1386,9 @@ def default_response(self):
                 orgtype=self.organization.orgtype,
                 organization_service=self.organization_service,
             ),
+            "save_organization_name_form": SaveOrganizationNameForm(
+                organization_service=self.organization_service,
+            ),
             "active_projects": self.active_projects,
         }
 
@@ -1471,13 +1474,10 @@ def save_organization_name(self):
                     "manage.organization.settings",
                     organization_name=self.organization.normalized_name,
                 )
+                + "#modal-close"
             )
-        else:
-            for error_list in form.errors.values():
-                for error in error_list:
-                    self.request.session.flash(error, queue="error")
 
-        return self.default_response
+        return {**self.default_response, "save_organization_name_form": form}
 
     @view_config(request_method="POST", request_param=["confirm_organization_name"])
     def delete_organization(self):


Test patch:
diff --git a/tests/unit/manage/test_views.py b/tests/unit/manage/test_views.py
--- a/tests/unit/manage/test_views.py
+++ b/tests/unit/manage/test_views.py
@@ -2654,6 +2654,14 @@ def test_manage_organization(
         )
         monkeypatch.setattr(views, "SaveOrganizationForm", save_organization_cls)
 
+        save_organization_name_obj = pretend.stub()
+        save_organization_name_cls = pretend.call_recorder(
+            lambda *a, **kw: save_organization_name_obj
+        )
+        monkeypatch.setattr(
+            views, "SaveOrganizationNameForm", save_organization_name_cls
+        )
+
         view = views.ManageOrganizationSettingsViews(organization, db_request)
         result = view.manage_organization()
 
@@ -2662,6 +2670,7 @@ def test_manage_organization(
         assert result == {
             "organization": organization,
             "save_organization_form": save_organization_obj,
+            "save_organization_name_form": save_organization_name_obj,
             "active_projects": view.active_projects,
         }
         assert save_organization_cls.calls == [
@@ -2740,6 +2749,14 @@ def test_save_organization_validation_fails(
         )
         monkeypatch.setattr(views, "SaveOrganizationForm", save_organization_cls)
 
+        save_organization_name_obj = pretend.stub()
+        save_organization_name_cls = pretend.call_recorder(
+            lambda *a, **kw: save_organization_name_obj
+        )
+        monkeypatch.setattr(
+            views, "SaveOrganizationNameForm", save_organization_name_cls
+        )
+
         view = views.ManageOrganizationSettingsViews(organization, db_request)
         result = view.save_organization()
 
@@ -2819,9 +2836,8 @@ def rename_organization(organization_id, organization_name):
         result = view.save_organization_name()
 
         assert isinstance(result, HTTPSeeOther)
-        assert (
-            result.headers["Location"]
-            == f"/manage/organization/{organization.normalized_name}/settings/"
+        assert result.headers["Location"] == (
+            f"/manage/organization/{organization.normalized_name}/settings/#modal-close"
         )
         assert organization_service.rename_organization.calls == [
             pretend.call(organization.id, "new-name")
@@ -2878,7 +2894,10 @@ def rename_organization(organization_id, organization_name):
         view = views.ManageOrganizationSettingsViews(organization, db_request)
         result = view.save_organization_name()
 
-        assert result == view.default_response
+        assert result == {
+            **view.default_response,
+            "save_organization_name_form": save_organization_name_obj,
+        }
         assert organization_service.rename_organization.calls == []
 
     def test_save_organization_name_disable_organizations(self, db_request):
@@ -2969,6 +2988,14 @@ def test_delete_organization_with_active_projects(
         )
         monkeypatch.setattr(views, "SaveOrganizationForm", save_organization_cls)
 
+        save_organization_name_obj = pretend.stub()
+        save_organization_name_cls = pretend.call_recorder(
+            lambda *a, **kw: save_organization_name_obj
+        )
+        monkeypatch.setattr(
+            views, "SaveOrganizationNameForm", save_organization_name_cls
+        )
+
         monkeypatch.setattr(
             organization_service,
             "delete_organization",