diff --git a/src/osw/cli/ops.py b/src/osw/cli/ops.py index 62cf9669..f91cd6cb 100644 --- a/src/osw/cli/ops.py +++ b/src/osw/cli/ops.py @@ -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: diff --git a/src/osw/express.py b/src/osw/express.py index 50da2d03..d9525fbc 100644 --- a/src/osw/express.py +++ b/src/osw/express.py @@ -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, @@ -67,10 +71,18 @@ class Config: @validator("domain") def validate_domain(cls, v): - pattern = r"^(?!-)[A-Za-z0-9.-]{1,63}(? 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``. diff --git a/src/osw/service/context.py b/src/osw/service/context.py index eb405bf9..3127e73a 100644 --- a/src/osw/service/context.py +++ b/src/osw/service/context.py @@ -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 diff --git a/tests/test_express_domain.py b/tests/test_express_domain.py new file mode 100644 index 00000000..d7e0c6a9 --- /dev/null +++ b/tests/test_express_domain.py @@ -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) diff --git a/tests/test_service_context.py b/tests/test_service_context.py index 63f89a2a..49cc06e7 100644 --- a/tests/test_service_context.py +++ b/tests/test_service_context.py @@ -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())