Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/osw/cli/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def status_instances(ctx: Context) -> dict:
# Not ctx.osw: that one is cached and bound to the active
# instance. These connections are ours alone, so we close each
# one instead of leaving it open for the rest of the command.
connection = ctx.osw_for(config.derive_domain(iri))
connection = ctx.osw_for(config.derive_connect_domain(iri))
entry["connected"] = True
_close_quietly(connection, iri)
except Exception as exc:
Expand Down
31 changes: 26 additions & 5 deletions src/osw/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ class OswExpress(OSW):
"""This class provides convenience functions for osw-python."""

domain: str
"""The domain of the OSL instance to connect to."""
"""The domain of the OSL instance to connect to, as a bare host with an
optional port, e.g. 'wiki.example.org' or 'localhost:8081'."""
scheme: str = "https"
"""The URL scheme used to connect, 'https' unless the domain was given with
an 'http://' prefix, e.g. for a local test wiki."""
cred_filepath: Optional[Union[str, Path]]
"""The filepath to the credentials file. Will be overwritten by the cred_filepath
of cred_mngr, if it posses a non-None value. If cred_filepath is None,
Expand All @@ -67,10 +71,18 @@ class Config:

@validator("domain")
def validate_domain(cls, v):
pattern = r"^(?!-)[A-Za-z0-9.-]{1,63}(?<!-)\.[A-Za-z]{2,}$"
# Dot-separated labels (a single label such as 'localhost' is allowed),
# each without a leading or trailing hyphen, and an optional port.
label = r"(?!-)[A-Za-z0-9-]{1,63}(?<!-)"
pattern = rf"^{label}(?:\.{label})*(?::\d{{1,5}})?$"
assert re.match(pattern, v), "The domain is not valid." # noqa: S101 pydantic validator idiom
return v

@validator("scheme")
def validate_scheme(cls, v):
assert v in ("http", "https"), "The scheme must be 'http' or 'https'." # noqa: S101 pydantic validator idiom
return v

@overload
def __init__(
self,
Expand Down Expand Up @@ -111,6 +123,13 @@ def __init__(
"\nIf no domain was set as environmental variable, 'domain' is a "
"required argument."
)
# An 'http://' or 'https://' prefix selects the scheme; the domain itself
# is stored bare, because other code builds URLs and IRIs from it.
scheme = "https"
match = re.match(r"^(https?)://", domain, flags=re.IGNORECASE)
if match:
scheme = match.group(1).lower()
domain = domain[match.end() :].rstrip("/")
if cred_mngr is not None and cred_mngr.cred_filepath is not None:
# If a credential manager is explicitly defined, that should have priority
cred_filepath = cred_mngr.cred_filepath[0]
Expand Down Expand Up @@ -144,7 +163,7 @@ def __init__(
cred_mngr = CredentialManager(cred_filepath=cred_filepath)
# Test if domain is reachable
try:
url = f"https://{domain}/wiki/Main_Page"
url = f"{scheme}://{domain}/wiki/Main_Page"
response = requests.get(url) # noqa: S113 reachability probe, TODO add timeout
if response.status_code == 200:
# Domain is reachable
Expand All @@ -155,8 +174,10 @@ def __init__(
)
except Exception as e:
raise ConnectionError(f"Could not connect to '{domain}'. Error: {e}")
site = WtSite(WtSite.WtSiteConfig(iri=domain, cred_mngr=cred_mngr))
super().__init__(**{"site": site, "domain": domain})
# WtSite defaults to https; only an http iri needs the scheme spelled out.
iri = domain if scheme == "https" else f"{scheme}://{domain}"
site = WtSite(WtSite.WtSiteConfig(iri=iri, cred_mngr=cred_mngr))
super().__init__(**{"site": site, "domain": domain, "scheme": scheme})
self.cred_mngr = cred_mngr
self.cred_filepath = cred_filepath
_logger.info(f"Connected to '{domain}'.")
Expand Down
13 changes: 13 additions & 0 deletions src/osw/service/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,19 @@ def derive_domain(iri: str) -> str:
return _derive_domain(iri)


def derive_connect_domain(iri: str) -> str:
"""Return the domain argument for ``OswExpress`` that connects to ``iri``.

This is the bare domain, prefixed with ``http://`` when ``iri`` names that
scheme, so that an http-only wiki (e.g. a local test wiki) is not reached
over https.
"""
domain = _derive_domain(iri)
if iri.lower().startswith("http://"):
return f"http://{domain}"
return domain


def set_active_instance(iri: str) -> None:
"""Set the active instance to ``iri``.

