Scert/backend/internal/httpapi/password_reset_local_test.go

47 lines
1.6 KiB
Go

package httpapi
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestPasswordResetResolutionOnlyOffersLocalAdminCommand(t *testing.T) {
cfg := readyTestConfig(t.TempDir())
cfg.Admin.Username = "owner"
cfg.Admin.Email = "owner@example.test"
server := New(Dependencies{Config: cfg, Repository: &stubDomainRepository{}})
for _, identifier := range []string{"owner", "owner@example.test"} {
t.Run(identifier, func(t *testing.T) {
body, err := json.Marshal(map[string]string{"identifier": identifier})
if err != nil {
t.Fatal(err)
}
request := httptest.NewRequest(http.MethodPost, "/api/v1/password-reset/resolve", strings.NewReader(string(body)))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("查询失败:%d %s", response.Code, response.Body.String())
}
var payload map[string]any
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if payload["account_type"] != "super_admin" || payload["action"] != "local_admin_command" {
t.Fatalf("超级管理员没有返回本机命令指引:%v", payload)
}
for _, field := range []string{"reset_path", "reset_url", "token", "nonce"} {
if _, exists := payload[field]; exists {
t.Fatalf("公开查询泄露了重置入口或会话字段:%s", field)
}
}
if len(response.Result().Cookies()) != 0 {
t.Fatal("公开查询不应创建重置会话")
}
})
}
}