mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-15 23:22:59 +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.
217 lines
6.5 KiB
Go
217 lines
6.5 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type ensureEmailCall struct {
|
|
userID int64
|
|
email string
|
|
}
|
|
|
|
type replaceEmailCall struct {
|
|
userID int64
|
|
oldEmail string
|
|
newEmail string
|
|
}
|
|
|
|
type emailSyncRepoStub struct {
|
|
user *User
|
|
nextID int64
|
|
updateCalls int
|
|
created []*User
|
|
updated []*User
|
|
ensureCalls []ensureEmailCall
|
|
replaceCalls []replaceEmailCall
|
|
ensureErr error
|
|
replaceErr error
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) CreateWithEmailAliasGuard(ctx context.Context, user *User) error {
|
|
return s.Create(ctx, user)
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) Create(_ context.Context, user *User) error {
|
|
if s.nextID != 0 && user.ID == 0 {
|
|
user.ID = s.nextID
|
|
}
|
|
s.created = append(s.created, user)
|
|
s.user = user
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetByID(_ context.Context, _ int64) (*User, error) {
|
|
if s.user == nil {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
cloned := *s.user
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetByEmail(_ context.Context, _ string) (*User, error) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetFirstAdmin(context.Context) (*User, error) {
|
|
return nil, fmt.Errorf("unexpected GetFirstAdmin call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) Update(_ context.Context, user *User, _ UserUpdateFields) error {
|
|
s.updateCalls++
|
|
s.updated = append(s.updated, user)
|
|
s.user = user
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) Delete(context.Context, int64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) GetUserAvatar(context.Context, int64) (*UserAvatar, error) {
|
|
return nil, fmt.Errorf("unexpected GetUserAvatar call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UpsertUserAvatar(context.Context, int64, UpsertUserAvatarInput) (*UserAvatar, error) {
|
|
return nil, fmt.Errorf("unexpected UpsertUserAvatar call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) DeleteUserAvatar(context.Context, int64) error {
|
|
return fmt.Errorf("unexpected DeleteUserAvatar call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) List(context.Context, pagination.PaginationParams) ([]User, *pagination.PaginationResult, error) {
|
|
return nil, nil, fmt.Errorf("unexpected List call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) ListWithFilters(context.Context, pagination.PaginationParams, UserListFilters) ([]User, *pagination.PaginationResult, error) {
|
|
return nil, nil, fmt.Errorf("unexpected ListWithFilters call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetLatestUsedAtByUserIDs(context.Context, []int64) (map[int64]*time.Time, error) {
|
|
return map[int64]*time.Time{}, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetLatestUsedAtByUserID(context.Context, int64) (*time.Time, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UpdateUserLastActiveAt(context.Context, int64, time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UpdateBalance(context.Context, int64, float64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) DeductBalance(context.Context, int64, float64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) UpdateConcurrency(context.Context, int64, int) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) ExistsByEmail(context.Context, string) (bool, error) { return false, nil }
|
|
|
|
func (s *emailSyncRepoStub) ExistsByEmailAlias(context.Context, string) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) AdjustBalance(ctx context.Context, id int64, delta float64) (BalanceChange, error) {
|
|
panic("unexpected AdjustBalance call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) SetBalance(ctx context.Context, id int64, value float64) (BalanceChange, error) {
|
|
panic("unexpected SetBalance call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) RemoveGroupFromAllowedGroups(context.Context, int64) (int64, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) BatchSetConcurrency(context.Context, []int64, int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
func (s *emailSyncRepoStub) BatchAddConcurrency(context.Context, []int64, int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
func (s *emailSyncRepoStub) BatchUpdateLimits(context.Context, []int64, *int, *int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) AddGroupToAllowedGroups(context.Context, int64, int64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) RemoveGroupFromUserAllowedGroups(context.Context, int64, int64) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) ListUserAuthIdentities(context.Context, int64) ([]UserAuthIdentityRecord, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UnbindUserAuthProvider(context.Context, int64, string) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) UpdateTotpSecret(context.Context, int64, *string) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) EnableTotp(context.Context, int64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) DisableTotp(context.Context, int64) error { return nil }
|
|
func (s *emailSyncRepoStub) GetByIDIncludeDeleted(ctx context.Context, id int64) (*User, error) {
|
|
return s.GetByID(ctx, id)
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) EnsureEmailAuthIdentity(_ context.Context, userID int64, email string) error {
|
|
s.ensureCalls = append(s.ensureCalls, ensureEmailCall{userID: userID, email: email})
|
|
return s.ensureErr
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) ReplaceEmailAuthIdentity(_ context.Context, userID int64, oldEmail, newEmail string) error {
|
|
s.replaceCalls = append(s.replaceCalls, replaceEmailCall{
|
|
userID: userID,
|
|
oldEmail: oldEmail,
|
|
newEmail: newEmail,
|
|
})
|
|
return s.replaceErr
|
|
}
|
|
|
|
func TestAdminService_CreateUser_DoesNotReturnPartialSuccessFromEmailIdentityResync(t *testing.T) {
|
|
repo := &emailSyncRepoStub{
|
|
nextID: 55,
|
|
ensureErr: fmt.Errorf("unexpected email resync"),
|
|
}
|
|
svc := &adminServiceImpl{userRepo: repo}
|
|
|
|
user, err := svc.CreateUser(context.Background(), &CreateUserInput{
|
|
Email: "admin-created@example.com",
|
|
Password: "strong-pass",
|
|
})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, user)
|
|
require.Equal(t, int64(55), user.ID)
|
|
require.Empty(t, repo.ensureCalls)
|
|
require.Empty(t, repo.replaceCalls)
|
|
}
|
|
|
|
func TestAdminService_UpdateUser_DoesNotReturnPartialSuccessFromEmailIdentityResync(t *testing.T) {
|
|
repo := &emailSyncRepoStub{
|
|
user: &User{
|
|
ID: 91,
|
|
Email: "before@example.com",
|
|
Role: RoleUser,
|
|
Status: StatusActive,
|
|
Concurrency: 3,
|
|
},
|
|
replaceErr: fmt.Errorf("unexpected email resync"),
|
|
}
|
|
svc := &adminServiceImpl{userRepo: repo}
|
|
|
|
updated, err := svc.UpdateUser(context.Background(), 91, &UpdateUserInput{
|
|
Email: "after@example.com",
|
|
})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, updated)
|
|
require.Equal(t, "after@example.com", updated.Email)
|
|
require.Empty(t, repo.replaceCalls)
|
|
require.Empty(t, repo.ensureCalls)
|
|
}
|