主要功能: - 实现完整的 RBAC 权限系统(账号、角色、权限的多对多关联) - 基于 owner_id + shop_id 的自动数据权限过滤 - 使用 PostgreSQL WITH RECURSIVE 查询下级账号 - Redis 缓存优化下级账号查询性能(30分钟过期) - 支持多租户数据隔离和层级权限管理 技术实现: - 新增 Account、Role、Permission 模型及关联关系表 - 实现 GORM Scopes 自动应用数据权限过滤 - 添加数据库迁移脚本(000002_rbac_data_permission、000003_add_owner_id_shop_id) - 完善错误码定义(1010-1027 为 RBAC 相关错误) - 重构 main.go 采用函数拆分提高可读性 测试覆盖: - 添加 Account、Role、Permission 的集成测试 - 添加数据权限过滤的单元测试和集成测试 - 添加下级账号查询和缓存的单元测试 - 添加 API 回归测试确保向后兼容 文档更新: - 更新 README.md 添加 RBAC 功能说明 - 更新 CLAUDE.md 添加技术栈和开发原则 - 添加 docs/004-rbac-data-permission/ 功能总结和使用指南 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
320 lines
8.5 KiB
Go
320 lines
8.5 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/store/postgres"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
|
)
|
|
|
|
// TestAccountModel_Create 测试创建账号
|
|
func TestAccountModel_Create(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
t.Run("创建 root 账号", func(t *testing.T) {
|
|
account := &model.Account{
|
|
Username: "root_user",
|
|
Phone: "13800000001",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypeRoot,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, account.ID)
|
|
assert.NotZero(t, account.CreatedAt)
|
|
assert.NotZero(t, account.UpdatedAt)
|
|
})
|
|
|
|
t.Run("创建带 parent_id 的账号", func(t *testing.T) {
|
|
// 先创建父账号
|
|
parent := &model.Account{
|
|
Username: "parent_user",
|
|
Phone: "13800000002",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, parent)
|
|
require.NoError(t, err)
|
|
|
|
// 创建子账号
|
|
child := &model.Account{
|
|
Username: "child_user",
|
|
Phone: "13800000003",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypeAgent,
|
|
ParentID: &parent.ID,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err = store.Create(ctx, child)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, child.ID)
|
|
assert.Equal(t, parent.ID, *child.ParentID)
|
|
})
|
|
|
|
t.Run("创建带 shop_id 的账号", func(t *testing.T) {
|
|
shopID := uint(100)
|
|
account := &model.Account{
|
|
Username: "shop_user",
|
|
Phone: "13800000004",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
ShopID: &shopID,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, account.ShopID)
|
|
assert.Equal(t, uint(100), *account.ShopID)
|
|
})
|
|
}
|
|
|
|
// TestAccountModel_GetByID 测试根据 ID 查询账号
|
|
func TestAccountModel_GetByID(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试账号
|
|
account := &model.Account{
|
|
Username: "test_user",
|
|
Phone: "13800000001",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("查询存在的账号", func(t *testing.T) {
|
|
found, err := store.GetByID(ctx, account.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, account.Username, found.Username)
|
|
assert.Equal(t, account.Phone, found.Phone)
|
|
assert.Equal(t, account.UserType, found.UserType)
|
|
})
|
|
|
|
t.Run("查询不存在的账号", func(t *testing.T) {
|
|
_, err := store.GetByID(ctx, 99999)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestAccountModel_GetByUsername 测试根据用户名查询账号
|
|
func TestAccountModel_GetByUsername(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试账号
|
|
account := &model.Account{
|
|
Username: "unique_user",
|
|
Phone: "13800000001",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("根据用户名查询", func(t *testing.T) {
|
|
found, err := store.GetByUsername(ctx, "unique_user")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, account.ID, found.ID)
|
|
})
|
|
|
|
t.Run("查询不存在的用户名", func(t *testing.T) {
|
|
_, err := store.GetByUsername(ctx, "nonexistent")
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestAccountModel_GetByPhone 测试根据手机号查询账号
|
|
func TestAccountModel_GetByPhone(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试账号
|
|
account := &model.Account{
|
|
Username: "phone_user",
|
|
Phone: "13800000001",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("根据手机号查询", func(t *testing.T) {
|
|
found, err := store.GetByPhone(ctx, "13800000001")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, account.ID, found.ID)
|
|
})
|
|
|
|
t.Run("查询不存在的手机号", func(t *testing.T) {
|
|
_, err := store.GetByPhone(ctx, "99900000000")
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
// TestAccountModel_Update 测试更新账号
|
|
func TestAccountModel_Update(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试账号
|
|
account := &model.Account{
|
|
Username: "update_user",
|
|
Phone: "13800000001",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("更新账号状态", func(t *testing.T) {
|
|
account.Status = constants.StatusDisabled
|
|
account.Updater = 2
|
|
err := store.Update(ctx, account)
|
|
require.NoError(t, err)
|
|
|
|
// 验证更新
|
|
found, err := store.GetByID(ctx, account.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, constants.StatusDisabled, found.Status)
|
|
assert.Equal(t, uint(2), found.Updater)
|
|
})
|
|
}
|
|
|
|
// TestAccountModel_List 测试查询账号列表
|
|
func TestAccountModel_List(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建多个测试账号
|
|
for i := 1; i <= 5; i++ {
|
|
account := &model.Account{
|
|
Username: testutils.GenerateUsername("list_user", i),
|
|
Phone: testutils.GeneratePhone("138", i),
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
t.Run("分页查询", func(t *testing.T) {
|
|
accounts, total, err := store.List(ctx, nil, nil)
|
|
require.NoError(t, err)
|
|
assert.GreaterOrEqual(t, len(accounts), 5)
|
|
assert.GreaterOrEqual(t, total, int64(5))
|
|
})
|
|
|
|
t.Run("带过滤条件查询", func(t *testing.T) {
|
|
filters := map[string]interface{}{
|
|
"user_type": constants.UserTypePlatform,
|
|
}
|
|
accounts, _, err := store.List(ctx, nil, filters)
|
|
require.NoError(t, err)
|
|
for _, acc := range accounts {
|
|
assert.Equal(t, constants.UserTypePlatform, acc.UserType)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestAccountModel_UniqueConstraints 测试唯一约束
|
|
func TestAccountModel_UniqueConstraints(t *testing.T) {
|
|
db, redisClient := testutils.SetupTestDB(t)
|
|
defer testutils.TeardownTestDB(t, db, redisClient)
|
|
|
|
store := postgres.NewAccountStore(db, redisClient)
|
|
ctx := context.Background()
|
|
|
|
// 创建测试账号
|
|
account := &model.Account{
|
|
Username: "unique_test",
|
|
Phone: "13800000001",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, account)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("重复用户名应失败", func(t *testing.T) {
|
|
duplicate := &model.Account{
|
|
Username: "unique_test", // 重复
|
|
Phone: "13800000002",
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, duplicate)
|
|
assert.Error(t, err)
|
|
})
|
|
|
|
t.Run("重复手机号应失败", func(t *testing.T) {
|
|
duplicate := &model.Account{
|
|
Username: "unique_test2",
|
|
Phone: "13800000001", // 重复
|
|
Password: "hashed_password",
|
|
UserType: constants.UserTypePlatform,
|
|
Status: constants.StatusEnabled,
|
|
Creator: 1,
|
|
Updater: 1,
|
|
}
|
|
err := store.Create(ctx, duplicate)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|