Skip to content

Commit

Permalink
Improve error messages when creating projects and publishers
Browse files Browse the repository at this point in the history
This change improves the error messages when uploading a new project
or creating a pending Trusted Publisher, when the new project's name
already exists or is too similar to an existing project.

Signed-off-by: Facundo Tuesca <[email protected]>
  • Loading branch information
facutuesca committed Jan 13, 2025
1 parent 1cc42e1 commit 7fc5519
Show file tree
Hide file tree
Showing 17 changed files with 313 additions and 137 deletions.
11 changes: 11 additions & 0 deletions tests/unit/accounts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3357,6 +3357,7 @@ def find_service(iface, name=None, context=None):
find_service=pretend.call_recorder(find_service),
route_url=pretend.stub(),
POST=MultiDict(),
user=pretend.stub(id=pretend.stub()),
registry=pretend.stub(
settings={
"github.token": "fake-api-token",
Expand Down Expand Up @@ -3525,13 +3526,15 @@ def test_manage_publishing(self, metrics, monkeypatch):
api_token="fake-api-token",
route_url=route_url,
check_project_name=project_service.check_project_name,
user=request.user,
)
]
assert pending_gitlab_publisher_form_cls.calls == [
pretend.call(
request.POST,
route_url=route_url,
check_project_name=project_service.check_project_name,
user=request.user,
)
]

Expand Down Expand Up @@ -3620,13 +3623,15 @@ def test_manage_publishing_admin_disabled(self, monkeypatch, pyramid_request):
api_token="fake-api-token",
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]
assert pending_gitlab_publisher_form_cls.calls == [
pretend.call(
pyramid_request.POST,
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]

Expand Down Expand Up @@ -3752,13 +3757,15 @@ def test_add_pending_oidc_publisher_admin_disabled(
api_token="fake-api-token",
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]
assert pending_gitlab_publisher_form_cls.calls == [
pretend.call(
pyramid_request.POST,
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]

Expand Down Expand Up @@ -3896,13 +3903,15 @@ def test_add_pending_oidc_publisher_user_cannot_register(
api_token="fake-api-token",
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]
assert pending_gitlab_publisher_form_cls.calls == [
pretend.call(
pyramid_request.POST,
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]

Expand Down Expand Up @@ -4617,13 +4626,15 @@ def test_delete_pending_oidc_publisher_admin_disabled(
api_token="fake-api-token",
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]
assert pending_gitlab_publisher_form_cls.calls == [
pretend.call(
pyramid_request.POST,
route_url=pyramid_request.route_url,
check_project_name=project_service.check_project_name,
user=pyramid_request.user,
)
]

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def test_fails_with_ultranormalized_names(

assert resp.status_code == 400
assert resp.status == (
"400 The name {!r} is too similar to an existing project. "
"400 The name {!r} is too similar to an existing project named 'toasting'. "
"See /the/help/url/ for more information."
).format(conflicting_name)

Expand Down
37 changes: 27 additions & 10 deletions tests/unit/oidc/forms/test_activestate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from webob.multidict import MultiDict

from warehouse.oidc.forms import activestate
from warehouse.packaging.interfaces import ProjectNameUnavailableReason
from warehouse.packaging.models import ProjectNameUnavailableExisting

fake_username = "some-username"
fake_org_name = "some-org"
Expand Down Expand Up @@ -50,6 +50,7 @@ def check_project_name(name):
MultiDict(data),
route_url=route_url,
check_project_name=check_project_name,
user=pretend.stub(),
)

# Test built-in validations
Expand All @@ -61,27 +62,43 @@ def check_project_name(name):
assert form._route_url == route_url
assert form.validate()

def test_validate_project_name_already_in_use(self, pyramid_config):
@pytest.mark.parametrize("is_project_owner", [True, False])
def test_validate_project_name_already_in_use(
self, pyramid_config, is_project_owner
):
route_url = pretend.call_recorder(lambda *args, **kwargs: "")
user = pretend.stub()
owners = []
if is_project_owner:
owners.append(user)

def check_project_name(name):
return ProjectNameUnavailableReason.AlreadyExists
return ProjectNameUnavailableExisting(
existing_project=pretend.stub(owners=owners)
)

form = activestate.PendingActiveStatePublisherForm(
route_url=route_url,
check_project_name=check_project_name,
user=user,
)

field = pretend.stub(data="some-project")
with pytest.raises(wtforms.validators.ValidationError):
form.validate_project_name(field)
assert route_url.calls == [
pretend.call(
"manage.project.settings.publishing",
project_name="some-project",
_query={"provider": {"activestate"}},
)
]

if is_project_owner:
# The project settings URL is only shown in the error message if
# the user is the owner of the project
assert route_url.calls == [
pretend.call(
"manage.project.settings.publishing",
project_name="some-project",
_query={"provider": {"activestate"}},
)
]
else:
assert route_url.calls == []


class TestActiveStatePublisherForm:
Expand Down
57 changes: 46 additions & 11 deletions tests/unit/oidc/forms/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@
from webob.multidict import MultiDict

from warehouse.oidc.forms import github
from warehouse.packaging.interfaces import ProjectNameUnavailableReason
from warehouse.packaging.models import (
ProjectNameUnavailableExisting,
ProjectNameUnavailableInvalid,
ProjectNameUnavailableProhibited,
ProjectNameUnavailableSimilar,
ProjectNameUnavailableStdlib,
)


class TestPendingGitHubPublisherForm:
def test_validate(self, monkeypatch):
route_url = pretend.stub()
user = pretend.stub()

def check_project_name(name):
return None # Name is available.
Expand All @@ -41,6 +48,7 @@ def check_project_name(name):
api_token=pretend.stub(),
route_url=route_url,
check_project_name=check_project_name,
user=user,
)

# We're testing only the basic validation here.
Expand All @@ -49,34 +57,61 @@ def check_project_name(name):

assert form._check_project_name == check_project_name
assert form._route_url == route_url
assert form._user == user
assert form.validate()

def test_validate_project_name_already_in_use(self, pyramid_config):
@pytest.mark.parametrize("is_project_owner", [True, False])
def test_validate_project_name_already_in_use(
self, pyramid_config, is_project_owner
):
route_url = pretend.call_recorder(lambda *args, **kwargs: "")
user = pretend.stub()
owners = []
if is_project_owner:
owners.append(user)

form = github.PendingGitHubPublisherForm(
api_token="fake-token",
route_url=route_url,
check_project_name=lambda name: ProjectNameUnavailableReason.AlreadyExists,
check_project_name=lambda name: ProjectNameUnavailableExisting(
existing_project=pretend.stub(owners=owners)
),
user=user,
)

field = pretend.stub(data="some-project")
with pytest.raises(wtforms.validators.ValidationError):
form.validate_project_name(field)
assert route_url.calls == [
pretend.call(
"manage.project.settings.publishing",
project_name="some-project",
_query={"provider": {"github"}},
)
]

@pytest.mark.parametrize("reason", list(ProjectNameUnavailableReason))
if is_project_owner:
# The project settings URL is only shown in the error message if
# the user is the owner of the project
assert route_url.calls == [
pretend.call(
"manage.project.settings.publishing",
project_name="some-project",
_query={"provider": {"github"}},
)
]
else:
assert route_url.calls == []

@pytest.mark.parametrize(
"reason",
[
ProjectNameUnavailableExisting(pretend.stub(owners=[pretend.stub()])),
ProjectNameUnavailableInvalid(),
ProjectNameUnavailableStdlib(),
ProjectNameUnavailableProhibited(),
ProjectNameUnavailableSimilar(pretend.stub()),
],
)
def test_validate_project_name_unavailable(self, reason, pyramid_config):
form = github.PendingGitHubPublisherForm(
api_token="fake-token",
route_url=pretend.call_recorder(lambda *args, **kwargs: ""),
check_project_name=lambda name: reason,
user=pretend.stub(),
)

field = pretend.stub(data="some-project")
Expand Down
44 changes: 31 additions & 13 deletions tests/unit/oidc/forms/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
from webob.multidict import MultiDict

from warehouse.oidc.forms import gitlab
from warehouse.packaging.interfaces import ProjectNameUnavailableReason
from warehouse.packaging.models import ProjectNameUnavailableExisting


class TestPendingGitLabPublisherForm:
def test_validate(self, monkeypatch):
route_url = pretend.stub()
user = pretend.stub()

def check_project_name(name):
return None # Name is available.
Expand All @@ -36,32 +37,51 @@ def check_project_name(name):
}
)
form = gitlab.PendingGitLabPublisherForm(
MultiDict(data), route_url=route_url, check_project_name=check_project_name
MultiDict(data),
route_url=route_url,
check_project_name=check_project_name,
user=user,
)

