基于 backend-code-audit 审计报告,修复剩余 P0/P1/P2 共 34 项问题: P0 生产 Bug: - 修复 time.Since(time.Now()) 计时逻辑错误 (P0-03) - generateRandomID 改用 crypto/rand 替代固定索引 (P0-04) - IncrementQuotaUsed 重写为 Ent 原子操作消除 TOCTOU 竞态 (P0-05) 安全加固: - gateway/openai handler 错误响应替换为泛化消息,防止内部信息泄露 (P1-14) - usage_log_repo dateFormat 参数改用白名单映射,防止 SQL 注入 (P1-16) - 默认配置安全加固:sslmode=prefer、response_headers=true、mode=release (P1-18/19, P2-15) 性能优化: - gateway handler 循环内 defer 替换为显式 releaseWait 闭包 (P1-02) - group_repo/promo_code_repo Count 前 Clone 查询避免状态污染 (P1-03) - usage_log_repo 四个查询添加 LIMIT 10000 防止 OOM (P1-07) - GetBatchUsageStats 添加时间范围参数,默认最近 30 天 (P1-10) - ip.go CIDR 预编译为包级变量 (P1-11) - BatchUpdateCredentials 重构为先验证后更新 (P1-13) 缓存一致性: - billing_cache 添加 jitteredTTL 防止缓存雪崩 (P2-10) - DeductUserBalance/UpdateSubscriptionUsage 错误传播修复 (P2-12) - UserService.UpdateBalance 成功后异步失效 billingCache (P2-13) 代码质量: - search 截断改为按 rune 处理,支持多字节字符 (P2-01) - TLS Handshake 改为 HandshakeContext 支持 context 取消 (P2-07) - CORS 预检添加 Access-Control-Max-Age: 86400 (P2-16) 测试覆盖: - 新增 user_service_test.go(UpdateBalance 缓存失效 6 个用例) - 新增 batch_update_credentials_test.go(fail-fast + 类型验证 7 个用例) - 新增 response_transformer_test.go、ip_test.go、usage_log_repo_unit_test.go、search_truncate_test.go - 集成测试:IncrementQuotaUsed 并发测试、billing_cache 错误传播测试 - config_test.go 补充 server.mode/sslmode 默认值断言 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
187 lines
6.7 KiB
Go
187 lines
6.7 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// --- mock: UserRepository ---
|
|
|
|
type mockUserRepo struct {
|
|
updateBalanceErr error
|
|
updateBalanceFn func(ctx context.Context, id int64, amount float64) error
|
|
}
|
|
|
|
func (m *mockUserRepo) Create(context.Context, *User) error { return nil }
|
|
func (m *mockUserRepo) GetByID(context.Context, int64) (*User, error) { return &User{}, nil }
|
|
func (m *mockUserRepo) GetByEmail(context.Context, string) (*User, error) { return &User{}, nil }
|
|
func (m *mockUserRepo) GetFirstAdmin(context.Context) (*User, error) { return &User{}, nil }
|
|
func (m *mockUserRepo) Update(context.Context, *User) error { return nil }
|
|
func (m *mockUserRepo) Delete(context.Context, int64) error { return nil }
|
|
func (m *mockUserRepo) List(context.Context, pagination.PaginationParams) ([]User, *pagination.PaginationResult, error) {
|
|
return nil, nil, nil
|
|
}
|
|
func (m *mockUserRepo) ListWithFilters(context.Context, pagination.PaginationParams, UserListFilters) ([]User, *pagination.PaginationResult, error) {
|
|
return nil, nil, nil
|
|
}
|
|
func (m *mockUserRepo) UpdateBalance(ctx context.Context, id int64, amount float64) error {
|
|
if m.updateBalanceFn != nil {
|
|
return m.updateBalanceFn(ctx, id, amount)
|
|
}
|
|
return m.updateBalanceErr
|
|
}
|
|
func (m *mockUserRepo) DeductBalance(context.Context, int64, float64) error { return nil }
|
|
func (m *mockUserRepo) UpdateConcurrency(context.Context, int64, int) error { return nil }
|
|
func (m *mockUserRepo) ExistsByEmail(context.Context, string) (bool, error) { return false, nil }
|
|
func (m *mockUserRepo) RemoveGroupFromAllowedGroups(context.Context, int64) (int64, error) {
|
|
return 0, nil
|
|
}
|
|
func (m *mockUserRepo) UpdateTotpSecret(context.Context, int64, *string) error { return nil }
|
|
func (m *mockUserRepo) EnableTotp(context.Context, int64) error { return nil }
|
|
func (m *mockUserRepo) DisableTotp(context.Context, int64) error { return nil }
|
|
|
|
// --- mock: APIKeyAuthCacheInvalidator ---
|
|
|
|
type mockAuthCacheInvalidator struct {
|
|
invalidatedUserIDs []int64
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (m *mockAuthCacheInvalidator) InvalidateAuthCacheByKey(context.Context, string) {}
|
|
func (m *mockAuthCacheInvalidator) InvalidateAuthCacheByGroupID(context.Context, int64) {}
|
|
func (m *mockAuthCacheInvalidator) InvalidateAuthCacheByUserID(_ context.Context, userID int64) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.invalidatedUserIDs = append(m.invalidatedUserIDs, userID)
|
|
}
|
|
|
|
// --- mock: BillingCache ---
|
|
|
|
type mockBillingCache struct {
|
|
invalidateErr error
|
|
invalidateCallCount atomic.Int64
|
|
invalidatedUserIDs []int64
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (m *mockBillingCache) GetUserBalance(context.Context, int64) (float64, error) { return 0, nil }
|
|
func (m *mockBillingCache) SetUserBalance(context.Context, int64, float64) error { return nil }
|
|
func (m *mockBillingCache) DeductUserBalance(context.Context, int64, float64) error { return nil }
|
|
func (m *mockBillingCache) InvalidateUserBalance(_ context.Context, userID int64) error {
|
|
m.invalidateCallCount.Add(1)
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.invalidatedUserIDs = append(m.invalidatedUserIDs, userID)
|
|
return m.invalidateErr
|
|
}
|
|
func (m *mockBillingCache) GetSubscriptionCache(context.Context, int64, int64) (*SubscriptionCacheData, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *mockBillingCache) SetSubscriptionCache(context.Context, int64, int64, *SubscriptionCacheData) error {
|
|
return nil
|
|
}
|
|
func (m *mockBillingCache) UpdateSubscriptionUsage(context.Context, int64, int64, float64) error {
|
|
return nil
|
|
}
|
|
func (m *mockBillingCache) InvalidateSubscriptionCache(context.Context, int64, int64) error {
|
|
return nil
|
|
}
|
|
|
|
// --- 测试 ---
|
|
|
|
func TestUpdateBalance_Success(t *testing.T) {
|
|
repo := &mockUserRepo{}
|
|
cache := &mockBillingCache{}
|
|
svc := NewUserService(repo, nil, cache)
|
|
|
|
err := svc.UpdateBalance(context.Background(), 42, 100.0)
|
|
require.NoError(t, err)
|
|
|
|
// 等待异步 goroutine 完成
|
|
require.Eventually(t, func() bool {
|
|
return cache.invalidateCallCount.Load() == 1
|
|
}, 2*time.Second, 10*time.Millisecond, "应异步调用 InvalidateUserBalance")
|
|
|
|
cache.mu.Lock()
|
|
defer cache.mu.Unlock()
|
|
require.Equal(t, []int64{42}, cache.invalidatedUserIDs, "应对 userID=42 失效缓存")
|
|
}
|
|
|
|
func TestUpdateBalance_NilBillingCache_NoPanic(t *testing.T) {
|
|
repo := &mockUserRepo{}
|
|
svc := NewUserService(repo, nil, nil) // billingCache = nil
|
|
|
|
err := svc.UpdateBalance(context.Background(), 1, 50.0)
|
|
require.NoError(t, err, "billingCache 为 nil 时不应 panic")
|
|
}
|
|
|
|
func TestUpdateBalance_CacheFailure_DoesNotAffectReturn(t *testing.T) {
|
|
repo := &mockUserRepo{}
|
|
cache := &mockBillingCache{invalidateErr: errors.New("redis connection refused")}
|
|
svc := NewUserService(repo, nil, cache)
|
|
|
|
err := svc.UpdateBalance(context.Background(), 99, 200.0)
|
|
require.NoError(t, err, "缓存失效失败不应影响主流程返回值")
|
|
|
|
// 等待异步 goroutine 完成(即使失败也应调用)
|
|
require.Eventually(t, func() bool {
|
|
return cache.invalidateCallCount.Load() == 1
|
|
}, 2*time.Second, 10*time.Millisecond, "即使失败也应调用 InvalidateUserBalance")
|
|
}
|
|
|
|
func TestUpdateBalance_RepoError_ReturnsError(t *testing.T) {
|
|
repo := &mockUserRepo{updateBalanceErr: errors.New("database error")}
|
|
cache := &mockBillingCache{}
|
|
svc := NewUserService(repo, nil, cache)
|
|
|
|
err := svc.UpdateBalance(context.Background(), 1, 100.0)
|
|
require.Error(t, err, "repo 失败时应返回错误")
|
|
require.Contains(t, err.Error(), "update balance")
|
|
|
|
// repo 失败时不应触发缓存失效
|
|
time.Sleep(100 * time.Millisecond)
|
|
require.Equal(t, int64(0), cache.invalidateCallCount.Load(),
|
|
"repo 失败时不应调用 InvalidateUserBalance")
|
|
}
|
|
|
|
func TestUpdateBalance_WithAuthCacheInvalidator(t *testing.T) {
|
|
repo := &mockUserRepo{}
|
|
auth := &mockAuthCacheInvalidator{}
|
|
cache := &mockBillingCache{}
|
|
svc := NewUserService(repo, auth, cache)
|
|
|
|
err := svc.UpdateBalance(context.Background(), 77, 300.0)
|
|
require.NoError(t, err)
|
|
|
|
// 验证 auth cache 同步失效
|
|
auth.mu.Lock()
|
|
require.Equal(t, []int64{77}, auth.invalidatedUserIDs)
|
|
auth.mu.Unlock()
|
|
|
|
// 验证 billing cache 异步失效
|
|
require.Eventually(t, func() bool {
|
|
return cache.invalidateCallCount.Load() == 1
|
|
}, 2*time.Second, 10*time.Millisecond)
|
|
}
|
|
|
|
func TestNewUserService_FieldsAssignment(t *testing.T) {
|
|
repo := &mockUserRepo{}
|
|
auth := &mockAuthCacheInvalidator{}
|
|
cache := &mockBillingCache{}
|
|
|
|
svc := NewUserService(repo, auth, cache)
|
|
require.NotNil(t, svc)
|
|
require.Equal(t, repo, svc.userRepo)
|
|
require.Equal(t, auth, svc.authCacheInvalidator)
|
|
require.Equal(t, cache, svc.billingCache)
|
|
}
|