Files
gochat/backend/internal/middleware/feature_flag_test.go
T
Rogee 37e6d77a2d fix: 帮助中心设置页报 Feature 'knowledge_base' is not available
三个问题:

1. router.go: portal 路由检查 FeatureKnowledgeBase("knowledge_base"),
   但账户 feature_flags 中只有 help_center,没有 knowledge_base。
   改为检查 FeatureHelpCenter("help_center")。

2. feature_flag.go: 中间件从 context 读取 feature_flags,但没有任何
   上游中间件设置它,导致 exists=false 直接 403。GoChat 是自托管非 SaaS,
   无 flags 时应放行(所有功能可用)。

3. feature_flag.go: Account.FeatureFlags 存储为 JSON 对象格式
   ({"help_center":true}),但旧代码只解析 JSON 数组格式。
   新增 JSON 对象解析支持。

测试:新增 JSONObjectFormat / JSONObjectFormatDisabled 用例,
更新 NoFeatureFlags 用例为 200(自托管放行)。
2026-07-30 11:40:02 +08:00

96 lines
2.9 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestFeatureFlagCheck_NoFeatureFlags(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(FeatureFlagCheck(FeatureCustomRoles))
r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
// No flags in context = allow (self-hosted mode, all features enabled)
assert.Equal(t, 200, w.Code)
}
func TestFeatureFlagCheck_FeatureEnabled(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(func(c *gin.Context) { c.Set("feature_flags", `["custom_roles","automation"]`); c.Next() })
r.Use(FeatureFlagCheck(FeatureCustomRoles))
r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
}
func TestFeatureFlagCheck_FeatureNotEnabled(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(func(c *gin.Context) { c.Set("feature_flags", `["automation"]`); c.Next() })
r.Use(FeatureFlagCheck(FeatureCustomRoles))
r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
assert.Equal(t, 403, w.Code)
}
func TestFeatureFlagCheck_JSONObjectFormat(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(func(c *gin.Context) {
c.Set("feature_flags", `{"help_center":true,"csat":false}`)
c.Next()
})
r.Use(FeatureFlagCheck(FeatureHelpCenter))
r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
}
func TestFeatureFlagCheck_JSONObjectFormatDisabled(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(func(c *gin.Context) {
c.Set("feature_flags", `{"help_center":true,"csat":false}`)
c.Next()
})
r.Use(FeatureFlagCheck(FeatureCSAT))
r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
assert.Equal(t, 403, w.Code)
}
func TestFeatureFlagCheck_InvalidJSON(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(func(c *gin.Context) { c.Set("feature_flags", "not-json"); c.Next() })
r.Use(FeatureFlagCheck(FeatureCustomRoles))
r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
// Invalid JSON that's not an empty string = treat as no matching flags = 403
assert.Equal(t, 403, w.Code)
}