Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <[email protected]>
  • Loading branch information
somtochiama committed Aug 25, 2021
1 parent 7036311 commit 45bb0e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/notifier/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (f Factory) Notifier(provider string) (Interface, error) {
case v1beta1.TelegramProvider:
n, err = NewTelegram(f.Channel, f.Token)
case v1beta1.LarkProvider:
n = NewLark(f.URL)
n, err = NewLark(f.URL)
default:
err = fmt.Errorf("provider %s not supported", provider)
}
Expand Down
10 changes: 8 additions & 2 deletions internal/notifier/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notifier

import (
"fmt"
"net/url"
"strings"

"github.com/fluxcd/pkg/runtime/events"
Expand Down Expand Up @@ -48,10 +49,15 @@ type LarkText struct {
Content string `json:"content"`
}

func NewLark(address string) *Lark {
func NewLark(address string) (*Lark, error) {
_, err := url.ParseRequestURI(address)
if err != nil {
return nil, fmt.Errorf("invalid Slack hook URL %s", address)
}

return &Lark{
URL: address,
}
}, nil
}

func (l *Lark) Post(event events.Event) error {
Expand Down
31 changes: 31 additions & 0 deletions internal/notifier/lark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package notifier

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestLark_Post(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
var payload LarkPayload
err = json.Unmarshal(b, &payload)
require.NoError(t, err)

require.Equal(t, "💫 gitrepository/webapp.gitops-system", payload.Card.Header.Title.Content)
require.Equal(t, "turquoise", payload.Card.Header.Template)
}))
defer ts.Close()

lark, err := NewLark(ts.URL)
require.NoError(t, err)

err = lark.Post(testEvent())
require.NoError(t, err)
}

0 comments on commit 45bb0e9

Please sign in to comment.