选号网 hfb_show 与主站共用后端,生产环境的 Unknown Host 保护会把未登记的 xh.hao858.com 以 site_not_found 拒绝,导致列表接口 404。 后端启动时读取 CADDY_SHOW_DOMAIN,把选号网域名登记为 dachui 的 showcase 租户域名;与主站/解忧域名重合时保留原类型。避免换库或重建后依赖人工补 SQL。
149 lines
4.6 KiB
Go
149 lines
4.6 KiB
Go
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 TestBootstrapDomainsRegistersShowDomainAsDachuiShowcase(t *testing.T) {
|
|
repo, _, _ := setupTenantRepoTest(t)
|
|
cfg := BootstrapConfig{
|
|
PrimaryDomain: "hao858.com",
|
|
JieyouDomain: "zuhaox.com",
|
|
// CADDY_SHOW_DOMAIN 按约定只填域名(不带协议);重复启动必须幂等。
|
|
ShowDomain: "xh.hao858.com",
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
if err := BootstrapDomains(t.Context(), repo.db, cfg); err != nil {
|
|
t.Fatalf("bootstrap domains run %d: %v", i+1, err)
|
|
}
|
|
}
|
|
|
|
repo.Invalidate()
|
|
scope, err := repo.ResolveHost(t.Context(), "xh.hao858.com")
|
|
if err != nil {
|
|
t.Fatalf("resolve show host: %v", err)
|
|
}
|
|
if scope == nil || scope.TenantCode != "dachui" || scope.DomainType != "showcase" {
|
|
t.Fatalf("show scope = %#v, want dachui/showcase", scope)
|
|
}
|
|
|
|
var count int64
|
|
if err := repo.db.Model(&model.TenantDomain{}).Count(&count).Error; err != nil {
|
|
t.Fatalf("count domains: %v", err)
|
|
}
|
|
if count != 4 {
|
|
t.Fatalf("domain count = %d, want 4", count)
|
|
}
|
|
}
|
|
|
|
func TestBootstrapDomainsKeepsRegisteredTypeWhenShowDomainOverlaps(t *testing.T) {
|
|
repo, _, _ := setupTenantRepoTest(t)
|
|
cfg := BootstrapConfig{
|
|
PrimaryDomain: "hao858.com",
|
|
JieyouDomain: "zuhaox.com",
|
|
// 误把选号网配成解忧域名时,不能让解忧的 storefront 被改写成 dachui/showcase。
|
|
ShowDomain: "zuhaox.com",
|
|
}
|
|
if err := BootstrapDomains(t.Context(), repo.db, cfg); err != nil {
|
|
t.Fatalf("bootstrap domains: %v", err)
|
|
}
|
|
|
|
repo.Invalidate()
|
|
scope, err := repo.ResolveHost(t.Context(), "zuhaox.com")
|
|
if err != nil {
|
|
t.Fatalf("resolve jieyou host: %v", err)
|
|
}
|
|
if scope == nil || scope.TenantCode != "jieyou" || scope.DomainType != "storefront" {
|
|
t.Fatalf("jieyou scope = %#v, want jieyou/storefront", scope)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|