diff --git a/tests/unit/oidc/forms/test_activestate.py b/tests/unit/oidc/forms/test_activestate.py index f9b0d8574d2c..e6a38094e081 100644 --- a/tests/unit/oidc/forms/test_activestate.py +++ b/tests/unit/oidc/forms/test_activestate.py @@ -68,7 +68,7 @@ def test_validate_project_name_already_in_use_owner(self, pyramid_config): owners = [user] def check_project_name(name): - return ProjectNameUnavailableExistingError( + raise ProjectNameUnavailableExistingError( existing_project=pretend.stub(owners=owners) ) @@ -98,7 +98,7 @@ def test_validate_project_name_already_in_use_not_owner(self, pyramid_config): owners = [] def check_project_name(name): - return ProjectNameUnavailableExistingError( + raise ProjectNameUnavailableExistingError( existing_project=pretend.stub(owners=owners) ) diff --git a/tests/unit/oidc/forms/test_github.py b/tests/unit/oidc/forms/test_github.py index f7930528b338..c1b1f2ec193d 100644 --- a/tests/unit/oidc/forms/test_github.py +++ b/tests/unit/oidc/forms/test_github.py @@ -65,12 +65,15 @@ def test_validate_project_name_already_in_use_owner(self, pyramid_config): user = pretend.stub() owners = [user] + def check_project_name(name): + raise ProjectNameUnavailableExistingError( + existing_project=pretend.stub(owners=owners) + ) + form = github.PendingGitHubPublisherForm( api_token="fake-token", route_url=route_url, - check_project_name=lambda name: ProjectNameUnavailableExistingError( - existing_project=pretend.stub(owners=owners) - ), + check_project_name=check_project_name, user=user, ) @@ -93,12 +96,15 @@ def test_validate_project_name_already_in_use_not_owner(self, pyramid_config): user = pretend.stub() owners = [] + def check_project_name(name): + raise ProjectNameUnavailableExistingError( + existing_project=pretend.stub(owners=owners) + ) + form = github.PendingGitHubPublisherForm( api_token="fake-token", route_url=route_url, - check_project_name=lambda name: ProjectNameUnavailableExistingError( - existing_project=pretend.stub(owners=owners) - ), + check_project_name=check_project_name, user=user, ) @@ -119,10 +125,13 @@ def test_validate_project_name_already_in_use_not_owner(self, pyramid_config): ], ) def test_validate_project_name_unavailable(self, reason, pyramid_config): + def check_project_name(name): + raise reason + form = github.PendingGitHubPublisherForm( api_token="fake-token", route_url=pretend.call_recorder(lambda *args, **kwargs: ""), - check_project_name=lambda name: reason, + check_project_name=check_project_name, user=pretend.stub(), ) diff --git a/tests/unit/oidc/forms/test_gitlab.py b/tests/unit/oidc/forms/test_gitlab.py index cd50060e81f7..dd6c2d887106 100644 --- a/tests/unit/oidc/forms/test_gitlab.py +++ b/tests/unit/oidc/forms/test_gitlab.py @@ -54,11 +54,14 @@ def test_validate_project_name_already_in_use_owner(self, pyramid_config): owners = [user] route_url = pretend.call_recorder(lambda *args, **kwargs: "my_url") + def check_project_name(name): + raise ProjectNameUnavailableExistingError( + existing_project=pretend.stub(owners=owners) + ) + form = gitlab.PendingGitLabPublisherForm( route_url=route_url, - check_project_name=lambda name: ProjectNameUnavailableExistingError( - existing_project=pretend.stub(owners=owners) - ), + check_project_name=check_project_name, user=user, ) @@ -81,11 +84,14 @@ def test_validate_project_name_already_in_use_not_owner(self, pyramid_config): owners = [] route_url = pretend.call_recorder(lambda *args, **kwargs: "my_url") + def check_project_name(name): + raise ProjectNameUnavailableExistingError( + existing_project=pretend.stub(owners=owners) + ) + form = gitlab.PendingGitLabPublisherForm( route_url=route_url, - check_project_name=lambda name: ProjectNameUnavailableExistingError( - existing_project=pretend.stub(owners=owners) - ), + check_project_name=check_project_name, user=user, ) diff --git a/tests/unit/oidc/forms/test_google.py b/tests/unit/oidc/forms/test_google.py index 9cb014a24801..cca87c6637c5 100644 --- a/tests/unit/oidc/forms/test_google.py +++ b/tests/unit/oidc/forms/test_google.py @@ -51,11 +51,15 @@ def test_validate_project_name_already_in_use_owner(self, pyramid_config): user = pretend.stub() owners = [user] route_url = pretend.call_recorder(lambda *args, **kwargs: "my_url") + + def check_project_name(name): + raise ProjectNameUnavailableExistingError( + existing_project=pretend.stub(owners=owners) + ) + form = google.PendingGooglePublisherForm( route_url=route_url, - check_project_name=lambda name: ProjectNameUnavailableExistingError( - existing_project=pretend.stub(owners=owners) - ), + check_project_name=check_project_name, user=user, ) @@ -77,11 +81,15 @@ def test_validate_project_name_already_in_use_not_owner(self, pyramid_config): user = pretend.stub() owners = [] route_url = pretend.call_recorder(lambda *args, **kwargs: "my_url") + + def check_project_name(name): + raise ProjectNameUnavailableExistingError( + existing_project=pretend.stub(owners=owners) + ) + form = google.PendingGooglePublisherForm( route_url=route_url, - check_project_name=lambda name: ProjectNameUnavailableExistingError( - existing_project=pretend.stub(owners=owners) - ), + check_project_name=check_project_name, user=user, ) diff --git a/warehouse/oidc/forms/_core.py b/warehouse/oidc/forms/_core.py index 75765a272a79..8ce51bcc0e80 100644 --- a/warehouse/oidc/forms/_core.py +++ b/warehouse/oidc/forms/_core.py @@ -36,55 +36,54 @@ class PendingPublisherMixin: def validate_project_name(self, field): project_name = field.data - match self._check_project_name(project_name): - case ProjectNameUnavailableInvalidError(): - raise wtforms.validators.ValidationError(_("Invalid project name")) - case ProjectNameUnavailableExistingError(existing_project=existing_project): - # If the user owns the existing project, the error message includes a - # link to the project settings that the user can modify. - if self._user in existing_project.owners: - url_params = { - name: value for name, value in self.data.items() if value - } - url_params["provider"] = {self.provider} - url = self._route_url( - "manage.project.settings.publishing", - project_name=project_name, - _query=url_params, - ) + try: + self._check_project_name(project_name) + except ProjectNameUnavailableInvalidError: + raise wtforms.validators.ValidationError(_("Invalid project name")) + except ProjectNameUnavailableExistingError as e: + # If the user owns the existing project, the error message includes a + # link to the project settings that the user can modify. + if self._user in e.existing_project.owners: + url_params = {name: value for name, value in self.data.items() if value} + url_params["provider"] = {self.provider} + url = self._route_url( + "manage.project.settings.publishing", + project_name=project_name, + _query=url_params, + ) - # We mark the error message as safe, so that the HTML hyperlink is - # not escaped by Jinja - raise wtforms.validators.ValidationError( - markupsafe.Markup( - _( - "This project already exists: use the project's " - "publishing settings here to " - "create a Trusted Publisher for it.", - mapping={"url": url}, - ) + # We mark the error message as safe, so that the HTML hyperlink is + # not escaped by Jinja + raise wtforms.validators.ValidationError( + markupsafe.Markup( + _( + "This project already exists: use the project's " + "publishing settings here to " + "create a Trusted Publisher for it.", + mapping={"url": url}, ) ) - else: - raise wtforms.validators.ValidationError( - _("This project already exists.") - ) - - case ProjectNameUnavailableProhibitedError(): - raise wtforms.validators.ValidationError( - _("This project name isn't allowed") ) - case ProjectNameUnavailableSimilarError(): + else: raise wtforms.validators.ValidationError( - _("This project name is too similar to an existing project") + _("This project already exists.") ) - case ProjectNameUnavailableStdlibError(): - raise wtforms.validators.ValidationError( - _( - "This project name isn't allowed (conflict with the Python" - " standard library module name)" - ) + + except ProjectNameUnavailableProhibitedError: + raise wtforms.validators.ValidationError( + _("This project name isn't allowed") + ) + except ProjectNameUnavailableSimilarError: + raise wtforms.validators.ValidationError( + _("This project name is too similar to an existing project") + ) + except ProjectNameUnavailableStdlibError: + raise wtforms.validators.ValidationError( + _( + "This project name isn't allowed (conflict with the Python" + " standard library module name)" ) + ) @property def provider(self) -> str: # pragma: no cover