Skip to content

Commit

Permalink
Add tests for search message and post reaction
Browse files Browse the repository at this point in the history
Signed-off-by: Jefersson Nathan <malukenho.dev@gmail.com>
  • Loading branch information
malukenho committed Jul 7, 2022
1 parent b2c4e7c commit 57e3e97
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
8 changes: 6 additions & 2 deletions provider/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ type LocatedMessage struct {
permalink string
}

type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

type Slack struct {
client *http.Client
client HTTPClient
notificationRules []model.NotificationRule
reactionRules []model.ReactionRule
webhookURL, message, avatar, username, token string
}

func NewSlack(client *http.Client, notificationRules []model.NotificationRule, reactionRules []model.ReactionRule, webhookURL, token, message, avatar, username string) *Slack {
func NewSlack(client HTTPClient, notificationRules []model.NotificationRule, reactionRules []model.ReactionRule, webhookURL, token, message, avatar, username string) *Slack {
return &Slack{
client: client,
notificationRules: notificationRules,
Expand Down
126 changes: 126 additions & 0 deletions provider/slack_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package provider

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"testing"
Expand All @@ -10,6 +12,18 @@ import (
"github.com/stretchr/testify/assert"
)

type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}

var (
GetDoFunc func(req *http.Request) (*http.Response, error)
)

func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return GetDoFunc(req)
}

func TestChannelsForMergeRequestSingleRule(t *testing.T) {
var mergeRequest model.MergeRequestInfo
var jsonString string
Expand Down Expand Up @@ -43,6 +57,7 @@ func TestChannelsForMergeRequestSingleRule(t *testing.T) {
)

assert.Equal(t, []string{"#tested"}, slack.ChannelsForMergeRequest(mergeRequest))
assert.Equal(t, []model.ReactionRule{{Action: "approved", Reaction: "thumbsup"}}, slack.GetReactionRules())
}

func TestChannelsForMergeRequestMultipleRules(t *testing.T) {
Expand Down Expand Up @@ -149,3 +164,114 @@ func TestChannelsForMergeRequestNotMatchingLabel(t *testing.T) {

assert.Equal(t, []string{}, slack.ChannelsForMergeRequest(mergeRequest))
}

func TestSearchForMessage(t *testing.T) {
var notifications []model.NotificationRule
var reactions []model.ReactionRule

json := `{"messages": {"matches": [{"channel": {"name": "channel-name", "id": "channel-id"}, "ts": "123", "permalink": "http://message-link"}]}}`
client := &MockClient{}
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
GetDoFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}

slack := NewSlack(
client,
notifications,
reactions,
"https://testing.com",
"New MR",
"https://avatar",
"Username",
"Token",
)

locatedMessage, err := slack.search("New MergeRequest Created")
assert.Nil(t, err)
assert.Equal(t, "channel-name", locatedMessage.channelName)
assert.Equal(t, "channel-id", locatedMessage.channelID)
assert.Equal(t, "123", locatedMessage.timestamp)
assert.Equal(t, "http://message-link", locatedMessage.permalink)
}

func TestPostReaction(t *testing.T) {
var notifications []model.NotificationRule
var reactions []model.ReactionRule

json := `{}`
client := &MockClient{}
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
GetDoFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}

slack := NewSlack(client, notifications, reactions, "https://testing.com", "New MR", "https://avatar", "Username", "Token")

locatedMessage := LocatedMessage{
channelID: "channel-id",
channelName: "channel-name",
timestamp: "123",
permalink: "http://message-link",
}

err := slack.postReaction(locatedMessage, model.ReactionRule{Action: "approved", Reaction: "thumbsup"})
assert.Nil(t, err)
}

func TestPostReactionFailsWithStatusCode(t *testing.T) {
var notifications []model.NotificationRule
var reactions []model.ReactionRule

json := `{}`
client := &MockClient{}
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
GetDoFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 404,
Body: r,
}, nil
}

slack := NewSlack(client, notifications, reactions, "https://testing.com", "New MR", "https://avatar", "Username", "Token")

locatedMessage := LocatedMessage{
channelID: "channel-id",
channelName: "channel-name",
timestamp: "123",
permalink: "http://message-link",
}

err := slack.postReaction(locatedMessage, model.ReactionRule{Action: "approved", Reaction: "thumbsup"})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "response codeStatus code: 404, expected 200")
}

func TestPostReactionFailsWithClientError(t *testing.T) {
var notifications []model.NotificationRule
var reactions []model.ReactionRule

client := &MockClient{}
GetDoFunc = func(*http.Request) (*http.Response, error) {
return nil, errors.New("Error from web server")
}

slack := NewSlack(client, notifications, reactions, "https://testing.com", "New MR", "https://avatar", "Username", "Token")

locatedMessage := LocatedMessage{
channelID: "channel-id",
channelName: "channel-name",
timestamp: "123",
permalink: "http://message-link",
}

err := slack.postReaction(locatedMessage, model.ReactionRule{Action: "approved", Reaction: "thumbsup"})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "Error from web server")
}

0 comments on commit 57e3e97

Please sign in to comment.