test_makerworld.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. """Tests for the MakerWorldService."""
  2. from __future__ import annotations
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. from urllib.error import HTTPError, URLError
  5. import httpx
  6. import pytest
  7. from backend.app.services.makerworld import (
  8. _MAX_3MF_BYTES,
  9. MAKERWORLD_API_BASE,
  10. MakerWorldAuthError,
  11. MakerWorldForbiddenError,
  12. MakerWorldNotFoundError,
  13. MakerWorldService,
  14. MakerWorldUnavailableError,
  15. MakerWorldUrlError,
  16. )
  17. class TestParseUrl:
  18. """MakerWorld URL extraction."""
  19. def test_strips_locale_prefix_and_slug(self):
  20. model, profile = MakerWorldService.parse_url(
  21. "https://makerworld.com/en/models/1400373-self-watering-seed-starter"
  22. )
  23. assert model == 1400373
  24. assert profile is None
  25. def test_extracts_profile_id_from_fragment(self):
  26. model, profile = MakerWorldService.parse_url("https://makerworld.com/en/models/1400373-slug#profileId-1452154")
  27. assert model == 1400373
  28. assert profile == 1452154
  29. def test_accepts_scheme_omitted(self):
  30. model, profile = MakerWorldService.parse_url("makerworld.com/models/999")
  31. assert model == 999
  32. assert profile is None
  33. def test_accepts_subdomain(self):
  34. # Defensive: if MakerWorld ever stands up a regional subdomain, still accept it
  35. model, _ = MakerWorldService.parse_url("https://www.makerworld.com/en/models/42")
  36. assert model == 42
  37. def test_rejects_non_makerworld_host(self):
  38. with pytest.raises(MakerWorldUrlError):
  39. MakerWorldService.parse_url("https://thingiverse.com/things/123")
  40. def test_rejects_malformed_url(self):
  41. # No /models/ segment anywhere in path
  42. with pytest.raises(MakerWorldUrlError):
  43. MakerWorldService.parse_url("https://makerworld.com/en/creators/foo")
  44. def test_rejects_empty(self):
  45. with pytest.raises(MakerWorldUrlError):
  46. MakerWorldService.parse_url("")
  47. class TestApiBase:
  48. """Sanity check on the module-level constant — changing it is a deploy-risk."""
  49. def test_api_base_targets_bambulab_backend(self):
  50. # ``api.bambulab.com`` is not Cloudflare-fronted; ``makerworld.com`` is
  51. # and returns empty JSON to plain httpx. Regressing this constant
  52. # silently breaks the whole integration.
  53. assert MAKERWORLD_API_BASE == "https://api.bambulab.com/v1/design-service"
  54. class TestGetDesign:
  55. """Metadata endpoint happy-path + error mapping."""
  56. @pytest.fixture
  57. def service(self):
  58. # Use a MagicMock for the client so each call can be individually stubbed
  59. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  60. svc._client.get = AsyncMock()
  61. return svc
  62. @pytest.mark.asyncio
  63. async def test_returns_decoded_json(self, service):
  64. resp = MagicMock()
  65. resp.status_code = 200
  66. resp.json.return_value = {"id": 1400373, "title": "Benchy"}
  67. service._client.get.return_value = resp
  68. data = await service.get_design(1400373)
  69. assert data == {"id": 1400373, "title": "Benchy"}
  70. @pytest.mark.asyncio
  71. async def test_hits_bambulab_api_base(self, service):
  72. resp = MagicMock()
  73. resp.status_code = 200
  74. resp.json.return_value = {"id": 1}
  75. service._client.get.return_value = resp
  76. await service.get_design(1)
  77. call = service._client.get.call_args
  78. # First positional arg is the URL — must be on the api.bambulab.com
  79. # backend, not the Cloudflare-fronted makerworld.com host.
  80. url = call.args[0] if call.args else call.kwargs.get("url")
  81. assert url == "https://api.bambulab.com/v1/design-service/design/1"
  82. @pytest.mark.asyncio
  83. async def test_sends_browser_like_headers(self, service):
  84. """Post-refactor the client uses a minimal Firefox-ish header set.
  85. The old ``x-bbl-*`` Bambu-app identification headers are gone —
  86. ``api.bambulab.com`` accepts browser-like headers cleanly."""
  87. resp = MagicMock()
  88. resp.status_code = 200
  89. resp.json.return_value = {"id": 1}
  90. service._client.get.return_value = resp
  91. await service.get_design(1)
  92. headers = service._client.get.call_args.kwargs["headers"]
  93. assert "Firefox" in headers["User-Agent"]
  94. assert headers["Accept-Language"].startswith("en-US")
  95. assert headers["Referer"] == "https://makerworld.com/"
  96. assert "Accept" in headers
  97. # The deprecated Bambu-identification headers must no longer be sent.
  98. for dead_header in (
  99. "x-bbl-client-type",
  100. "x-bbl-client-version",
  101. "x-bbl-app-source",
  102. "x-bbl-client-name",
  103. ):
  104. assert dead_header not in headers
  105. @pytest.mark.asyncio
  106. async def test_maps_404_to_not_found(self, service):
  107. resp = MagicMock()
  108. resp.status_code = 404
  109. service._client.get.return_value = resp
  110. with pytest.raises(MakerWorldNotFoundError):
  111. await service.get_design(404)
  112. @pytest.mark.asyncio
  113. async def test_maps_401_to_auth_error(self, service):
  114. resp = MagicMock()
  115. resp.status_code = 401
  116. resp.json.return_value = {"code": 1, "error": "Please log in"}
  117. service._client.get.return_value = resp
  118. with pytest.raises(MakerWorldAuthError) as exc_info:
  119. await service.get_design(1)
  120. # Upstream's own message is surfaced to the caller
  121. assert "Please log in" in str(exc_info.value)
  122. @pytest.mark.asyncio
  123. async def test_maps_403_to_forbidden_with_upstream_reason(self, service):
  124. """403 is distinct from 401: auth was valid, MakerWorld refuses the
  125. specific resource (content-gated, region-locked, etc.). The upstream
  126. reason must reach the user so they know what to do."""
  127. resp = MagicMock()
  128. resp.status_code = 403
  129. resp.json.return_value = {
  130. "code": 15001,
  131. "error": "This model is only available to members",
  132. }
  133. service._client.get.return_value = resp
  134. with pytest.raises(MakerWorldForbiddenError) as exc_info:
  135. await service.get_design(1)
  136. assert "members" in str(exc_info.value)
  137. @pytest.mark.asyncio
  138. async def test_maps_5xx_to_unavailable(self, service):
  139. resp = MagicMock()
  140. resp.status_code = 503
  141. service._client.get.return_value = resp
  142. with pytest.raises(MakerWorldUnavailableError):
  143. await service.get_design(1)
  144. @pytest.mark.asyncio
  145. async def test_maps_timeout_to_unavailable(self, service):
  146. service._client.get.side_effect = httpx.TimeoutException("tooo slow")
  147. with pytest.raises(MakerWorldUnavailableError):
  148. await service.get_design(1)
  149. @pytest.mark.asyncio
  150. async def test_rejects_non_dict_json(self, service):
  151. resp = MagicMock()
  152. resp.status_code = 200
  153. resp.json.return_value = [1, 2, 3] # list, not dict
  154. service._client.get.return_value = resp
  155. with pytest.raises(MakerWorldUnavailableError):
  156. await service.get_design(1)
  157. class TestGetProfileDownload:
  158. """The new auth-gated 3MF manifest endpoint on the Bambu iot-service.
  159. Replaces the removed ``get_instance_download`` / ``get_model_download``
  160. helpers — YASTL#51's endpoint mints the signed CDN URL from the same
  161. long-lived Bambu Cloud bearer users already have.
  162. """
  163. def _make_service(self, *, auth_token: str | None = "tok-abc") -> MakerWorldService:
  164. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), auth_token=auth_token)
  165. svc._client.get = AsyncMock()
  166. return svc
  167. @pytest.mark.asyncio
  168. async def test_requires_auth_token(self):
  169. svc = self._make_service(auth_token=None)
  170. with pytest.raises(MakerWorldAuthError):
  171. await svc.get_profile_download(1452154, "US2bb73b106683e5")
  172. @pytest.mark.asyncio
  173. async def test_returns_signed_manifest(self):
  174. svc = self._make_service()
  175. resp = MagicMock()
  176. resp.status_code = 200
  177. resp.json.return_value = {
  178. "name": "benchy.3mf",
  179. "url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
  180. }
  181. svc._client.get.return_value = resp
  182. manifest = await svc.get_profile_download(1452154, "US2bb73b106683e5")
  183. assert manifest["url"].startswith("https://makerworld.bblmw.com/")
  184. assert manifest["name"] == "benchy.3mf"
  185. @pytest.mark.asyncio
  186. async def test_sends_bearer_and_model_id_query(self):
  187. """Auth goes in ``Authorization`` and the alphanumeric modelId as a
  188. ``model_id`` query param — this is what YASTL#51 reverse-engineered."""
  189. svc = self._make_service(auth_token="tok-abc")
  190. resp = MagicMock()
  191. resp.status_code = 200
  192. resp.json.return_value = {"url": "https://makerworld.bblmw.com/x.3mf"}
  193. svc._client.get.return_value = resp
  194. await svc.get_profile_download(1452154, "US2bb73b106683e5")
  195. call = svc._client.get.call_args
  196. url = call.args[0] if call.args else call.kwargs.get("url")
  197. assert url == "https://api.bambulab.com/v1/iot-service/api/user/profile/1452154"
  198. assert call.kwargs["headers"]["Authorization"] == "Bearer tok-abc"
  199. assert call.kwargs["params"] == {"model_id": "US2bb73b106683e5"}
  200. @pytest.mark.asyncio
  201. async def test_maps_401_to_auth_error(self):
  202. svc = self._make_service()
  203. resp = MagicMock()
  204. resp.status_code = 401
  205. resp.json.return_value = {"error": "token expired"}
  206. svc._client.get.return_value = resp
  207. with pytest.raises(MakerWorldAuthError):
  208. await svc.get_profile_download(1, "M1")
  209. @pytest.mark.asyncio
  210. async def test_maps_403_to_forbidden(self):
  211. svc = self._make_service()
  212. resp = MagicMock()
  213. resp.status_code = 403
  214. resp.json.return_value = {"error": "paid model"}
  215. svc._client.get.return_value = resp
  216. with pytest.raises(MakerWorldForbiddenError) as exc_info:
  217. await svc.get_profile_download(1, "M1")
  218. assert "paid model" in str(exc_info.value)
  219. @pytest.mark.asyncio
  220. async def test_maps_404_to_not_found(self):
  221. svc = self._make_service()
  222. resp = MagicMock()
  223. resp.status_code = 404
  224. svc._client.get.return_value = resp
  225. with pytest.raises(MakerWorldNotFoundError):
  226. await svc.get_profile_download(1, "M1")
  227. @pytest.mark.asyncio
  228. async def test_maps_timeout_to_unavailable(self):
  229. svc = self._make_service()
  230. svc._client.get.side_effect = httpx.TimeoutException("nope")
  231. with pytest.raises(MakerWorldUnavailableError):
  232. await svc.get_profile_download(1, "M1")
  233. @pytest.mark.asyncio
  234. async def test_rejects_non_dict_json(self):
  235. svc = self._make_service()
  236. resp = MagicMock()
  237. resp.status_code = 200
  238. resp.json.return_value = ["not", "a", "dict"]
  239. svc._client.get.return_value = resp
  240. with pytest.raises(MakerWorldUnavailableError):
  241. await svc.get_profile_download(1, "M1")
  242. class TestDownload3MF:
  243. """SSRF guard + size cap + streaming behaviour."""
  244. def _stream_ctx(self, resp):
  245. ctx = MagicMock()
  246. ctx.__aenter__ = AsyncMock(return_value=resp)
  247. ctx.__aexit__ = AsyncMock(return_value=None)
  248. return ctx
  249. @pytest.mark.asyncio
  250. @pytest.mark.parametrize(
  251. "url",
  252. [
  253. "https://example.com/steal.3mf",
  254. "https://169.254.169.254/meta", # EC2 metadata
  255. "http://internal.host/loot",
  256. "http://127.0.0.1/loot",
  257. ],
  258. )
  259. async def test_rejects_non_allowed_hosts(self, url):
  260. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  261. with pytest.raises(MakerWorldUrlError):
  262. await svc.download_3mf(url)
  263. @pytest.mark.asyncio
  264. async def test_s3_host_delegates_to_urllib_path(self):
  265. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  266. with patch(
  267. "backend.app.services.makerworld._download_s3_urllib",
  268. new=AsyncMock(return_value=(b"payload", "file.3mf")),
  269. ) as mocked:
  270. payload, filename = await svc.download_3mf(
  271. "https://s3.us-west-2.amazonaws.com/bucket/key/file.3mf?X-Amz-Signature=abc"
  272. )
  273. mocked.assert_awaited_once()
  274. # First arg is the verbatim URL — must NOT be round-tripped through
  275. # httpx/urlparse.urlencode since that breaks S3 SigV4.
  276. args = mocked.await_args.args
  277. assert args[0] == ("https://s3.us-west-2.amazonaws.com/bucket/key/file.3mf?X-Amz-Signature=abc")
  278. assert payload == b"payload"
  279. assert filename == "file.3mf"
  280. @pytest.mark.asyncio
  281. async def test_cdn_url_uses_httpx_with_minimal_headers(self):
  282. """Signed CDN URLs already carry the auth in the query string — don't
  283. leak the Bambu Cloud bearer to the CDN too. The client is reduced to a
  284. single ``User-Agent`` header; no ``Authorization``, no ``x-bbl-*``."""
  285. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient), auth_token="tok-abc")
  286. resp = MagicMock()
  287. resp.status_code = 200
  288. async def _chunks():
  289. yield b"PK\x03\x04"
  290. resp.aiter_bytes = lambda: _chunks()
  291. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  292. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  293. call = svc._client.stream.call_args
  294. headers = call.kwargs["headers"]
  295. # Minimal: UA only. No bearer to the CDN.
  296. assert "Authorization" not in headers
  297. assert all(not k.startswith("x-bbl") for k in headers)
  298. assert "User-Agent" in headers
  299. # Redirects off — host allowlist is only meaningful on the initial URL.
  300. assert call.kwargs["follow_redirects"] is False
  301. @pytest.mark.asyncio
  302. async def test_happy_path_streams_bytes(self):
  303. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  304. resp = MagicMock()
  305. resp.status_code = 200
  306. async def _chunks():
  307. yield b"PK\x03\x04" # 3MF = zip magic
  308. yield b"rest of file"
  309. resp.aiter_bytes = lambda: _chunks()
  310. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  311. payload, filename = await svc.download_3mf(
  312. "https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k"
  313. )
  314. assert payload.startswith(b"PK\x03\x04")
  315. assert filename == "foo.3mf"
  316. @pytest.mark.asyncio
  317. async def test_http_error_on_cdn_path_raises_unavailable(self):
  318. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  319. resp = MagicMock()
  320. resp.status_code = 500
  321. resp.aiter_bytes = lambda: (_ for _ in ())
  322. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  323. with pytest.raises(MakerWorldUnavailableError):
  324. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  325. @pytest.mark.asyncio
  326. async def test_exceeds_size_cap_raises(self):
  327. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  328. resp = MagicMock()
  329. resp.status_code = 200
  330. # Cap is 200 MB — emit one "chunk" that reports exceeding it.
  331. oversized = _MAX_3MF_BYTES + 1
  332. async def _chunks():
  333. # Emit a bytes object whose ``len()`` is oversized, without
  334. # actually allocating 200 MB in the test process.
  335. yield b"\x00" * oversized
  336. resp.aiter_bytes = lambda: _chunks()
  337. svc._client.stream = MagicMock(return_value=self._stream_ctx(resp))
  338. with pytest.raises(MakerWorldUnavailableError, match="cap"):
  339. await svc.download_3mf("https://makerworld.bblmw.com/makerworld/model/X/Y/foo.3mf?exp=1&key=k")
  340. class TestS3UrllibDownload:
  341. """Module-level ``_download_s3_urllib`` — the verbatim-URL path for S3."""
  342. @pytest.mark.asyncio
  343. async def test_returns_bytes_and_filename(self):
  344. from backend.app.services.makerworld import _download_s3_urllib
  345. fake_resp = MagicMock()
  346. fake_resp.status = 200
  347. # Simulate urllib's file-like ``read(n)`` interface.
  348. fake_resp.read = MagicMock(side_effect=[b"hello", b""])
  349. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  350. fake_resp.__exit__ = MagicMock(return_value=None)
  351. fake_opener = MagicMock()
  352. fake_opener.open = MagicMock(return_value=fake_resp)
  353. with patch("urllib.request.build_opener", return_value=fake_opener):
  354. data, filename = await _download_s3_urllib(
  355. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  356. "fallback.3mf",
  357. )
  358. assert data == b"hello"
  359. assert filename == "fallback.3mf"
  360. @pytest.mark.asyncio
  361. async def test_redirect_is_treated_as_error(self):
  362. """The ``_NoRedirect`` handler returns ``None`` from ``redirect_request``,
  363. which makes ``urllib`` raise ``HTTPError`` instead of following. The
  364. wrapper must surface that as ``MakerWorldUnavailableError``."""
  365. from backend.app.services.makerworld import _download_s3_urllib
  366. fake_opener = MagicMock()
  367. fake_opener.open = MagicMock(
  368. side_effect=HTTPError(
  369. "https://s3.example/redirect",
  370. 302,
  371. "Found",
  372. {}, # type: ignore[arg-type]
  373. None,
  374. )
  375. )
  376. with (
  377. patch("urllib.request.build_opener", return_value=fake_opener),
  378. pytest.raises(MakerWorldUnavailableError),
  379. ):
  380. await _download_s3_urllib(
  381. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  382. "fallback.3mf",
  383. )
  384. @pytest.mark.asyncio
  385. async def test_non_200_raises_unavailable(self):
  386. from backend.app.services.makerworld import _download_s3_urllib
  387. fake_resp = MagicMock()
  388. fake_resp.status = 403
  389. fake_resp.read = MagicMock(return_value=b"")
  390. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  391. fake_resp.__exit__ = MagicMock(return_value=None)
  392. fake_opener = MagicMock()
  393. fake_opener.open = MagicMock(return_value=fake_resp)
  394. with (
  395. patch("urllib.request.build_opener", return_value=fake_opener),
  396. pytest.raises(MakerWorldUnavailableError),
  397. ):
  398. await _download_s3_urllib(
  399. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  400. "fallback.3mf",
  401. )
  402. @pytest.mark.asyncio
  403. async def test_size_cap_enforced(self):
  404. from backend.app.services.makerworld import _download_s3_urllib
  405. fake_resp = MagicMock()
  406. fake_resp.status = 200
  407. # A single oversized chunk trips the cap on the first iteration.
  408. fake_resp.read = MagicMock(side_effect=[b"\x00" * (_MAX_3MF_BYTES + 1), b""])
  409. fake_resp.__enter__ = MagicMock(return_value=fake_resp)
  410. fake_resp.__exit__ = MagicMock(return_value=None)
  411. fake_opener = MagicMock()
  412. fake_opener.open = MagicMock(return_value=fake_resp)
  413. with (
  414. patch("urllib.request.build_opener", return_value=fake_opener),
  415. pytest.raises(MakerWorldUnavailableError, match="cap"),
  416. ):
  417. await _download_s3_urllib(
  418. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  419. "fallback.3mf",
  420. )
  421. @pytest.mark.asyncio
  422. async def test_network_error_mapped_to_unavailable(self):
  423. from backend.app.services.makerworld import _download_s3_urllib
  424. fake_opener = MagicMock()
  425. fake_opener.open = MagicMock(side_effect=URLError("dns fail"))
  426. with (
  427. patch("urllib.request.build_opener", return_value=fake_opener),
  428. pytest.raises(MakerWorldUnavailableError),
  429. ):
  430. await _download_s3_urllib(
  431. "https://s3.us-west-2.amazonaws.com/b/k/file.3mf?sig=abc",
  432. "fallback.3mf",
  433. )
  434. class TestFetchThumbnail:
  435. """Proxy the CDN thumbnails so img-src CSP doesn't need to allow external hosts."""
  436. @pytest.fixture
  437. def service(self):
  438. svc = MakerWorldService(client=MagicMock(spec=httpx.AsyncClient))
  439. svc._client.get = AsyncMock()
  440. return svc
  441. @pytest.mark.asyncio
  442. async def test_rejects_non_cdn_host(self, service):
  443. with pytest.raises(MakerWorldUrlError):
  444. await service.fetch_thumbnail("https://evil.example.com/img.jpg")
  445. @pytest.mark.asyncio
  446. async def test_rejects_loopback(self, service):
  447. # SSRF: don't let anyone abuse this as an open proxy toward 127.0.0.1
  448. with pytest.raises(MakerWorldUrlError):
  449. await service.fetch_thumbnail("http://127.0.0.1/secret.jpg")
  450. @pytest.mark.asyncio
  451. async def test_does_not_follow_redirects(self, service):
  452. """Host allowlist is only enforced on the initial URL — a 302 from the
  453. CDN to any other host would otherwise bypass the allowlist. ``follow_
  454. redirects=False`` pins that behaviour in the wire contract."""
  455. resp = MagicMock()
  456. resp.status_code = 200
  457. resp.headers = {"content-type": "image/jpeg"}
  458. resp.content = b"\xff\xd8\xff\xe0JFIF"
  459. service._client.get.return_value = resp
  460. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/cover.jpg")
  461. assert service._client.get.call_args.kwargs["follow_redirects"] is False
  462. @pytest.mark.asyncio
  463. async def test_rejects_html_content_type_even_with_image_extension(self, service):
  464. # An upstream error page (HTML) at a .jpg URL must be refused —
  465. # otherwise we'd forward it to the browser under an image framing.
  466. resp = MagicMock()
  467. resp.status_code = 200
  468. resp.headers = {"content-type": "text/html"}
  469. resp.content = b"<html>error page</html>"
  470. service._client.get.return_value = resp
  471. with pytest.raises(MakerWorldUnavailableError):
  472. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/cover.jpg")
  473. @pytest.mark.asyncio
  474. async def test_happy_path_with_proper_image_content_type(self, service):
  475. resp = MagicMock()
  476. resp.status_code = 200
  477. resp.headers = {"content-type": "image/jpeg; charset=binary"}
  478. resp.content = b"\xff\xd8\xff\xe0JFIF" # JPEG magic bytes
  479. service._client.get.return_value = resp
  480. payload, content_type = await service.fetch_thumbnail(
  481. "https://makerworld.bblmw.com/makerworld/model/X/cover.jpg"
  482. )
  483. assert payload == b"\xff\xd8\xff\xe0JFIF"
  484. # Semi-colon params stripped
  485. assert content_type == "image/jpeg"
  486. @pytest.mark.asyncio
  487. async def test_infers_mime_from_extension_when_cdn_lies(self, service):
  488. """MakerWorld's CDN returns application/octet-stream for real PNG/JPG
  489. files. Relying on upstream content-type alone would fail every
  490. thumbnail request; fall back to the URL extension."""
  491. resp = MagicMock()
  492. resp.status_code = 200
  493. resp.headers = {"content-type": "application/octet-stream"}
  494. resp.content = b"\x89PNG\r\n\x1a\n" # PNG magic bytes
  495. service._client.get.return_value = resp
  496. payload, content_type = await service.fetch_thumbnail(
  497. "https://makerworld.bblmw.com/makerworld/model/X/design/abc.png"
  498. )
  499. assert payload.startswith(b"\x89PNG")
  500. assert content_type == "image/png"
  501. @pytest.mark.asyncio
  502. async def test_refuses_when_no_extension_and_non_image_type(self, service):
  503. """If the URL carries no image extension AND upstream doesn't declare
  504. image/*, we can't confidently serve it as an image — refuse."""
  505. resp = MagicMock()
  506. resp.status_code = 200
  507. resp.headers = {"content-type": "application/octet-stream"}
  508. resp.content = b"who knows what this is"
  509. service._client.get.return_value = resp
  510. with pytest.raises(MakerWorldUnavailableError):
  511. await service.fetch_thumbnail("https://makerworld.bblmw.com/makerworld/model/X/blob")