Files
user-system/internal/api/handler/settings_handler_test.go
long-agent 582ad7a069 test: add comprehensive test coverage and improve code quality
- Add new test files for auth, service, and handler modules
- Improve test organization and coverage
- Refactor code for better maintainability
- Add captcha, settings, stats, and theme handler tests
- Add auth module tests (CAS, OAuth, password, SSO, state)
- Add service layer tests for auth, export, permissions, roles
- All Go tests pass (exit code 0)
- All frontend tests pass (325 tests in 59 files)
2026-04-17 20:43:50 +08:00

50 lines
1.3 KiB
Go

package handler_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/user-management-system/internal/api/handler"
"github.com/user-management-system/internal/service"
)
// =============================================================================
// Settings Handler Tests - TDD approach
// =============================================================================
func TestSettingsHandler_GetSettings(t *testing.T) {
gin.SetMode(gin.TestMode)
settingsSvc := service.NewSettingsService()
h := handler.NewSettingsHandler(settingsSvc)
t.Run("获取系统设置成功", func(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/api/v1/admin/settings", nil)
h.GetSettings(c)
if w.Code != http.StatusOK {
t.Errorf("期望状态码 %d, 得到 %d", http.StatusOK, w.Code)
}
var resp map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("解析响应失败: %v", err)
}
if resp["code"].(float64) != 0 {
t.Errorf("期望 code=0, 得到 %v", resp["code"])
}
data := resp["data"].(map[string]interface{})
if data["system"] == nil {
t.Error("system 不应为空")
}
})
}