Skip to content

Commit

Permalink
Warn if the server changes the scheme, host or port during ``devpi us…
Browse files Browse the repository at this point in the history
…e``.
  • Loading branch information
fschulze committed May 28, 2022
1 parent f52baca commit 7a6749e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client/devpi/use.py
@@ -1,4 +1,5 @@
from copy import deepcopy
from operator import attrgetter
import itertools
import os
import sys
Expand Down Expand Up @@ -476,6 +477,16 @@ def main(hub, args=None):
current = hub.current.switch_to_temporary(hub, url)
if url or current.index:
current.configure_fromurl(hub, url, client_cert=args.client_cert)
url_parts = attrgetter('scheme', 'hostname', 'port')(URL(url))
if url_parts[0]:
# only check if a full url was used
new_url = current.index_url if current.index else current.root_url
new_parts = attrgetter('scheme', 'hostname', 'port')(new_url)
for url_part, new_part in zip(url_parts, new_parts):
if url_part != new_part:
hub.warn(
"The server has rewritten the url to: %s" % new_url)
break

if args.list:
rooturl = current.root_url
Expand Down
1 change: 1 addition & 0 deletions client/news/url_rewritten.feature
@@ -0,0 +1 @@
Warn if the server changes the scheme, host or port during ``devpi use``.
68 changes: 68 additions & 0 deletions client/testing/test_use.py
Expand Up @@ -134,6 +134,74 @@ def test_local_config_no_auth_key(self, cmd_devpi, create_venv, monkeypatch):
local_current_config = json.load(f)
assert 'auth' not in local_current_config

def test_rewritten_url_scheme(self, capfd, cmd_devpi, mock_http_api):
from devpi_common.url import URL
mock_http_api.set(
"http://devpi/foo/bar/+api", 200, result=dict(
index="https://devpi/foo/bar",
login="/+login",
authstatus=["noauth", ""]))
mock_http_api.set(
"https://devpi/foo/bar?no_projects=", 200, result=dict())
hub = cmd_devpi("use", "http://devpi/foo/bar")
(out, err) = capfd.readouterr()
assert 'The server has rewritten the url to: https://devpi/foo/bar\n' in out
assert hub.current.index_url == URL('https://devpi/foo/bar')

def test_rewritten_url_host(self, capfd, cmd_devpi, mock_http_api):
from devpi_common.url import URL
mock_http_api.set(
"http://devpi/foo/bar/+api", 200, result=dict(
index="http://127.0.0.1/foo/bar",
login="/+login",
authstatus=["noauth", ""]))
mock_http_api.set(
"http://127.0.0.1/foo/bar?no_projects=", 200, result=dict())
hub = cmd_devpi("use", "http://devpi/foo/bar")
(out, err) = capfd.readouterr()
assert 'The server has rewritten the url to: http://127.0.0.1/foo/bar\n' in out
assert hub.current.index_url == URL('http://127.0.0.1/foo/bar')

def test_rewritten_url_port(self, capfd, cmd_devpi, mock_http_api):
from devpi_common.url import URL
mock_http_api.set(
"http://devpi/foo/bar/+api", 200, result=dict(
index="http://devpi:3141/foo/bar",
login="/+login",
authstatus=["noauth", ""]))
mock_http_api.set(
"http://devpi:3141/foo/bar?no_projects=", 200, result=dict())
hub = cmd_devpi("use", "http://devpi/foo/bar")
(out, err) = capfd.readouterr()
assert 'The server has rewritten the url to: http://devpi:3141/foo/bar\n' in out
assert hub.current.index_url == URL('http://devpi:3141/foo/bar')

def test_rewritten_url_path(self, capfd, cmd_devpi, mock_http_api):
from devpi_common.url import URL
mock_http_api.set(
"http://devpi/foo/bar/+api", 200, result=dict(
index="http://devpi/foo/bar/",
login="/+login",
authstatus=["noauth", ""]))
mock_http_api.set(
"http://devpi/foo/bar/?no_projects=", 200, result=dict())
hub = cmd_devpi("use", "http://devpi/foo/bar")
(out, err) = capfd.readouterr()
assert 'The server has rewritten the url to:' not in out
assert hub.current.index_url == URL('http://devpi/foo/bar/')

def test_rewritten_url_root(self, capfd, cmd_devpi, mock_http_api):
from devpi_common.url import URL
mock_http_api.set(
"http://devpi/+api", 200, result=dict(
login="https://devpi/+login",
authstatus=["noauth", ""]))
hub = cmd_devpi("use", "http://devpi/")
(out, err) = capfd.readouterr()
assert 'The server has rewritten the url to: https://devpi/\n' in out
assert hub.current.index_url == URL('')
assert hub.current.root_url == URL('https://devpi/')

@pytest.mark.skipif("config.option.fast")
def test_use_list_doesnt_write(self, tmpdir, cmd_devpi, mock_http_api):
import time
Expand Down

0 comments on commit 7a6749e

Please sign in to comment.