Files
junhong_cmp_fiber/tests/unit/permission_cache_test.go
huang 028cfaa7aa feat: 实现权限检查功能并添加Redis缓存优化
- 完成 CheckPermission 方法的完整实现(账号→角色→权限查询链)
- 实现 Redis 缓存机制,大幅提升权限查询性能(~12倍提升)
- 自动缓存失效:角色/权限变更时清除相关用户缓存
- 新增完整的单元测试和集成测试(10个测试用例全部通过)
- 添加权限检查使用文档和缓存机制说明
- 归档 implement-permission-check OpenSpec 提案

性能优化:
- 首次查询: ~18ms(3次DB查询 + 1次Redis写入)
- 缓存命中: ~1.5ms(1次Redis查询)
- TTL: 30分钟,自动失效机制保证数据一致性
2026-01-16 18:15:32 +08:00

164 lines
4.8 KiB
Go

package unit
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/service/permission"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
"github.com/break/junhong_cmp_fiber/tests/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPermissionCache_FirstCallMissSecondHit(t *testing.T) {
db, rdb := testutils.SetupTestDB(t)
defer testutils.TeardownTestDB(t, db, rdb)
ctx := context.Background()
accountStore := postgres.NewAccountStore(db, rdb)
roleStore := postgres.NewRoleStore(db)
permStore := postgres.NewPermissionStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, rdb)
rolePermStore := postgres.NewRolePermissionStore(db, rdb)
permSvc := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
testUser := &model.Account{
Username: "testuser",
Phone: "13900000001",
Password: "Test@123456",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
require.NoError(t, accountStore.Create(ctx, testUser))
testRole := &model.Role{
RoleName: "测试角色",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
require.NoError(t, roleStore.Create(ctx, testRole))
testPerm := &model.Permission{
PermName: "测试权限",
PermCode: "test:read",
PermType: constants.PermissionTypeButton,
Platform: constants.PlatformWeb,
Status: constants.StatusEnabled,
}
require.NoError(t, permStore.Create(ctx, testPerm))
require.NoError(t, accountRoleStore.Create(ctx, &model.AccountRole{
AccountID: testUser.ID,
RoleID: testRole.ID,
}))
require.NoError(t, rolePermStore.Create(ctx, &model.RolePermission{
RoleID: testRole.ID,
PermID: testPerm.ID,
}))
ctx = middleware.SetUserContext(ctx, &middleware.UserContextInfo{
UserID: testUser.ID,
UserType: testUser.UserType,
})
cacheKey := constants.RedisUserPermissionsKey(testUser.ID)
cachedData, err := rdb.Get(ctx, cacheKey).Result()
assert.Error(t, err)
assert.Empty(t, cachedData)
hasPermission, err := permSvc.CheckPermission(ctx, testUser.ID, "test:read", constants.PlatformWeb)
require.NoError(t, err)
assert.True(t, hasPermission)
cachedData, err = rdb.Get(ctx, cacheKey).Result()
require.NoError(t, err)
assert.NotEmpty(t, cachedData)
type cacheItem struct {
PermCode string `json:"perm_code"`
Platform string `json:"platform"`
}
var cached []cacheItem
require.NoError(t, json.Unmarshal([]byte(cachedData), &cached))
assert.Len(t, cached, 1)
assert.Equal(t, "test:read", cached[0].PermCode)
assert.Equal(t, constants.PlatformWeb, cached[0].Platform)
hasPermission2, err := permSvc.CheckPermission(ctx, testUser.ID, "test:read", constants.PlatformWeb)
require.NoError(t, err)
assert.True(t, hasPermission2)
}
func TestPermissionCache_ExpiredAfter30Minutes(t *testing.T) {
db, rdb := testutils.SetupTestDB(t)
defer testutils.TeardownTestDB(t, db, rdb)
ctx := context.Background()
accountStore := postgres.NewAccountStore(db, rdb)
roleStore := postgres.NewRoleStore(db)
permStore := postgres.NewPermissionStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, rdb)
rolePermStore := postgres.NewRolePermissionStore(db, rdb)
permSvc := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
testUser := &model.Account{
Username: "testuser2",
Phone: "13900000002",
Password: "Test@123456",
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
require.NoError(t, accountStore.Create(ctx, testUser))
testRole := &model.Role{
RoleName: "测试角色2",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
require.NoError(t, roleStore.Create(ctx, testRole))
testPerm := &model.Permission{
PermName: "测试权限2",
PermCode: "test:write",
PermType: constants.PermissionTypeButton,
Platform: constants.PlatformWeb,
Status: constants.StatusEnabled,
}
require.NoError(t, permStore.Create(ctx, testPerm))
require.NoError(t, accountRoleStore.Create(ctx, &model.AccountRole{
AccountID: testUser.ID,
RoleID: testRole.ID,
}))
require.NoError(t, rolePermStore.Create(ctx, &model.RolePermission{
RoleID: testRole.ID,
PermID: testPerm.ID,
}))
ctx = middleware.SetUserContext(ctx, &middleware.UserContextInfo{
UserID: testUser.ID,
UserType: testUser.UserType,
})
hasPermission, err := permSvc.CheckPermission(ctx, testUser.ID, "test:write", constants.PlatformWeb)
require.NoError(t, err)
assert.True(t, hasPermission)
cacheKey := constants.RedisUserPermissionsKey(testUser.ID)
ttl, err := rdb.TTL(ctx, cacheKey).Result()
require.NoError(t, err)
assert.True(t, ttl > 29*time.Minute && ttl <= 30*time.Minute)
}