proxy_update_probe_invalidation_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. type updatingProxyRepoStub struct {
  9. *proxyRepoStub
  10. proxy *Proxy
  11. updateCalls int
  12. }
  13. func (s *updatingProxyRepoStub) GetByID(context.Context, int64) (*Proxy, error) {
  14. copy := *s.proxy
  15. return &copy, nil
  16. }
  17. func (s *updatingProxyRepoStub) Update(_ context.Context, proxy *Proxy) error {
  18. s.updateCalls++
  19. copy := *proxy
  20. s.proxy = &copy
  21. return nil
  22. }
  23. func TestBothProxyUpdateServicesUseRepositoryUpdateBoundary(t *testing.T) {
  24. t.Run("ProxyService", func(t *testing.T) {
  25. repo := &updatingProxyRepoStub{
  26. proxyRepoStub: &proxyRepoStub{},
  27. proxy: &Proxy{ID: 9, Protocol: "http", Host: "old.example", Port: 8080, Status: StatusActive},
  28. }
  29. svc := NewProxyService(repo)
  30. host := "new.example"
  31. _, err := svc.Update(context.Background(), 9, UpdateProxyRequest{Host: &host})
  32. require.NoError(t, err)
  33. require.Equal(t, 1, repo.updateCalls)
  34. require.Equal(t, host, repo.proxy.Host)
  35. })
  36. t.Run("adminService", func(t *testing.T) {
  37. repo := &updatingProxyRepoStub{
  38. proxyRepoStub: &proxyRepoStub{},
  39. proxy: &Proxy{
  40. ID: 9,
  41. Protocol: "http",
  42. Host: "old.example",
  43. Port: 8080,
  44. Status: StatusActive,
  45. FallbackMode: FallbackModeNone,
  46. ExpiryWarnDays: 7,
  47. },
  48. }
  49. svc := &adminServiceImpl{proxyRepo: repo}
  50. warnDays := 7
  51. _, err := svc.UpdateProxy(context.Background(), 9, &UpdateProxyInput{
  52. Host: "new.example",
  53. FallbackMode: FallbackModeNone,
  54. ExpiryWarnDays: &warnDays,
  55. })
  56. require.NoError(t, err)
  57. require.Equal(t, 1, repo.updateCalls)
  58. require.Equal(t, "new.example", repo.proxy.Host)
  59. })
  60. }