account_handler_passthrough_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package admin
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/gin-gonic/gin"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestAccountHandler_Create_AnthropicAPIKeyPassthroughExtraForwarded(t *testing.T) {
  12. gin.SetMode(gin.TestMode)
  13. adminSvc := newStubAdminService()
  14. handler := NewAccountHandler(
  15. adminSvc,
  16. nil,
  17. nil,
  18. nil,
  19. nil,
  20. nil,
  21. nil,
  22. nil,
  23. nil,
  24. nil,
  25. nil,
  26. nil,
  27. nil,
  28. nil,
  29. )
  30. router := gin.New()
  31. router.POST("/api/v1/admin/accounts", handler.Create)
  32. body := map[string]any{
  33. "name": "anthropic-key-1",
  34. "platform": "anthropic",
  35. "type": "apikey",
  36. "credentials": map[string]any{
  37. "api_key": "sk-ant-xxx",
  38. "base_url": "https://api.anthropic.com",
  39. },
  40. "extra": map[string]any{
  41. "anthropic_passthrough": true,
  42. },
  43. "concurrency": 1,
  44. "priority": 1,
  45. }
  46. raw, err := json.Marshal(body)
  47. require.NoError(t, err)
  48. rec := httptest.NewRecorder()
  49. req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/accounts", bytes.NewReader(raw))
  50. req.Header.Set("Content-Type", "application/json")
  51. router.ServeHTTP(rec, req)
  52. require.Equal(t, http.StatusOK, rec.Code)
  53. require.Len(t, adminSvc.createdAccounts, 1)
  54. created := adminSvc.createdAccounts[0]
  55. require.Equal(t, "anthropic", created.Platform)
  56. require.Equal(t, "apikey", created.Type)
  57. require.NotNil(t, created.Extra)
  58. require.Equal(t, true, created.Extra["anthropic_passthrough"])
  59. }