Repository: pypi/warehouse

Issue:
Flakey organization-related test
I noticed there is an organization-related test that is currently occasionally failing on `main`:

```
=================================== FAILURES ===================================
______________________ TestOrganizationList.test_no_query ______________________

self = <tests.unit.admin.views.test_organizations.TestOrganizationList object at 0x7fd3ab8ef4f0>
enable_organizations = None
db_request = <pyramid.testing.DummyRequest object at 0x7fd3a257edc0>

    def test_no_query(self, enable_organizations, db_request):
        organizations = sorted(
            [OrganizationFactory.create() for _ in range(30)],
            key=lambda o: o.normalized_name,
        )
        result = views.organization_list(db_request)
    
>       assert result["organizations"].items == organizations[:25]
E       AssertionError: assert [Organization...ulture'), ...] == [Organization...ulture'), ...]
E         At index 22 diff: Organization(name='product') != Organization(name='product')
E         Full diff:
E           [
E            Organization(name='and'),
E            Organization(name='anything'),
E            Organization(name='artist'),
E            Organization(name='base'),
E            Organization(name='business'),
E            Organization(name='culture'),
E            Organization(name='effect'),
E            Organization(name='effect'),
E            Organization(name='end'),
E            Organization(name='fast'),
E            Organization(name='financial'),
E            Organization(name='five'),
E            Organization(name='garden'),
E            Organization(name='grow'),
E            Organization(name='himself'),
E            Organization(name='information'),
E            Organization(name='itself'),
E            Organization(name='many'),
E            Organization(name='mean'),
E            Organization(name='moment'),
E            Organization(name='politics'),
E            Organization(name='prepare'),
E            Organization(name='product'),
E            Organization(name='product'),
E            Organization(name='real'),
E           ]

tests/unit/admin/views/test_organizations.py:33: AssertionError
```

@divbzero @sterbo Can you look into this?


Discussion:
You can see the failure here: https://github.com/pypa/warehouse/runs/6260354769?check_suite_focus=true
@di Yes, let me look into this.

Resolving patch:
diff --git a/warehouse/manage/views.py b/warehouse/manage/views.py
--- a/warehouse/manage/views.py
+++ b/warehouse/manage/views.py
@@ -1022,9 +1022,7 @@ def create_organization(self):
                 tag="organization:create",
                 additional={"created_by_user_id": str(self.request.user.id)},
             )
