Files
bambuddy/backend/tests/unit/test_makerworld_routes.py
T
MartinNYHC 5da403ba0c Feature/makerworld (#1099)
* feat(makerworld): URL-paste import and print for MakerWorld models

  Add a dedicated /makerworld sidebar page where users paste a MakerWorld
  model URL and get the full plate list + one-click "Import to Library" or
  "Print Now". Closes the workflow gap that kept LAN-only users on the
  Bambu Handy app solely for MakerWorld download-and-send.

  The authenticated tier reuses the existing Bambu Cloud token that
  Bambuddy already stores for firmware checks and slicer settings --
  MakerWorld shares the same auth backend, so the same JWT works there.
  No separate OAuth flow, no companion browser extension, no credential
  hijack. Anonymous users can still paste a URL and see model metadata;
  the 3MF download itself requires the Cloud login.

  Print Now hands off to the existing PrintModal (plate picker + AMS
  mapping + dispatch) so multi-filament models work via the same code
  path as library-file prints. Imported 3MFs are stored through a new
  shared save_3mf_bytes_to_library() helper so the multipart upload
  route and the MakerWorld import route don't duplicate 3MF parsing +
  thumbnail extraction logic.

  LibraryFile gains indexed source_type + source_url columns. Re-pasting
  a URL for a model already in the library returns the existing row
  instead of re-downloading -- dedupe is by canonicalised URL, not SHA256,
  because MakerWorld's download URLs are signed and change per request.

  Thumbnail proxy (/makerworld/thumbnail) hot-links through the backend
  instead of directly to makerworld.bblmw.com -- the SPA's img-src CSP
  stays strict and users' IPs don't hit MakerWorld's CDN logs. The
  endpoint is intentionally unauthenticated since <img> tags can't carry
  a Bearer token; SSRF-guarded by a CDN host allowlist so it can't be
  used as a generic proxy.

  Search and browse-catalogue are explicitly out of scope. The public
  design/search endpoint returns empty results from server-originated
  requests (likely needs csrf/session state reproducible only from a
  real browser), and the __NEXT_DATA__ HTML fallback is blocked by
  Cloudflare. URL-paste covers the realistic discovery pattern (Reddit /
  YouTube / shared links).

  Headers match kloshi-io/makerworld-api-reverse's production-tested set
  (User-Agent: 3d-printing-service/1.0, x-bbl-* client identifiers,
  Referer). The /instance/{id}/f3mf call includes ?type=download which
  community userscripts use to signal legitimate download intent. 418
  responses (MakerWorld's CAPTCHA gate) retry once with backoff and then
  surface a clear actionable error with an "Open on MakerWorld" fallback
  link; we never try to evade bot detection.

  Permissions: new makerworld:view (browse metadata, view thumbnails) and
  makerworld:import (save 3MFs to library). Administrators and Operators
  get both; Viewers get view-only. Migration grants these to existing
  groups based on whether they already have library:upload / library:read.

  Disclaimer in the UI and wiki page mirrors kloshi's framing: not
  affiliated with or endorsed by MakerWorld or Bambu Lab, interoperability
  only, not intended to circumvent access controls.

  Tests: 30 backend (service + routes) + 4 frontend. Full backend suite
  (1931 tests) clean. Frontend build clean.

* feat(makerworld): ship working URL-paste import via api.bambulab.com iot-service

  The MakerWorld integration shipped in 0.2.4b1 dev was broken for most
  public models: the makerworld.com/design-service path returns "Please
  log in to download models" even with a valid Bambu Cloud bearer,
  because it's cookie-gated behind Cloudflare. Published reverse-
  engineering projects work around this by pasting browser cookies; we
  route around it entirely by using the api.bambulab.com/iot-service
  endpoint (documented by Pr0zak/YASTL#51), which accepts the same
  bearer Bambuddy already has and returns a presigned S3 URL.

  Working flow:
    GET api.bambulab.com/v1/design-service/design/{id}  → metadata
    GET api.bambulab.com/v1/iot-service/api/user/profile/{pid}?model_id=<str>
         Authorization: Bearer {cloud_token}             → signed S3 URL
    urllib.request (no redirects, no query re-encoding)  → bytes

  Notes on each step:
    - The model_id query param is the alphanumeric string from the
      design response (e.g. US2bb73b106683e5), NOT the integer designId
      from the /models/{N} URL. The import route fetches design metadata
      first to get it.
    - S3 presigned URLs MUST be fetched with urllib (not httpx/curl_cffi)
      because the signature is computed over exact query-string bytes;
      any normalising encoder breaks it with SignatureDoesNotMatch 400s
      (YASTL#52 hit the same issue). Wrapped in a no-redirect opener so
      the .amazonaws.com host allowlist guarantee isn't bypassed by a
      302 elsewhere.
    - The canonical source_url now includes profile_id so different
      plates of the same model get distinct library entries. Older rows
      from dev builds keep the model-level URL; the resolve endpoint's
      "already imported" check LIKEs both shapes.

  UI rebuild:
    - Per-plate Save + Save & Slice in Bambu Studio / OrcaSlicer (the
      plate is unsliced source, so "Print Now" was misleading and is
      replaced by an explicit slicer hand-off).
    - Import all plates with sequential progress.
    - Folder picker (default: auto-created top-level "MakerWorld"
      folder, created on first import, folder tree invalidated so
      File Manager shows it immediately).
    - Image gallery per plate with keyboard-navigable lightbox.
    - Recent imports sidebar (sticky on lg+, vertical list with
      jump-to-library / slicer / open-on-makerworld icons).
    - Inline follow-up actions on imported plate rows so the user
      doesn't scroll back to a top-of-page card.
    - Per-plate delete via the standard ConfirmModal (no window.confirm).
    - Elapsed-time + phase label during import so the 10-30s synchronous
      POST doesn't feel frozen.
    - URL-change detection drops the preview when the pasted URL
      diverges from the resolved one.

  Security hardening (found in review):
    - DOMPurify.sanitize on the MakerWorld HTML summary before
      dangerouslySetInnerHTML (user-authored content).
    - <img> tags in that HTML routed through the thumbnail proxy so
      the SPA's img-src 'self' data: blob: CSP isn't widened.
    - /makerworld/thumbnail uses follow_redirects=False (the host
      allowlist only covers the initial URL).
    - 3MF CDN fetch strips the bearer (signed URL is the credential).
    - S3 fetch uses a no-op HTTPRedirectHandler for the same reason.
    - Upstream filename is os.path.basename'd before persisting.

  Tests: 46 backend service unit tests, 19 route tests, 12 frontend
  tests — all passing. All user-facing strings localised across the
  8 UI languages.

* - frontend/src/App.tsx — removed the 3 stale <AdminRoute> lines (kept the 3 <PermissionRoute> equivalents). TSC + Vite both clean.
  - backend/tests/integration/test_auth_api.py — added # pragma: allowlist secret + # noqa: S106 on the test fixture line that GitGuardian flagged.
2026-04-23 14:10:14 +02:00

447 lines
18 KiB
Python

"""Tests for the /makerworld/* route handlers.
Mocks ``MakerWorldService`` so tests don't hit the real MakerWorld API. We
still cover: URL validation, metadata passthrough, already-imported detection,
source-URL-based dedupe on import, auto-creation of the MakerWorld default
folder, canonical URL shape, filename basenaming, and the ``/recent-imports``
listing endpoint.
"""
from __future__ import annotations
from datetime import datetime, timedelta
from unittest.mock import AsyncMock, patch
import pytest
from backend.app.api.routes.makerworld import _canonical_url
from backend.app.models.library import LibraryFile, LibraryFolder
def _fake_service(**stubs):
"""Build an AsyncMock MakerWorldService with the given async method stubs."""
svc = AsyncMock()
svc.close = AsyncMock()
for name, value in stubs.items():
if callable(value) and not isinstance(value, AsyncMock):
setattr(svc, name, AsyncMock(side_effect=value))
else:
setattr(svc, name, AsyncMock(return_value=value))
return svc
def _default_design(alphanumeric: str = "US2bb73b106683e5", model_id: int = 1400373):
"""Shape the backend needs from ``/design/{id}``: the alphanumeric
``modelId`` field that iot-service requires, plus at least one instance
so the importer has a ``profile_id`` to fall back on."""
return {
"id": model_id,
"modelId": alphanumeric,
"title": "Seed Starter",
"instances": [{"profileId": 298919107, "title": "9 cells"}],
}
def _default_manifest(name: str = "benchy.3mf"):
return {
"name": name,
"url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
}
class TestCanonicalUrl:
"""Unit test the dedupe-key builder directly — regressions break dedupe
silently so it's worth pinning the exact shape."""
def test_without_profile_id(self):
assert _canonical_url(1400373) == "https://makerworld.com/models/1400373"
def test_without_profile_id_when_none(self):
assert _canonical_url(1400373, None) == "https://makerworld.com/models/1400373"
def test_with_profile_id(self):
assert _canonical_url(1400373, 298919107) == ("https://makerworld.com/models/1400373#profileId-298919107")
class TestStatus:
@pytest.mark.asyncio
async def test_status_reports_no_token_by_default(self, async_client, db_session):
resp = await async_client.get("/api/v1/makerworld/status")
assert resp.status_code == 200
body = resp.json()
# Fresh in-memory DB has no stored token, so can_download must be false
assert body == {"has_cloud_token": False, "can_download": False}
class TestResolve:
@pytest.mark.asyncio
async def test_rejects_non_makerworld_url(self, async_client):
resp = await async_client.post(
"/api/v1/makerworld/resolve",
json={"url": "https://thingiverse.com/thing/1"},
)
assert resp.status_code == 400
assert "makerworld" in resp.json()["detail"].lower()
@pytest.mark.asyncio
async def test_happy_path_returns_design_and_instances(self, async_client):
design_payload = {"id": 1400373, "title": "Seed Starter"}
instances_payload = {
"total": 2,
"hits": [
{"id": 1452154, "profileId": 298919107, "title": "9 cells"},
{"id": 1452158, "profileId": 298919564, "title": "12 cells"},
],
}
svc = _fake_service(get_design=design_payload, get_design_instances=instances_payload)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/resolve",
json={"url": "https://makerworld.com/en/models/1400373-slug#profileId-1452154"},
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["model_id"] == 1400373
assert body["profile_id"] == 1452154
assert body["design"] == design_payload
assert len(body["instances"]) == 2
assert body["already_imported_library_ids"] == []
@pytest.mark.asyncio
async def test_flags_already_imported_library_ids(self, async_client, db_session):
# Seed a matching LibraryFile so resolve() reports it back
existing = LibraryFile(
filename="prev.3mf",
file_path="library/files/prev.3mf",
file_type="3mf",
file_size=100,
source_type="makerworld",
source_url="https://makerworld.com/models/1400373",
)
db_session.add(existing)
await db_session.commit()
await db_session.refresh(existing)
svc = _fake_service(
get_design={"id": 1400373},
get_design_instances={"total": 0, "hits": []},
)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/resolve",
json={"url": "https://makerworld.com/en/models/1400373"},
)
assert resp.status_code == 200, resp.text
assert resp.json()["already_imported_library_ids"] == [existing.id]
class TestImport:
"""End-to-end of POST /makerworld/import — mocks the service but exercises
real DB writes, real ``save_3mf_bytes_to_library``, real folder auto-creation."""
_FAKE_3MF_BYTES = b"PK\x03\x04not-a-real-3mf"
@pytest.mark.asyncio
async def test_returns_existing_on_source_url_match(self, async_client, db_session):
"""Re-importing a model we already have must NOT re-download.
Dedupe key is ``{model_id}#profileId-{profile_id}`` — matches the
canonical URL the route constructs, not the legacy model-only shape.
"""
existing = LibraryFile(
filename="already-here.3mf",
file_path="library/files/already.3mf",
file_type="3mf",
file_size=500,
source_type="makerworld",
source_url="https://makerworld.com/models/1400373#profileId-298919107",
)
db_session.add(existing)
await db_session.commit()
await db_session.refresh(existing)
svc = _fake_service(
get_design=_default_design(),
get_profile_download=_default_manifest(),
)
svc.download_3mf = AsyncMock() # must remain uncalled
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/import",
json={"model_id": 1400373, "profile_id": 298919107},
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["library_file_id"] == existing.id
assert body["was_existing"] is True
assert body["profile_id"] == 298919107
svc.download_3mf.assert_not_called()
@pytest.mark.asyncio
async def test_autocreates_makerworld_folder_when_folder_id_none(self, async_client, db_session):
"""Default destination — a top-level "MakerWorld" folder — is created
on first import so users don't have to set it up."""
svc = _fake_service(
get_design=_default_design(),
get_profile_download=_default_manifest(),
download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/import",
json={"model_id": 1400373, "profile_id": 298919107, "folder_id": None},
)
assert resp.status_code == 200, resp.text
# The new folder should exist, at the root.
from sqlalchemy import select
result = await db_session.execute(
select(LibraryFolder).where(LibraryFolder.name == "MakerWorld", LibraryFolder.parent_id.is_(None))
)
folder = result.scalar_one()
assert resp.json()["folder_id"] == folder.id
@pytest.mark.asyncio
async def test_uses_existing_folder_when_folder_id_provided(self, async_client, db_session):
"""Caller-supplied ``folder_id`` must be honoured even if the default
``MakerWorld`` folder also exists — no silent hijacking."""
folder = LibraryFolder(name="MyCustomFolder", parent_id=None)
db_session.add(folder)
await db_session.commit()
await db_session.refresh(folder)
svc = _fake_service(
get_design=_default_design(),
get_profile_download=_default_manifest(),
download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/import",
json={"model_id": 1400373, "profile_id": 298919107, "folder_id": folder.id},
)
assert resp.status_code == 200, resp.text
assert resp.json()["folder_id"] == folder.id
@pytest.mark.asyncio
async def test_canonical_source_url_includes_profile_id(self, async_client, db_session):
"""The saved row's ``source_url`` must include ``#profileId-`` so two
plates of the same model become two library rows (dedupe is per-plate)."""
svc = _fake_service(
get_design=_default_design(),
get_profile_download=_default_manifest(),
download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/import",
json={"model_id": 1400373, "profile_id": 298919107},
)
assert resp.status_code == 200, resp.text
from sqlalchemy import select
row = (
await db_session.execute(select(LibraryFile).where(LibraryFile.id == resp.json()["library_file_id"]))
).scalar_one()
assert row.source_url == "https://makerworld.com/models/1400373#profileId-298919107"
@pytest.mark.asyncio
async def test_filename_from_upstream_is_basenamed(self, async_client, db_session):
"""Defence-in-depth: a malicious ``name`` from the upstream manifest
(e.g. ``"../../evil.3mf"``) must not persist path components into the
library row. On-disk storage uses a UUID already, this is belt-and-
braces protection for the human-readable field."""
svc = _fake_service(
get_design=_default_design(),
get_profile_download={
"name": "../../evil.3mf",
"url": "https://makerworld.bblmw.com/makerworld/model/X/Y/f.3mf?exp=1&key=k",
},
download_3mf=(self._FAKE_3MF_BYTES, "fallback.3mf"),
)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/import",
json={"model_id": 1400373, "profile_id": 298919107},
)
assert resp.status_code == 200, resp.text
assert resp.json()["filename"] == "evil.3mf"
@pytest.mark.asyncio
async def test_response_includes_profile_id(self, async_client, db_session):
"""UI matches imports back to the plate row via ``profile_id`` — the
response field must always be populated, even when the caller provided
it explicitly (rather than the backend falling back to design defaults)."""
svc = _fake_service(
get_design=_default_design(),
get_profile_download=_default_manifest(),
download_3mf=(self._FAKE_3MF_BYTES, "benchy.3mf"),
)
with patch("backend.app.api.routes.makerworld._build_service", AsyncMock(return_value=svc)):
resp = await async_client.post(
"/api/v1/makerworld/import",
json={"model_id": 1400373, "profile_id": 298919107},
)
assert resp.status_code == 200, resp.text
assert resp.json()["profile_id"] == 298919107
class TestRecentImports:
"""GET /makerworld/recent-imports — sidebar feed on the MakerWorld page."""
@pytest.mark.asyncio
async def test_empty_when_no_makerworld_imports(self, async_client):
resp = await async_client.get("/api/v1/makerworld/recent-imports")
assert resp.status_code == 200
assert resp.json() == []
@pytest.mark.asyncio
async def test_returns_items_newest_first(self, async_client, db_session):
# Seed three rows with explicit, decreasing created_at timestamps so
# ordering doesn't depend on auto-increment PK ordering.
base = datetime(2025, 1, 1, 12, 0, 0)
older = LibraryFile(
filename="older.3mf",
file_path="library/older.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url="https://makerworld.com/models/1",
created_at=base,
)
middle = LibraryFile(
filename="middle.3mf",
file_path="library/middle.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url="https://makerworld.com/models/2",
created_at=base + timedelta(hours=1),
)
newer = LibraryFile(
filename="newer.3mf",
file_path="library/newer.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url="https://makerworld.com/models/3",
created_at=base + timedelta(hours=2),
)
# Unrelated non-MakerWorld file must NOT show up.
other = LibraryFile(
filename="manual.3mf",
file_path="library/manual.3mf",
file_type="3mf",
file_size=10,
source_type=None,
source_url=None,
created_at=base + timedelta(hours=3),
)
db_session.add_all([older, middle, newer, other])
await db_session.commit()
resp = await async_client.get("/api/v1/makerworld/recent-imports")
assert resp.status_code == 200, resp.text
body = resp.json()
names = [row["filename"] for row in body]
assert names == ["newer.3mf", "middle.3mf", "older.3mf"]
@pytest.mark.asyncio
async def test_response_matches_pydantic_shape(self, async_client, db_session):
"""Lock the exact key set so the frontend's typed ``MakerworldRecentImport``
doesn't silently fall out of sync with the backend schema."""
row = LibraryFile(
filename="x.3mf",
file_path="library/x.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url="https://makerworld.com/models/1#profileId-2",
)
db_session.add(row)
await db_session.commit()
resp = await async_client.get("/api/v1/makerworld/recent-imports")
assert resp.status_code == 200, resp.text
item = resp.json()[0]
assert set(item.keys()) == {
"library_file_id",
"filename",
"folder_id",
"thumbnail_path",
"source_url",
"created_at",
}
assert item["source_url"] == "https://makerworld.com/models/1#profileId-2"
@pytest.mark.asyncio
async def test_limit_is_honoured(self, async_client, db_session):
for i in range(5):
db_session.add(
LibraryFile(
filename=f"f{i}.3mf",
file_path=f"library/f{i}.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url=f"https://makerworld.com/models/{i}",
)
)
await db_session.commit()
resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=2")
assert resp.status_code == 200
assert len(resp.json()) == 2
@pytest.mark.asyncio
async def test_limit_clamped_to_minimum(self, async_client, db_session):
"""``limit=0`` or negative must clamp to 1 — a zero limit would be
silently swallowed by SQL and return nothing, which is surprising."""
db_session.add(
LibraryFile(
filename="one.3mf",
file_path="library/one.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url="https://makerworld.com/models/1",
)
)
await db_session.commit()
resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=0")
assert resp.status_code == 200
assert len(resp.json()) == 1
@pytest.mark.asyncio
async def test_limit_clamped_to_maximum(self, async_client, db_session):
"""``limit`` is clamped to 50 so a pathological client can't request
the whole table. We seed 60 rows and assert the response is capped."""
for i in range(60):
db_session.add(
LibraryFile(
filename=f"f{i}.3mf",
file_path=f"library/f{i}.3mf",
file_type="3mf",
file_size=10,
source_type="makerworld",
source_url=f"https://makerworld.com/models/{i}",
)
)
await db_session.commit()
resp = await async_client.get("/api/v1/makerworld/recent-imports?limit=9999")
assert resp.status_code == 200
assert len(resp.json()) == 50