Smail/backend/controllers/setup_contract_test.go
smail f60d3b0880
Some checks are pending
Security Checks / security (push) Waiting to run
整理前后端与测试构建目录并完善配置和安全引导
统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
2026-09-05 19:19:52 +08:00

75 lines
2.3 KiB
Go

package controllers
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"git.pcux.cn/xuans/Smail/config"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
)
func TestSetupReturnsJSONForUnsupportedOperation(t *testing.T) {
request := httptest.NewRequest("POST", "/api/v1/setup", strings.NewReader(`{"step":"unknown","action":"unknown"}`))
response := httptest.NewRecorder()
Setup(&appcontext.Context{}, response, request)
if response.Code != 200 {
t.Fatalf("setup status = %d, want 200", response.Code)
}
if contentType := response.Header().Get("Content-Type"); !strings.HasPrefix(contentType, "application/json") {
t.Fatalf("setup content type = %q, want JSON", contentType)
}
var payload struct {
ErrorNo int `json:"errorNo"`
ErrorMsg string `json:"errorMsg"`
}
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
t.Fatalf("decode setup response: %v", err)
}
if payload.ErrorNo != 100 || payload.ErrorMsg != "不支持的初始化操作" {
t.Fatalf("unexpected setup response: %+v", payload)
}
}
func TestBuildSetupSSLResponseUsesConfiguredProxyPorts(t *testing.T) {
cfg := &config.Config{HttpPort: 9001}
response := buildSetupSSLResponse(
config.SSLTypeUser,
cfg,
config.HTTPConfig{
Port: 9001,
HTTPSPort: 9002,
TLSMode: config.HTTPSTLSModeProxy,
HTTPSEnabled: false,
},
"xuans.com",
true,
)
if response.HTTPPort != 9001 || response.HTTPSPort != 9002 {
t.Fatalf("unexpected configured ports: http=%d https=%d", response.HTTPPort, response.HTTPSPort)
}
if response.TLSMode != config.HTTPSTLSModeProxy || response.HTTPSEnabled {
t.Fatalf("unexpected proxy settings: mode=%q enabled=%v", response.TLSMode, response.HTTPSEnabled)
}
if response.WebDomain != "xuans.com" || !response.Ready {
t.Fatalf("unexpected setup response identity: domain=%q ready=%v", response.WebDomain, response.Ready)
}
}
func TestBuildSetupSSLResponseDefaultsHTTPSPort(t *testing.T) {
response := buildSetupSSLResponse(
config.SSLTypeAutoHTTP,
&config.Config{HttpPort: 9001},
config.HTTPConfig{Port: 9001},
"mail.example.com",
false,
)
if response.HTTPPort != 9001 || response.HTTPSPort != 443 {
t.Fatalf("unexpected default ports: http=%d https=%d", response.HTTPPort, response.HTTPSPort)
}
}