Expand Down
4 changes: 3 additions & 1 deletion src/osw/service/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def osw(self) -> OswExpress:
``CredentialManager`` and passed to ``OswExpress`` explicitly.
"""
if self._osw is None:
self._osw = self.osw_for(self._require_active_domain())
self._require_active_domain()
iri = config.get_active_iri()
self._osw = self.osw_for(config.derive_connect_domain(iri))
return self._osw

@osw.setter
Expand Down
117 changes: 117 additions & 0 deletions tests/test_express_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""OswExpress accepts an http(s) scheme prefix and local hosts with a port."""

from unittest.mock import MagicMock

import pytest

import osw.express


@pytest.fixture
def connect(mocker):
"""Build an OswExpress with the network calls replaced by fakes.

Returns a function that takes the domain and returns the instance, the URL
of the reachability check and the iri passed to WtSite.
"""

def _connect(domain):
get = mocker.patch(
"osw.express.requests.get", return_value=MagicMock(status_code=200)
)
wtsite = mocker.patch(
"osw.express.WtSite",
MagicMock(return_value=MagicMock(spec=osw.express.WtSite)),
)
instance = osw.express.OswExpress(
domain=domain, cred_mngr=MagicMock(cred_filepath=None)
)
probe_url = get.call_args.args[0]
iri = wtsite.WtSiteConfig.call_args.kwargs["iri"]
return instance, probe_url, iri

return _connect


@pytest.mark.parametrize(
"given,domain,scheme,probe_url,iri",
[
(
"wiki.example.org",
"wiki.example.org",
"https",
"https://wiki.example.org/wiki/Main_Page",
"wiki.example.org",
),
(
"https://wiki.example.org/",
"wiki.example.org",
"https",
"https://wiki.example.org/wiki/Main_Page",
"wiki.example.org",
),
(
"localhost:8081",
"localhost:8081",
"https",
"https://localhost:8081/wiki/Main_Page",
"localhost:8081",
),
(
"http://localhost:8081",
"localhost:8081",
"http",
"http://localhost:8081/wiki/Main_Page",
"http://localhost:8081",
),
(
"HTTP://127.0.0.1:8081",
"127.0.0.1:8081",
"http",
"http://127.0.0.1:8081/wiki/Main_Page",
"http://127.0.0.1:8081",
),
],
)
def test_scheme_prefix_and_local_hosts(connect, given, domain, scheme, probe_url, iri):
instance, probed, site_iri = connect(given)
assert instance.domain == domain
assert instance.scheme == scheme
assert probed == probe_url
assert site_iri == iri


@pytest.mark.parametrize(
"domain,valid",
[
("wiki-dev.open-semantic-lab.org", True),
("sub.domain.co.uk", True),
("localhost", True),
("localhost:8081", True),
("127.0.0.1:8081", True),
("mediawiki", True),
("http://localhost:8081", False),
("not a domain!", False),
("-invalid.com", False),
("invalid-.com", False),
("wiki.example.org:port", False),
("", False),
],
)
def test_validate_domain(domain, valid):
if valid:
assert osw.express.OswExpress.validate_domain(domain) == domain
else:
with pytest.raises(AssertionError):
osw.express.OswExpress.validate_domain(domain)


@pytest.mark.parametrize(
"scheme,valid", [("http", True), ("https", True), ("ftp", False)]
)
def test_validate_scheme(scheme, valid):
if valid:
assert osw.express.OswExpress.validate_scheme(scheme) == scheme
else:
with pytest.raises(AssertionError):
osw.express.OswExpress.validate_scheme(scheme)
20 changes: 20 additions & 0 deletions tests/test_service_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ def test_osw_property_raises_not_configured_when_no_active_domain(
assert "--instance" in str(exc_info.value)


@pytest.mark.parametrize(
"active_iri,connect_domain",
[
("http://localhost:8081", "http://localhost:8081"),
("https://wiki.example.org/w/", "wiki.example.org"),
("wiki.example.org", "wiki.example.org"),
],
)
def test_osw_property_keeps_an_http_scheme(monkeypatch, active_iri, connect_domain):
"""An http active iri reaches OswExpress with its scheme, https stays bare."""
monkeypatch.setattr(config, "get_active_iri", lambda: active_iri)
ctx = Context(_settings())
osw_for = MagicMock()
monkeypatch.setattr(ctx, "osw_for", osw_for)

_ = ctx.osw

osw_for.assert_called_once_with(connect_domain)


# -- limit ----------------------------------------------------------------
def test_limit_falls_back_to_settings_max_results():
ctx = Context(_settings(), osw=object())
Expand Down
Loading