Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve ports when munging repository URLs #1112

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/fix-repo-urls-with-auth-and-port.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix bug for Repository URLs with auth where the port was lost. When attempting
to prevent printing authentication credentials in URLs provided with username
and password, we did not properly handle the case where the URL also contains
a port (when reconstructing the URL). This is now handled and tested to ensure
no regressions.
41 changes: 32 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,38 @@ def test_get_repository_config_missing(config_file):
assert utils.get_repository_from_config(config_file, "pypi") == exp


def test_get_repository_config_url_with_auth(config_file):
repository_url = "https://user:pass@notexisting.python.org/pypi"
exp = {
"repository": "https://notexisting.python.org/pypi",
"username": "user",
"password": "pass",
}
assert utils.get_repository_from_config(config_file, "foo", repository_url) == exp
assert utils.get_repository_from_config(config_file, "pypi", repository_url) == exp
@pytest.mark.parametrize(
"repository_url, expected_config",
[
(
"https://user:pass@notexisting.python.org/pypi",
{
"repository": "https://notexisting.python.org/pypi",
"username": "user",
"password": "pass",
},
),
(
"https://auser:pass@pypi.proxy.local.repo.net:8443",
{
"repository": "https://pypi.proxy.local.repo.net:8443",
"username": "auser",
"password": "pass",
},
),
],
)
def test_get_repository_config_url_with_auth(
config_file, repository_url, expected_config
):
assert (
utils.get_repository_from_config(config_file, "foo", repository_url)
== expected_config
)
assert (
utils.get_repository_from_config(config_file, "pypi", repository_url)
== expected_config
)


@pytest.mark.parametrize(
Expand Down
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,22 @@ commands =

[testenv:changelog]
basepython = python3
skip_install = True
deps =
towncrier
commands =
towncrier build {posargs}


# Usage:
# tox -e create-changelog-item -- [additional arguments] {filename}.{bugfix,feature,doc,removal,misc}
[testenv:create-changelog-item]
basepython = python3
skip_install = True
deps = towncrier
commands =
towncrier create --config pyproject.toml {posargs}

[testenv:release]
# specify Python 3 to use platform's default Python 3
basepython = python3
Expand Down
4 changes: 3 additions & 1 deletion twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def _config_from_repository_url(url: str) -> RepositoryConfig:
if parsed.username:
config["username"] = parsed.username
config["password"] = parsed.password
config["repository"] = urlunparse((parsed.scheme, parsed.hostname) + parsed[2:])
config["repository"] = cast(
str, rfc3986.urlparse(url).copy_with(userinfo=None).unsplit()
)
config["repository"] = normalize_repository_url(cast(str, config["repository"]))
return config

Expand Down