Files
digital-psychology/apps/api/internal/integration/image_card_deck_write_test.go
T
jackyu66gitandCursor 0158036341 feat(ECR-045): ImageCardDeck 写面闭环并 Closed
复用 admin.explore.write、POST/PUT+审计、C端 GET /cards/decks、
H5 无 active 空态/失败回退;migration 000054。无牌面内容编辑。
ExploreConfig Loop STOP,禁自动 ECR-046。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 21:29:43 +08:00

117 lines
3.7 KiB
Go

package integration_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
func TestExploreImageCardDeckWrite(t *testing.T) {
r, pool := setupAPIPool(t)
ctx := context.Background()
tok := adminLogin(t, r, "admin", "change-me")
devKey := fmt.Sprintf("deck-cfg-%d", time.Now().UnixNano())
code := fmt.Sprintf("deck_w_%d", time.Now().UnixNano()%1_000_000)
body := map[string]any{"code": code, "title": "测试意象牌组", "active": true}
env, httpCode := doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/explore/image-card-decks", 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.Fatal("bad create")
}
t.Cleanup(func() {
_, _ = pool.Exec(ctx, `DELETE FROM image_card_decks WHERE id=$1`, created.ID)
})
_, httpCode = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/explore/image-card-decks", body, tok)
if httpCode != http.StatusConflict {
t.Fatalf("dup expected 409 got %d", httpCode)
}
env, _, httpCode = doJSONExpect(t, r, http.MethodGet, "/api/v1/cards/decks", nil, devKey, 0)
if httpCode != 200 {
t.Fatalf("public %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 %#v", pub.Items)
}
body["active"] = false
_, httpCode = doAdminJSON(t, r, http.MethodPut, "/api/v1/admin/explore/image-card-decks/"+created.ID, body, tok)
if httpCode != 200 {
t.Fatalf("update %d", httpCode)
}
env, _, _ = doJSONExpect(t, r, http.MethodGet, "/api/v1/cards/decks", nil, devKey, 0)
_ = json.Unmarshal(env.Data, &pub)
for _, it := range pub.Items {
if it.Code == code {
t.Fatal("inactive still listed")
}
}
_, _ = pool.Exec(ctx, `UPDATE image_card_decks SET active=false`)
t.Cleanup(func() {
_, _ = pool.Exec(ctx, `UPDATE image_card_decks SET active=true WHERE system=true`)
})
env, _, _ = doJSONExpect(t, r, http.MethodGet, "/api/v1/cards/decks", nil, devKey, 0)
_ = json.Unmarshal(env.Data, &pub)
if len(pub.Items) != 0 {
t.Fatalf("expected empty after all inactive, got %#v", pub.Items)
}
var n int
_ = pool.QueryRow(ctx, `
SELECT COUNT(*) FROM admin_audit_logs
WHERE action IN ('explore.image_card_deck.create','explore.image_card_deck.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, "dc_ro_"+limitedRoleID.String()[:8])
_, _ = pool.Exec(ctx, `INSERT INTO admin_role_permissions(role_id, code) VALUES ($1,'admin.explore.read')`, limitedRoleID)
hash, _ := bcrypt.GenerateFromPassword([]byte("ro-pass"), bcrypt.DefaultCost)
roUser := fmt.Sprintf("dcro_%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/explore/image-card-decks", map[string]any{
"code": "x_ro", "title": "no", "active": true,
}, roTok)
if httpCode != http.StatusForbidden {
t.Fatalf("expected 403 got %d", httpCode)
}
}