Files
sub2api/backend/internal/service/credential_shadow_test.go
T
李建琦 6d655c9903
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
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

47 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 OAuthAPI Key 类型)→ 返回 error
badRepo := newStubCredRepo(&Account{ID: 100, Platform: PlatformOpenAI, Type: AccountTypeAPIKey})
_, err = resolveCredentialAccount(ctx, badRepo, shadow)
require.Error(t, err)
}