| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- //go:build unit
- package service
- import (
- "context"
- "testing"
- "github.com/stretchr/testify/require"
- )
- type updatingProxyRepoStub struct {
- *proxyRepoStub
- proxy *Proxy
- updateCalls int
- }
- func (s *updatingProxyRepoStub) GetByID(context.Context, int64) (*Proxy, error) {
- copy := *s.proxy
- return ©, nil
- }
- func (s *updatingProxyRepoStub) Update(_ context.Context, proxy *Proxy) error {
- s.updateCalls++
- copy := *proxy
- s.proxy = ©
- return nil
- }
- func TestBothProxyUpdateServicesUseRepositoryUpdateBoundary(t *testing.T) {
- t.Run("ProxyService", func(t *testing.T) {
- repo := &updatingProxyRepoStub{
- proxyRepoStub: &proxyRepoStub{},
- proxy: &Proxy{ID: 9, Protocol: "http", Host: "old.example", Port: 8080, Status: StatusActive},
- }
- svc := NewProxyService(repo)
- host := "new.example"
- _, err := svc.Update(context.Background(), 9, UpdateProxyRequest{Host: &host})
- require.NoError(t, err)
- require.Equal(t, 1, repo.updateCalls)
- require.Equal(t, host, repo.proxy.Host)
- })
- t.Run("adminService", func(t *testing.T) {
- repo := &updatingProxyRepoStub{
- proxyRepoStub: &proxyRepoStub{},
- proxy: &Proxy{
- ID: 9,
- Protocol: "http",
- Host: "old.example",
- Port: 8080,
- Status: StatusActive,
- FallbackMode: FallbackModeNone,
- ExpiryWarnDays: 7,
- },
- }
- svc := &adminServiceImpl{proxyRepo: repo}
- warnDays := 7
- _, err := svc.UpdateProxy(context.Background(), 9, &UpdateProxyInput{
- Host: "new.example",
- FallbackMode: FallbackModeNone,
- ExpiryWarnDays: &warnDays,
- })
- require.NoError(t, err)
- require.Equal(t, 1, repo.updateCalls)
- require.Equal(t, "new.example", repo.proxy.Host)
- })
- }
|