Files
junhong_cmp_fiber/tests/integration/account_role_test.go
huang 23eb0307bb
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m30s
feat: 实现门店套餐分配功能并统一测试基础设施
新增功能:
- 门店套餐分配管理(shop_package_allocation):支持门店套餐库存管理
- 门店套餐系列分配管理(shop_series_allocation):支持套餐系列分配和佣金层级设置
- 我的套餐查询(my_package):支持门店查询自己的套餐分配情况

测试改进:
- 统一集成测试基础设施,新增 testutils.NewIntegrationTestEnv
- 重构所有集成测试使用新的测试环境设置
- 移除旧的测试辅助函数和冗余测试文件
- 新增 test_helpers_test.go 统一任务测试辅助

技术细节:
- 新增数据库迁移 000025_create_shop_allocation_tables
- 新增 3 个 Handler、Service、Store 和对应的单元测试
- 更新 OpenAPI 文档和文档生成器
- 测试覆盖率:Service 层 > 90%

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-28 10:45:16 +08:00

258 lines
7.3 KiB
Go

package integration
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/break/junhong_cmp_fiber/internal/model"
accountService "github.com/break/junhong_cmp_fiber/internal/service/account"
postgresStore "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/integ"
)
// TestAccountRoleAssociation_AssignRoles 测试账号角色分配功能
func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
env := integ.NewIntegrationTestEnv(t)
// 初始化 Store 和 Service
accountStore := postgresStore.NewAccountStore(env.TX, env.Redis)
roleStore := postgresStore.NewRoleStore(env.TX)
accountRoleStore := postgresStore.NewAccountRoleStore(env.TX, env.Redis)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
// 获取超级管理员上下文
userCtx := env.GetSuperAdminContext()
t.Run("成功分配单个角色", func(t *testing.T) {
// 创建测试账号
account := &model.Account{
Username: "single_role_test",
Phone: "13800000100",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
// 创建测试角色
role := &model.Role{
RoleName: "单角色测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(role)
// 分配角色
ars, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
assert.Len(t, ars, 1)
assert.Equal(t, account.ID, ars[0].AccountID)
assert.Equal(t, role.ID, ars[0].RoleID)
})
t.Run("成功分配多个角色", func(t *testing.T) {
// 创建测试账号
account := &model.Account{
Username: "multi_role_test",
Phone: "13800000101",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
// 创建多个测试角色
roles := make([]*model.Role, 3)
roleIDs := make([]uint, 3)
for i := 0; i < 3; i++ {
roles[i] = &model.Role{
RoleName: "多角色测试_" + string(rune('A'+i)),
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(roles[i])
roleIDs[i] = roles[i].ID
}
// 分配角色
ars, err := accService.AssignRoles(userCtx, account.ID, roleIDs)
require.NoError(t, err)
assert.Len(t, ars, 3)
})
t.Run("获取账号的角色列表", func(t *testing.T) {
// 创建测试账号
account := &model.Account{
Username: "get_roles_test",
Phone: "13800000102",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
// 创建并分配角色
role := &model.Role{
RoleName: "获取角色列表测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(role)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
// 获取角色列表
roles, err := accService.GetRoles(userCtx, account.ID)
require.NoError(t, err)
assert.Len(t, roles, 1)
assert.Equal(t, role.ID, roles[0].ID)
})
t.Run("移除账号的角色", func(t *testing.T) {
// 创建测试账号
account := &model.Account{
Username: "remove_role_test",
Phone: "13800000103",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
// 创建并分配角色
role := &model.Role{
RoleName: "移除角色测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(role)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
// 移除角色
err = accService.RemoveRole(userCtx, account.ID, role.ID)
require.NoError(t, err)
// 验证角色已被软删除
var ar model.AccountRole
err = env.RawDB().Unscoped().Where("account_id = ? AND role_id = ?", account.ID, role.ID).First(&ar).Error
require.NoError(t, err)
assert.NotNil(t, ar.DeletedAt)
})
t.Run("重复分配角色不会创建重复记录", func(t *testing.T) {
// 创建测试账号
account := &model.Account{
Username: "duplicate_role_test",
Phone: "13800000104",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
// 创建测试角色
role := &model.Role{
RoleName: "重复分配测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(role)
// 第一次分配
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
// 第二次分配相同角色
_, err = accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
// 验证只有一条记录
var count int64
env.RawDB().Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", account.ID, role.ID).Count(&count)
assert.Equal(t, int64(1), count)
})
t.Run("账号不存在时分配角色失败", func(t *testing.T) {
role := &model.Role{
RoleName: "账号不存在测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(role)
_, err := accService.AssignRoles(userCtx, 99999, []uint{role.ID})
assert.Error(t, err)
})
t.Run("角色不存在时分配失败", func(t *testing.T) {
account := &model.Account{
Username: "role_not_exist_test",
Phone: "13800000105",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{99999})
assert.Error(t, err)
})
}
// TestAccountRoleAssociation_SoftDelete 测试软删除对账号角色关联的影响
func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
env := integ.NewIntegrationTestEnv(t)
// 初始化 Store 和 Service
accountStore := postgresStore.NewAccountStore(env.TX, env.Redis)
roleStore := postgresStore.NewRoleStore(env.TX)
accountRoleStore := postgresStore.NewAccountRoleStore(env.TX, env.Redis)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
// 获取超级管理员上下文
userCtx := env.GetSuperAdminContext()
t.Run("软删除角色后重新分配可以恢复", func(t *testing.T) {
// 创建测试数据
account := &model.Account{
Username: "restore_role_test",
Phone: "13800000200",
Password: "hashedpassword",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(account)
role := &model.Role{
RoleName: "恢复角色测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.TX.Create(role)
// 分配角色
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
// 移除角色
err = accService.RemoveRole(userCtx, account.ID, role.ID)
require.NoError(t, err)
// 重新分配角色
ars, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
assert.Len(t, ars, 1)
// 验证关联已恢复
roles, err := accService.GetRoles(userCtx, account.ID)
require.NoError(t, err)
assert.Len(t, roles, 1)
})
}