feat(cabana): plugin updates and marketplace search on the Plugins screen
- GET /plugins looks up each versioned plugin's releases through kiosk.Client and applies plugin:update's rule (NewestCompatible above the BaseVersion), with the update command and the framework a newer release needs - GET /plugins/marketplace forwards q and category (validated like the registry), marks installed and incompatible plugins and returns the install command; ids outside the plugin ID shape are dropped - 4 s per-call timeout, at most 4 lookups at once, in-process cache (searches 5 min, releases 15 min, failures 1 min); a failing registry gives status unavailable, an empty plugins.registry_url sends nothing - SPA: update badges and Marketplace tab with copy buttons (clipboard fallback), hidden when the registry is disabled; no install button - OpenAPI, SPA types, inventories, conformance case, en/pl strings, docs
This commit is contained in:
@@ -4,11 +4,16 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
"git.golem15.com/golem15/summercms/modules/kiosk"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
)
|
||||
|
||||
@@ -46,7 +51,7 @@ func pluginsService(t *testing.T, cfg *compass.Config, installed *party.Installe
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return &service{app: app, reg: reg}
|
||||
return &service{app: app, reg: reg, catalog: newPluginCatalog(cfg)}
|
||||
}
|
||||
|
||||
// pluginsCall runs handler for principal on GET target and returns the
|
||||
@@ -161,4 +166,317 @@ func TestPluginsScreenSmoke(t *testing.T) {
|
||||
t.Fatalf("configured registry reported %+v", old.Registry)
|
||||
}
|
||||
})
|
||||
|
||||
pluginsRegistrySubtests(t)
|
||||
}
|
||||
|
||||
// fakeRegistry is a registry API v1 server: versions per plugin ID, the
|
||||
// plugin list, a request log, and switches for failures and slowness.
|
||||
type fakeRegistry struct {
|
||||
srv *httptest.Server
|
||||
mu sync.Mutex
|
||||
requests []string
|
||||
versions map[string][]kiosk.RegistryVersion
|
||||
list []kiosk.RegistryPlugin
|
||||
fail atomic.Bool
|
||||
delay atomic.Int64
|
||||
}
|
||||
|
||||
func newFakeRegistry(t *testing.T) *fakeRegistry {
|
||||
t.Helper()
|
||||
f := &fakeRegistry{versions: map[string][]kiosk.RegistryVersion{}}
|
||||
f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
f.requests = append(f.requests, r.URL.RequestURI())
|
||||
f.mu.Unlock()
|
||||
if d := time.Duration(f.delay.Load()); d > 0 {
|
||||
select {
|
||||
case <-time.After(d):
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
if f.fail.Load() {
|
||||
http.Error(w, "boom", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
path := strings.TrimPrefix(r.URL.Path, kiosk.RegistryAPIPath)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case path == "/plugins":
|
||||
out := []kiosk.RegistryPlugin{}
|
||||
q, category := r.URL.Query().Get("q"), r.URL.Query().Get("category")
|
||||
for _, p := range f.list {
|
||||
if (category == "" || p.Category == category) && (q == "" || strings.Contains(p.ID+" "+p.Name, q)) {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(kiosk.ListResponse{Data: out, Meta: kiosk.ListMeta{Total: len(out)}})
|
||||
case strings.HasPrefix(path, "/plugins/") && strings.HasSuffix(path, "/versions"):
|
||||
id := strings.TrimSuffix(strings.TrimPrefix(path, "/plugins/"), "/versions")
|
||||
versions, ok := f.versions[id]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_ = json.NewEncoder(w).Encode(kiosk.ErrorResponse{Error: kiosk.RegistryError{Code: kiosk.RegistryErrorNotFound, Message: "no such plugin"}})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(kiosk.VersionsResponse{Data: versions})
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(f.srv.Close)
|
||||
return f
|
||||
}
|
||||
|
||||
// log returns the request URIs so far and clears the log.
|
||||
func (f *fakeRegistry) log() []string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
out := f.requests
|
||||
f.requests = nil
|
||||
return out
|
||||
}
|
||||
|
||||
// registryFixture lists acme.blog (v0.1.1 for this framework, v0.2.0 for
|
||||
// a newer one), acme.translate (newest compatible v0.1.1) and a
|
||||
// marketplace of three plugins.
|
||||
func registryFixture(t *testing.T) *fakeRegistry {
|
||||
t.Helper()
|
||||
f := newFakeRegistry(t)
|
||||
f.versions["acme.blog"] = []kiosk.RegistryVersion{
|
||||
{Version: "v0.2.0", Framework: "v0.7.0"},
|
||||
{Version: "v0.1.1", Framework: "v0.6.0"},
|
||||
{Version: "v0.1.0", Framework: "v0.5.0"},
|
||||
}
|
||||
f.versions["acme.translate"] = []kiosk.RegistryVersion{
|
||||
{Version: "v0.1.1", Framework: "v0.6.0"},
|
||||
{Version: "v0.1.0", Framework: "v0.6.0"},
|
||||
}
|
||||
f.list = []kiosk.RegistryPlugin{
|
||||
{ID: "acme.blog", Name: "Blog", Description: "Posts.", Category: "content", LatestVersion: "v0.2.0", LatestFramework: "v0.7.0"},
|
||||
{ID: "acme.journal", Name: "Journal", Description: "A <b>journal</b>.", Category: "content", Featured: true, IconURL: "https://registry.example.test/icon.png", LatestVersion: "v0.3.0", LatestFramework: "v0.6.0"},
|
||||
{ID: "acme.shop", Name: "Shop", Description: "Orders.", Category: "commerce", LatestVersion: "v1.0.0", LatestFramework: "v0.5.0"},
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func pluginByID(t *testing.T, overview PluginsOverview, id string) InstalledPlugin {
|
||||
t.Helper()
|
||||
for _, p := range overview.Plugins {
|
||||
if p.ID == id {
|
||||
return p
|
||||
}
|
||||
}
|
||||
t.Fatalf("plugin %s missing from %+v", id, overview.Plugins)
|
||||
return InstalledPlugin{}
|
||||
}
|
||||
|
||||
func decodeMarketplace(t *testing.T, rec *httptest.ResponseRecorder) MarketplaceResponse {
|
||||
t.Helper()
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body MarketplaceResponse
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode %s: %v", rec.Body.String(), err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// pluginsRegistrySubtests are TestPluginsScreenSmoke's update badge and
|
||||
// marketplace subtests, run against a fake registry.
|
||||
func pluginsRegistrySubtests(t *testing.T) {
|
||||
t.Run("updates", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
svc := pluginsService(t, pluginsConfig(t, f.srv.URL), testInstalled(), nil)
|
||||
overview := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
if !overview.Registry.Enabled || overview.Registry.Status != registryStatusOK {
|
||||
t.Fatalf("registry = %+v", overview.Registry)
|
||||
}
|
||||
blog := pluginByID(t, overview, "acme.blog")
|
||||
if !blog.Listed || blog.Latest != "v0.1.1" || !blog.UpdateAvailable || blog.UpdateCommand != "summer plugin:update acme.blog" || blog.LatestFramework != "v0.7.0" {
|
||||
t.Fatalf("blog = %+v", blog)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("describe_base", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
svc := pluginsService(t, pluginsConfig(t, f.srv.URL), testInstalled(), nil)
|
||||
overview := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
translate := pluginByID(t, overview, "acme.translate")
|
||||
if !translate.Listed || translate.Latest != "v0.1.1" || translate.UpdateAvailable || translate.UpdateCommand != "" || translate.LatestFramework != "" {
|
||||
t.Fatalf("translate at v0.1.1-3-gabc1234 = %+v", translate)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unlisted", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
delete(f.versions, "acme.translate")
|
||||
svc := pluginsService(t, pluginsConfig(t, f.srv.URL), testInstalled(), nil)
|
||||
overview := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
translate := pluginByID(t, overview, "acme.translate")
|
||||
if translate.Listed || translate.Latest != "" || translate.UpdateAvailable || translate.UpdateCommand != "" {
|
||||
t.Fatalf("unlisted translate = %+v", translate)
|
||||
}
|
||||
if overview.Registry.Status != registryStatusOK {
|
||||
t.Fatalf("a 404 for one plugin made the registry %+v", overview.Registry)
|
||||
}
|
||||
// The local plugin (no version) is never looked up.
|
||||
for _, uri := range f.log() {
|
||||
if strings.Contains(uri, "acme.local") {
|
||||
t.Fatalf("local plugin looked up: %s", uri)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unavailable", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
f.fail.Store(true)
|
||||
svc := pluginsService(t, pluginsConfig(t, f.srv.URL), testInstalled(), nil)
|
||||
overview := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
if !overview.Registry.Enabled || overview.Registry.Status != registryStatusUnavailable || len(overview.Plugins) != 3 {
|
||||
t.Fatalf("500 registry overview = %+v", overview)
|
||||
}
|
||||
if blog := pluginByID(t, overview, "acme.blog"); blog.Version != "v0.1.0" || blog.UpdateAvailable || blog.Listed {
|
||||
t.Fatalf("blog with the registry down = %+v", blog)
|
||||
}
|
||||
market := decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsSuperuser, "/plugins/marketplace?q=journal"))
|
||||
if market.Meta.Registry.Status != registryStatusUnavailable || len(market.Data) != 0 {
|
||||
t.Fatalf("500 registry marketplace = %+v", market)
|
||||
}
|
||||
|
||||
// A registry slower than the call timeout is unavailable too.
|
||||
slow := registryFixture(t)
|
||||
slow.delay.Store(int64(2 * time.Second))
|
||||
svc = pluginsService(t, pluginsConfig(t, slow.srv.URL), testInstalled(), nil)
|
||||
svc.pluginCatalog().timeout = 50 * time.Millisecond
|
||||
start := time.Now()
|
||||
overview = decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
if overview.Registry.Status != registryStatusUnavailable || len(overview.Plugins) != 3 {
|
||||
t.Fatalf("slow registry overview = %+v", overview)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > time.Second {
|
||||
t.Fatalf("slow registry held the answer for %v", elapsed)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("disabled", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
svc := pluginsService(t, pluginsConfig(t, ""), testInstalled(), nil)
|
||||
overview := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
if overview.Registry.Enabled || overview.Registry.Status != registryStatusDisabled {
|
||||
t.Fatalf("disabled registry = %+v", overview.Registry)
|
||||
}
|
||||
for _, p := range overview.Plugins {
|
||||
if p.Listed || p.UpdateAvailable || p.Latest != "" {
|
||||
t.Fatalf("disabled registry still reported %+v", p)
|
||||
}
|
||||
}
|
||||
market := decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsSuperuser, "/plugins/marketplace"))
|
||||
if market.Meta.Registry.Enabled || market.Meta.Registry.Status != registryStatusDisabled || len(market.Data) != 0 {
|
||||
t.Fatalf("disabled marketplace = %+v", market)
|
||||
}
|
||||
if got := f.log(); len(got) != 0 {
|
||||
t.Fatalf("requests with the registry disabled: %v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("marketplace", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
svc := pluginsService(t, pluginsConfig(t, f.srv.URL), testInstalled(), nil)
|
||||
market := decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsViewer, "/plugins/marketplace?q=journal&category=content"))
|
||||
if market.Meta.Registry.Status != registryStatusOK || len(market.Data) != 1 {
|
||||
t.Fatalf("marketplace = %+v", market)
|
||||
}
|
||||
journal := market.Data[0]
|
||||
if journal.ID != "acme.journal" || journal.Name != "Journal" || journal.Description != "A <b>journal</b>." || !journal.Featured ||
|
||||
journal.LatestVersion != "v0.3.0" || journal.Installed || !journal.Compatible || journal.InstallCommand != "summer plugin:install acme.journal" ||
|
||||
journal.IconURL != "https://registry.example.test/icon.png" || journal.Category != "content" {
|
||||
t.Fatalf("journal = %+v", journal)
|
||||
}
|
||||
forwarded := false
|
||||
for _, uri := range f.log() {
|
||||
if strings.Contains(uri, "q=journal") && strings.Contains(uri, "category=content") {
|
||||
forwarded = true
|
||||
}
|
||||
}
|
||||
if !forwarded {
|
||||
t.Fatal("q and category were not forwarded to the registry")
|
||||
}
|
||||
if strings.Join(market.Meta.Categories, ",") != "commerce,content" {
|
||||
t.Fatalf("categories = %v", market.Meta.Categories)
|
||||
}
|
||||
|
||||
all := decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsViewer, "/plugins/marketplace"))
|
||||
if len(all.Data) != 3 || all.Meta.Total != 3 {
|
||||
t.Fatalf("unfiltered marketplace = %+v", all)
|
||||
}
|
||||
blog := all.Data[0]
|
||||
if blog.ID != "acme.blog" || !blog.Installed || blog.Compatible || blog.LatestFramework != "v0.7.0" {
|
||||
t.Fatalf("installed blog needing a newer framework = %+v", blog)
|
||||
}
|
||||
|
||||
for _, target := range []string{
|
||||
"/plugins/marketplace?q=" + strings.Repeat("a", 101),
|
||||
"/plugins/marketplace?category=Content",
|
||||
"/plugins/marketplace?category=" + strings.Repeat("a", 41),
|
||||
} {
|
||||
rec := pluginsCall(svc, (*service).pluginsMarketplace, pluginsViewer, target)
|
||||
if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), `"invalid_query"`) {
|
||||
t.Fatalf("%s: status=%d body=%s", target, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
if rec := pluginsCall(svc, (*service).pluginsMarketplace, pluginsDenied, "/plugins/marketplace"); rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("marketplace without %s: status=%d", PermissionViewPlugins, rec.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("cache", func(t *testing.T) {
|
||||
f := registryFixture(t)
|
||||
svc := pluginsService(t, pluginsConfig(t, f.srv.URL), testInstalled(), nil)
|
||||
now := time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC)
|
||||
svc.pluginCatalog().now = func() time.Time { return now }
|
||||
|
||||
decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsSuperuser, "/plugins/marketplace?q=blog"))
|
||||
if first := f.log(); len(first) == 0 {
|
||||
t.Fatal("no registry request on the first load")
|
||||
}
|
||||
decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsSuperuser, "/plugins/marketplace?q=blog"))
|
||||
if again := f.log(); len(again) != 0 {
|
||||
t.Fatalf("cached answers still called the registry: %v", again)
|
||||
}
|
||||
|
||||
// After the marketplace window (5 minutes) the search is asked
|
||||
// again; the versions (15 minutes) are not.
|
||||
now = now.Add(6 * time.Minute)
|
||||
decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins"))
|
||||
decodeMarketplace(t, pluginsCall(svc, (*service).pluginsMarketplace, pluginsSuperuser, "/plugins/marketplace?q=blog"))
|
||||
for _, uri := range f.log() {
|
||||
if strings.Contains(uri, "/versions") {
|
||||
t.Fatalf("versions asked again inside their window: %s", uri)
|
||||
}
|
||||
}
|
||||
|
||||
// A failure is cached for a minute only.
|
||||
f.fail.Store(true)
|
||||
now = now.Add(16 * time.Minute)
|
||||
if got := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins")); got.Registry.Status != registryStatusUnavailable {
|
||||
t.Fatalf("failing registry = %+v", got.Registry)
|
||||
}
|
||||
f.fail.Store(false)
|
||||
f.log()
|
||||
if got := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins")); got.Registry.Status != registryStatusUnavailable {
|
||||
t.Fatalf("failure not cached: %+v", got.Registry)
|
||||
}
|
||||
if got := f.log(); len(got) != 0 {
|
||||
t.Fatalf("cached failure still called the registry: %v", got)
|
||||
}
|
||||
now = now.Add(2 * time.Minute)
|
||||
if got := decodeOverview(t, pluginsCall(svc, (*service).pluginsInstalled, pluginsSuperuser, "/plugins")); got.Registry.Status != registryStatusOK {
|
||||
t.Fatalf("recovered registry = %+v", got.Registry)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user