package cabana import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "sync" "testing" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/bouncer" "git.golem15.com/golem15/summercms/modules/pact" "git.golem15.com/golem15/summercms/modules/party" ) // TestAccountItemsSmoke covers per-item rows on My account contributions, // on Postgres so item actions run in a real transaction: a section without // Items still answers items null, a section with Items lists the // administrator's rows and the item actions they may run, an item action on // a listed id runs with the administrator and item ids inside a // transaction, the same id posted by another administrator (whose Items // list it not) is 404 without Run, Run answering pact.ErrAccountItemNotFound // is 404, a missing permission is 403, and item actions without Items fail // boot. func TestAccountItemsSmoke(t *testing.T) { gdb := consentGorm(t) var mu sync.Mutex type call struct { admin uint item string tx bool } var calls []call const adminA, adminB = 41, 42 rows := map[uint][]pact.AccountItem{ adminA: { {ID: "c1", Title: "Agent one", Subtitle: "Full access", Meta: []string{"Connected 2026-11-20"}}, {ID: "gone", Title: "Agent two"}, }, } revoke := pact.AccountItemAction{ Name: "revoke", Label: "acme::lang.revoke", Confirm: "Revoke :title?", Danger: true, Run: func(ctx context.Context, in pact.AccountItemActionInput) (pact.AccountActionResult, error) { _, tx := TxFromContext(ctx) mu.Lock() calls = append(calls, call{admin: in.AdminID, item: in.ItemID, tx: tx}) mu.Unlock() if in.ItemID == "gone" { return pact.AccountActionResult{}, pact.ErrAccountItemNotFound } return pact.AccountActionResult{Message: "acme::lang.revoked"}, nil }, } locked := pact.AccountItemAction{ Name: "rename", Label: "Rename", Permissions: []string{"acme.items.manage"}, Run: func(context.Context, pact.AccountItemActionInput) (pact.AccountActionResult, error) { return pact.AccountActionResult{}, nil }, } sections := []pact.AccountContribution{ {Name: "plain", Label: "Plain section"}, { Name: "connections", Label: "Connections", ItemsEmpty: "Nothing connected.", Items: func(_ context.Context, adminID uint) ([]pact.AccountItem, error) { return rows[adminID], nil }, ItemActions: []pact.AccountItemAction{revoke, locked}, }, } reg := &Registry{byID: map[string]*CompiledController{}} if err := compileAccountContributions(reg, []party.Plugin{contributor{id: "acme.links", sections: sections}}); err != nil { t.Fatal(err) } app := backpack.New(nil) if err := app.Publish(gdb); err != nil { t.Fatal(err) } svc := &service{app: app, reg: reg} principal := func(id uint) *bouncer.Principal { return &bouncer.Principal{ID: id, Backend: true} } list := func(p *bouncer.Principal) []map[string]any { t.Helper() req := httptest.NewRequest(http.MethodGet, "/account/contributions", nil) req = req.WithContext(bouncer.WithUser(req.Context(), p)) rec := httptest.NewRecorder() interactiveOnly(svc.accountContributionsList)(rec, req) if rec.Code != http.StatusOK { t.Fatalf("list = %d %s", rec.Code, rec.Body.String()) } var body struct { Data []map[string]any `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatal(err) } return body.Data } act := func(p *bouncer.Principal, item, action string) *httptest.ResponseRecorder { req := httptest.NewRequest(http.MethodPost, "/account/contribution-items/connections/"+item+"/actions/"+action, strings.NewReader("{}")) req.Header.Set("X-Requested-With", "XMLHttpRequest") req.SetPathValue("name", "connections") req.SetPathValue("item", item) req.SetPathValue("action", action) req = req.WithContext(bouncer.WithUser(req.Context(), p)) rec := httptest.NewRecorder() interactiveOnly(requireAjax(svc.accountContributionItemAction))(rec, req) return rec } data := list(principal(adminA)) if len(data) != 2 { t.Fatalf("sections = %+v", data) } if items, ok := data[0]["items"]; !ok || items != nil || data[0]["item_actions"] != nil { t.Fatalf("section without Items = %+v, want items null", data[0]) } items, _ := data[1]["items"].([]any) if len(items) != 2 { t.Fatalf("items = %+v", data[1]["items"]) } first, _ := items[0].(map[string]any) if first["id"] != "c1" || first["title"] != "Agent one" || first["subtitle"] != "Full access" { t.Fatalf("first item = %+v", first) } actions, _ := data[1]["item_actions"].([]any) if len(actions) != 1 || actions[0].(map[string]any)["name"] != "revoke" || actions[0].(map[string]any)["confirm"] != "Revoke :title?" || data[1]["items_empty"] != "Nothing connected." { t.Fatalf("item actions = %+v empty=%v", data[1]["item_actions"], data[1]["items_empty"]) } // Admin B has no rows: an empty array, never null. if dataB := list(principal(adminB)); dataB[1]["items"] == nil { t.Fatalf("admin B items = %+v, want []", dataB[1]) } if rec := act(principal(adminA), "c1", "revoke"); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "acme::lang.revoked") { t.Fatalf("revoke = %d %s", rec.Code, rec.Body.String()) } if len(calls) != 1 || calls[0] != (call{admin: adminA, item: "c1", tx: true}) { t.Fatalf("calls = %+v", calls) } // Admin B posts admin A's item id: 404 and Run is not called. if rec := act(principal(adminB), "c1", "revoke"); rec.Code != http.StatusNotFound || len(calls) != 1 { t.Fatalf("cross-admin = %d %s calls=%d", rec.Code, rec.Body.String(), len(calls)) } // Run answering ErrAccountItemNotFound: 404. if rec := act(principal(adminA), "gone", "revoke"); rec.Code != http.StatusNotFound || len(calls) != 2 { t.Fatalf("gone item = %d %s calls=%d", rec.Code, rec.Body.String(), len(calls)) } if rec := act(principal(adminA), "c1", "rename"); rec.Code != http.StatusForbidden { t.Fatalf("missing permission = %d %s", rec.Code, rec.Body.String()) } if rec := act(principal(adminA), "c1", "nope"); rec.Code != http.StatusNotFound { t.Fatalf("unknown item action = %d %s", rec.Code, rec.Body.String()) } // Boot refuses item actions without Items, and the other bad shapes. for name, bad := range map[string]pact.AccountContribution{ "item actions without Items": {Name: "x", Label: "x", ItemActions: []pact.AccountItemAction{revoke}}, "bad item action name": {Name: "x", Label: "x", Items: sections[1].Items, ItemActions: []pact.AccountItemAction{{Name: "no!", Label: "x", Run: revoke.Run}}}, "duplicate item action": {Name: "x", Label: "x", Items: sections[1].Items, ItemActions: []pact.AccountItemAction{revoke, revoke}}, "nil item run": {Name: "x", Label: "x", Items: sections[1].Items, ItemActions: []pact.AccountItemAction{{Name: "a", Label: "x"}}}, "empty item label": {Name: "x", Label: "x", Items: sections[1].Items, ItemActions: []pact.AccountItemAction{{Name: "a", Run: revoke.Run}}}, } { err := compileAccountContributions(&Registry{}, []party.Plugin{contributor{id: "acme.bad", sections: []pact.AccountContribution{bad}}}) if err == nil || !strings.HasPrefix(err.Error(), "cabana: plugin acme.bad contributes account section") { t.Fatalf("%s: err = %v", name, err) } } }