From b1c39c13a57195849403cd7db3b371baae7fff0b Mon Sep 17 00:00:00 2001 From: favonia Date: Mon, 6 Nov 2023 02:04:26 -0600 Subject: [PATCH] test: more tests --- internal/monitor/healthchecks.go | 2 +- internal/monitor/healthchecks_test.go | 12 ++++++------ internal/monitor/uptimekuma.go | 2 +- internal/monitor/uptimekuma_test.go | 2 +- internal/notifier/shoutrrr.go | 5 ++++- internal/notifier/shoutrrr_test.go | 23 ++++++++++++++++++----- internal/pp/emoji.go | 4 ++-- 7 files changed, 33 insertions(+), 17 deletions(-) diff --git a/internal/monitor/healthchecks.go b/internal/monitor/healthchecks.go index cafc2e5f..02c2d345 100644 --- a/internal/monitor/healthchecks.go +++ b/internal/monitor/healthchecks.go @@ -149,7 +149,7 @@ func (h *Healthchecks) ping(ctx context.Context, ppfmt pp.PP, endpoint string, m return false } - ppfmt.Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", endpointDescription) + ppfmt.Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", endpointDescription) return true } diff --git a/internal/monitor/healthchecks_test.go b/internal/monitor/healthchecks_test.go index 405fdfc8..839d86f7 100644 --- a/internal/monitor/healthchecks_test.go +++ b/internal/monitor/healthchecks_test.go @@ -114,7 +114,7 @@ func TestHealthchecksEndPoints(t *testing.T) { func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `default (root)`), //nolint:lll + m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `default (root)`), ) }, }, @@ -159,7 +159,7 @@ func TestHealthchecksEndPoints(t *testing.T) { func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/start"`), //nolint:lll + m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/start"`), ) }, }, @@ -174,7 +174,7 @@ func TestHealthchecksEndPoints(t *testing.T) { func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/fail"`), //nolint:lll + m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/fail"`), ) }, }, @@ -189,7 +189,7 @@ func TestHealthchecksEndPoints(t *testing.T) { func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/log"`), //nolint:lll + m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/log"`), ) }, }, @@ -204,7 +204,7 @@ func TestHealthchecksEndPoints(t *testing.T) { func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/0"`), + m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/0"`), ) }, }, @@ -219,7 +219,7 @@ func TestHealthchecksEndPoints(t *testing.T) { func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/1"`), + m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/1"`), ) }, }, diff --git a/internal/monitor/uptimekuma.go b/internal/monitor/uptimekuma.go index 61fce814..51e5beff 100644 --- a/internal/monitor/uptimekuma.go +++ b/internal/monitor/uptimekuma.go @@ -147,7 +147,7 @@ func (h *UptimeKuma) ping(ctx context.Context, ppfmt pp.PP, param UptimeKumaRequ return false } - ppfmt.Infof(pp.EmojiNotification, "Successfully pinged Uptime Kuma") + ppfmt.Infof(pp.EmojiPing, "Pinged Uptime Kuma") return true } diff --git a/internal/monitor/uptimekuma_test.go b/internal/monitor/uptimekuma_test.go index e88b186a..f378f609 100644 --- a/internal/monitor/uptimekuma_test.go +++ b/internal/monitor/uptimekuma_test.go @@ -102,7 +102,7 @@ func TestUptimeKumaEndPoints(t *testing.T) { successPP := func(m *mocks.MockPP) { gomock.InOrder( m.EXPECT().Warningf(pp.EmojiUserWarning, httpUnsafeMsg), - m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged Uptime Kuma"), + m.EXPECT().Infof(pp.EmojiPing, "Pinged Uptime Kuma"), ) } diff --git a/internal/notifier/shoutrrr.go b/internal/notifier/shoutrrr.go index d6c586bd..3ffa9422 100644 --- a/internal/notifier/shoutrrr.go +++ b/internal/notifier/shoutrrr.go @@ -56,9 +56,12 @@ func (s *Shoutrrr) Send(_ context.Context, ppfmt pp.PP, msg string) bool { allOk := true for _, err := range errs { if err != nil { - ppfmt.Errorf(pp.EmojiUserError, "Failed to send message: %v", err) + ppfmt.Errorf(pp.EmojiError, "Failed to send some shoutrrr message: %v", err) allOk = false } } + if allOk { + ppfmt.Infof(pp.EmojiNotification, "Sent shoutrrr message") + } return allOk } diff --git a/internal/notifier/shoutrrr_test.go b/internal/notifier/shoutrrr_test.go index 67853ce8..ec3d4dbf 100644 --- a/internal/notifier/shoutrrr_test.go +++ b/internal/notifier/shoutrrr_test.go @@ -12,6 +12,7 @@ import ( "github.com/favonia/cloudflare-ddns/internal/mocks" "github.com/favonia/cloudflare-ddns/internal/notifier" + "github.com/favonia/cloudflare-ddns/internal/pp" ) func TestShoutrrrDescripbe(t *testing.T) { @@ -30,17 +31,29 @@ func TestShoutrrrSend(t *testing.T) { t.Parallel() for name, tc := range map[string]struct { - url string + path string + service func(serverURL string) string message string pinged bool ok bool prepareMockPP func(*mocks.MockPP) }{ "success": { - "/", "hello", + "/greeting", + func(serverURL string) string { return "generic+" + serverURL + "/greeting" }, + "hello", true, true, func(m *mocks.MockPP) { - gomock.InOrder() + m.EXPECT().Infof(pp.EmojiNotification, "Sent shoutrrr message") + }, + }, + "ill-formed url": { + "", + func(_serverURL string) string { return "generic+https://0.0.0.0" }, + "hello", + false, false, + func(m *mocks.MockPP) { + m.EXPECT().Errorf(pp.EmojiError, "Failed to send some shoutrrr message: %v", gomock.Any()) }, }, } { @@ -56,7 +69,7 @@ func TestShoutrrrSend(t *testing.T) { pinged := false server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { require.Equal(t, http.MethodPost, r.Method) - require.Equal(t, tc.url, r.URL.EscapedPath()) + require.Equal(t, tc.path, r.URL.EscapedPath()) reqBody, err := io.ReadAll(r.Body) require.NoError(t, err) @@ -65,7 +78,7 @@ func TestShoutrrrSend(t *testing.T) { pinged = true })) - s, ok := notifier.NewShoutrrr(mockPP, []string{"generic+" + server.URL + tc.url}) + s, ok := notifier.NewShoutrrr(mockPP, []string{tc.service(server.URL)}) require.True(t, ok) ok = s.Send(context.Background(), mockPP, tc.message) require.Equal(t, tc.ok, ok) diff --git a/internal/pp/emoji.go b/internal/pp/emoji.go index 792710ea..184fe7c5 100644 --- a/internal/pp/emoji.go +++ b/internal/pp/emoji.go @@ -19,8 +19,8 @@ const ( EmojiUpdateRecord Emoji = "๐Ÿ“ก" // updating DNS records EmojiClearRecord Emoji = "๐Ÿงน" // clearing DNS records - EmojiNotification Emoji = "๐Ÿ””" // sending out notifications, pinging, health checks - EmojiRepeat Emoji = "๐Ÿ”" // repeating things + EmojiPing Emoji = "๐Ÿ””" // pinging and health checks + EmojiNotification Emoji = "๐Ÿ“จ" // notifications EmojiSignal Emoji = "๐Ÿšจ" // catching signals EmojiAlreadyDone Emoji = "๐Ÿคท" // DNS records were already up to date