credential_shadow_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. // stubCredRepo 是最小化 AccountRepository stub,仅实现 GetByID,供 credential_shadow_test 使用。
  8. // 嵌入接口满足完整方法集;未实现的方法若被调用会 panic,从而快速暴露误调用。
  9. type stubCredRepo struct {
  10. AccountRepository
  11. parent *Account
  12. }
  13. func (s *stubCredRepo) GetByID(_ context.Context, _ int64) (*Account, error) {
  14. return s.parent, nil
  15. }
  16. func newStubCredRepo(parent *Account) AccountRepository {
  17. return &stubCredRepo{parent: parent}
  18. }
  19. func TestResolveCredentialAccount(t *testing.T) {
  20. ctx := context.Background()
  21. pid := int64(100)
  22. // 普通账号(非影子)→ 返回自身
  23. parent := &Account{ID: 100, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Status: StatusActive}
  24. repo := newStubCredRepo(parent)
  25. got, err := resolveCredentialAccount(ctx, repo, parent)
  26. require.NoError(t, err)
  27. require.Equal(t, int64(100), got.ID)
  28. // 影子账号 + 合法 OpenAI OAuth 母账号 → 返回母账号
  29. shadow := &Account{ID: 200, ParentAccountID: &pid, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
  30. got, err = resolveCredentialAccount(ctx, repo, shadow)
  31. require.NoError(t, err)
  32. require.Equal(t, int64(100), got.ID)
  33. // 影子账号 + 母账号非 OpenAI OAuth(API Key 类型)→ 返回 error
  34. badRepo := newStubCredRepo(&Account{ID: 100, Platform: PlatformOpenAI, Type: AccountTypeAPIKey})
  35. _, err = resolveCredentialAccount(ctx, badRepo, shadow)
  36. require.Error(t, err)
  37. }