Skip to content

Commit

Permalink
Merge pull request #1112 from pypa/bug/1111
Browse files Browse the repository at this point in the history
Preserve ports when munging repository URLs
  • Loading branch information
sigmavirus24 committed May 21, 2024
2 parents c588793 + 3eb9121 commit 6fbf880
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
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

0 comments on commit 6fbf880

Please sign in to comment.