用户软删除后使用记录仍在,但身份(邮箱)被 ent 软删除拦截器隐藏。本次在 三条管理员只读路径定点穿透软删除过滤,并把删除状态传播到前端标记,零新表/ 迁移/回填: - 后端穿透:富化 usage 日志(loadUsers)、用户搜索(ListWithFilters + UserListFilters.IncludeDeleted)、点击详情(GetByIDIncludeDeleted / GetUserIncludeDeleted + getById ?include_deleted 分支) - 状态传播:service.User / dto.User 新增 DeletedAt;SearchUsers 标记 deleted - 前端:表格与余额弹窗展示"已删除"徽标、筛选下拉标注并排序、点击走 include_deleted;新增 i18n admin.usage.userDeletedBadge - 安全:普通用户 usage 仅查本人(无 PII 泄漏);主用户列表与默认 getById 行为不变(已删用户仍 404);仅 admin 搜索设 IncludeDeleted 后端 build / 三态 vet / unit 全量 / 仓储集成全绿;前端 typecheck / vitest / 改动文件 eslint 全清。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
243 lines
7.7 KiB
Go
243 lines
7.7 KiB
Go
//go:build unit
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAdminAuthJWTValidatesTokenVersion(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
cfg := &config.Config{JWT: config.JWTConfig{Secret: "test-secret", ExpireHour: 1}}
|
|
authService := service.NewAuthService(nil, nil, nil, nil, cfg, nil, nil, nil, nil, nil, nil, nil, nil)
|
|
|
|
admin := &service.User{
|
|
ID: 1,
|
|
Email: "admin@example.com",
|
|
Role: service.RoleAdmin,
|
|
Status: service.StatusActive,
|
|
TokenVersion: 2,
|
|
Concurrency: 1,
|
|
}
|
|
|
|
userRepo := &stubUserRepo{
|
|
getByID: func(ctx context.Context, id int64) (*service.User, error) {
|
|
if id != admin.ID {
|
|
return nil, service.ErrUserNotFound
|
|
}
|
|
clone := *admin
|
|
return &clone, nil
|
|
},
|
|
}
|
|
userService := service.NewUserService(userRepo, nil, nil, nil)
|
|
|
|
router := gin.New()
|
|
router.Use(gin.HandlerFunc(NewAdminAuthMiddleware(authService, userService, nil)))
|
|
router.GET("/t", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
})
|
|
|
|
t.Run("token_version_mismatch_rejected", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion - 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusUnauthorized, w.Code)
|
|
require.Contains(t, w.Body.String(), "TOKEN_REVOKED")
|
|
})
|
|
|
|
t.Run("token_version_match_allows", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
})
|
|
|
|
t.Run("websocket_token_version_mismatch_rejected", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion - 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Upgrade", "websocket")
|
|
req.Header.Set("Connection", "Upgrade")
|
|
req.Header.Set("Sec-WebSocket-Protocol", "sub2api-admin, jwt."+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusUnauthorized, w.Code)
|
|
require.Contains(t, w.Body.String(), "TOKEN_REVOKED")
|
|
})
|
|
|
|
t.Run("websocket_token_version_match_allows", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Upgrade", "websocket")
|
|
req.Header.Set("Connection", "Upgrade")
|
|
req.Header.Set("Sec-WebSocket-Protocol", "sub2api-admin, jwt."+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
})
|
|
}
|
|
|
|
type stubUserRepo struct {
|
|
getByID func(ctx context.Context, id int64) (*service.User, error)
|
|
}
|
|
|
|
func (s *stubUserRepo) Create(ctx context.Context, user *service.User) error {
|
|
panic("unexpected Create call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetByID(ctx context.Context, id int64) (*service.User, error) {
|
|
if s.getByID == nil {
|
|
panic("GetByID not stubbed")
|
|
}
|
|
return s.getByID(ctx, id)
|
|
}
|
|
|
|
func (s *stubUserRepo) GetByEmail(ctx context.Context, email string) (*service.User, error) {
|
|
panic("unexpected GetByEmail call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetFirstAdmin(ctx context.Context) (*service.User, error) {
|
|
panic("unexpected GetFirstAdmin call")
|
|
}
|
|
|
|
func (s *stubUserRepo) Update(ctx context.Context, user *service.User) error {
|
|
panic("unexpected Update call")
|
|
}
|
|
|
|
func (s *stubUserRepo) Delete(ctx context.Context, id int64) error {
|
|
panic("unexpected Delete call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetUserAvatar(ctx context.Context, userID int64) (*service.UserAvatar, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *stubUserRepo) UpsertUserAvatar(ctx context.Context, userID int64, input service.UpsertUserAvatarInput) (*service.UserAvatar, error) {
|
|
panic("unexpected UpsertUserAvatar call")
|
|
}
|
|
|
|
func (s *stubUserRepo) DeleteUserAvatar(ctx context.Context, userID int64) error {
|
|
panic("unexpected DeleteUserAvatar call")
|
|
}
|
|
|
|
func (s *stubUserRepo) List(ctx context.Context, params pagination.PaginationParams) ([]service.User, *pagination.PaginationResult, error) {
|
|
panic("unexpected List call")
|
|
}
|
|
|
|
func (s *stubUserRepo) ListWithFilters(ctx context.Context, params pagination.PaginationParams, filters service.UserListFilters) ([]service.User, *pagination.PaginationResult, error) {
|
|
panic("unexpected ListWithFilters call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetLatestUsedAtByUserIDs(ctx context.Context, userIDs []int64) (map[int64]*time.Time, error) {
|
|
panic("unexpected GetLatestUsedAtByUserIDs call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetLatestUsedAtByUserID(ctx context.Context, userID int64) (*time.Time, error) {
|
|
panic("unexpected GetLatestUsedAtByUserID call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateUserLastActiveAt(ctx context.Context, userID int64, activeAt time.Time) error {
|
|
panic("unexpected UpdateUserLastActiveAt call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateBalance(ctx context.Context, id int64, amount float64) error {
|
|
panic("unexpected UpdateBalance call")
|
|
}
|
|
|
|
func (s *stubUserRepo) DeductBalance(ctx context.Context, id int64, amount float64) error {
|
|
panic("unexpected DeductBalance call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateConcurrency(ctx context.Context, id int64, amount int) error {
|
|
panic("unexpected UpdateConcurrency call")
|
|
}
|
|
|
|
func (s *stubUserRepo) BatchSetConcurrency(context.Context, []int64, int) (int, error) { return 0, nil }
|
|
func (s *stubUserRepo) BatchAddConcurrency(context.Context, []int64, int) (int, error) { return 0, nil }
|
|
|
|
func (s *stubUserRepo) ExistsByEmail(ctx context.Context, email string) (bool, error) {
|
|
panic("unexpected ExistsByEmail call")
|
|
}
|
|
|
|
func (s *stubUserRepo) RemoveGroupFromAllowedGroups(ctx context.Context, groupID int64) (int64, error) {
|
|
panic("unexpected RemoveGroupFromAllowedGroups call")
|
|
}
|
|
|
|
func (s *stubUserRepo) RemoveGroupFromUserAllowedGroups(ctx context.Context, userID int64, groupID int64) error {
|
|
panic("unexpected RemoveGroupFromUserAllowedGroups call")
|
|
}
|
|
|
|
func (s *stubUserRepo) AddGroupToAllowedGroups(ctx context.Context, userID int64, groupID int64) error {
|
|
panic("unexpected AddGroupToAllowedGroups call")
|
|
}
|
|
|
|
func (s *stubUserRepo) ListUserAuthIdentities(ctx context.Context, userID int64) ([]service.UserAuthIdentityRecord, error) {
|
|
panic("unexpected ListUserAuthIdentities call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UnbindUserAuthProvider(context.Context, int64, string) error {
|
|
panic("unexpected UnbindUserAuthProvider call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateTotpSecret(ctx context.Context, userID int64, encryptedSecret *string) error {
|
|
panic("unexpected UpdateTotpSecret call")
|
|
}
|
|
|
|
func (s *stubUserRepo) EnableTotp(ctx context.Context, userID int64) error {
|
|
panic("unexpected EnableTotp call")
|
|
}
|
|
|
|
func (s *stubUserRepo) DisableTotp(ctx context.Context, userID int64) error {
|
|
panic("unexpected DisableTotp call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetByIDIncludeDeleted(ctx context.Context, id int64) (*service.User, error) {
|
|
panic("unexpected GetByIDIncludeDeleted call")
|
|
}
|