重构迁移核心数据结构到认证模块;完善中间件初始化逻辑以及 logger 记录过程
This commit is contained in:
@@ -4,17 +4,18 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"platform/pkg/testutil"
|
||||
"platform/web/auth"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 创建测试用的认证上下文
|
||||
func createTestAuthContext() AuthContext {
|
||||
func createTestAuthContext() auth.Context {
|
||||
//goland:noinspection ALL
|
||||
return AuthContext{
|
||||
Payload: Payload{
|
||||
Type: PayloadUser,
|
||||
return auth.Context{
|
||||
Payload: auth.Payload{
|
||||
Type: auth.PayloadUser,
|
||||
Id: 1001,
|
||||
},
|
||||
Permissions: map[string]struct{}{
|
||||
@@ -31,11 +32,11 @@ func createTestAuthContext() AuthContext {
|
||||
func Test_sessionService_Create(t *testing.T) {
|
||||
mr := testutil.SetupRedisTest(t)
|
||||
ctx := context.Background()
|
||||
auth := createTestAuthContext()
|
||||
authCtx := createTestAuthContext()
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
auth AuthContext
|
||||
auth auth.Context
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -47,7 +48,7 @@ func Test_sessionService_Create(t *testing.T) {
|
||||
name: "创建会话",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
auth: auth,
|
||||
auth: authCtx,
|
||||
},
|
||||
want: func(td *TokenDetails) bool {
|
||||
// 验证令牌存在且格式正确
|
||||
@@ -60,7 +61,7 @@ func Test_sessionService_Create(t *testing.T) {
|
||||
return false
|
||||
}
|
||||
// 验证认证信息正确
|
||||
if !reflect.DeepEqual(td.Auth, auth) {
|
||||
if !reflect.DeepEqual(td.Auth, authCtx) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -100,11 +101,11 @@ func Test_sessionService_Create(t *testing.T) {
|
||||
func Test_sessionService_Find(t *testing.T) {
|
||||
testutil.SetupRedisTest(t)
|
||||
ctx := context.Background()
|
||||
auth := createTestAuthContext()
|
||||
authCtx := createTestAuthContext()
|
||||
s := &sessionService{}
|
||||
|
||||
// 创建一个有效的会话
|
||||
td, err := s.Create(ctx, auth, true)
|
||||
td, err := s.Create(ctx, authCtx, true)
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建测试会话: %v", err)
|
||||
}
|
||||
@@ -119,7 +120,7 @@ func Test_sessionService_Find(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *AuthContext
|
||||
want *auth.Context
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
@@ -128,7 +129,7 @@ func Test_sessionService_Find(t *testing.T) {
|
||||
ctx: ctx,
|
||||
token: validToken,
|
||||
},
|
||||
want: &auth,
|
||||
want: &authCtx,
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
@@ -159,11 +160,11 @@ func Test_sessionService_Find(t *testing.T) {
|
||||
func Test_sessionService_Refresh(t *testing.T) {
|
||||
mr := testutil.SetupRedisTest(t)
|
||||
ctx := context.Background()
|
||||
auth := createTestAuthContext()
|
||||
authCtx := createTestAuthContext()
|
||||
s := &sessionService{}
|
||||
|
||||
// 创建一个初始会话
|
||||
td, err := s.Create(ctx, auth, true)
|
||||
td, err := s.Create(ctx, authCtx, true)
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建初始会话: %v", err)
|
||||
}
|
||||
@@ -197,7 +198,7 @@ func Test_sessionService_Refresh(t *testing.T) {
|
||||
return false
|
||||
}
|
||||
// 验证认证信息一致
|
||||
if !reflect.DeepEqual(td.Auth, auth) {
|
||||
if !reflect.DeepEqual(td.Auth, authCtx) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -251,11 +252,11 @@ func Test_sessionService_Refresh(t *testing.T) {
|
||||
func Test_sessionService_Remove(t *testing.T) {
|
||||
mr := testutil.SetupRedisTest(t)
|
||||
ctx := context.Background()
|
||||
auth := createTestAuthContext()
|
||||
authCtx := createTestAuthContext()
|
||||
s := &sessionService{}
|
||||
|
||||
// 创建一个会话
|
||||
td, err := s.Create(ctx, auth, true)
|
||||
td, err := s.Create(ctx, authCtx, true)
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建测试会话: %v", err)
|
||||
}
|
||||
@@ -312,7 +313,7 @@ func Test_sessionService_Remove(t *testing.T) {
|
||||
|
||||
func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
type fields struct {
|
||||
Payload Payload
|
||||
Payload auth.Payload
|
||||
Permissions map[string]struct{}
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
@@ -328,7 +329,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
{
|
||||
name: "用户拥有所需权限",
|
||||
fields: fields{
|
||||
Payload: Payload{Type: PayloadUser, Id: 1},
|
||||
Payload: auth.Payload{Type: auth.PayloadUser, Id: 1},
|
||||
Permissions: map[string]struct{}{
|
||||
"read": {},
|
||||
"write": {},
|
||||
@@ -343,7 +344,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
{
|
||||
name: "用户拥有至少一个所需权限",
|
||||
fields: fields{
|
||||
Payload: Payload{Type: PayloadUser, Id: 1},
|
||||
Payload: auth.Payload{Type: auth.PayloadUser, Id: 1},
|
||||
Permissions: map[string]struct{}{
|
||||
"read": {},
|
||||
},
|
||||
@@ -357,7 +358,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
{
|
||||
name: "用户没有所需权限",
|
||||
fields: fields{
|
||||
Payload: Payload{Type: PayloadUser, Id: 1},
|
||||
Payload: auth.Payload{Type: auth.PayloadUser, Id: 1},
|
||||
Permissions: map[string]struct{}{
|
||||
"read": {},
|
||||
},
|
||||
@@ -371,7 +372,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
{
|
||||
name: "空权限列表",
|
||||
fields: fields{
|
||||
Payload: Payload{Type: PayloadUser, Id: 1},
|
||||
Payload: auth.Payload{Type: auth.PayloadUser, Id: 1},
|
||||
Permissions: map[string]struct{}{},
|
||||
Metadata: nil,
|
||||
},
|
||||
@@ -383,7 +384,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
{
|
||||
name: "nil权限列表",
|
||||
fields: fields{
|
||||
Payload: Payload{Type: PayloadUser, Id: 1},
|
||||
Payload: auth.Payload{Type: auth.PayloadUser, Id: 1},
|
||||
Permissions: nil,
|
||||
Metadata: nil,
|
||||
},
|
||||
@@ -395,7 +396,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
{
|
||||
name: "nil认证上下文",
|
||||
fields: fields{
|
||||
Payload: Payload{},
|
||||
Payload: auth.Payload{},
|
||||
Permissions: nil,
|
||||
Metadata: nil,
|
||||
},
|
||||
@@ -408,7 +409,7 @@ func TestAuthContext_AnyPermission(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
a := &AuthContext{
|
||||
a := &auth.Context{
|
||||
Payload: tt.fields.Payload,
|
||||
Permissions: tt.fields.Permissions,
|
||||
Metadata: tt.fields.Metadata,
|
||||
|
||||
Reference in New Issue
Block a user