smtp2shoutrrr/config_test.go
Full-Stack Developer 8c12783e95
Some checks failed
CI / goreleaser-lint (pull_request) Failing after 37s
CI / format (pull_request) Successful in 1m20s
CI / test (pull_request) Successful in 2m36s
CI / lint (pull_request) Successful in 3m34s
CI / build (pull_request) Has been cancelled
deps: upgrade Go to 1.27.1, refresh dependencies and drop gotoolkit
Replaces the gotoolkit helpers with the standard library and the
libraries they wrapped:

- gotoolkit/encoding TOML wrapper -> pelletier/go-toml/v2 directly,
  behind a new smtp2shoutrrr.LoadConfig shared by both commands.
- gotoolkit/service + gotoolkit/model.Server -> signal.NotifyContext
  and a concrete *Server whose Start shuts the listener down
  gracefully when its context is cancelled.

Also drops the unused ANONYMOUS SASL client from cmd/sendmail, bumps
alpine, golangci-lint and the CI actions, and adds coverage for config
loading and the server lifecycle.

Closes FMG-2

Co-authored-by: multica-agent <github@multica.ai>
2026-09-07 16:28:11 +00:00

78 lines
1.8 KiB
Go

package smtp2shoutrrr
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func writeConfig(t *testing.T, contents string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "config.toml")
require.NoError(t, os.WriteFile(path, []byte(contents), 0o600))
return path
}
func TestLoadConfig(t *testing.T) {
path := writeConfig(t, `
Port = 2525
Username = "user"
Password = "secret"
[[Recipients]]
Addresses = ["user@example.com"]
Targets = ["ntfy://ntfy.sh/topic"]
[[Recipients]]
Addresses = ["legacy@example.com"]
Target = "ntfy://ntfy.sh/legacy"
[CatchAll]
Targets = ["ntfy://ntfy.sh/catch-all"]
`)
config, err := LoadConfig(path)
require.NoError(t, err)
require.Equal(t, 2525, config.Port)
require.Equal(t, "user", config.Username)
require.Equal(t, "secret", config.Password)
require.Len(t, config.Recipients, 2)
require.Equal(t, []string{"user@example.com"}, config.Recipients[0].Addresses)
require.Equal(t, []string{"ntfy://ntfy.sh/topic"}, config.Recipients[0].Targets)
require.Equal(t, "ntfy://ntfy.sh/legacy", config.Recipients[1].Target)
require.NotNil(t, config.CatchAll)
require.Equal(t, []string{"ntfy://ntfy.sh/catch-all"}, config.CatchAll.Targets)
}
func TestLoadConfigAppliesDefaults(t *testing.T) {
path := writeConfig(t, `
[[Recipients]]
Addresses = ["user@example.com"]
Targets = ["ntfy://ntfy.sh/topic"]
`)
config, err := LoadConfig(path)
require.NoError(t, err)
require.Equal(t, 11125, config.Port)
require.Equal(t, "username", config.Username)
require.Equal(t, "password", config.Password)
require.Nil(t, config.CatchAll)
}
func TestLoadConfigMissingFile(t *testing.T) {
_, err := LoadConfig(filepath.Join(t.TempDir(), "does-not-exist.toml"))
require.Error(t, err)
}
func TestLoadConfigInvalidTOML(t *testing.T) {
_, err := LoadConfig(writeConfig(t, "Port = "))
require.Error(t, err)
}