upgrade024_custom_regression_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestSecurityPolicyAuthSnapshotRoundTrip(t *testing.T) {
  8. svc := &APIKeyService{}
  9. key := &APIKey{ID: 1, UserID: 2, User: &User{ID: 2}, Group: &Group{
  10. ID: 3, SecurityPolicyEnabled: true, SecurityPolicyMode: SecurityPolicyModeBlockRequest, SecurityPolicyEmailEnabled: true,
  11. }}
  12. snapshot := svc.snapshotFromAPIKey(context.Background(), key)
  13. restored := svc.snapshotToAPIKey("test-key", snapshot)
  14. require.True(t, restored.Group.SecurityPolicyEnabled)
  15. require.Equal(t, SecurityPolicyModeBlockRequest, restored.Group.SecurityPolicyMode)
  16. require.True(t, restored.Group.SecurityPolicyEmailEnabled)
  17. _, accepted, err := svc.applyAuthCacheEntry("test-key", &APIKeyAuthCacheEntry{Snapshot: &APIKeyAuthSnapshot{Version: 24}})
  18. require.NoError(t, err)
  19. require.False(t, accepted, "old cache entries omit security policy and must be reloaded")
  20. }
  21. func TestGlobalPricingOverridesGroupImagePriceInBothGateways(t *testing.T) {
  22. ctx := context.Background()
  23. billing := &BillingService{}
  24. global := NewGlobalModelPricingService(&stubGlobalPricingRepo{rows: []GlobalModelPrice{{
  25. ID: 1, ModelPattern: "test-image", BillingMode: BillingModeImage, PerRequestPrice: fptr(0.2), Enabled: true,
  26. }}})
  27. resolver := NewModelPricingResolverWithGlobal(nil, billing, global)
  28. key := &APIKey{Group: &Group{ID: 3, ImagePrice1K: fptr(99)}}
  29. gateway := &GatewayService{billingService: billing, resolver: resolver}
  30. cost := gateway.calculateImageCost(ctx, &ForwardResult{ImageCount: 2, ImageSize: "1K"}, key, "test-image", 1)
  31. require.InDelta(t, 0.4, cost.ActualCost, 1e-9)
  32. openAI := &OpenAIGatewayService{billingService: billing, resolver: resolver}
  33. cost = openAI.calculateOpenAIImageCost(ctx, "test-image", key, &OpenAIForwardResult{ImageCount: 2, ImageSize: "1K"}, 1)
  34. require.InDelta(t, 0.4, cost.ActualCost, 1e-9)
  35. }