Skip to content

Commit

Permalink
test(notifier): add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Nov 6, 2023
1 parent fe17d86 commit 9e1a721
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/notifier/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func (s *Shoutrrr) Describe(callback func(service, params string)) {
}

func (s *Shoutrrr) Send(_ context.Context, ppfmt pp.PP, msg string) bool {
err := s.Router.Send(msg, &types.Params{})
if err != nil {
ppfmt.Errorf(pp.EmojiUserError, "Failed to send message: %v", err)
return false
errs := s.Router.Send(msg, &types.Params{})
allOk := true
for _, err := range errs {
if err != nil {
ppfmt.Errorf(pp.EmojiUserError, "Failed to send message: %v", err)
allOk = false
}

Check warning on line 61 in internal/notifier/shoutrrr.go

View check run for this annotation

Codecov / codecov/patch

internal/notifier/shoutrrr.go#L59-L61

Added lines #L59 - L61 were not covered by tests
}
return true
return allOk
}
52 changes: 52 additions & 0 deletions internal/notifier/shoutrrr_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package notifier_test

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -21,3 +25,51 @@ func TestShoutrrrDescripbe(t *testing.T) {
require.Equal(t, "generic", service)
})
}

func TestShoutrrrSend(t *testing.T) {
t.Parallel()

for name, tc := range map[string]struct {
url string
message string
pinged bool
ok bool
prepareMockPP func(*mocks.MockPP)
}{
"success": {
"/", "hello",
true, true,
func(m *mocks.MockPP) {
gomock.InOrder()
},
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)
mockPP := mocks.NewMockPP(mockCtrl)
if tc.prepareMockPP != nil {
tc.prepareMockPP(mockPP)
}

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())

reqBody, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Equal(t, tc.message, string(reqBody))

pinged = true
}))

s, ok := notifier.NewShoutrrr(mockPP, []string{"generic+" + server.URL + tc.url})
require.True(t, ok)
ok = s.Send(context.Background(), mockPP, tc.message)
require.Equal(t, tc.ok, ok)
require.Equal(t, tc.pinged, pinged)
})
}
}

0 comments on commit 9e1a721

Please sign in to comment.