Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// stubCredRepo 是最小化 AccountRepository stub,仅实现 GetByID,供 credential_shadow_test 使用。
|
||||
// 嵌入接口满足完整方法集;未实现的方法若被调用会 panic,从而快速暴露误调用。
|
||||
type stubCredRepo struct {
|
||||
AccountRepository
|
||||
parent *Account
|
||||
}
|
||||
|
||||
func (s *stubCredRepo) GetByID(_ context.Context, _ int64) (*Account, error) {
|
||||
return s.parent, nil
|
||||
}
|
||||
|
||||
func newStubCredRepo(parent *Account) AccountRepository {
|
||||
return &stubCredRepo{parent: parent}
|
||||
}
|
||||
|
||||
func TestResolveCredentialAccount(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
pid := int64(100)
|
||||
|
||||
// 普通账号(非影子)→ 返回自身
|
||||
parent := &Account{ID: 100, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Status: StatusActive}
|
||||
repo := newStubCredRepo(parent)
|
||||
got, err := resolveCredentialAccount(ctx, repo, parent)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(100), got.ID)
|
||||
|
||||
// 影子账号 + 合法 OpenAI OAuth 母账号 → 返回母账号
|
||||
shadow := &Account{ID: 200, ParentAccountID: &pid, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
|
||||
got, err = resolveCredentialAccount(ctx, repo, shadow)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(100), got.ID)
|
||||
|
||||
// 影子账号 + 母账号非 OpenAI OAuth(API Key 类型)→ 返回 error
|
||||
badRepo := newStubCredRepo(&Account{ID: 100, Platform: PlatformOpenAI, Type: AccountTypeAPIKey})
|
||||
_, err = resolveCredentialAccount(ctx, badRepo, shadow)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user