api_key_service_validation_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //go:build unit
  2. package service
  3. import (
  4. "math"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestValidateCreateAPIKeyRequestNumericLimits(t *testing.T) {
  9. positiveExpiry := 1
  10. require.NoError(t, validateCreateAPIKeyRequest(CreateAPIKeyRequest{
  11. Quota: 1e100, RateLimit5h: 1e100, ExpiresInDays: &positiveExpiry,
  12. }))
  13. require.NoError(t, validateCreateAPIKeyRequest(CreateAPIKeyRequest{}))
  14. invalidExpiry := 0
  15. tests := []CreateAPIKeyRequest{
  16. {Quota: -1},
  17. {Quota: math.NaN()},
  18. {Quota: math.Inf(1)},
  19. {RateLimit5h: -1},
  20. {RateLimit1d: math.NaN()},
  21. {RateLimit7d: math.Inf(-1)},
  22. {ExpiresInDays: &invalidExpiry},
  23. }
  24. for _, req := range tests {
  25. require.Error(t, validateCreateAPIKeyRequest(req))
  26. }
  27. }
  28. func TestValidateUpdateAPIKeyRequestNumericLimits(t *testing.T) {
  29. zero, large, negative, nan, inf := 0.0, 1e100, -1.0, math.NaN(), math.Inf(1)
  30. require.NoError(t, validateUpdateAPIKeyRequest(UpdateAPIKeyRequest{Quota: &zero, RateLimit7d: &large}))
  31. for _, req := range []UpdateAPIKeyRequest{
  32. {Quota: &negative},
  33. {RateLimit5h: &nan},
  34. {RateLimit1d: &inf},
  35. {RateLimit7d: &negative},
  36. } {
  37. require.Error(t, validateUpdateAPIKeyRequest(req))
  38. }
  39. }