-            self.organization_service.add_catalog_entry(
-                organization.name, organization.id
-            )
+            self.organization_service.add_catalog_entry(organization.id)
             self.organization_service.record_event(
                 organization.id,
                 tag="organization:catalog_entry:add",
diff --git a/warehouse/organizations/interfaces.py b/warehouse/organizations/interfaces.py
--- a/warehouse/organizations/interfaces.py
+++ b/warehouse/organizations/interfaces.py
@@ -38,7 +38,7 @@ def add_organization(name, display_name, orgtype, link_url, description):
         attributes.
         """
 
-    def add_catalog_entry(name, organization_id):
+    def add_catalog_entry(organization_id):
         """
         Adds the organization name to the organization name catalog
         """
diff --git a/warehouse/organizations/services.py b/warehouse/organizations/services.py
--- a/warehouse/organizations/services.py
+++ b/warehouse/organizations/services.py
@@ -12,6 +12,7 @@
 
 import datetime
 
+from sqlalchemy import func
 from sqlalchemy.orm.exc import NoResultFound
 from zope.interface import implementer
 
@@ -51,10 +52,11 @@ def find_organizationid(self, name):
         Find the unique organization identifier for the given normalized name or None
         if there is no organization with the given name.
         """
+        normalized_name = func.normalize_pep426_name(name)
         try:
             organization = (
                 self.db.query(Organization.id)
-                .filter(Organization.normalized_name == name)
+                .filter(Organization.normalized_name == normalized_name)
                 .one()
             )
         except NoResultFound:
@@ -79,13 +81,14 @@ def add_organization(self, name, display_name, orgtype, link_url, description):
 
         return organization
 
-    def add_catalog_entry(self, name, organization_id):
+    def add_catalog_entry(self, organization_id):
         """
         Adds the organization name to the organization name catalog
         """
         organization = self.get_organization(organization_id)
         catalog_entry = OrganizationNameCatalog(
-            normalized_name=name, organization_id=organization.id
+            normalized_name=organization.normalized_name,
+            organization_id=organization.id,
         )
 
         self.db.add(catalog_entry)


Test patch:
diff --git a/tests/common/db/organizations.py b/tests/common/db/organizations.py
--- a/tests/common/db/organizations.py
+++ b/tests/common/db/organizations.py
@@ -35,8 +35,7 @@ class Meta:
         model = Organization
 
     id = factory.Faker("uuid4", cast_to=None)
-    name = factory.Faker("word")
-    normalized_name = factory.Faker("word")
+    name = factory.Faker("pystr", max_chars=12)
     display_name = factory.Faker("word")
     orgtype = "Community"
     link_url = factory.Faker("uri")
diff --git a/tests/unit/admin/views/test_organizations.py b/tests/unit/admin/views/test_organizations.py
--- a/tests/unit/admin/views/test_organizations.py
+++ b/tests/unit/admin/views/test_organizations.py
@@ -30,9 +30,7 @@ def test_no_query(self, enable_organizations, db_request):
         )
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[:25]
-        assert result["query"] == ""
-        assert result["terms"] == []
+        assert result == {"organizations": organizations[:25], "query": "", "terms": []}
 
     def test_with_page(self, enable_organizations, db_request):
         organizations = sorted(
@@ -42,9 +40,7 @@ def test_with_page(self, enable_organizations, db_request):
         db_request.GET["page"] = "2"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[25:50]
-        assert result["query"] == ""
-        assert result["terms"] == []
+        assert result == {"organizations": organizations[25:], "query": "", "terms": []}
 
     def test_with_invalid_page(self, enable_organizations):
         request = pretend.stub(
@@ -63,9 +59,11 @@ def test_basic_query(self, enable_organizations, db_request):
         db_request.GET["q"] = organizations[0].name
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == [organizations[0]]
-        assert result["query"] == organizations[0].name
-        assert result["terms"] == [organizations[0].name]
+        assert result == {
+            "organizations": [organizations[0]],
+            "query": organizations[0].name,
+            "terms": [organizations[0].name],
+        }
 
     def test_name_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -75,9 +73,11 @@ def test_name_query(self, enable_organizations, db_request):
         db_request.GET["q"] = f"name:{organizations[0].name}"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == [organizations[0]]
-        assert result["query"] == f"name:{organizations[0].name}"
-        assert result["terms"] == [f"name:{organizations[0].name}"]
+        assert result == {
+            "organizations": [organizations[0]],
+            "query": f"name:{organizations[0].name}",
+            "terms": [f"name:{organizations[0].name}"],
+        }
 
     def test_organization_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -87,9 +87,11 @@ def test_organization_query(self, enable_organizations, db_request):
         db_request.GET["q"] = f"organization:{organizations[0].display_name}"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == [organizations[0]]
-        assert result["query"] == f"organization:{organizations[0].display_name}"
-        assert result["terms"] == [f"organization:{organizations[0].display_name}"]
+        assert result == {
+            "organizations": [organizations[0]],
+            "query": f"organization:{organizations[0].display_name}",
+            "terms": [f"organization:{organizations[0].display_name}"],
+        }
 
     def test_url_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -99,9 +101,11 @@ def test_url_query(self, enable_organizations, db_request):
         db_request.GET["q"] = f"url:{organizations[0].link_url}"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == [organizations[0]]
-        assert result["query"] == f"url:{organizations[0].link_url}"
-        assert result["terms"] == [f"url:{organizations[0].link_url}"]
+        assert result == {
+            "organizations": [organizations[0]],
+            "query": f"url:{organizations[0].link_url}",
+            "terms": [f"url:{organizations[0].link_url}"],
+        }
 
     def test_description_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -111,9 +115,11 @@ def test_description_query(self, enable_organizations, db_request):
         db_request.GET["q"] = f"description:'{organizations[0].description}'"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == [organizations[0]]
-        assert result["query"] == f"description:'{organizations[0].description}'"
-        assert result["terms"] == [f"description:{organizations[0].description}"]
+        assert result == {
+            "organizations": [organizations[0]],
+            "query": f"description:'{organizations[0].description}'",
+            "terms": [f"description:{organizations[0].description}"],
+        }
 
     def test_is_approved_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -128,9 +134,11 @@ def test_is_approved_query(self, enable_organizations, db_request):
         db_request.GET["q"] = "is:approved"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[:2]
-        assert result["query"] == "is:approved"
-        assert result["terms"] == ["is:approved"]
+        assert result == {
+            "organizations": organizations[:2],
+            "query": "is:approved",
+            "terms": ["is:approved"],
+        }
 
     def test_is_declined_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -145,9 +153,11 @@ def test_is_declined_query(self, enable_organizations, db_request):
         db_request.GET["q"] = "is:declined"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[2:3]
-        assert result["query"] == "is:declined"
-        assert result["terms"] == ["is:declined"]
+        assert result == {
+            "organizations": organizations[2:3],
+            "query": "is:declined",
+            "terms": ["is:declined"],
+        }
 
     def test_is_submitted_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -162,9 +172,11 @@ def test_is_submitted_query(self, enable_organizations, db_request):
         db_request.GET["q"] = "is:submitted"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[3:]
-        assert result["query"] == "is:submitted"
-        assert result["terms"] == ["is:submitted"]
+        assert result == {
+            "organizations": organizations[3:],
+            "query": "is:submitted",
+            "terms": ["is:submitted"],
+        }
 
     def test_is_active_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -179,9 +191,11 @@ def test_is_active_query(self, enable_organizations, db_request):
         db_request.GET["q"] = "is:active"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[:2]
-        assert result["query"] == "is:active"
-        assert result["terms"] == ["is:active"]
+        assert result == {
+            "organizations": organizations[:2],
+            "query": "is:active",
+            "terms": ["is:active"],
+        }
 
     def test_is_inactive_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -196,9 +210,11 @@ def test_is_inactive_query(self, enable_organizations, db_request):
         db_request.GET["q"] = "is:inactive"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[2:]
-        assert result["query"] == "is:inactive"
-        assert result["terms"] == ["is:inactive"]
+        assert result == {
+            "organizations": organizations[2:],
+            "query": "is:inactive",
+            "terms": ["is:inactive"],
+        }
 
     def test_is_invalid_query(self, enable_organizations, db_request):
         organizations = sorted(
@@ -208,9 +224,11 @@ def test_is_invalid_query(self, enable_organizations, db_request):
         db_request.GET["q"] = "is:not-actually-a-valid-query"
         result = views.organization_list(db_request)
 
-        assert result["organizations"].items == organizations[:25]
-        assert result["query"] == "is:not-actually-a-valid-query"
-        assert result["terms"] == ["is:not-actually-a-valid-query"]
+        assert result == {
+            "organizations": organizations[:25],
+            "query": "is:not-actually-a-valid-query",
+            "terms": ["is:not-actually-a-valid-query"],
+        }
 
     def test_disable_organizations(self, db_request):
         with pytest.raises(HTTPNotFound):
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
@@ -2445,10 +2445,7 @@ def test_create_organization(self, monkeypatch):
             )
         ]
         assert organization_service.add_catalog_entry.calls == [
-            pretend.call(
-                organization.name,
-                organization.id,
-            )
+            pretend.call(organization.id)
         ]
         assert organization_service.add_organization_role.calls == [
             pretend.call(
diff --git a/tests/unit/organizations/test_services.py b/tests/unit/organizations/test_services.py
--- a/tests/unit/organizations/test_services.py
+++ b/tests/unit/organizations/test_services.py
@@ -86,9 +86,7 @@ def test_add_organization(self, organization_service):
     def test_add_catalog_entry(self, organization_service):
         organization = OrganizationFactory.create()
 
-        catalog_entry = organization_service.add_catalog_entry(
-            organization.normalized_name, organization.id
-        )
+        catalog_entry = organization_service.add_catalog_entry(organization.id)
         assert catalog_entry.normalized_name == organization.normalized_name
         assert catalog_entry.organization_id == organization.id