agnes-the-ai-analyst/tests/test_marketplace_api.py
minasarustamyan dc5e0e0d11
Marketplace UX overhaul: rich plugin/skill/agent detail + filename rename (#251)
* Rename agnes-metadata.json to marketplace-metadata.json

Curated marketplace enrichment file (.claude-plugin/agnes-metadata.json)
becomes marketplace-metadata.json. Clean cut, no fallback — curators of
upstream marketplace repos must rename the file on their side.

Python API renames mirror the file rename: read_agnes_metadata →
read_marketplace_metadata, AGNES_METADATA_REL → MARKETPLACE_METADATA_REL,
AGNES_METADATA_MAX_BYTES → MARKETPLACE_METADATA_MAX_BYTES. Synth Claude
Code marketplace strip rule (.agnes/** + the metadata file) follows the
new filename.

* Marketplace detail polish: window cover + 715:310 aspect + helper alignment

- Plugin & item (skill/agent) detail hero: 160x160 square cover replaced
  with a macOS-style window frame (3 traffic-light dots + titlebar label
  showing the entity name). Body is constrained to 715:310 so curator-
  uploaded covers no longer crop to a square. Window is 380px wide; meta
  column and absolutely-positioned top-right install/remove actions stay
  put. Fallback when no cover_photo_url (translucent gradient + PL/SK/AG
  initials) is unchanged, just inside the window body.

- Inner skill/agent cards in the plugin detail's Internal structure
  section adopt the same 715:310 aspect (was fixed 78px tall). No window
  chrome on inner cards — just the matching proportions so covers read
  consistently across hero, grid tiles, and listing cards.

- Curated nested item helper text ("This skill is part of ... — add the
  bundle to your stack to use it") now stacks UNDER the "Open parent
  plugin" button instead of being a side-by-side flex sibling in the
  actions-row. Added align-self: flex-end so the 260px helper box
  anchors at the right edge of the 300px actions column, matching the
  button's right edge.

* Marketplace My tab: surface the same category + type filters as Flea

- Frontend: mp-cat-row and mp-type-row now show on tab=my (previously
  hidden — type was flea-only, category was flea/curated-only). Curated
  browse stays plugin-only and continues to hide the type pills.
  fetchOne() sends the `type` param for tab=my too, so the items
  endpoint's existing my-branch filter actually receives it.

- Backend categories endpoint, tab=my branch: when the type filter is
  set to skill/agent, skip counting curated subscriptions. Curated
  plugins are always type='plugin', so they wouldn't survive the items
  endpoint's type filter; including them in the category counts made
  the pill numbers overstate what users could actually see in the
  grid. type=None or type='plugin' keeps the previous behaviour.

- CHANGELOG entry under [Unreleased].

* Marketplace plugin detail: render rich content from marketplace-metadata.json

Adds five optional plugin-level fields to marketplace-metadata.json and
renders them on the curated plugin detail page + listing card:

* display_name — friendly h1 / listing-card name / mac-window titlebar
  label (overrides the technical plugin id)
* tagline — punchy 1-line value prop for the hero subtitle and the
  listing card description (replacing the verbose marketplace.json
  description on cards)
* description — multi-paragraph markdown body, server-side rendered
  through markdown-it-py and sanitized through nh3 with a
  description-scoped allowlist (no iframes / no raw HTML / no
  javascript: links). Powers the "What it does" panel.
* use_cases[] — {title, description, prompt} entries that render as a
  3-column "When to use it" card grid; each card shows the literal
  prompt as a code chip so users can copy-paste into Claude Code.
* sample_interaction — {user, assistant} dialog rendered in a Claude
  Code-style dark Catppuccin Mocha transcript panel: monospace user
  row with a green ">" prompt indicator + sans-serif assistant body
  with markdown formatting (peach bold, yellow italic, pink inline
  code, mantle-dark fenced code blocks).

All five fields are optional; UI sections only render when populated,
so plugins without enrichment look identical to before. Fields are
read on-demand from the working tree (cached by mtime per marketplace
slug) so curator edits land at the next request without waiting for
a sync cycle — same pattern as the existing inner-skill/agent
enrichment path. No DB schema bump.

Skill / agent rich-content rendering is deferred to a later phase
(needs a source-of-truth decision: extend plugin.yml? LLM-generate
from SKILL.md / agent.md?). The schema accepts the same fields at
skill/agent level today for forward compatibility but the UI ignores
them for now.

Also: stripped a stale `background-color: var(--bg)` from the global
`code` rule in style.css (was making inline code visually disappear
on the page background).

* Skill / agent detail: render rich content from marketplace-metadata.json

Brings the skill/agent detail pages to parity with the plugin detail
page. Same rich-content schema (display_name, tagline, description as
markdown, use_cases[], sample_interaction) plus two per-item additions:

* invocation — curator-provided literal command string. When set,
  overrides the computed "<manifest_name>:<inner_name>" chip and
  cleanly supports both "/" skill prefix and "@" agent prefix (the
  hardcoded "/" in the chip markup is hidden when the curator provides
  the invocation, so /grpn-eng:query <q> and @grpn-eng:cto-architect
  both render correctly).
* when_to_use — markdown disambiguation block ("Use this for X. For
  similar Y, see /other-skill") rendered into a new "When to use this"
  panel below the Example section.

Skill / agent category is now per-item overridable in
marketplace-metadata.json. When absent, the API keeps the parent
plugin's category as the badge so existing items don't lose their
category until curators opt in to per-item categorization.

The new "Example" Q&A panel uses the same Claude Code-style dark
Catppuccin Mocha transcript treatment as the plugin detail —
monospace user row with a green ">" prompt indicator + sans-serif
assistant body with markdown formatting.

All new fields are optional and read on-demand from the working tree.
Skills / agents whose marketplace-metadata.json doesn't carry rich
content render exactly the same way they did before (frontmatter
description + computed slash command + cover from existing v32
enrichment). No DB schema bump.

* Fix TypeError in skill / agent detail when curator sets per-item category

`curated_skill_detail` and `curated_agent_detail` were passing both
`**parent` (from `_curated_inner_parent_fields`, which returns the
parent plugin's category as a fallback) and `**enrichment` (from
`_curated_inner_enrichment`, which returns the per-item category
override when the curator set one) into `InnerDetailResponse(...)`.

Python function-call kwargs unpacking with overlapping keys raises
`TypeError: got multiple values for keyword argument 'category'`
— it doesn't merge like a literal dict does. The bug only surfaced
when the marketplace-metadata.json carried a `category` field at
skill / agent level (curator opting into per-item categorization);
items without that override hit the endpoint cleanly because only
parent provided the key.

Fix: build `merged = {**parent, **enrichment}` first (literal-dict
syntax DOES merge, with the right-hand-side winning) and unpack the
merged dict. Curator override still wins via the merge order, and
the same pattern is future-proof for any other field that lands in
both layers later.

Plus a regression test in test_marketplace_metadata.py asserting
that the inner-resolver carries `category` for downstream merging.

* Marketplace detail: tolerate partial curator JSON

Server constructed UseCase / SampleInteraction via raw dict indexing
(uc["title"], sample["assistant"]), so a curator commit missing any
required Pydantic field crashed the whole plugin / skill / agent detail
endpoint with a 500. Route both constructions through _safe_use_case /
_safe_sample_interaction helpers — partial input silently drops the
malformed card / section instead of breaking the page.

Regression test in test_marketplace_api.py covers the three shapes:
use_case missing a key, use_case with an empty string, and
sample_interaction with only user (no assistant). Sibling rich fields
still render.

* Address PR-251 review (must-fixes + S2/S3 polish) + release-cut 0.50.0

Five must-fixes from the review pass (3 from @cvrysanek's two-stage
review, 2 from my independent pass), plus the 0.50.0 release-cut as the
last commit on this PR per CLAUDE.md (CLAUDE.md "Release-cut belongs
to the PR" rule added in v0.49.1).

Must-fixes
----------

1. Cache eviction: bounded LRU instead of per-marketplace predicate.
   The previous predicate (`k[0] == marketplace_id and k[1] != mtime_ns`)
   only swept stale entries for the CURRENT marketplace; with N>100
   distinct marketplaces each holding one mtime key, the cap silently
   failed and memory grew linearly. Replaced with OrderedDict-backed
   bounded LRU at cap=256, drop oldest insert on overflow.
   Cache stress test pinned in test_marketplace_metadata.py.

2. Render CPU cap: per-field byte cap on description / when_to_use /
   sample_interaction.assistant via MARKETPLACE_METADATA_FIELD_MAX_BYTES
   (= 64 KiB). Without this, a 1 MiB curator markdown body × QPS =
   curator-controlled CPU burn through pure-Python markdown-it-py.
   Truncation respects UTF-8 boundaries and logs a warning so the
   curator sees the cap fire on the next sync. Test for cap +
   UTF-8-boundary preservation.

3. Inner-detail bypassed the metadata cache. _curated_inner_enrichment,
   _curated_inner_cover, and curated_detail all called
   read_marketplace_metadata directly, defeating the mtime cache the
   plugin listing already shared. Routed all three through
   _read_metadata_cached so skill/agent detail hits are O(1) re-parses
   per marketplace per mtime instead of O(QPS).

4. Truthy-vs-presence trap in plugin/inner enrichment merge. API-layer
   writers used `if resolved.get(k):` which silently dropped any
   future falsy-but-valid resolver field (bool featured=False, int
   priority=0, str category=''). Switched to presence check
   (`if k in resolved`) so the resolver is the authority on field
   presence; `{**parent, **enrichment}` merge respects whatever the
   resolver decided to ship.

5. Vendor-agnostic OSS cleanup. Removed operator-specific token
   references (/grpn-eng:, @grpn-eng:, .foundryai/) from
   src/marketplace_metadata.py docstring, app/web/templates/
   marketplace_item_detail.html JS comment, docs/curated-marketplace-
   format.md, and tests/test_marketplace_metadata.py fixtures. Replaced
   with generic /my-plugin:tool / @my-agent:role / .example/ placeholders.

CHANGELOG
---------
- New "### Fixed (PR #251 follow-ups)" section documenting all 4
  code-side must-fixes
- New "### Internal" section noting the vendor cleanup + new tests
- BREAKING bullet for the file rename now covers operator-side
  migration: running instances see plugin enrichment disappear from
  the UI until upstream curator renames + nightly sync overwrites the
  working tree; POST /api/marketplaces/{id}/sync forces refresh sooner
- Stripped /grpn-eng: leaks from the existing skill/agent rich-content
  bullet

Tests
-----
128 targeted tests pass (test_marketplace_metadata, test_marketplace_api,
test_marketplace, test_markdown_render, test_marketplace_synth_strip,
test_marketplace_filter). New tests added:
- 6 XSS regression tests on render_safe (javascript:/data:/vbscript:
  schemes via autolink, reference link, and mixed-case + positive
  http/https/mailto + noopener noreferrer rel)
- 3 byte-cap tests (truncation + UTF-8 boundary + under-cap pass-through)
- 1 cache eviction stress test (>256 marketplaces -> bounded at cap)
- 1 truthy-vs-presence resolver-contract test

Release-cut
-----------
- pyproject.toml 0.49.1 -> 0.50.0 (minor; BREAKING file rename per
  pre-1.0 CHANGELOG note: "breaking changes called out under Changed
  or Removed with the BREAKING marker")
- CHANGELOG [Unreleased] -> [0.50.0] - 2026-05-12, new empty
  [Unreleased] on top.

---------

Co-authored-by: Minas Arustamyan <arustamyan.minas@gmail.com>
Co-authored-by: ZdenekSrotyr <zdenek.srotyr@keboola.com>
2026-05-12 08:38:39 +00:00

669 lines
28 KiB
Python

"""Integration tests for the unified /api/marketplace endpoints.
Covers the v28 Model B browse + install surface: per-tab listing,
categories, curated detail with RBAC guard, and subscribe/unsubscribe.
"""
from __future__ import annotations
import io
import json
import zipfile
from datetime import datetime, timezone
import pytest
from fastapi.testclient import TestClient
@pytest.fixture
def web_client(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path))
monkeypatch.setenv("TESTING", "1")
monkeypatch.setenv("JWT_SECRET_KEY", "test-secret-key-min-32-characters!!")
(tmp_path / "state").mkdir()
(tmp_path / "analytics").mkdir()
(tmp_path / "extracts").mkdir()
from src.db import close_system_db
close_system_db()
from app.main import create_app
app = create_app()
yield TestClient(app)
close_system_db()
def _create_user(client, email, password="UserPass1!"):
from argon2 import PasswordHasher
from src.db import get_system_db
from src.repositories.users import UserRepository
ph = PasswordHasher()
conn = get_system_db()
user_id = email.split("@")[0]
UserRepository(conn).create(
id=user_id, email=email, name=user_id, password_hash=ph.hash(password),
)
conn.close()
r = client.post("/auth/token", json={"email": email, "password": password})
assert r.status_code == 200, r.text
return user_id, {"access_token": r.json()["access_token"]}
def _seed_curated_grant(
*,
user_id: str,
marketplace: str,
plugin: str,
plugin_meta: dict | None = None,
group_name: str | None = None,
) -> tuple[str, str]:
from src.db import get_system_db
from src.repositories.user_groups import UserGroupsRepository
from src.repositories.user_group_members import UserGroupMembersRepository
from src.repositories.resource_grants import ResourceGrantsRepository
conn = get_system_db()
try:
existing = conn.execute(
"SELECT 1 FROM marketplace_registry WHERE id = ?", [marketplace],
).fetchone()
if not existing:
conn.execute(
"INSERT INTO marketplace_registry (id, name, url, registered_at) "
"VALUES (?, ?, ?, ?)",
[marketplace, marketplace.upper(),
f"https://example.test/{marketplace}.git",
datetime.now(timezone.utc)],
)
meta = {"name": plugin, "version": "1.0", "description": "desc"}
if plugin_meta:
meta.update(plugin_meta)
conn.execute(
"INSERT INTO marketplace_plugins "
"(marketplace_id, name, description, version, category, raw, updated_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
[
marketplace, plugin, meta.get("description"), meta.get("version"),
meta.get("category"), json.dumps(meta),
datetime.now(timezone.utc),
],
)
gname = group_name or f"G-{user_id}-{marketplace}"
gid = UserGroupsRepository(conn).create(name=gname)["id"]
UserGroupMembersRepository(conn).add_member(user_id, gid, source="admin")
grant_id = ResourceGrantsRepository(conn).create(
group_id=gid, resource_type="marketplace_plugin",
resource_id=f"{marketplace}/{plugin}",
)
return gid, grant_id
finally:
conn.close()
def _make_skill_zip(skill_name: str = "code-review") -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr(
f"{skill_name}/SKILL.md",
f"---\nname: {skill_name}\ndescription: A test skill.\n---\nbody",
)
return buf.getvalue()
# ---------------------------------------------------------------------------
# /api/marketplace/items
# ---------------------------------------------------------------------------
class TestListItems:
def test_curated_empty_for_user_without_grants(self, web_client):
_, cookies = _create_user(web_client, "alice@x.com")
r = web_client.get("/api/marketplace/items?tab=curated", cookies=cookies)
assert r.status_code == 200
data = r.json()
assert data["total"] == 0
assert data["items"] == []
def test_curated_lists_granted_plugins(self, web_client):
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
r = web_client.get("/api/marketplace/items?tab=curated", cookies=cookies)
assert r.status_code == 200
data = r.json()
assert data["total"] == 1
assert data["items"][0]["source"] == "curated"
assert data["items"][0]["name"] == "alpha"
assert data["items"][0]["installed"] is False
assert data["items"][0]["marketplace_slug"] == "mkt-x"
def test_flea_lists_uploads(self, web_client):
_, cookies = _create_user(web_client, "alice@x.com")
web_client.post(
"/api/store/entities",
files={"file": ("s.zip", _make_skill_zip("alpha"), "application/zip")},
data={"type": "skill"}, cookies=cookies,
)
r = web_client.get("/api/marketplace/items?tab=flea", cookies=cookies)
assert r.status_code == 200
data = r.json()
assert data["total"] == 1
assert data["items"][0]["source"] == "flea"
# Invocation name suffixed with -by-<owner>
assert "alpha" in data["items"][0]["name"]
def test_my_subscriptions_default_empty(self, web_client):
"""Without explicit install, a granted curated plugin doesn't show
up under tab=my (Model B)."""
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
r = web_client.get("/api/marketplace/items?tab=my", cookies=cookies)
assert r.status_code == 200
data = r.json()
assert data["total"] == 0
def test_my_stack_carries_marketplace_metadata_enrichment(self, web_client):
"""Once a curated plugin is in the user's stack (subscribed), the
``tab=my`` card MUST carry the same marketplace-metadata enrichment
(cover_photo_url, video_url, category override) the ``tab=curated``
card shows. Previously the My Stack handler built rows from the
on-disk ``marketplace.json``, which doesn't carry those columns —
same plugin → cover photo on Curated, gradient placeholder on
My Stack.
"""
from src.db import get_system_db
from src.repositories.user_curated_subscriptions import (
UserCuratedSubscriptionsRepository,
)
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
# Backfill the marketplace-metadata enrichment columns on the seeded
# plugin row — same shape `_refresh_plugin_cache` writes after a
# nightly sync that picked up a curator's marketplace-metadata.json.
cover = "/api/marketplace/curated/mkt-x/alpha/asset/cover.png"
video = "https://www.youtube.com/watch?v=abc123"
conn = get_system_db()
try:
conn.execute(
"UPDATE marketplace_plugins SET cover_photo_url = ?, "
"video_url = ?, category = ? "
"WHERE marketplace_id = 'mkt-x' AND name = 'alpha'",
[cover, video, "Code & Engineering"],
)
UserCuratedSubscriptionsRepository(conn).subscribe(
user_id=user_id, marketplace_id="mkt-x", plugin_name="alpha",
)
finally:
conn.close()
r = web_client.get("/api/marketplace/items?tab=my", cookies=cookies)
assert r.status_code == 200, r.text
data = r.json()
assert data["total"] == 1, data
item = data["items"][0]
assert item["source"] == "curated"
assert item["name"] == "alpha"
# The bug the test guards: ``photo_url`` (mapped from
# ``marketplace_plugins.cover_photo_url``) used to be hard-coded
# None on the My Stack path. Now the My Stack handler looks up the
# enriched marketplace_plugins row and surfaces it — matching the
# Curated tab. ``MarketplaceItem`` flattens the column name to
# ``photo_url``; see :func:`_curated_to_item`.
assert item["photo_url"] == cover, (
"My Stack must surface marketplace-metadata cover_photo_url, not None"
)
assert item["category"] == "Code & Engineering"
# ---------------------------------------------------------------------------
# /api/marketplace/categories
# ---------------------------------------------------------------------------
class TestCategories:
def test_curated_categories_count(self, web_client):
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(
user_id=user_id, marketplace="mkt-x", plugin="alpha",
plugin_meta={"category": "Code & Engineering"},
)
_seed_curated_grant(
user_id=user_id, marketplace="mkt-x", plugin="beta",
plugin_meta={"category": "Code & Engineering"},
group_name="G-alice-mkt-x-beta",
)
r = web_client.get(
"/api/marketplace/categories?tab=curated", cookies=cookies,
)
assert r.status_code == 200
data = r.json()
cats = {c["name"]: c["count"] for c in data["items"]}
assert cats.get("Code & Engineering") == 2
def test_categories_skip_empty(self, web_client):
_, cookies = _create_user(web_client, "alice@x.com")
r = web_client.get(
"/api/marketplace/categories?tab=curated", cookies=cookies,
)
assert r.status_code == 200
data = r.json()
assert data["items"] == [] # no plugins in scope → no categories
# ---------------------------------------------------------------------------
# Curated detail + install
# ---------------------------------------------------------------------------
class TestCuratedDetail:
def test_detail_403_without_grant(self, web_client):
_, cookies = _create_user(web_client, "alice@x.com")
r = web_client.get(
"/api/marketplace/curated/some-mp/some-plugin", cookies=cookies,
)
assert r.status_code == 403
def test_detail_200_with_grant(self, web_client):
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
r = web_client.get(
"/api/marketplace/curated/mkt-x/alpha", cookies=cookies,
)
assert r.status_code == 200, r.text
data = r.json()
assert data["plugin_name"] == "alpha"
assert data["installed"] is False
# New fields populated for the redesigned plugin detail page.
assert "files" in data and isinstance(data["files"], list)
assert "docs" in data and isinstance(data["docs"], list)
assert data["install_count"] == 0
def test_detail_rich_content_from_marketplace_metadata(
self, web_client, tmp_path,
):
"""When curator wrote rich content into marketplace-metadata.json, the
detail endpoint surfaces display_name, tagline, description_long_html
(server-rendered markdown), use_cases, and sample_interaction. The
on-demand parser reads from `${DATA_DIR}/marketplaces/<id>/...` —
this test seeds that file and verifies the API response carries
the fields through to PluginDetailResponse."""
import json
from pathlib import Path
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
# Write a marketplace-metadata.json to the working tree the on-demand
# parser will read.
marketplaces_dir = Path(tmp_path) / "marketplaces" / "mkt-x" / ".claude-plugin"
marketplaces_dir.mkdir(parents=True, exist_ok=True)
(marketplaces_dir / "marketplace-metadata.json").write_text(json.dumps({
"plugins": {
"alpha": {
"display_name": "Friendly Alpha",
"tagline": "One-line value prop.",
"description": "Para 1.\n\nPara 2 with **bold**.",
"use_cases": [
{"title": "Find owner", "description": "X+Y.", "prompt": "/q"},
],
"sample_interaction": {
"user": "What?",
"assistant": "Here's *the* answer.",
},
},
},
}), encoding="utf-8")
r = web_client.get("/api/marketplace/curated/mkt-x/alpha", cookies=cookies)
assert r.status_code == 200, r.text
data = r.json()
assert data["display_name"] == "Friendly Alpha"
assert data["tagline"] == "One-line value prop."
# description_long_html is the server-rendered markdown body.
assert "<strong>bold</strong>" in data["description_long_html"]
assert "<p>Para 1.</p>" in data["description_long_html"]
assert len(data["use_cases"]) == 1
assert data["use_cases"][0]["title"] == "Find owner"
# sample_interaction carries both the raw assistant text + rendered HTML.
assert data["sample_interaction"]["user"] == "What?"
assert "<em>the</em>" in data["sample_interaction"]["assistant_html"]
def test_detail_falls_back_when_no_rich_content(self, web_client):
"""No marketplace-metadata.json on disk → API returns the historical
shape with rich fields left null / empty. No 500, no crash."""
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
r = web_client.get(
"/api/marketplace/curated/mkt-x/alpha", cookies=cookies,
)
assert r.status_code == 200, r.text
data = r.json()
assert data["display_name"] is None
assert data["tagline"] is None
assert data["description_long_html"] is None
assert data["use_cases"] == []
assert data["sample_interaction"] is None
def test_detail_tolerates_partial_curator_json(self, web_client, tmp_path):
"""Curator commits a sample_interaction with only ``user`` (forgot
``assistant``) and a use_cases entry missing ``prompt``. The endpoint
must skip the malformed sections instead of 500-ing on Pydantic's
required-field validation — PR description promises rich content
renders only when populated, partial population should degrade
gracefully."""
import json
from pathlib import Path
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
marketplaces_dir = Path(tmp_path) / "marketplaces" / "mkt-x" / ".claude-plugin"
marketplaces_dir.mkdir(parents=True, exist_ok=True)
(marketplaces_dir / "marketplace-metadata.json").write_text(json.dumps({
"plugins": {
"alpha": {
"display_name": "Friendly Alpha",
"use_cases": [
{"title": "Good one", "description": "X.", "prompt": "/q"},
{"title": "Missing prompt", "description": "Y."},
{"title": "Empty prompt", "description": "Z.", "prompt": ""},
],
"sample_interaction": {"user": "Just user, no assistant"},
},
},
}), encoding="utf-8")
r = web_client.get("/api/marketplace/curated/mkt-x/alpha", cookies=cookies)
assert r.status_code == 200, r.text
data = r.json()
# Good card survives; malformed cards are dropped.
assert len(data["use_cases"]) == 1
assert data["use_cases"][0]["title"] == "Good one"
# Partial sample_interaction is dropped, not crashed.
assert data["sample_interaction"] is None
# The well-formed field next to the broken ones still renders.
assert data["display_name"] == "Friendly Alpha"
def test_detail_html_is_sanitized(self, web_client, tmp_path):
"""Curator-written `<script>` in description markdown must NOT
survive into description_long_html — defense-in-depth check."""
import json
from pathlib import Path
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
marketplaces_dir = Path(tmp_path) / "marketplaces" / "mkt-x" / ".claude-plugin"
marketplaces_dir.mkdir(parents=True, exist_ok=True)
(marketplaces_dir / "marketplace-metadata.json").write_text(json.dumps({
"plugins": {
"alpha": {
"description": "Hello <script>alert(1)</script> world",
},
},
}), encoding="utf-8")
r = web_client.get("/api/marketplace/curated/mkt-x/alpha", cookies=cookies)
assert r.status_code == 200, r.text
html = r.json()["description_long_html"] or ""
assert "<script>" not in html
# `alert(1)` could appear as escaped text inside the rendered HTML;
# what we MUST not see is unescaped `<script>` tags executing it.
# Verify the literal `<script` open-tag never reaches the response.
assert "<script" not in html.lower()
def test_install_403_without_grant(self, web_client):
_, cookies = _create_user(web_client, "alice@x.com")
r = web_client.post(
"/api/marketplace/curated/some-mp/some-plugin/install",
cookies=cookies,
)
assert r.status_code == 403
def test_install_uninstall_round_trip(self, web_client):
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
# Install.
r = web_client.post(
"/api/marketplace/curated/mkt-x/alpha/install", cookies=cookies,
)
assert r.status_code == 200, r.text
# Verify in DB.
from src.db import get_system_db
from src.repositories.user_curated_subscriptions import (
UserCuratedSubscriptionsRepository,
)
conn = get_system_db()
try:
assert UserCuratedSubscriptionsRepository(conn).is_subscribed(
user_id, "mkt-x", "alpha",
)
finally:
conn.close()
# Detail now reports installed=True.
d = web_client.get(
"/api/marketplace/curated/mkt-x/alpha", cookies=cookies,
).json()
assert d["installed"] is True
# Uninstall.
r = web_client.delete(
"/api/marketplace/curated/mkt-x/alpha/install", cookies=cookies,
)
assert r.status_code == 200
conn = get_system_db()
try:
assert not UserCuratedSubscriptionsRepository(conn).is_subscribed(
user_id, "mkt-x", "alpha",
)
finally:
conn.close()
# ---------------------------------------------------------------------------
# Curated nested skill / agent detail — extended response shape
# ---------------------------------------------------------------------------
def _seed_curated_skill_on_disk(
tmp_path, marketplace: str, plugin: str, skill: str,
*, files: dict[str, str] | None = None,
):
"""Materialize a skill on disk so curated_skill_detail can read it.
`files` maps relative paths inside the skill dir to file contents.
SKILL.md is always written; extra files surface in the Files section.
"""
skill_dir = tmp_path / "marketplaces" / marketplace / "plugins" / plugin / "skills" / skill
skill_dir.mkdir(parents=True, exist_ok=True)
(skill_dir / "SKILL.md").write_text(
f"---\nname: {skill}\ndescription: A test skill.\n---\nbody",
encoding="utf-8",
)
for rel, content in (files or {}).items():
target = skill_dir / rel
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(content, encoding="utf-8")
def _seed_curated_agent_on_disk(
tmp_path, marketplace: str, plugin: str, agent: str,
):
agents_dir = tmp_path / "marketplaces" / marketplace / "plugins" / plugin / "agents"
agents_dir.mkdir(parents=True, exist_ok=True)
(agents_dir / f"{agent}.md").write_text(
f"---\nname: {agent}\ndescription: A test agent.\n---\nbody",
encoding="utf-8",
)
class TestCuratedInnerDetail:
def test_skill_detail_includes_parent_metadata_and_files(
self, web_client, tmp_path,
):
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(
user_id=user_id, marketplace="mkt-x", plugin="alpha",
plugin_meta={"category": "Data", "author": {"name": "ops-team"}},
)
_seed_curated_skill_on_disk(
tmp_path, "mkt-x", "alpha", "data-explorer",
files={"REFERENCE.md": "ref docs"},
)
r = web_client.get(
"/api/marketplace/curated/mkt-x/alpha/skill/data-explorer",
cookies=cookies,
)
assert r.status_code == 200, r.text
d = r.json()
# Inner-detail fields.
assert d["kind"] == "skill"
assert d["name"] == "data-explorer"
assert d["description"] == "A test skill."
# Parent plugin metadata surfaced for the redesigned hero / sidebar.
assert d["category"] == "Data"
assert d["marketplace_name"] # registry display name
assert d["parent_updated_at"] is not None
# Bundle + files.
assert d["bundle_size"] is not None and d["bundle_size"] > 0
names = {f["path"] for f in d["files"]}
assert "SKILL.md" in names
assert "REFERENCE.md" in names
def test_agent_detail_single_file(self, web_client, tmp_path):
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
_seed_curated_agent_on_disk(tmp_path, "mkt-x", "alpha", "incident-responder")
r = web_client.get(
"/api/marketplace/curated/mkt-x/alpha/agent/incident-responder",
cookies=cookies,
)
assert r.status_code == 200, r.text
d = r.json()
assert d["kind"] == "agent"
# Agents are flat single-file .md → exactly one file entry.
assert len(d["files"]) == 1
assert d["files"][0]["path"] == "incident-responder.md"
assert d["bundle_size"] == d["files"][0]["size"]
class TestSafeJoinContainment:
"""Defense-in-depth unit tests for ``_safe_join`` — the helper backing
``_read_inner`` / ``curated_skill_detail`` / ``curated_agent_detail``.
The threat model is a curated marketplace's git mirror containing a
booby-trapped symlink (or a future regression in Starlette's ``[^/]+``
path-param regex letting ``..`` slip through). HTTP-level ``..`` tests
aren't useful — httpx normalizes ``..`` segments before they reach the
wire — so the guard is verified at the function boundary.
"""
def _plugin_root(self, tmp_path):
root = tmp_path / "marketplaces" / "mkt-x" / "plugins" / "alpha"
(root / "skills").mkdir(parents=True)
(root / "agents").mkdir(parents=True)
return root
def test_resolves_normal_skill_path(self, tmp_path):
from app.api.marketplace import _safe_join
root = self._plugin_root(tmp_path)
skill_dir = root / "skills" / "data-explorer"
skill_dir.mkdir()
(skill_dir / "SKILL.md").write_text("body", encoding="utf-8")
result = _safe_join(root, "skills", "data-explorer", "SKILL.md")
assert result is not None
assert result == (skill_dir / "SKILL.md").resolve()
def test_dotdot_segment_escaping_root_returns_none(self, tmp_path):
from app.api.marketplace import _safe_join
root = self._plugin_root(tmp_path)
# Plant a sibling plugin's file that `..` traversal would otherwise reach.
sibling = tmp_path / "marketplaces" / "mkt-x" / "plugins" / "beta"
sibling.mkdir(parents=True)
(sibling / "SECRET.md").write_text("cross-plugin secret", encoding="utf-8")
# /skills/../../beta/SECRET.md would resolve to the sibling's file.
assert _safe_join(root, "skills", "..", "..", "beta", "SECRET.md") is None
def test_symlink_outside_plugin_returns_none(self, tmp_path):
import os, sys
if sys.platform == "win32":
pytest.skip("Symlink creation requires elevated permissions on Windows")
from app.api.marketplace import _safe_join
root = self._plugin_root(tmp_path)
outside = tmp_path / "secrets" / "OTHER.md"
outside.parent.mkdir(parents=True)
outside.write_text("cross-plugin secret", encoding="utf-8")
# A curator-planted symlink inside skills/evil/ pointing outside the
# plugin tree must not resolve through the guard.
evil_dir = root / "skills" / "evil"
evil_dir.mkdir()
os.symlink(outside, evil_dir / "SKILL.md")
assert _safe_join(root, "skills", "evil", "SKILL.md") is None
def test_missing_file_returns_none(self, tmp_path):
from app.api.marketplace import _safe_join
root = self._plugin_root(tmp_path)
assert _safe_join(root, "skills", "nope", "SKILL.md") is None
def test_inner_endpoint_404s_on_symlink_escape(self, web_client, tmp_path):
"""End-to-end: the symlink containment check actually wires through
the HTTP endpoint to a 404 (not a leaked 200)."""
import os, sys
if sys.platform == "win32":
pytest.skip("Symlink creation requires elevated permissions on Windows")
user_id, cookies = _create_user(web_client, "alice@x.com")
_seed_curated_grant(user_id=user_id, marketplace="mkt-x", plugin="alpha")
outside = tmp_path / "secrets" / "OTHER.md"
outside.parent.mkdir(parents=True)
outside.write_text(
"---\nname: leaked\n---\ncross-plugin secret", encoding="utf-8",
)
evil_dir = (
tmp_path / "marketplaces" / "mkt-x" / "plugins" / "alpha"
/ "skills" / "evil"
)
evil_dir.mkdir(parents=True)
os.symlink(outside, evil_dir / "SKILL.md")
r = web_client.get(
"/api/marketplace/curated/mkt-x/alpha/skill/evil",
cookies=cookies,
)
assert r.status_code == 404, r.text
assert r.json()["detail"] == "skill_not_found"
# ---------------------------------------------------------------------------
# Flea standalone detail — extended response shape
# ---------------------------------------------------------------------------
class TestFleaDetail:
def test_flea_skill_detail_populates_files_owner_install_count(
self, web_client,
):
_, cookies = _create_user(web_client, "alice@x.com")
# Upload a skill into the Store.
up = web_client.post(
"/api/store/entities",
files={"file": ("s.zip", _make_skill_zip("alpha"), "application/zip")},
data={"type": "skill"}, cookies=cookies,
)
assert up.status_code == 201, up.text
entity_id = up.json()["id"]
r = web_client.get(
f"/api/marketplace/flea/{entity_id}/detail", cookies=cookies,
)
assert r.status_code == 200, r.text
d = r.json()
assert d["source"] == "flea"
assert d["entity_id"] == entity_id
# Files walked from disk.
assert isinstance(d["files"], list) and len(d["files"]) >= 1
# Friendly owner_display falls through to users.name (email local-part
# is the seeded `name` in _create_user → 'alice').
assert d["owner_display"] == "alice"
# install_count starts at 0; bumps after install/uninstall toggle.
assert d["install_count"] == 0
# docs is always a list (empty when uploader didn't ship any).
assert isinstance(d["docs"], list)