openai_gateway_service_shadow_token_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. // TestGetAccessToken_SparkShadowResolvesToParent 验证对影子账号调用 GetAccessToken
  8. // 时能透明地解析到母账号的凭据,防止 refresh_token 脱钩。
  9. // 影子账号不持凭据;断言必须返回母账号的 access_token。
  10. func TestGetAccessToken_SparkShadowResolvesToParent(t *testing.T) {
  11. ctx := context.Background()
  12. parentID := int64(100)
  13. parent := Account{
  14. ID: parentID,
  15. Platform: PlatformOpenAI,
  16. Type: AccountTypeOAuth,
  17. Status: StatusActive,
  18. Credentials: map[string]any{
  19. "access_token": "parent-access-token",
  20. },
  21. }
  22. shadow := Account{
  23. ID: 200,
  24. Platform: PlatformOpenAI,
  25. Type: AccountTypeOAuth,
  26. ParentAccountID: &parentID,
  27. // 影子账号不持凭据,与生产语义一致
  28. }
  29. repo := &stubOpenAIAccountRepo{accounts: []Account{parent}}
  30. svc := &OpenAIGatewayService{
  31. accountRepo: repo,
  32. // openAITokenProvider=nil → 走降级路径,直接读 account.GetOpenAIAccessToken()
  33. }
  34. // Before fix (RED): shadow 无凭据 → GetOpenAIAccessToken()="" → error
  35. // After fix (GREEN): shadow 解析到 parent → 返回 parent 的 "parent-access-token"
  36. token, tokenType, err := svc.GetAccessToken(ctx, &shadow)
  37. require.NoError(t, err)
  38. require.Equal(t, "parent-access-token", token)
  39. require.Equal(t, "oauth", tokenType)
  40. }