mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 03:52:26 +08:00
UserRepository.Update and APIKeyRepository.Update rewrote the whole row on every call, regardless of which fields the caller meant to change. Several columns on those tables are maintained by dedicated atomic paths (balance deduction, quota and rate-limit counters, limit adjustments, activity timestamps), so a caller holding a slightly older snapshot could silently roll them back - a lost update. Both methods now take an explicit column mask and persist only the columns the caller declares; everything else keeps its current database value. - All user and API-key call sites declare exactly what they mutate, which turns admin edits and profile saves into genuine partial updates. - Email uniqueness locking/lookup and allowed_groups sync only run when those fields are part of the update. - UserUpdateFields deliberately has no balance/total_recharged members, so Update cannot touch them. New AdjustBalance/SetBalance apply the change in a single statement and return before/after values; admin balance adjustment uses them instead of read-modify-write. - promo_codes.used_count is no longer written by Update; it is only ever incremented by the redemption path. - The billing hot path that marks an API key quota-exhausted writes only status. - Dropped a no-op row write in RevokeAllUserTokens: users has no token_version column, so it persisted nothing while still overwriting concurrently-updated columns. Adds integration coverage that a stale snapshot cannot revert concurrent atomic writes, and unit coverage pinning the column set each entry point declares.
84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// 这些用例锁死"每个入口只声明自己真正要改的列":
|
|
// 任何退回整行回写的改动都会让并发写入被陈旧快照覆盖,并在这里变红。
|
|
|
|
func TestUpdateProfile_OnlyDeclaresRequestedColumns(t *testing.T) {
|
|
username := "renamed"
|
|
tests := []struct {
|
|
name string
|
|
req UpdateProfileRequest
|
|
want UserUpdateFields
|
|
}{
|
|
{
|
|
name: "username only",
|
|
req: UpdateProfileRequest{Username: &username},
|
|
want: UserUpdateFields{Username: true},
|
|
},
|
|
{
|
|
name: "notify settings only",
|
|
req: UpdateProfileRequest{BalanceNotifyEnabled: boolPtr(true)},
|
|
want: UserUpdateFields{BalanceNotifySettings: true},
|
|
},
|
|
{
|
|
name: "username and notify threshold",
|
|
req: UpdateProfileRequest{Username: &username, BalanceNotifyThreshold: float64Ptr(1.5)},
|
|
want: UserUpdateFields{Username: true, BalanceNotifySettings: true},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
repo := &mockUserRepo{getByIDUser: &User{ID: 7, Balance: 0.30, Status: StatusActive}}
|
|
svc := NewUserService(repo, nil, nil, nil)
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), 7, tt.req)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []UserUpdateFields{tt.want}, repo.updateFields)
|
|
})
|
|
}
|
|
}
|
|
|
|
// 只改头像时用户行没有任何列要写,不应产生一次整行更新。
|
|
func TestUpdateProfile_AvatarOnlySkipsUserRowWrite(t *testing.T) {
|
|
repo := &mockUserRepo{getByIDUser: &User{ID: 7, Balance: 0.30}}
|
|
svc := NewUserService(repo, nil, nil, nil)
|
|
|
|
avatar := "https://cdn.example.com/a.png"
|
|
_, err := svc.UpdateProfile(context.Background(), 7, UpdateProfileRequest{AvatarURL: &avatar})
|
|
require.NoError(t, err)
|
|
require.Len(t, repo.upsertAvatarArgs, 1, "avatar must still be stored")
|
|
require.Equal(t, []UserUpdateFields{{}}, repo.updateFields, "no user column should be declared")
|
|
}
|
|
|
|
func TestChangePassword_OnlyDeclaresPasswordHash(t *testing.T) {
|
|
user := &User{ID: 7, Balance: 0.30}
|
|
require.NoError(t, user.SetPassword("old-password"))
|
|
repo := &mockUserRepo{getByIDUser: user}
|
|
svc := NewUserService(repo, nil, nil, nil)
|
|
|
|
err := svc.ChangePassword(context.Background(), 7, ChangePasswordRequest{
|
|
CurrentPassword: "old-password",
|
|
NewPassword: "new-password",
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, []UserUpdateFields{{PasswordHash: true}}, repo.updateFields)
|
|
}
|
|
|
|
func TestUpdateStatus_OnlyDeclaresStatus(t *testing.T) {
|
|
repo := &mockUserRepo{getByIDUser: &User{ID: 7, Balance: 0.30, Status: StatusActive}}
|
|
svc := NewUserService(repo, nil, nil, nil)
|
|
|
|
require.NoError(t, svc.UpdateStatus(context.Background(), 7, StatusDisabled))
|
|
require.Equal(t, []UserUpdateFields{{Status: true}}, repo.updateFields)
|
|
}
|