Files
wangjia 68608d6471
ci-pangolin / Lint — shellcheck (pull_request) Successful in 13s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (pull_request) Successful in 24s
ci-pangolin / Cleartext Scan — Android 禁明文 (pull_request) Successful in 20s
ci-pangolin / OpenAPI Sync Check (pull_request) Successful in 34s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (pull_request) Successful in 22s
ci-pangolin / Flutter — analyze + test (pull_request) Successful in 36s
ci-pangolin / Codegen Drift — token 生成物未漂移 (pull_request) Successful in 3s
ci-pangolin / DS-flow — 原型/跨端同源/代码色单源闸 (pull_request) Successful in 4s
ci-pangolin / Go — build + test (pull_request) Failing after 13s
ci-pangolin / E2E Smoke — L4 进程级端到端 (pull_request) Failing after 11s
ci-pangolin / Go — integration (mysql/redis testcontainers) (pull_request) Failing after 4m37s
ci-pangolin / Golden — 视觉回归 (全量:components/auth/desktop/tablet) (pull_request) Failing after 19s
feat(server/auth): 新用户注册欢迎通知——注册同事务插 reward 类到账(零孤儿)
CreateUserWithTrial 新增本地 Noticer 接口(同 InsertNoticeTx 签名,参照 reward
包做法避免 notices→auth 循环依赖);SQLStore 加 noticer 字段 + SetNoticer,
nil-safe。注册事务插完 trial 订阅、commit 前,若已注入 noticer 则同事务插一条
type=reward 的双语欢迎通知(标题/正文按 trialDays 参数化),失败即整事务回滚,
与 reward/pay 到账钩子同语义、零孤儿。main.go 在 noticesStore 构造后装配
authStore.SetNoticer(noticesStore)(authStore 提升到 if-block 外声明以跨块可见)。

顺带修复 isDuplicateKey 只认 MySQL 错误文案的 dialect 缺口——sqlite 下重复邮箱
注册此前会落入错误分支返回不透明的「auth: insert user」,而非 ErrEmailTaken;
现同时匹配 sqlite 的 "UNIQUE constraint failed"。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P9G7E3wmAYL9KeYCVZVsqu
2026-07-13 19:03:18 +08:00

81 lines
2.8 KiB
Go

package auth_test
import (
"context"
"fmt"
"testing"
"github.com/wangjia/pangolin/server/internal/auth"
"github.com/wangjia/pangolin/server/internal/notices"
)
// TestCreateUserWithTrial_InsertsWelcomeNotice: 注册同事务应插入一条欢迎到账通知,
// 双语文案按 trialDays 参数化。用真实 notices.Store(而非 fake)验证落库结果;
// 放在外部测试包(package auth_test)是因为 notices 反向 import auth(handler.go),
// 若同包(package auth)测试文件再 import notices 会成环 —— 参照 store_sqlite_test.go
// 里 fakeNoticer 单测的注释。
func TestCreateUserWithTrial_InsertsWelcomeNotice(t *testing.T) {
db := auth.OpenAuthDBForTest(t)
s := auth.NewSQLStore(db)
s.SetNoticer(notices.NewStore(db))
u, err := s.CreateUserWithTrial(context.Background(), "welcome@example.com", "hash", 7)
if err != nil {
t.Fatalf("CreateUserWithTrial: %v", err)
}
var n int
if err := db.QueryRow(`SELECT COUNT(*) FROM notices WHERE type='reward' AND user_id=?`, u.ID).Scan(&n); err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatalf("welcome notice count = %d, want 1", n)
}
var typ, titleZH, titleEN, bodyZH, bodyEN string
row := db.QueryRow(`SELECT type, title_zh, title_en, body_zh, body_en FROM notices WHERE user_id=?`, u.ID)
if err := row.Scan(&typ, &titleZH, &titleEN, &bodyZH, &bodyEN); err != nil {
t.Fatal(err)
}
if typ != "reward" {
t.Fatalf("type = %q, want reward", typ)
}
if titleZH != "欢迎加入 Pangolin 🎉" {
t.Fatalf("titleZH = %q", titleZH)
}
if titleEN != "Welcome to Pangolin 🎉" {
t.Fatalf("titleEN = %q", titleEN)
}
wantBodyZH := fmt.Sprintf("已为你开通 %d 天 PRO 会员,现在就选节点连接,畅享全球网络。", 7)
if bodyZH != wantBodyZH {
t.Fatalf("bodyZH = %q, want %q", bodyZH, wantBodyZH)
}
wantBodyEN := fmt.Sprintf("Your %d-day PRO trial is active. Pick a node and connect to enjoy unrestricted access.", 7)
if bodyEN != wantBodyEN {
t.Fatalf("bodyEN = %q, want %q", bodyEN, wantBodyEN)
}
}
// TestCreateUserWithTrial_DupEmail_NoNotice: 邮箱重复 → 用户插入失败(事务回滚),
// 不应留下任何通知(零孤儿)。
func TestCreateUserWithTrial_DupEmail_NoNotice(t *testing.T) {
db := auth.OpenAuthDBForTest(t)
s := auth.NewSQLStore(db)
s.SetNoticer(notices.NewStore(db))
if _, err := s.CreateUserWithTrial(context.Background(), "dup@example.com", "hash", 7); err != nil {
t.Fatalf("first CreateUserWithTrial: %v", err)
}
if _, err := s.CreateUserWithTrial(context.Background(), "dup@example.com", "hash2", 7); err != auth.ErrEmailTaken {
t.Fatalf("second CreateUserWithTrial err = %v, want ErrEmailTaken", err)
}
var n int
if err := db.QueryRow(`SELECT COUNT(*) FROM notices`).Scan(&n); err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatalf("notices count = %d, want 1 (only the first registration's welcome notice)", n)
}
}