package integration_test import ( "context" "encoding/json" "fmt" "net/http" "testing" "time" "github.com/google/uuid" "golang.org/x/crypto/bcrypt" ) func TestOpsCMSFeedSlotWrite(t *testing.T) { r, pool := setupAPIPool(t) ctx := context.Background() tok := adminLogin(t, r, "admin", "change-me") devKey := fmt.Sprintf("feed-slot-write-%d", time.Now().UnixNano()) code := fmt.Sprintf("slot_w_%d", time.Now().UnixNano()%1_000_000) body := map[string]any{ "code": code, "title": "测试槽", "slot_key": "home.test", "placement": "home", "active": true, } env, httpCode := doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/cms/feed-slots", body, tok) if httpCode != 200 || env.Code != 0 { t.Fatalf("create http=%d code=%d msg=%s", httpCode, env.Code, env.Message) } var created struct { ID string `json:"id"` Code string `json:"code"` } _ = json.Unmarshal(env.Data, &created) if created.ID == "" { t.Fatalf("bad create") } t.Cleanup(func() { _, _ = pool.Exec(ctx, `DELETE FROM ops_feed_slots WHERE id=$1`, created.ID) }) _, httpCode = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/cms/feed-slots", body, tok) if httpCode != http.StatusConflict { t.Fatalf("dup expected 409 got %d", httpCode) } env, _, httpCode = doJSONExpect(t, r, http.MethodGet, "/api/v1/home/feed-slots?placement=home", nil, devKey, 0) if httpCode != 200 { t.Fatalf("home slots %d", httpCode) } var pub struct { Items []struct { Code string `json:"code"` } `json:"items"` } _ = json.Unmarshal(env.Data, &pub) found := false for _, it := range pub.Items { if it.Code == code { found = true break } } if !found { t.Fatalf("missing active slot %#v", pub.Items) } body["active"] = false _, httpCode = doAdminJSON(t, r, http.MethodPut, "/api/v1/admin/cms/feed-slots/"+created.ID, body, tok) if httpCode != 200 { t.Fatalf("update %d", httpCode) } env, _, _ = doJSONExpect(t, r, http.MethodGet, "/api/v1/home/feed-slots?placement=home", nil, devKey, 0) _ = json.Unmarshal(env.Data, &pub) for _, it := range pub.Items { if it.Code == code { t.Fatalf("inactive still listed") } } var n int _ = pool.QueryRow(ctx, ` SELECT COUNT(*) FROM admin_audit_logs WHERE action IN ('cms.feed_slot.create','cms.feed_slot.update') AND target_id=$1`, created.ID).Scan(&n) if n < 2 { t.Fatalf("audit %d", n) } limitedRoleID := uuid.New() _, _ = pool.Exec(ctx, `INSERT INTO admin_roles(id, name, system) VALUES ($1,$2,false)`, limitedRoleID, "fs_ro_"+limitedRoleID.String()[:8]) _, _ = pool.Exec(ctx, `INSERT INTO admin_role_permissions(role_id, code) VALUES ($1,'admin.cms.read')`, limitedRoleID) hash, _ := bcrypt.GenerateFromPassword([]byte("ro-pass"), bcrypt.DefaultCost) roUser := fmt.Sprintf("fsro_%d", time.Now().UnixNano()) _, _ = pool.Exec(ctx, `INSERT INTO admin_accounts(username, password_hash, role_id) VALUES ($1,$2,$3)`, roUser, string(hash), limitedRoleID) t.Cleanup(func() { _, _ = pool.Exec(ctx, `DELETE FROM admin_accounts WHERE username=$1`, roUser) _, _ = pool.Exec(ctx, `DELETE FROM admin_roles WHERE id=$1`, limitedRoleID) }) roTok := adminLogin(t, r, roUser, "ro-pass") _, httpCode = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/cms/feed-slots", map[string]any{ "code": "x_ro", "title": "no", "slot_key": "home.x", "placement": "home", "active": true, }, roTok) if httpCode != http.StatusForbidden { t.Fatalf("expected 403 got %d", httpCode) } }