243 lines
7.8 KiB
Go
243 lines
7.8 KiB
Go
// service_hierarchy_test.go(原 Day28):上级切换(级联路径+防环)/标星置顶/服务团队/实名状态机。
|
|
package account
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"go-sms/internal/model/entity"
|
|
errs "go-sms/internal/pkg/errors"
|
|
)
|
|
|
|
// newSvc28 内存库(单连接隔离)。
|
|
func newSvc28(t *testing.T) *Service {
|
|
t.Helper()
|
|
s := newTestService(t)
|
|
if sqlDB, err := s.config.DB().DB(); err == nil {
|
|
sqlDB.SetMaxOpenConns(1)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// create28 开户(parent 可为 0)并断言 path 已初始化。
|
|
func create28(t *testing.T, s *Service, username string, parent int64) *entity.AccountUser {
|
|
t.Helper()
|
|
u, err := s.CreateAccount(context.Background(), &CreateAccountRequest{
|
|
Username: username, Password: "secret66", Role: "customer", Business: BusinessSMS,
|
|
ParentID: parent,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.Path == "" || u.Path[len(u.Path)-1] != '/' {
|
|
t.Fatalf("%s path 未初始化: %q", username, u.Path)
|
|
}
|
|
return u
|
|
}
|
|
|
|
// pathOf 读库中当前 path。
|
|
func pathOf(t *testing.T, s *Service, id int64) string {
|
|
t.Helper()
|
|
var u entity.AccountUser
|
|
if err := s.config.DB().Select("id, path").Where("id = ?", id).Take(&u).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return u.Path
|
|
}
|
|
|
|
// TestChangeParent_PathCascade 验收:切换后自身与子孙路径级联重写,旁支不变。
|
|
func TestChangeParent_PathCascade(t *testing.T) {
|
|
s := newSvc28(t)
|
|
ctx := context.Background()
|
|
root := create28(t, s, "d28-root", 0) // /root/
|
|
a := create28(t, s, "d28-a", root.ID) // /root/a/
|
|
b := create28(t, s, "d28-b", a.ID) // /root/a/b/
|
|
c := create28(t, s, "d28-c", b.ID) // /root/a/b/c/
|
|
other := create28(t, s, "d28-o", root.ID) // /root/o/
|
|
|
|
// B 切换挂到 root 下:/root/b/c/,A 不受影响
|
|
if err := s.ChangeParent(ctx, b.ID, root.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := pathOf(t, s, b.ID); got != root.Path+fmt.Sprintf("%d/", b.ID) {
|
|
t.Fatalf("B path = %q, 期望 %q", got, root.Path+fmt.Sprintf("%d/", b.ID))
|
|
}
|
|
if got := pathOf(t, s, c.ID); got != root.Path+fmt.Sprintf("%d/%d/", b.ID, c.ID) {
|
|
t.Fatalf("子孙 C 未级联: %q", got)
|
|
}
|
|
if got := pathOf(t, s, a.ID); got != root.Path+fmt.Sprintf("%d/", a.ID) {
|
|
t.Fatalf("旁支 A 不应变: %q", got)
|
|
}
|
|
if got := pathOf(t, s, other.ID); got != root.Path+fmt.Sprintf("%d/", other.ID) {
|
|
t.Fatalf("旁支 O 不应变: %q", got)
|
|
}
|
|
var b2 entity.AccountUser
|
|
s.config.DB().Select("id, parent_id").Where("id = ?", b.ID).Take(&b2)
|
|
if b2.ParentID != root.ID {
|
|
t.Fatalf("parent_id = %d, 期望 %d", b2.ParentID, root.ID)
|
|
}
|
|
desc, err := s.Descendants(ctx, root.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(desc) != 4 { // a, b, c, other
|
|
t.Fatalf("root 后代 = %d, 期望 4", len(desc))
|
|
}
|
|
}
|
|
|
|
// TestChangeParent_PreventCycle 验收:切给自己/切给自己后代/不存在 → 拒绝且路径不变。
|
|
func TestChangeParent_PreventCycle(t *testing.T) {
|
|
s := newSvc28(t)
|
|
ctx := context.Background()
|
|
root := create28(t, s, "d28-r2", 0)
|
|
a := create28(t, s, "d28-a2", root.ID)
|
|
b := create28(t, s, "d28-b2", a.ID)
|
|
before := pathOf(t, s, a.ID)
|
|
|
|
if err := s.ChangeParent(ctx, a.ID, a.ID); err == nil {
|
|
t.Fatal("切给自己应拒绝")
|
|
}
|
|
if err := s.ChangeParent(ctx, a.ID, b.ID); err == nil {
|
|
t.Fatal("切给自己的后代应拒绝(防环)")
|
|
} else if bizErr, ok := err.(*errs.Error); !ok || bizErr.Code != errs.InvalidParam.Code {
|
|
t.Fatalf("防环应 ISC:400, 实际 %v", err)
|
|
}
|
|
if err := s.ChangeParent(ctx, a.ID, 99999); err == nil {
|
|
t.Fatal("新上级不存在应 404")
|
|
}
|
|
if got := pathOf(t, s, a.ID); got != before {
|
|
t.Fatalf("失败切换不应改动路径: %q -> %q", before, got)
|
|
}
|
|
}
|
|
|
|
// TestStar_PinnedTop 验收:标星账号在列表置顶;OnlyStarred 过滤。
|
|
func TestStar_PinnedTop(t *testing.T) {
|
|
s := newSvc28(t)
|
|
ctx := context.Background()
|
|
u1 := create28(t, s, "d28-s1", 0)
|
|
u2 := create28(t, s, "d28-s2", 0)
|
|
u3 := create28(t, s, "d28-s3", 0)
|
|
|
|
if err := s.SetStar(ctx, u1.ID, true); err != nil { // 最早创建但标星
|
|
t.Fatal(err)
|
|
}
|
|
users, _, err := s.ListAccounts(ctx, &ListQuery{Page: 1, PageSize: 10, Status: -1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if users[0].ID != u1.ID || users[0].IsStarred != 1 {
|
|
t.Fatalf("标星应置顶, 首行 id=%d starred=%d", users[0].ID, users[0].IsStarred)
|
|
}
|
|
if users[1].ID != u3.ID { // 其余按 id 倒序
|
|
t.Fatalf("次行应 id 倒序, got %d", users[1].ID)
|
|
}
|
|
only, total, _ := s.ListAccounts(ctx, &ListQuery{Page: 1, PageSize: 10, Status: -1, OnlyStarred: true})
|
|
if total != 1 || only[0].ID != u1.ID {
|
|
t.Fatalf("OnlyStarred 过滤异常: total=%d", total)
|
|
}
|
|
if err := s.SetStar(ctx, u1.ID, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
users, _, _ = s.ListAccounts(ctx, &ListQuery{Page: 1, PageSize: 10, Status: -1})
|
|
if users[0].ID == u1.ID {
|
|
t.Fatal("取消标星后不应置顶")
|
|
}
|
|
if err := s.SetStar(ctx, 99999, true); err == nil {
|
|
t.Fatal("不存在账号应 404")
|
|
}
|
|
_ = u2
|
|
}
|
|
|
|
// TestSvcTeam_Bind 绑定/变更/清除 + 长度校验。
|
|
func TestSvcTeam_Bind(t *testing.T) {
|
|
s := newSvc28(t)
|
|
ctx := context.Background()
|
|
u := create28(t, s, "d28-team", 0)
|
|
|
|
if err := s.SetSvcTeam(ctx, u.ID, "华南一组"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fresh, _, err := s.GetAccount(ctx, u.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fresh.SvcTeam != "华南一组" {
|
|
t.Fatalf("svc_team = %q", fresh.SvcTeam)
|
|
}
|
|
if err := s.SetSvcTeam(ctx, u.ID, " "); err != nil { // 清除
|
|
t.Fatal(err)
|
|
}
|
|
if fresh, _, _ = s.GetAccount(ctx, u.ID); fresh.SvcTeam != "" {
|
|
t.Fatalf("清除失败: %q", fresh.SvcTeam)
|
|
}
|
|
if err := s.SetSvcTeam(ctx, u.ID, string(make([]byte, 65))); err == nil {
|
|
t.Fatal("超长应 400")
|
|
}
|
|
if err := s.SetSvcTeam(ctx, 99999, "x"); err == nil {
|
|
t.Fatal("不存在账号应 404")
|
|
}
|
|
}
|
|
|
|
// TestRealname_StateMachine 验收:审核流转状态机合法(提交→通过终态;驳回→可重提)。
|
|
func TestRealname_StateMachine(t *testing.T) {
|
|
s := newSvc28(t)
|
|
ctx := context.Background()
|
|
u := create28(t, s, "d28-real", 0)
|
|
|
|
if err := s.RealnameReview(ctx, u.ID, true, ""); err == nil { // 未提交直接审核
|
|
t.Fatal("未提交资料不可审核")
|
|
}
|
|
if err := s.RealnameSubmit(ctx, u.ID, "张三", "123"); err == nil {
|
|
t.Fatal("证件号过短应 400")
|
|
}
|
|
if err := s.RealnameSubmit(ctx, u.ID, "张三", "350203199001011234"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, _ := s.GetRealname(ctx, u.ID)
|
|
if info.Status != RealnamePending || info.IDCard != MaskIDCard("350203199001011234") {
|
|
t.Fatalf("提交后状态/脱敏异常: %+v", info)
|
|
}
|
|
if err := s.RealnameReview(ctx, u.ID, false, "证件照片模糊"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info, _ = s.GetRealname(ctx, u.ID); info.Status != RealnameRejected {
|
|
t.Fatalf("驳回后状态 = %d", info.Status)
|
|
}
|
|
if err := s.RealnameSubmit(ctx, u.ID, "张三", "350203199001011235"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.RealnameReview(ctx, u.ID, true, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info, _ = s.GetRealname(ctx, u.ID); info.Status != RealnameApproved {
|
|
t.Fatalf("通过后状态 = %d", info.Status)
|
|
}
|
|
if err := s.RealnameSubmit(ctx, u.ID, "张三", "350203199001011236"); err == nil {
|
|
t.Fatal("已通过不可重复提交")
|
|
}
|
|
if err := s.RealnameReview(ctx, u.ID, true, ""); err == nil {
|
|
t.Fatal("终态不可再审核")
|
|
}
|
|
if err := s.RealnameSubmit(ctx, 99999, "李四", "350203199001011237"); err == nil {
|
|
t.Fatal("不存在账号应 404")
|
|
}
|
|
fresh, _, _ := s.GetAccount(ctx, u.ID)
|
|
if fresh.Remark != "实名审核通过" {
|
|
t.Fatalf("审核备注 = %q", fresh.Remark)
|
|
}
|
|
}
|
|
|
|
// TestMaskIDCard 脱敏边界。
|
|
func TestMaskIDCard(t *testing.T) {
|
|
if MaskIDCard("1234567890") != "1234**7890" {
|
|
t.Fatalf("10 位脱敏 = %q", MaskIDCard("1234567890"))
|
|
}
|
|
if MaskIDCard("123") != "***" {
|
|
t.Fatalf("短串应全掩码: %q", MaskIDCard("123"))
|
|
}
|
|
if MaskIDCard("350203199001011234") != "3502**********1234" {
|
|
t.Fatalf("18 位脱敏 = %q", MaskIDCard("350203199001011234"))
|
|
}
|
|
}
|