feat(tenant): 增加租户基础与域名解析
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package tenancy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
)
|
||||
|
||||
func setupTenantRepoTest(t *testing.T) (*Repository, model.Tenant, model.Tenant) {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
dachui := model.Tenant{Code: "dachui", Name: "大锤商行", Status: "active"}
|
||||
jieyou := model.Tenant{Code: "jieyou", Name: "解忧商行", Status: "active"}
|
||||
if err := db.Create(&dachui).Error; err != nil {
|
||||
t.Fatalf("create dachui tenant: %v", err)
|
||||
}
|
||||
if err := db.Create(&jieyou).Error; err != nil {
|
||||
t.Fatalf("create jieyou tenant: %v", err)
|
||||
}
|
||||
return NewRepository(db), dachui, jieyou
|
||||
}
|
||||
|
||||
func TestResolveHostNormalizesAndIgnoresInactiveDomains(t *testing.T) {
|
||||
repo, dachui, jieyou := setupTenantRepoTest(t)
|
||||
if err := repo.UpsertDomain(t.Context(), "dachui", "hao858.com", "storefront"); err != nil {
|
||||
t.Fatalf("upsert dachui domain: %v", err)
|
||||
}
|
||||
if err := repo.db.Create(&model.TenantDomain{
|
||||
TenantID: jieyou.ID,
|
||||
Host: "disabled.example.com",
|
||||
DomainType: "storefront",
|
||||
Status: "disabled",
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("create disabled domain: %v", err)
|
||||
}
|
||||
|
||||
scope, err := repo.ResolveHost(t.Context(), "HAO858.COM:443")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve host: %v", err)
|
||||
}
|
||||
if scope == nil || scope.TenantID != dachui.ID || scope.TenantCode != "dachui" {
|
||||
t.Fatalf("resolved scope = %#v, want dachui", scope)
|
||||
}
|
||||
|
||||
scope, err = repo.ResolveHost(t.Context(), "disabled.example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve disabled host: %v", err)
|
||||
}
|
||||
if scope != nil {
|
||||
t.Fatalf("disabled domain resolved to %#v", scope)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapDomainsIsIdempotentAndRejectsHostConflict(t *testing.T) {
|
||||
repo, _, _ := setupTenantRepoTest(t)
|
||||
cfg := BootstrapConfig{PrimaryDomain: "hao858.com", JieyouDomain: "zuhaox.com"}
|
||||
if err := BootstrapDomains(t.Context(), repo.db, cfg); err != nil {
|
||||
t.Fatalf("bootstrap domains: %v", err)
|
||||
}
|
||||
if err := BootstrapDomains(t.Context(), repo.db, cfg); err != nil {
|
||||
t.Fatalf("bootstrap domains second run: %v", err)
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := repo.db.Model(&model.TenantDomain{}).Count(&count).Error; err != nil {
|
||||
t.Fatalf("count domains: %v", err)
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("domain count = %d, want 3", count)
|
||||
}
|
||||
if err := repo.UpsertDomain(t.Context(), "jieyou", "hao858.com", "storefront"); !errors.Is(err, ErrDomainConflict) {
|
||||
t.Fatalf("conflicting domain error = %v, want ErrDomainConflict", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCode(t *testing.T) {
|
||||
repo, dachui, _ := setupTenantRepoTest(t)
|
||||
scope, err := repo.ResolveCode(t.Context(), " DAC HUI ")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve empty code: %v", err)
|
||||
}
|
||||
if scope != nil {
|
||||
t.Fatalf("invalid code resolved to %#v", scope)
|
||||
}
|
||||
scope, err = repo.ResolveCode(t.Context(), "DACHUI")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve code: %v", err)
|
||||
}
|
||||
if scope == nil || scope.TenantID != dachui.ID {
|
||||
t.Fatalf("resolved scope = %#v, want dachui", scope)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user