|
| 1 | +"""Unit tests for how a wiki file controller finds its session. |
| 2 | +
|
| 3 | +The session is optional: a controller built from a file entity connects on |
| 4 | +first use. A file that is about to be uploaded has no URL yet - that is what |
| 5 | +the upload produces - so the URL cannot be the only way to find the wiki. |
| 6 | +
|
| 7 | +Offline: the session is never built here, only the inputs that decide which |
| 8 | +one would be. |
| 9 | +""" |
| 10 | + |
| 11 | +from io import BytesIO |
| 12 | + |
| 13 | +import pytest |
| 14 | +from oold.backend.auth import UserPwdCredential, set_credential |
| 15 | + |
| 16 | +import osw.controller.file.wiki as wiki_module |
| 17 | +from osw.controller.file.local import LocalFileController |
| 18 | +from osw.controller.file.wiki import OSW, WikiFileController, WtSite |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def _clean_credentials(monkeypatch): |
| 23 | + """Keep the process-wide credential store and session cache per test.""" |
| 24 | + import oold.backend.auth as auth |
| 25 | + |
| 26 | + saved = auth._credentials.get() |
| 27 | + auth._credentials.set({}) |
| 28 | + wiki_module.clear_sessions() |
| 29 | + yield |
| 30 | + auth._credentials.set(saved) |
| 31 | + wiki_module.clear_sessions() |
| 32 | + |
| 33 | + |
| 34 | +def _credential(iri, username="bot", password="secret"): |
| 35 | + return UserPwdCredential(iri=iri, username=username, password=password) |
| 36 | + |
| 37 | + |
| 38 | +def _a_session() -> OSW: |
| 39 | + """A stand-in session: only which one travels is under test. |
| 40 | +
|
| 41 | + pydantic v1 copies a model on assignment, so the controller does not hold |
| 42 | + the very object that was passed in; the sessions are told apart by the |
| 43 | + site they carry. |
| 44 | + """ |
| 45 | + return OSW.construct(site=WtSite.__new__(WtSite)) |
| 46 | + |
| 47 | + |
| 48 | +def _controller(**kwargs): |
| 49 | + return WikiFileController(**kwargs) |
| 50 | + |
| 51 | + |
| 52 | +# -- choosing the wiki -------------------------------------------------------- |
| 53 | + |
| 54 | + |
| 55 | +def test_a_url_decides_the_wiki(): |
| 56 | + controller = _controller(download_url="https://wiki.example.org/wiki/File:A.bin") |
| 57 | + assert controller._session_url() == "https://wiki.example.org/wiki/File:A.bin" |
| 58 | + |
| 59 | + |
| 60 | +def test_without_a_url_the_single_registered_credential_decides(): |
| 61 | + """A fresh upload has no URL, so the credential is the only pointer.""" |
| 62 | + set_credential(_credential("wiki.example.org")) |
| 63 | + assert _controller()._session_url() == "wiki.example.org" |
| 64 | + |
| 65 | + |
| 66 | +def test_without_a_url_and_without_a_credential_the_error_says_so(): |
| 67 | + with pytest.raises(ValueError, match="no credential is registered"): |
| 68 | + _controller()._session_url() |
| 69 | + |
| 70 | + |
| 71 | +def test_several_credentials_without_a_url_are_ambiguous(): |
| 72 | + set_credential(_credential("wiki.example.org")) |
| 73 | + set_credential(_credential("other.example.org")) |
| 74 | + with pytest.raises(ValueError, match=r"other\.example\.org"): |
| 75 | + _controller()._session_url() |
| 76 | + |
| 77 | + |
| 78 | +def test_a_session_that_was_passed_in_is_kept(): |
| 79 | + sentinel = _a_session() |
| 80 | + controller = _controller(osw=sentinel) |
| 81 | + assert controller._ensure_session().site is sentinel.site |
| 82 | + |
| 83 | + |
| 84 | +# -- the session cache -------------------------------------------------------- |
| 85 | + |
| 86 | + |
| 87 | +def test_sessions_are_cached_per_credential(monkeypatch): |
| 88 | + """Two path-scoped credentials of one host are two logins.""" |
| 89 | + built = [] |
| 90 | + monkeypatch.setattr( |
| 91 | + wiki_module, "OSW", lambda site: built.append(site) or f"session{len(built)}" |
| 92 | + ) |
| 93 | + monkeypatch.setattr(wiki_module, "WtSite", lambda config: config) |
| 94 | + monkeypatch.setattr(wiki_module.WtSite, "WtSiteConfig", dict, raising=False) |
| 95 | + set_credential(_credential("wiki.example.org/a", username="a")) |
| 96 | + set_credential(_credential("wiki.example.org/b", username="b")) |
| 97 | + |
| 98 | + first = wiki_module.session_for("https://wiki.example.org/a/File:X") |
| 99 | + second = wiki_module.session_for("https://wiki.example.org/b/File:X") |
| 100 | + again = wiki_module.session_for("https://wiki.example.org/a/File:Y") |
| 101 | + |
| 102 | + assert first != second |
| 103 | + assert again == first |
| 104 | + |
| 105 | + |
| 106 | +def test_a_replaced_credential_replaces_the_session(monkeypatch): |
| 107 | + """A later set_credential must not be ignored for the process lifetime.""" |
| 108 | + built = [] |
| 109 | + monkeypatch.setattr( |
| 110 | + wiki_module, "OSW", lambda site: built.append(site) or f"session{len(built)}" |
| 111 | + ) |
| 112 | + monkeypatch.setattr(wiki_module, "WtSite", lambda config: config) |
| 113 | + monkeypatch.setattr(wiki_module.WtSite, "WtSiteConfig", dict, raising=False) |
| 114 | + set_credential(_credential("wiki.example.org", password="old")) |
| 115 | + first = wiki_module.session_for("https://wiki.example.org/File:X") |
| 116 | + |
| 117 | + set_credential(_credential("wiki.example.org", password="new")) |
| 118 | + second = wiki_module.session_for("https://wiki.example.org/File:X") |
| 119 | + |
| 120 | + assert second != first |
| 121 | + |
| 122 | + |
| 123 | +def test_a_missing_credential_is_reported_with_the_host(): |
| 124 | + with pytest.raises(ValueError, match=r"wiki\.example\.org"): |
| 125 | + wiki_module.session_for("https://wiki.example.org/File:X") |
| 126 | + |
| 127 | + |
| 128 | +# -- from_other --------------------------------------------------------------- |
| 129 | + |
| 130 | + |
| 131 | +def test_from_other_keeps_a_session_the_source_carries(): |
| 132 | + sentinel = _a_session() |
| 133 | + source = WikiFileController(osw=sentinel, download_url="https://w.example/x") |
| 134 | + |
| 135 | + target = WikiFileController.from_other(source) |
| 136 | + |
| 137 | + assert target.osw.site is sentinel.site |
| 138 | + |
| 139 | + |
| 140 | +def test_from_other_takes_the_session_it_is_given(): |
| 141 | + sentinel = _a_session() |
| 142 | + other = _a_session() |
| 143 | + source = WikiFileController(osw=other, download_url="https://w.example/x") |
| 144 | + |
| 145 | + target = WikiFileController.from_other(source, osw=sentinel) |
| 146 | + |
| 147 | + assert target.osw.site is sentinel.site |
| 148 | + assert target.osw.site is not other.site |
| 149 | + |
| 150 | + |
| 151 | +# -- the local controller ----------------------------------------------------- |
| 152 | + |
| 153 | + |
| 154 | +def test_local_controller_needs_a_file(): |
| 155 | + with pytest.raises(ValueError, match="needs a file"): |
| 156 | + LocalFileController() |
| 157 | + |
| 158 | + |
| 159 | +def test_local_controller_accepts_path_or_local_path(tmp_path): |
| 160 | + target = tmp_path / "a.bin" |
| 161 | + target.write_bytes(b"x") |
| 162 | + |
| 163 | + assert LocalFileController(path=target).path == target |
| 164 | + assert LocalFileController(local_path=str(target)).path == target |
| 165 | + |
| 166 | + |
| 167 | +def test_local_controller_round_trips_a_file(tmp_path): |
| 168 | + controller = LocalFileController(path=tmp_path / "a.bin") |
| 169 | + controller.put(BytesIO(b"payload")) |
| 170 | + with controller.get() as handle: |
| 171 | + assert handle.read() == b"payload" |
0 commit comments