"""Integration: the librarian's Crossref-contact resolution. CONJURER_CROSSREF_MAILTO alone is a valid, complete configuration. A missing netrc must NOT produce a "credentials missing" warning in that case - the old code warned on every single search even though the env var was set and used. Only a genuine absence of any contact should warn (and then raise). """ import logging import sys import types import pytest if "habanero" not in sys.modules: _habanero = types.ModuleType("habanero") _habanero.Crossref = object sys.modules["habanero"] = _habanero import conjurer_librarian as lib # noqa: E402 class _DummyCrossref: """Accepts the kwargs the real Crossref does, so Librarian() can construct.""" def __init__(self, **kwargs): self.kwargs = kwargs @pytest.fixture(autouse=True) def _crossref_and_missing_netrc(monkeypatch): # Build with a harmless Crossref, and force the netrc read to miss so the # env-var path is what's exercised. monkeypatch.setattr(lib, "Crossref", _DummyCrossref) monkeypatch.setattr(lib, "NETRC_FILE", "/nonexistent/conjurer/.netrc") def test_env_mailto_alone_does_not_warn(monkeypatch, caplog): monkeypatch.setenv("CONJURER_CROSSREF_MAILTO", "mtuszowski@example.com") with caplog.at_level(logging.WARNING, logger="conjurer_librarian"): librarian = lib.Librarian(lib.app, "kwas foliowy", "uuid-1", False) assert librarian.uuid == "uuid-1" # constructed fine assert not any( "credentials missing" in r.getMessage().lower() or "not configured" in r.getMessage().lower() for r in caplog.records ), "a missing netrc must not warn when CONJURER_CROSSREF_MAILTO is set" def test_no_contact_anywhere_raises(monkeypatch): monkeypatch.delenv("CONJURER_CROSSREF_MAILTO", raising=False) with pytest.raises(RuntimeError): lib.Librarian(lib.app, "kwas foliowy", "uuid-2", False)