feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func TestRepositoryUnreadCountAndMarkRead(t *testing.T) {
|
||||
@@ -191,3 +192,53 @@ func TestNotificationTenantIsolation(t *testing.T) {
|
||||
t.Fatalf("autoNotif.DistributionSiteID = %v, want %d", autoNotif.DistributionSiteID, siteID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationTenantScopeAndAppendDerivation(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Tenant{}, &model.User{}, &model.Notification{}); err != nil {
|
||||
t.Fatalf("AutoMigrate() error = %v", err)
|
||||
}
|
||||
tenantA := model.Tenant{Code: "notify-a", Name: "通知 A", Status: "active"}
|
||||
tenantB := model.Tenant{Code: "notify-b", Name: "通知 B", Status: "active"}
|
||||
if err := db.Create(&tenantA).Error; err != nil {
|
||||
t.Fatalf("创建租户 A: %v", err)
|
||||
}
|
||||
if err := db.Create(&tenantB).Error; err != nil {
|
||||
t.Fatalf("创建租户 B: %v", err)
|
||||
}
|
||||
userA := model.User{TenantID: tenantA.ID, Phone: "13700000001"}
|
||||
userB := model.User{TenantID: tenantB.ID, Phone: "13700000002"}
|
||||
if err := db.Create(&userA).Error; err != nil {
|
||||
t.Fatalf("创建用户 A: %v", err)
|
||||
}
|
||||
if err := db.Create(&userB).Error; err != nil {
|
||||
t.Fatalf("创建用户 B: %v", err)
|
||||
}
|
||||
if err := Append(db,
|
||||
Entry{UserID: userA.ID, Title: "A 通知"},
|
||||
Entry{UserID: userB.ID, Title: "B 通知"},
|
||||
); err != nil {
|
||||
t.Fatalf("Append(): %v", err)
|
||||
}
|
||||
var rows []model.Notification
|
||||
if err := db.Order("id ASC").Find(&rows).Error; err != nil {
|
||||
t.Fatalf("查询通知: %v", err)
|
||||
}
|
||||
if len(rows) != 2 || rows[0].TenantID != tenantA.ID || rows[1].TenantID != tenantB.ID {
|
||||
t.Fatalf("通知租户回填异常: %+v", rows)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantA.ID})
|
||||
listA, err := repo.List(ctxA, userA.ID, 1, 20)
|
||||
if err != nil {
|
||||
t.Fatalf("查询租户 A 通知: %v", err)
|
||||
}
|
||||
if listA.Total != 1 {
|
||||
t.Fatalf("租户 A 通知数 = %d, want 1", listA.Total)
|
||||
}
|
||||
ctxWrong := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantB.ID})
|
||||
if err := repo.MarkRead(ctxWrong, userA.ID, rows[0].ID); !errors.Is(err, ErrNotificationNotFound) {
|
||||
t.Fatalf("跨租户标记已读 error = %v, want not found", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user