主要变更: - 新增B端认证系统(后台+H5):登录、登出、Token刷新、密码修改 - 完善商户管理和商户账号管理功能 - 补全单元测试(ShopService: 72.5%, ShopAccountService: 79.8%) - 新增集成测试(商户管理+商户账号管理) - 归档OpenSpec提案(add-shop-account-management, implement-b-end-auth-system) - 完善文档(使用指南、API文档、认证架构说明) 测试统计: - 13个测试套件,37个测试用例,100%通过率 - 平均覆盖率76.2%,达标 OpenSpec验证:通过(strict模式)
170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/pkg/auth"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CreateTestAccount 创建测试账号
|
|
// userType: 1=超级管理员, 2=平台用户, 3=代理账号, 4=企业账号
|
|
func CreateTestAccount(t *testing.T, db *gorm.DB, username, password string, userType int, shopID, enterpriseID *uint) *model.Account {
|
|
t.Helper()
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
require.NoError(t, err)
|
|
|
|
phone := "13800000000"
|
|
if len(username) >= 8 {
|
|
phone = "138" + username[len(username)-8:]
|
|
} else {
|
|
phone = "138" + username + "00000000"
|
|
if len(phone) > 11 {
|
|
phone = phone[:11]
|
|
}
|
|
}
|
|
|
|
account := &model.Account{
|
|
BaseModel: model.BaseModel{
|
|
Creator: 1,
|
|
Updater: 1,
|
|
},
|
|
Username: username,
|
|
Phone: phone,
|
|
Password: string(hashedPassword),
|
|
UserType: userType,
|
|
ShopID: shopID,
|
|
EnterpriseID: enterpriseID,
|
|
Status: 1,
|
|
}
|
|
|
|
err = db.Create(account).Error
|
|
require.NoError(t, err)
|
|
|
|
return account
|
|
}
|
|
|
|
// GenerateTestToken 为测试账号生成 token
|
|
func GenerateTestToken(t *testing.T, rdb *redis.Client, account *model.Account, device string) (accessToken, refreshToken string) {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
|
|
var shopID, enterpriseID uint
|
|
if account.ShopID != nil {
|
|
shopID = *account.ShopID
|
|
}
|
|
if account.EnterpriseID != nil {
|
|
enterpriseID = *account.EnterpriseID
|
|
}
|
|
|
|
tokenInfo := &auth.TokenInfo{
|
|
UserID: account.ID,
|
|
UserType: account.UserType,
|
|
ShopID: shopID,
|
|
EnterpriseID: enterpriseID,
|
|
Username: account.Username,
|
|
Device: device,
|
|
IP: "127.0.0.1",
|
|
}
|
|
|
|
tokenManager := auth.NewTokenManager(rdb, 24*time.Hour, 7*24*time.Hour)
|
|
accessToken, refreshToken, err := tokenManager.GenerateTokenPair(ctx, tokenInfo)
|
|
require.NoError(t, err)
|
|
|
|
return accessToken, refreshToken
|
|
}
|
|
|
|
// CreateSuperAdmin 创建或获取超级管理员测试账号
|
|
func CreateSuperAdmin(t *testing.T, db *gorm.DB) *model.Account {
|
|
t.Helper()
|
|
|
|
var existing model.Account
|
|
err := db.Where("user_type = ?", constants.UserTypeSuperAdmin).First(&existing).Error
|
|
if err == nil {
|
|
return &existing
|
|
}
|
|
|
|
return CreateTestAccount(t, db, "superadmin", "password123", constants.UserTypeSuperAdmin, nil, nil)
|
|
}
|
|
|
|
// CreatePlatformUser 创建平台用户测试账号
|
|
func CreatePlatformUser(t *testing.T, db *gorm.DB) *model.Account {
|
|
t.Helper()
|
|
return CreateTestAccount(t, db, "platformuser", "password123", constants.UserTypePlatform, nil, nil)
|
|
}
|
|
|
|
// CreateAgentUser 创建代理账号测试账号
|
|
func CreateAgentUser(t *testing.T, db *gorm.DB, shopID uint) *model.Account {
|
|
t.Helper()
|
|
return CreateTestAccount(t, db, "agentuser", "password123", constants.UserTypeAgent, &shopID, nil)
|
|
}
|
|
|
|
// CreateEnterpriseUser 创建企业账号测试账号
|
|
func CreateEnterpriseUser(t *testing.T, db *gorm.DB, enterpriseID uint) *model.Account {
|
|
t.Helper()
|
|
return CreateTestAccount(t, db, "enterpriseuser", "password123", constants.UserTypeEnterprise, nil, &enterpriseID)
|
|
}
|
|
|
|
// CreateTestShop 创建测试商户
|
|
func CreateTestShop(t *testing.T, db *gorm.DB, name, code string, level int, parentID *uint) *model.Shop {
|
|
t.Helper()
|
|
|
|
shop := &model.Shop{
|
|
BaseModel: model.BaseModel{
|
|
Creator: 1,
|
|
Updater: 1,
|
|
},
|
|
ShopName: name,
|
|
ShopCode: code,
|
|
Level: level,
|
|
Status: 1,
|
|
}
|
|
|
|
if parentID != nil {
|
|
shop.ParentID = parentID
|
|
}
|
|
|
|
err := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
return shop
|
|
}
|
|
|
|
// SetupAuthMiddleware 设置认证中间件(用于集成测试)
|
|
func SetupAuthMiddleware(t *testing.T, tokenManager *auth.TokenManager, allowedUserTypes []int) func(token string) bool {
|
|
t.Helper()
|
|
|
|
return func(token string) bool {
|
|
ctx := context.Background()
|
|
tokenInfo, err := tokenManager.ValidateAccessToken(ctx, token)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// 检查用户类型
|
|
if len(allowedUserTypes) > 0 {
|
|
allowed := false
|
|
for _, userType := range allowedUserTypes {
|
|
if tokenInfo.UserType == userType {
|
|
allowed = true
|
|
break
|
|
}
|
|
}
|
|
if !allowed {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|