完善服务端交互日志与初始化测试
统一划分 MTOP 与 WebSocket 日志等级,增加 Token 安全指纹和请求耗时诊断;补充 init-admin 临时数据库测试;同步 README 的镜像发布说明。
This commit is contained in:
1 parent
ad224bf4e9
commit
d7abbfb2b6
16 files changed
+274
-74
No files matched your search
+36
-28
@@ -7,8 +7,10 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -32,66 +34,72 @@ func main() {
|
||||
resolved = *dbPath
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if err := run(context.Background(), resolved, bufio.NewReader(os.Stdin), os.Stdout); err != nil {
|
||||
fatalf("初始化管理员失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// run 执行一次初始化流程。把数据库、输入和输出显式注入后,CLI 的核心行为
|
||||
// 可以在临时数据库中回归测试,而不会调用 os.Exit 或污染用户数据。
|
||||
func run(ctx context.Context, resolved string, reader *bufio.Reader, out io.Writer) error {
|
||||
database, dialect, err := db.Open(ctx, resolved)
|
||||
if err != nil {
|
||||
fatalf("打开数据库失败: %v", err)
|
||||
return fmt.Errorf("打开数据库失败: %w", err)
|
||||
}
|
||||
defer database.Close()
|
||||
store := db.NewStore(database, dialect)
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
existing, err := store.Users.GetAdmin(ctx)
|
||||
if err != nil && !isNotFound(err) {
|
||||
fatalf("查询 admin 失败: %v", err)
|
||||
return fmt.Errorf("查询 admin 失败: %w", err)
|
||||
}
|
||||
|
||||
if existing != nil {
|
||||
fmt.Printf("管理员用户 %s 已存在\n", existing.Username)
|
||||
fmt.Print("是否重置管理员密码?(y/N): ")
|
||||
fmt.Fprintf(out, "管理员用户 %s 已存在\n", existing.Username)
|
||||
fmt.Fprint(out, "是否重置管理员密码?(y/N): ")
|
||||
ans, _ := reader.ReadString('\n')
|
||||
if !strings.HasPrefix(strings.ToLower(strings.TrimSpace(ans)), "y") {
|
||||
fmt.Println("跳过初始化")
|
||||
return
|
||||
fmt.Fprintln(out, "跳过初始化")
|
||||
return nil
|
||||
}
|
||||
pw := promptPasswordTwice(reader)
|
||||
pw := promptPasswordTwice(reader, out)
|
||||
if _, err := auth.InitAdmin(ctx, store, "", pw); err != nil {
|
||||
fatalf("重置 admin 密码失败: %v", err)
|
||||
return fmt.Errorf("重置 admin 密码失败: %w", err)
|
||||
}
|
||||
fmt.Printf("重置完成:已更新 %s 的密码\n", existing.Username)
|
||||
return
|
||||
fmt.Fprintf(out, "重置完成:已更新 %s 的密码\n", existing.Username)
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("=== 初始化管理员账号(CLI)===")
|
||||
fmt.Print("请输入管理员邮箱: ")
|
||||
fmt.Fprintln(out, "=== 初始化管理员账号(CLI)===")
|
||||
fmt.Fprint(out, "请输入管理员邮箱: ")
|
||||
email, _ := reader.ReadString('\n')
|
||||
email = strings.TrimSpace(email)
|
||||
if email == "" {
|
||||
fatalf("邮箱不能为空")
|
||||
return errors.New("邮箱不能为空")
|
||||
}
|
||||
pw := promptPasswordTwice(reader)
|
||||
pw := promptPasswordTwice(reader, out)
|
||||
|
||||
created, err := auth.InitAdmin(ctx, store, email, pw)
|
||||
if err != nil {
|
||||
fatalf("创建 admin 用户失败:%v", err)
|
||||
return fmt.Errorf("创建 admin 用户失败:%w", err)
|
||||
}
|
||||
if created {
|
||||
fmt.Println("初始化完成:已创建 admin 用户")
|
||||
fmt.Fprintln(out, "初始化完成:已创建 admin 用户")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// promptPasswordTwice 两次输入密码(不回显)并校验一致性。
|
||||
func promptPasswordTwice(reader *bufio.Reader) string {
|
||||
func promptPasswordTwice(reader *bufio.Reader, out io.Writer) string {
|
||||
for {
|
||||
p1 := readPasswordNoEcho(reader, "请输入管理员密码(不回显): ")
|
||||
p1 := readPasswordNoEcho(reader, "请输入管理员密码(不回显): ", out)
|
||||
if p1 == "" {
|
||||
fmt.Println("密码不能为空")
|
||||
fmt.Fprintln(out, "密码不能为空")
|
||||
continue
|
||||
}
|
||||
p2 := readPasswordNoEcho(reader, "请再次输入管理员密码(不回显): ")
|
||||
p2 := readPasswordNoEcho(reader, "请再次输入管理员密码(不回显): ", out)
|
||||
if p1 != p2 {
|
||||
fmt.Println("两次输入不一致,请重试")
|
||||
fmt.Fprintln(out, "两次输入不一致,请重试")
|
||||
continue
|
||||
}
|
||||
return p1
|
||||
@@ -100,11 +108,11 @@ func promptPasswordTwice(reader *bufio.Reader) string {
|
||||
|
||||
// readPasswordNoEcho 不回显读取密码。非 TTY 时回退到普通读取(如管道),
|
||||
// 共用同一 bufio.Reader 避免缓冲吞掉后续输入。
|
||||
func readPasswordNoEcho(reader *bufio.Reader, prompt string) string {
|
||||
fmt.Print(prompt)
|
||||
func readPasswordNoEcho(reader *bufio.Reader, prompt string, out io.Writer) string {
|
||||
fmt.Fprint(out, prompt)
|
||||
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
b, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Println()
|
||||
fmt.Fprintln(out)
|
||||
if err != nil {
|
||||
fatalf("读取密码失败: %v", err)
|
||||
}
|
||||
@@ -116,7 +124,7 @@ func readPasswordNoEcho(reader *bufio.Reader, prompt string) string {
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func isNotFound(err error) bool { return err == db.ErrNotFound }
|
||||
func isNotFound(err error) bool { return errors.Is(err, db.ErrNotFound) }
|
||||
|
||||
func fatalf(format string, a ...any) {
|
||||
fmt.Fprintf(os.Stderr, format+"\n", a...)
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
func TestRunCreatesAdminInTemporaryDatabase(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "init-admin.db")
|
||||
var out bytes.Buffer
|
||||
err := run(context.Background(), dbPath, bufio.NewReader(strings.NewReader("admin@example.com\nsecret\nsecret\n")), &out)
|
||||
if err != nil {
|
||||
t.Fatalf("run create: %v", err)
|
||||
}
|
||||
if !strings.Contains(out.String(), "初始化完成") {
|
||||
t.Fatalf("missing create confirmation: %s", out.String())
|
||||
}
|
||||
d, dialect, err := db.Open(context.Background(), dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.Close()
|
||||
admin, err := db.NewStore(d, dialect).Users.GetAdmin(context.Background())
|
||||
if err != nil || admin == nil || admin.Email != "admin@example.com" {
|
||||
t.Fatalf("admin=%+v err=%v", admin, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunExistingAdminCanSkipReset(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "init-admin.db")
|
||||
input := "admin@example.com\nsecret\nsecret\n"
|
||||
if err := run(context.Background(), dbPath, bufio.NewReader(strings.NewReader(input)), &bytes.Buffer{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if err := run(context.Background(), dbPath, bufio.NewReader(strings.NewReader("n\n")), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out.String(), "跳过初始化") {
|
||||
t.Fatalf("missing skip confirmation: %s", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunExistingAdminResetsPasswordAfterMismatchRetry(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "init-admin.db")
|
||||
if err := run(context.Background(), dbPath, bufio.NewReader(strings.NewReader("admin@example.com\nold\nold\n")), &bytes.Buffer{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := "y\nnew\nnot-the-same\nnew\nnew\n"
|
||||
if err := run(context.Background(), dbPath, bufio.NewReader(strings.NewReader(input)), &bytes.Buffer{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d, dialect, err := db.Open(context.Background(), dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.Close()
|
||||
store := db.NewStore(d, dialect)
|
||||
if _, ok, _ := store.Users.VerifyAndUpgrade(context.Background(), "admin", "old"); ok {
|
||||
t.Fatal("old password should be invalid")
|
||||
}
|
||||
if _, ok, _ := store.Users.VerifyAndUpgrade(context.Background(), "admin", "new"); !ok {
|
||||
t.Fatal("new password should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunRejectsEmptyEmail(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "init-admin.db")
|
||||
err := run(context.Background(), dbPath, bufio.NewReader(strings.NewReader("\n")), &bytes.Buffer{})
|
||||
if err == nil || !strings.Contains(err.Error(), "邮箱不能为空") {
|
||||
t.Fatalf("err=%v, want empty email error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainCLIEntrypointUsesDatabaseFlag(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "main.db")
|
||||
stdin, writer, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldArgs, oldStdin, oldEnv, oldCommandLine := os.Args, os.Stdin, os.Getenv("DATABASE_URL"), flag.CommandLine
|
||||
defer func() {
|
||||
os.Args, os.Stdin, flag.CommandLine = oldArgs, oldStdin, oldCommandLine
|
||||
_ = os.Setenv("DATABASE_URL", oldEnv)
|
||||
_ = stdin.Close()
|
||||
}()
|
||||
os.Args = []string{"init-admin", "-db", dbPath}
|
||||
os.Stdin = stdin
|
||||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||
flag.CommandLine.SetOutput(io.Discard)
|
||||
_ = os.Unsetenv("DATABASE_URL")
|
||||
go func() {
|
||||
_, _ = writer.Write([]byte("admin@example.com\nmain-secret\nmain-secret\n"))
|
||||
_ = writer.Close()
|
||||
}()
|
||||
main()
|
||||
}
|
||||
Reference in new issue
Block a user