feat(tenant): 增加租户基础与域名解析

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent 79b8bc3ac1
commit ca57f604bf
24 changed files with 1329 additions and 1931 deletions
+72
View File
@@ -3,10 +3,12 @@ package router
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"hfb_sys/backend/internal/config"
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
"go.uber.org/zap"
)
@@ -56,6 +58,8 @@ func TestExtractedAdminRoutesRemainRegisteredWithoutDuplicates(t *testing.T) {
"PUT /api/admin/roles/:id/permissions",
"PUT /api/admin/admin-users/:id/password",
"POST /api/admin/announcements/:id/publish",
"GET /api/site/context",
"GET /api/site/check-domain",
"GET /api/distribution/sites/:id/listings",
"GET /api/distribution/sites/:id/wallet",
} {
@@ -64,3 +68,71 @@ func TestExtractedAdminRoutesRemainRegisteredWithoutDuplicates(t *testing.T) {
}
}
}
func TestSiteContextRouteUsesTenantHost(t *testing.T) {
db := database.NewTestDB()
tenant := model.Tenant{
Code: "jieyou",
Name: "解忧商行",
Status: "active",
ProfileJSON: []byte(`{"site_name":"解忧商行","logo_url":"https://cdn.example/logo.png"}`),
FeaturesJSON: []byte(
`{"rental_listing":true}`,
),
}
if err := db.Create(&tenant).Error; err != nil {
t.Fatalf("create tenant: %v", err)
}
if err := db.Create(&model.TenantDomain{
TenantID: tenant.ID,
Host: "zuhaox.com",
DomainType: "storefront",
Status: "active",
}).Error; err != nil {
t.Fatalf("create tenant domain: %v", err)
}
if err := db.Create(&model.DistributionSite{
SiteCode: "legacy_dist",
SiteName: "历史分销站",
Domain: "legacy.example.com",
Status: "active",
}).Error; err != nil {
t.Fatalf("create legacy distribution site: %v", err)
}
engine := New(testConfig(), Dependencies{DB: db}, zap.NewNop())
req := httptest.NewRequest(http.MethodGet, "/api/site/context", nil)
req.Host = "zuhaox.com"
rec := httptest.NewRecorder()
engine.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String())
}
body := rec.Body.String()
for _, want := range []string{`"tenant_code":"jieyou"`, `"site_name":"解忧商行"`, `"rental_listing":true`} {
if !strings.Contains(body, want) {
t.Fatalf("body missing %s: %s", want, body)
}
}
req = httptest.NewRequest(http.MethodGet, "/api/site/context", nil)
req.Host = "unknown.example.com"
rec = httptest.NewRecorder()
engine.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("unknown host status = %d, want 404", rec.Code)
}
for domain, wantStatus := range map[string]int{
"zuhaox.com": http.StatusOK,
"legacy.example.com": http.StatusOK,
"unknown.example.com": http.StatusNotFound,
} {
req = httptest.NewRequest(http.MethodGet, "/api/site/check-domain?domain="+domain, nil)
rec = httptest.NewRecorder()
engine.ServeHTTP(rec, req)
if rec.Code != wantStatus {
t.Fatalf("check domain %s status = %d, want %d; body=%s", domain, rec.Code, wantStatus, rec.Body.String())
}
}
}