From fb809214072de6baa0b8ab096d2e30e7fbdf8626 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Wed, 6 Jul 2022 16:03:39 +0200 Subject: [PATCH] gitlab.go: 100% code coverage Signed-off-by: Jefersson Nathan --- handler/gitlab.go | 3 +-- handler/gitlab_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/handler/gitlab.go b/handler/gitlab.go index 292d41c..d9391f3 100644 --- a/handler/gitlab.go +++ b/handler/gitlab.go @@ -57,8 +57,7 @@ func (g *Gitlab) ServeHTTP(writer http.ResponseWriter, request *http.Request) { return } - err = g.provider.NotifyMergeRequestCreated(mr) - if err != nil { + if err = g.provider.NotifyMergeRequestCreated(mr); err != nil { return err } diff --git a/handler/gitlab_test.go b/handler/gitlab_test.go index d5adb2e..ef1d6a9 100644 --- a/handler/gitlab_test.go +++ b/handler/gitlab_test.go @@ -1,6 +1,7 @@ package handler_test import ( + "errors" "io/ioutil" "net/http" "net/http/httptest" @@ -163,3 +164,30 @@ func TestPostOnSlackWithMergeRequestRejected(t *testing.T) { assert.Equal(t, "We cannot handle rejected event action", recorder.Body.String()) } + +func TestPostOnSlackWithReactToMessageFailure(t *testing.T) { + dat, err := ioutil.ReadFile("../payload/merge_request-rejected.json") + assert.Nil(t, err) + + recorder := httptest.NewRecorder() + request := httptest.NewRequest( + http.MethodPost, + "http://some-url.com", + strings.NewReader(string(dat)), + ) + + provider := &SpyProvider{ + NotifyMergeRequestCreatedFunc: func(mergeRequest model.MergeRequestInfo) (err error) { + assert.FailNow(t, "Code should not reach this method") + + return + }, + ReactToMessageFunc: func(mergeRequest model.MergeRequestInfo, reactionRule model.ReactionRule) (err error) { + return errors.New("Error from ReactToMessage") + }, + } + + handler.NewGitlab(provider).ServeHTTP(recorder, request) + + assert.Equal(t, "We cannot handle rejected event action", recorder.Body.String()) +}