All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
新增功能: - 店铺佣金查询:店铺佣金统计、店铺佣金记录列表、店铺提现记录 - 佣金提现审批:提现申请列表、审批通过、审批拒绝 - 提现配置管理:配置列表、新增配置、获取当前生效配置 - 企业管理:企业列表、创建、更新、删除、获取详情 - 企业卡授权:授权列表、批量授权、批量取消授权、统计 - 客户账号管理:账号列表、创建、更新状态、重置密码 - 我的佣金:佣金统计、佣金记录、提现申请、提现记录 数据库变更: - 扩展 tb_commission_withdrawal_request 新增提现单号等字段 - 扩展 tb_account 新增 is_primary 字段 - 扩展 tb_commission_record 新增 shop_id、balance_after - 扩展 tb_commission_withdrawal_setting 新增每日提现次数限制 - 扩展 tb_iot_card、tb_device 新增 shop_id 冗余字段 - 新建 tb_enterprise_card_authorization 企业卡授权表 - 新建 tb_asset_allocation_record 资产分配记录表 - 数据迁移:owner_type 枚举值 agent 统一为 shop 测试: - 新增 7 个单元测试文件覆盖各服务 - 修复集成测试 Redis 依赖问题
428 lines
12 KiB
Go
428 lines
12 KiB
Go
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/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) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
accountStore := postgres.NewAccountStore(db, redisClient)
|
|
shopStore := postgres.NewShopStore(db, redisClient)
|
|
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
|
|
|
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
|
|
|
t.Run("查询账号列表-空结果", func(t *testing.T) {
|
|
ctx := createCustomerAccountTestContext(1)
|
|
|
|
req := &model.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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
createReq := &model.CreateCustomerAccountReq{
|
|
Username: "测试账号用户",
|
|
Phone: "13900000001",
|
|
Password: "Test123456",
|
|
ShopID: shop.ID,
|
|
}
|
|
_, err = service.Create(ctx, createReq)
|
|
require.NoError(t, err)
|
|
|
|
req := &model.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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
createReq := &model.CreateCustomerAccountReq{
|
|
Username: "店铺筛选账号",
|
|
Phone: "13900000002",
|
|
Password: "Test123456",
|
|
ShopID: shop.ID,
|
|
}
|
|
_, err = service.Create(ctx, createReq)
|
|
require.NoError(t, err)
|
|
|
|
req := &model.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) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
accountStore := postgres.NewAccountStore(db, redisClient)
|
|
shopStore := postgres.NewShopStore(db, redisClient)
|
|
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
|
|
|
service := customer_account.New(db, 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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
req := &model.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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
req1 := &model.CreateCustomerAccountReq{
|
|
Username: "账号一",
|
|
Phone: "13900000011",
|
|
Password: "Test123456",
|
|
ShopID: shop.ID,
|
|
}
|
|
_, err = service.Create(ctx, req1)
|
|
require.NoError(t, err)
|
|
|
|
req2 := &model.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 := &model.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 := &model.CreateCustomerAccountReq{
|
|
Username: "未授权账号",
|
|
Phone: "13900000013",
|
|
Password: "Test123456",
|
|
ShopID: 1,
|
|
}
|
|
|
|
_, err := service.Create(ctx, req)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestCustomerAccountService_Update(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
accountStore := postgres.NewAccountStore(db, redisClient)
|
|
shopStore := postgres.NewShopStore(db, redisClient)
|
|
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
|
|
|
service := customer_account.New(db, 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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
createReq := &model.CreateCustomerAccountReq{
|
|
Username: "待编辑账号",
|
|
Phone: "13900000020",
|
|
Password: "Test123456",
|
|
ShopID: shop.ID,
|
|
}
|
|
created, err := service.Create(ctx, createReq)
|
|
require.NoError(t, err)
|
|
|
|
newName := "编辑后账号"
|
|
updateReq := &model.UpdateCustomerAccountReq{
|
|
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 := &model.UpdateCustomerAccountReq{
|
|
Username: &newName,
|
|
}
|
|
|
|
_, err := service.Update(ctx, 99999, updateReq)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
accountStore := postgres.NewAccountStore(db, redisClient)
|
|
shopStore := postgres.NewShopStore(db, redisClient)
|
|
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
|
|
|
service := customer_account.New(db, 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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
createReq := &model.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 = db.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) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
accountStore := postgres.NewAccountStore(db, redisClient)
|
|
shopStore := postgres.NewShopStore(db, redisClient)
|
|
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
|
|
|
service := customer_account.New(db, 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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
createReq := &model.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 = db.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 := db.Create(shop).Error
|
|
require.NoError(t, err)
|
|
|
|
createReq := &model.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 = db.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)
|
|
})
|
|
}
|