refactor(account): 统一账号管理API、完善权限检查和操作审计
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m17s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m17s
- 合并 customer_account 和 shop_account 路由到统一的 account 接口 - 新增统一认证接口 (auth handler) - 实现越权防护中间件和权限检查工具函数 - 新增操作审计日志模型和服务 - 更新数据库迁移 (版本 39: account_operation_log 表) - 补充集成测试覆盖权限检查和审计日志场景
This commit is contained in:
@@ -1,433 +0,0 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/customer_account"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createCustomerAccountTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_List(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("查询账号列表-空结果", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
req := &dto.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询账号列表-按用户名筛选", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "列表测试店铺",
|
||||
ShopCode: "SHOP_LIST_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
Username: "测试账号用户",
|
||||
Phone: "13900000001",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Username: "测试账号",
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
|
||||
t.Run("查询账号列表-按店铺筛选", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "筛选测试店铺",
|
||||
ShopCode: "SHOP_FILTER_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
Username: "店铺筛选账号",
|
||||
Phone: "13900000002",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ShopID: &shop.ID,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Create(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("新增代理商账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "新增账号测试店铺",
|
||||
ShopCode: "SHOP_CREATE_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000010",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.CreateCustomerAccountReq{
|
||||
Username: "新代理账号",
|
||||
Phone: "13900000010",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, "新代理账号", result.Username)
|
||||
assert.Equal(t, "13900000010", result.Phone)
|
||||
assert.Equal(t, constants.UserTypeAgent, result.UserType)
|
||||
assert.Equal(t, constants.StatusEnabled, result.Status)
|
||||
})
|
||||
|
||||
t.Run("新增账号-手机号已存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "手机号测试店铺",
|
||||
ShopCode: "SHOP_CREATE_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000011",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req1 := &dto.CreateCustomerAccountReq{
|
||||
Username: "账号一",
|
||||
Phone: "13900000011",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &dto.CreateCustomerAccountReq{
|
||||
Username: "账号二",
|
||||
Phone: "13900000011",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("新增账号-店铺不存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
req := &dto.CreateCustomerAccountReq{
|
||||
Username: "无效店铺账号",
|
||||
Phone: "13900000012",
|
||||
Password: "Test123456",
|
||||
ShopID: 99999,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("新增账号-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &dto.CreateCustomerAccountReq{
|
||||
Username: "未授权账号",
|
||||
Phone: "13900000013",
|
||||
Password: "Test123456",
|
||||
ShopID: 1,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Update(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("编辑账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "编辑账号测试店铺",
|
||||
ShopCode: "SHOP_UPDATE_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000020",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
Username: "待编辑账号",
|
||||
Phone: "13900000020",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := "编辑后账号"
|
||||
updateReq := &dto.UpdateCustomerAccountRequest{
|
||||
Username: &newName,
|
||||
}
|
||||
|
||||
updated, err := service.Update(ctx, created.ID, updateReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "编辑后账号", updated.Username)
|
||||
})
|
||||
|
||||
t.Run("编辑账号-不存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
newName := "不存在账号"
|
||||
updateReq := &dto.UpdateCustomerAccountRequest{
|
||||
Username: &newName,
|
||||
}
|
||||
|
||||
_, err := service.Update(ctx, 99999, updateReq)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("修改密码", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "密码测试店铺",
|
||||
ShopCode: "SHOP_PWD_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000030",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
Username: "密码测试账号",
|
||||
Phone: "13900000030",
|
||||
Password: "OldPass123",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdatePassword(ctx, created.ID, "NewPass456")
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
})
|
||||
|
||||
t.Run("修改不存在账号密码应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
err := service.UpdatePassword(ctx, 99999, "NewPass789")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("禁用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "状态测试店铺",
|
||||
ShopCode: "SHOP_STATUS_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000040",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
Username: "状态测试账号",
|
||||
Phone: "13900000040",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("启用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "启用测试店铺",
|
||||
ShopCode: "SHOP_STATUS_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000041",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
Username: "启用测试账号",
|
||||
Phone: "13900000041",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusEnabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("更新不存在账号状态应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
@@ -1,406 +0,0 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/shop_account"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
// TestShopAccountService_Create 测试创建商户账号
|
||||
func TestShopAccountService_Create(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("创建商户账号成功", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试商户",
|
||||
ShopCode: "TEST_SHOP_001",
|
||||
Level: 1,
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.CreateShopAccountRequest{
|
||||
ShopID: shop.ID,
|
||||
Username: testutils.GenerateUsername("account", 1),
|
||||
Phone: testutils.GeneratePhone("139", 1),
|
||||
Password: "password123",
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotZero(t, result.ID)
|
||||
assert.Equal(t, req.Username, result.Username)
|
||||
assert.Equal(t, constants.UserTypeAgent, result.UserType)
|
||||
assert.Equal(t, shop.ID, result.ShopID)
|
||||
})
|
||||
|
||||
t.Run("创建商户账号-商户不存在应失败", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
req := &dto.CreateShopAccountRequest{
|
||||
ShopID: 99999,
|
||||
Username: testutils.GenerateUsername("account", 2),
|
||||
Phone: testutils.GeneratePhone("139", 2),
|
||||
Password: "password123",
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
|
||||
appErr, ok := err.(*errors.AppError)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, errors.CodeShopNotFound, appErr.Code)
|
||||
})
|
||||
|
||||
t.Run("创建商户账号-用户名重复应失败", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试商户2",
|
||||
ShopCode: "TEST_SHOP_002",
|
||||
Level: 1,
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
username := testutils.GenerateUsername("duplicate", 1)
|
||||
req1 := &dto.CreateShopAccountRequest{
|
||||
ShopID: shop.ID,
|
||||
Username: username,
|
||||
Phone: testutils.GeneratePhone("138", 1),
|
||||
Password: "password123",
|
||||
}
|
||||
_, err = service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &dto.CreateShopAccountRequest{
|
||||
ShopID: shop.ID,
|
||||
Username: username,
|
||||
Phone: testutils.GeneratePhone("138", 2),
|
||||
Password: "password123",
|
||||
}
|
||||
result, err := service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("未授权访问应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &dto.CreateShopAccountRequest{
|
||||
ShopID: 1,
|
||||
Username: "test",
|
||||
Phone: "13800000000",
|
||||
Password: "password123",
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestShopAccountService_Update 测试更新商户账号
|
||||
func TestShopAccountService_Update(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("更新商户账号成功", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试商户",
|
||||
ShopCode: "TEST_SHOP_003",
|
||||
Level: 1,
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
account := &model.Account{
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
Username: testutils.GenerateUsername("olduser", 1),
|
||||
Phone: testutils.GeneratePhone("136", 1),
|
||||
Password: "password123",
|
||||
UserType: constants.UserTypeAgent,
|
||||
ShopID: &shop.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
err = accountStore.Create(ctx, account)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.UpdateShopAccountRequest{
|
||||
Username: testutils.GenerateUsername("newuser", 1),
|
||||
}
|
||||
|
||||
result, err := service.Update(ctx, account.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, req.Username, result.Username)
|
||||
})
|
||||
|
||||
t.Run("更新不存在的账号应失败", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
req := &dto.UpdateShopAccountRequest{
|
||||
Username: "newuser",
|
||||
}
|
||||
|
||||
result, err := service.Update(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("未授权访问应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &dto.UpdateShopAccountRequest{
|
||||
Username: "newuser",
|
||||
}
|
||||
|
||||
result, err := service.Update(ctx, 1, req)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestShopAccountService_UpdatePassword 测试更新密码
|
||||
func TestShopAccountService_UpdatePassword(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("更新密码成功", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试商户",
|
||||
ShopCode: "TEST_SHOP_004",
|
||||
Level: 1,
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
account := &model.Account{
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
Username: testutils.GenerateUsername("pwduser", 1),
|
||||
Phone: testutils.GeneratePhone("135", 1),
|
||||
Password: "oldpassword",
|
||||
UserType: constants.UserTypeAgent,
|
||||
ShopID: &shop.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
err = accountStore.Create(ctx, account)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.UpdateShopAccountPasswordRequest{
|
||||
NewPassword: "newpassword123",
|
||||
}
|
||||
err = service.UpdatePassword(ctx, account.ID, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
updatedAccount, err := accountStore.GetByID(ctx, account.ID)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "oldpassword", updatedAccount.Password)
|
||||
})
|
||||
|
||||
t.Run("更新不存在的账号密码应失败", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
req := &dto.UpdateShopAccountPasswordRequest{
|
||||
NewPassword: "newpassword",
|
||||
}
|
||||
err := service.UpdatePassword(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("未授权访问应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &dto.UpdateShopAccountPasswordRequest{
|
||||
NewPassword: "newpassword",
|
||||
}
|
||||
err := service.UpdatePassword(ctx, 1, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
// TestShopAccountService_UpdateStatus 测试更新状态
|
||||
func TestShopAccountService_UpdateStatus(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("更新状态成功", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试商户",
|
||||
ShopCode: "TEST_SHOP_005",
|
||||
Level: 1,
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
account := &model.Account{
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
Username: testutils.GenerateUsername("statususer", 1),
|
||||
Phone: testutils.GeneratePhone("134", 1),
|
||||
Password: "password",
|
||||
UserType: constants.UserTypeAgent,
|
||||
ShopID: &shop.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
err = accountStore.Create(ctx, account)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.UpdateShopAccountStatusRequest{
|
||||
Status: constants.StatusDisabled,
|
||||
}
|
||||
err = service.UpdateStatus(ctx, account.ID, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
updatedAccount, err := accountStore.GetByID(ctx, account.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, updatedAccount.Status)
|
||||
})
|
||||
|
||||
t.Run("更新不存在的账号状态应失败", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
req := &dto.UpdateShopAccountStatusRequest{
|
||||
Status: constants.StatusDisabled,
|
||||
}
|
||||
err := service.UpdateStatus(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("未授权访问应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &dto.UpdateShopAccountStatusRequest{
|
||||
Status: constants.StatusDisabled,
|
||||
}
|
||||
err := service.UpdateStatus(ctx, 1, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
// TestShopAccountService_List 测试查询商户账号列表
|
||||
func TestShopAccountService_List(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("查询商户账号列表", func(t *testing.T) {
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试商户",
|
||||
ShopCode: "TEST_SHOP_006",
|
||||
Level: 1,
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
for i := 1; i <= 3; i++ {
|
||||
account := &model.Account{
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
Username: testutils.GenerateUsername("listuser", i),
|
||||
Phone: testutils.GeneratePhone("133", i),
|
||||
Password: "password",
|
||||
UserType: constants.UserTypeAgent,
|
||||
ShopID: &shop.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
err = accountStore.Create(ctx, account)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
req := &dto.ShopAccountListRequest{
|
||||
ShopID: &shop.ID,
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
accounts, total, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, len(accounts), 3)
|
||||
assert.GreaterOrEqual(t, total, int64(3))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user