From 514eaaac9eb08fb42d43f47cb980a048fe54490d Mon Sep 17 00:00:00 2001 From: Leon Silcott Date: Tue, 1 Nov 2022 06:05:22 +0000 Subject: [PATCH] Updated notify_test and fakeApi so we can verify that Notify handles the ChatPostMessage errors as we expect --- notifier/mattermost/mattermost_test.go | 4 +-- notifier/mattermost/notify_test.go | 44 ++++++++++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/notifier/mattermost/mattermost_test.go b/notifier/mattermost/mattermost_test.go index 35d073b..8b022ab 100644 --- a/notifier/mattermost/mattermost_test.go +++ b/notifier/mattermost/mattermost_test.go @@ -6,9 +6,9 @@ import ( type fakeAPI struct { API - FakeChatPostMessage func(attachments []slack.Attachment) error + ChatPostMessageError error } func (f *fakeAPI) ChatPostMessage(attachments []slack.Attachment) error { - return f.FakeChatPostMessage(attachments) + return f.ChatPostMessageError } diff --git a/notifier/mattermost/notify_test.go b/notifier/mattermost/notify_test.go index 435f112..fdcf652 100644 --- a/notifier/mattermost/notify_test.go +++ b/notifier/mattermost/notify_test.go @@ -1,18 +1,21 @@ package mattermost import ( + "fmt" "testing" - "github.com/ashwanthkumar/slack-go-webhook" "github.com/mercari/tfnotify/terraform" + "github.com/stretchr/testify/assert" ) func TestNotify(t *testing.T) { testCases := []struct { - config Config - body string - exitCode int - ok bool + config Config + body string + exitCode int + ok bool + fakeAPI *fakeAPI + expectedApiErrorString string }{ { config: Config{ @@ -26,6 +29,9 @@ func TestNotify(t *testing.T) { body: "Plan: 1 to add", exitCode: 0, ok: true, + fakeAPI: &fakeAPI{ + ChatPostMessageError: nil, + }, }, { config: Config{ @@ -39,11 +45,26 @@ func TestNotify(t *testing.T) { body: "Plan: 1 to add", exitCode: 0, ok: true, + fakeAPI: &fakeAPI{ + ChatPostMessageError: nil, + }, }, - } - fake := fakeAPI{ - FakeChatPostMessage: func(attachments []slack.Attachment) error { - return nil + { + config: Config{ + Webhook: "webhook", + Channel: "", + Botname: "botname", + Message: "", + Parser: terraform.NewPlanParser(), + Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate), + }, + body: "Plan: 1 to add", + exitCode: 0, + ok: false, + fakeAPI: &fakeAPI{ + ChatPostMessageError: fmt.Errorf("500 Internal Server Error"), + }, + expectedApiErrorString: "500 Internal Server Error", }, } @@ -52,7 +73,7 @@ func TestNotify(t *testing.T) { if err != nil { t.Fatal(err) } - client.API = &fake + client.API = testCase.fakeAPI exitCode, err := client.Notify.Notify(testCase.body) if (err == nil) != testCase.ok { t.Errorf("got error %q", err) @@ -60,5 +81,8 @@ func TestNotify(t *testing.T) { if exitCode != testCase.exitCode { t.Errorf("got %q but want %q", exitCode, testCase.exitCode) } + if err != nil { + assert.EqualError(t, err, testCase.expectedApiErrorString) + } } }