97 lines
3.4 KiB
Go
97 lines
3.4 KiB
Go
package csmanualentry
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
)
|
|
|
|
func manualEntryTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func TestManualEntriesAndOptionsAreScopedByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
tenants := []model.Tenant{
|
|
{Code: "manual_entry_a", Name: "录单A站", Status: "active"},
|
|
{Code: "manual_entry_b", Name: "录单B站", Status: "active"},
|
|
}
|
|
if err := db.Create(&tenants).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := model.AdminUser{Username: "manual-cs", Nickname: "录单客服", PasswordHash: "x", Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewRepository(db)
|
|
ctxA := manualEntryTenantContext(tenants[0].ID)
|
|
ctxB := manualEntryTenantContext(tenants[1].ID)
|
|
|
|
entryA, err := repo.Create(ctxA, admin.ID, admin.Nickname, admin.Username, "SAME-ENTRY", EntryGroupCollect, "平台A", "客服A", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entryB, err := repo.Create(ctxB, admin.ID, admin.Nickname, admin.Username, "SAME-ENTRY", EntryGroupCollect, "平台B", "客服B", "")
|
|
if err != nil {
|
|
t.Fatalf("same entry number in another tenant: %v", err)
|
|
}
|
|
if entryA.ID == entryB.ID || entryA.TenantID != tenants[0].ID || entryB.TenantID != tenants[1].ID {
|
|
t.Fatalf("tenant entries A=%+v B=%+v", entryA, entryB)
|
|
}
|
|
|
|
listA, err := repo.List(ctxA, ListQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listB, err := repo.List(ctxB, ListQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if listA.Total != 1 || listA.Items[0].ID != entryA.ID || listB.Total != 1 || listB.Items[0].ID != entryB.ID {
|
|
t.Fatalf("tenant manual entry lists A=%+v B=%+v", listA, listB)
|
|
}
|
|
if _, err := repo.FindByID(ctxB, entryA.ID); !errors.Is(err, ErrEntryNotFound) {
|
|
t.Fatalf("cross-tenant find error = %v", err)
|
|
}
|
|
if _, err := repo.Update(ctxB, entryA.ID, "HACK", EntryGroupSell, "", "", ""); !errors.Is(err, ErrEntryNotFound) {
|
|
t.Fatalf("cross-tenant update error = %v", err)
|
|
}
|
|
if err := repo.Delete(ctxB, entryA.ID); !errors.Is(err, ErrEntryNotFound) {
|
|
t.Fatalf("cross-tenant delete error = %v", err)
|
|
}
|
|
|
|
optionA, err := repo.CreateOption(ctxA, OptionTypePlatform, "同名平台", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
optionB, err := repo.CreateOption(ctxB, OptionTypePlatform, "同名平台", "")
|
|
if err != nil {
|
|
t.Fatalf("same option name in another tenant: %v", err)
|
|
}
|
|
if optionA.ID == optionB.ID || optionA.TenantID != tenants[0].ID || optionB.TenantID != tenants[1].ID {
|
|
t.Fatalf("tenant options A=%+v B=%+v", optionA, optionB)
|
|
}
|
|
optionsA, err := repo.ListAllOptions(ctxA)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
optionsB, err := repo.ListAllOptions(ctxB)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(optionsA.Platforms) != 1 || optionsA.Platforms[0].ID != optionA.ID ||
|
|
len(optionsB.Platforms) != 1 || optionsB.Platforms[0].ID != optionB.ID {
|
|
t.Fatalf("tenant options list A=%+v B=%+v", optionsA, optionsB)
|
|
}
|
|
if _, err := repo.UpdateOption(ctxB, optionA.ID, "越权", ""); !errors.Is(err, ErrOptionNotFound) {
|
|
t.Fatalf("cross-tenant option update error = %v", err)
|
|
}
|
|
if err := repo.DeleteOption(ctxB, optionA.ID); !errors.Is(err, ErrOptionNotFound) {
|
|
t.Fatalf("cross-tenant option delete error = %v", err)
|
|
}
|
|
}
|