重构错误处理逻辑,使用 fiber.Error 统一返回错误状态码;统一授权枚举值定义到 auth 包
This commit is contained in:
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"platform/web/auth"
|
||||
"platform/web/models"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -10,10 +11,10 @@ import (
|
||||
|
||||
// mockSessionService 用于模拟Session服务的行为
|
||||
type mockSessionService struct {
|
||||
createFunc func(ctx context.Context, auth AuthContext) (*TokenDetails, error)
|
||||
createFunc func(ctx context.Context, authCtx auth.Context) (*TokenDetails, error)
|
||||
}
|
||||
|
||||
func (m *mockSessionService) Find(ctx context.Context, token string) (*AuthContext, error) {
|
||||
func (m *mockSessionService) Find(ctx context.Context, token string) (*auth.Context, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
func (m *mockSessionService) Refresh(ctx context.Context, refreshToken string) (*TokenDetails, error) {
|
||||
@@ -22,8 +23,8 @@ func (m *mockSessionService) Refresh(ctx context.Context, refreshToken string) (
|
||||
func (m *mockSessionService) Remove(ctx context.Context, accessToken, refreshToken string) error {
|
||||
panic("implement me")
|
||||
}
|
||||
func (m *mockSessionService) Create(ctx context.Context, auth AuthContext, remember bool) (*TokenDetails, error) {
|
||||
return m.createFunc(ctx, auth)
|
||||
func (m *mockSessionService) Create(ctx context.Context, authCtx auth.Context, remember bool) (*TokenDetails, error) {
|
||||
return m.createFunc(ctx, authCtx)
|
||||
}
|
||||
|
||||
func Test_authService_OauthClientCredentials(t *testing.T) {
|
||||
@@ -52,7 +53,7 @@ func Test_authService_OauthClientCredentials(t *testing.T) {
|
||||
mockCreateErr error
|
||||
want *TokenDetails
|
||||
wantErr bool
|
||||
wantPayload Payload
|
||||
wantPayload auth.Payload
|
||||
}{
|
||||
{
|
||||
name: "成功 - 机密客户端 (Spec=0)",
|
||||
@@ -64,8 +65,8 @@ func Test_authService_OauthClientCredentials(t *testing.T) {
|
||||
mockCreateErr: nil,
|
||||
want: expectedToken,
|
||||
wantErr: false,
|
||||
wantPayload: Payload{
|
||||
Type: PayloadClientConfidential,
|
||||
wantPayload: auth.Payload{
|
||||
Type: auth.PayloadSecuredServer,
|
||||
Id: 1,
|
||||
},
|
||||
},
|
||||
@@ -79,8 +80,8 @@ func Test_authService_OauthClientCredentials(t *testing.T) {
|
||||
mockCreateErr: nil,
|
||||
want: expectedToken,
|
||||
wantErr: false,
|
||||
wantPayload: Payload{
|
||||
Type: PayloadClientPublic,
|
||||
wantPayload: auth.Payload{
|
||||
Type: auth.PayloadPublicServer,
|
||||
Id: 1,
|
||||
},
|
||||
},
|
||||
@@ -94,8 +95,8 @@ func Test_authService_OauthClientCredentials(t *testing.T) {
|
||||
mockCreateErr: nil,
|
||||
want: expectedToken,
|
||||
wantErr: false,
|
||||
wantPayload: Payload{
|
||||
Type: PayloadClientPublic,
|
||||
wantPayload: auth.Payload{
|
||||
Type: auth.PayloadPublicServer,
|
||||
Id: 1,
|
||||
},
|
||||
},
|
||||
@@ -106,23 +107,23 @@ func Test_authService_OauthClientCredentials(t *testing.T) {
|
||||
|
||||
// 为每个测试用例设置模拟的Session服务
|
||||
mockSession := &mockSessionService{
|
||||
createFunc: func(ctx context.Context, auth AuthContext) (*TokenDetails, error) {
|
||||
createFunc: func(ctx context.Context, authCtx auth.Context) (*TokenDetails, error) {
|
||||
// 验证权限映射
|
||||
if len(auth.Permissions) != len(tt.args.scope) {
|
||||
t.Errorf("Permissions length = %v, want %v", len(auth.Permissions), len(tt.args.scope))
|
||||
for key := range auth.Permissions {
|
||||
if _, ok := auth.Permissions[key]; !ok {
|
||||
if len(authCtx.Permissions) != len(tt.args.scope) {
|
||||
t.Errorf("Permissions length = %v, want %v", len(authCtx.Permissions), len(tt.args.scope))
|
||||
for key := range authCtx.Permissions {
|
||||
if _, ok := authCtx.Permissions[key]; !ok {
|
||||
t.Errorf("Permissions[%s] not found", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 验证Payload
|
||||
if auth.Payload.Type != tt.wantPayload.Type {
|
||||
t.Errorf("Payload.Type = %v, want %v", auth.Payload.Type, tt.wantPayload.Type)
|
||||
if authCtx.Payload.Type != tt.wantPayload.Type {
|
||||
t.Errorf("Payload.Type = %v, want %v", authCtx.Payload.Type, tt.wantPayload.Type)
|
||||
}
|
||||
if auth.Payload.Id != tt.wantPayload.Id {
|
||||
t.Errorf("Payload.Id = %v, want %v", auth.Payload.Id, tt.wantPayload.Id)
|
||||
if authCtx.Payload.Id != tt.wantPayload.Id {
|
||||
t.Errorf("Payload.Id = %v, want %v", authCtx.Payload.Id, tt.wantPayload.Id)
|
||||
}
|
||||
|
||||
return expectedToken, tt.mockCreateErr
|
||||
|
||||
Reference in New Issue
Block a user