feat(role): 新增平台角色管理功能增强
- 权限表增加 available_for_role_types 字段,支持标记权限可用角色类型 - 权限列表和权限树接口支持按 available_for_role_type 过滤 - 新增角色状态切换接口 PUT /api/admin/roles/:id/status - 角色分配权限时验证权限的可用角色类型 - 完善数据库迁移脚本和单元测试 - 补充数据库迁移相关开发规范文档
This commit is contained in:
@@ -517,3 +517,109 @@ func TestRoleAPI_RemovePermission(t *testing.T) {
|
||||
assert.NotNil(t, rp.DeletedAt)
|
||||
})
|
||||
}
|
||||
|
||||
// TestRoleAPI_UpdateStatus 测试角色状态切换 API
|
||||
func TestRoleAPI_UpdateStatus(t *testing.T) {
|
||||
env := setupRoleTestEnv(t)
|
||||
defer env.teardown()
|
||||
|
||||
// 添加测试中间件
|
||||
testUserID := uint(1)
|
||||
env.app.Use(func(c *fiber.Ctx) error {
|
||||
ctx := middleware.SetUserContext(c.UserContext(), middleware.NewSimpleUserContext(testUserID, constants.UserTypeSuperAdmin, 0))
|
||||
c.SetUserContext(ctx)
|
||||
return c.Next()
|
||||
})
|
||||
|
||||
// 创建测试角色
|
||||
testRole := &model.Role{
|
||||
RoleName: "状态切换测试角色",
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(testRole)
|
||||
|
||||
t.Run("成功禁用角色", func(t *testing.T) {
|
||||
reqBody := model.UpdateRoleStatusRequest{
|
||||
Status: constants.StatusDisabled,
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(reqBody)
|
||||
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/admin/roles/%d/status", testRole.ID), bytes.NewReader(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := env.app.Test(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var result response.Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, result.Code)
|
||||
|
||||
// 验证数据库中状态已更新
|
||||
var updated model.Role
|
||||
env.db.First(&updated, testRole.ID)
|
||||
assert.Equal(t, constants.StatusDisabled, updated.Status)
|
||||
})
|
||||
|
||||
t.Run("成功启用角色", func(t *testing.T) {
|
||||
reqBody := model.UpdateRoleStatusRequest{
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(reqBody)
|
||||
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/admin/roles/%d/status", testRole.ID), bytes.NewReader(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := env.app.Test(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var result response.Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, result.Code)
|
||||
|
||||
// 验证数据库中状态已更新
|
||||
var updated model.Role
|
||||
env.db.First(&updated, testRole.ID)
|
||||
assert.Equal(t, constants.StatusEnabled, updated.Status)
|
||||
})
|
||||
|
||||
t.Run("角色不存在返回错误", func(t *testing.T) {
|
||||
reqBody := model.UpdateRoleStatusRequest{
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(reqBody)
|
||||
req := httptest.NewRequest("PUT", "/api/admin/roles/99999/status", bytes.NewReader(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := env.app.Test(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
var result response.Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, errors.CodeRoleNotFound, result.Code)
|
||||
})
|
||||
|
||||
t.Run("无效状态值返回错误", func(t *testing.T) {
|
||||
reqBody := map[string]interface{}{
|
||||
"status": 99, // 无效状态
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(reqBody)
|
||||
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/admin/roles/%d/status", testRole.ID), bytes.NewReader(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := env.app.Test(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
var result response.Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, 0, result.Code)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user