admin_proxy_date_validation_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "encoding/json"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestAdminProxyRejectsOutOfRangeExpiry(t *testing.T) {
  11. for _, year := range []int{-1, 10000} {
  12. expiry := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
  13. t.Run(expiry.String(), func(t *testing.T) {
  14. // A nil repository ensures rejection happens before any persistence or probe.
  15. svc := &adminServiceImpl{}
  16. _, err := svc.CreateProxy(context.Background(), &CreateProxyInput{ExpiresAt: &expiry})
  17. require.Error(t, err)
  18. require.Contains(t, err.Error(), "proxy expiry year")
  19. _, err = svc.UpdateProxy(context.Background(), 1, &UpdateProxyInput{ExpiresAt: &expiry})
  20. require.Error(t, err)
  21. require.Contains(t, err.Error(), "proxy expiry year")
  22. })
  23. }
  24. }
  25. func TestAdminProxyAcceptsJSONExpiryBoundaries(t *testing.T) {
  26. for _, year := range []int{0, 9999} {
  27. expiry := time.Date(year, 12, 31, 23, 59, 59, 0, time.UTC)
  28. repo := &updatingProxyRepoStub{proxyRepoStub: &proxyRepoStub{}, proxy: &Proxy{ID: 9}}
  29. svc := &adminServiceImpl{proxyRepo: repo}
  30. got, err := svc.UpdateProxy(context.Background(), 9, &UpdateProxyInput{ExpiresAt: &expiry})
  31. require.NoError(t, err)
  32. _, err = json.Marshal(got)
  33. require.NoError(t, err)
  34. require.Equal(t, &expiry, repo.proxy.ExpiresAt)
  35. }
  36. }