assert form._route_url == route_url
assert form._check_project_name == check_project_name
assert form._user == user
# We're testing only the basic validation here.
assert form.validate()

def test_validate_project_name_already_in_use(self, pyramid_config):
@pytest.mark.parametrize("is_project_owner", [True, False])
def test_validate_project_name_already_in_use(
self, pyramid_config, is_project_owner
):
user = pretend.stub()
owners = []
if is_project_owner:
owners.append(user)
route_url = pretend.call_recorder(lambda *args, **kwargs: "my_url")

form = gitlab.PendingGitLabPublisherForm(
route_url=route_url,
check_project_name=lambda name: ProjectNameUnavailableReason.AlreadyExists,
check_project_name=lambda name: ProjectNameUnavailableExisting(
existing_project=pretend.stub(owners=owners)
),
user=user,
)

field = pretend.stub(data="some-project")
with pytest.raises(wtforms.validators.ValidationError):
form.validate_project_name(field)
assert route_url.calls == [
pretend.call(
"manage.project.settings.publishing",
project_name="some-project",
_query={"provider": {"gitlab"}},
)
]
if is_project_owner:
# The project settings URL is only shown in the error message if
# the user is the owner of the project
assert route_url.calls == [
pretend.call(
"manage.project.settings.publishing",
project_name="some-project",
_query={"provider": {"gitlab"}},
)
]
else:
assert route_url.calls == []


class TestGitLabPublisherForm:
Expand Down Expand Up @@ -138,7 +158,6 @@ def test_validate_basic_invalid_fields(self, monkeypatch, data):
["invalid.git", "invalid.atom", "invalid--project"],
)
def test_reserved_project_names(self, project_name):

data = MultiDict(
{
"namespace": "some",
Expand All @@ -160,7 +179,6 @@ def test_reserved_project_names(self, project_name):
],
)
def test_reserved_organization_names(self, namespace):

data = MultiDict(
{
"namespace": namespace,
Expand Down
Loading

0 comments on commit 7fc5519

Please sign in to comment.