diff --git a/alert_test.go b/alert_test.go index 717890d..453adb5 100644 --- a/alert_test.go +++ b/alert_test.go @@ -282,3 +282,149 @@ func TestAlert_isThrottlingEnabled(t *testing.T) { }) } } + +func TestNewMsTeam(t *testing.T) { + tests := []struct { + name string + err error + expandos *Expandos + want MsTeam + }{ + { + name: "default", + err: errors.New("test error"), + expandos: &Expandos{ + MsTeamsAlertCardSubject: "Test Alert", + MsTeamsCardSubject: "Test Card", + MsTeamsError: "Test Error", + }, + want: MsTeam{ + Type: "AdaptiveCard", + Version: "1.2", + Body: []BodyStruct{ + { + Type: "TextBlock", + Text: "Test Alert", + Items: []ItemStruct{ + { + Type: "TextBlock", + Text: "Test Card", + Weight: "Bolder", + Size: "Medium", + }, + { + Type: "TextBlock", + Text: "Error: Test Error", + Weight: "Lighter", + Size: "Small", + }, + }, + }, + }, + Actions: []ActionStruct{ + { + Type: "Action.OpenUrl", + Title: "View Details", + URL: os.Getenv("MS_TEAMS_WEBHOOK"), + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewMsTeam(tt.err, tt.expandos) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewMsTeam() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMsTeam_Send(t *testing.T) { + tests := []struct { + name string + card MsTeam + wantErr bool + }{ + { + name: "send_success", + card: MsTeam{ + Type: "AdaptiveCard", + Version: "1.2", + Body: []BodyStruct{ + { + Type: "TextBlock", + Text: "Test Alert", + Items: []ItemStruct{ + { + Type: "TextBlock", + Text: "Test Card", + Weight: "Bolder", + Size: "Medium", + }, + { + Type: "TextBlock", + Text: "Error: Test Error", + Weight: "Lighter", + Size: "Small", + }, + }, + }, + }, + Actions: []ActionStruct{ + { + Type: "Action.OpenUrl", + Title: "View Details", + URL: os.Getenv("MS_TEAMS_WEBHOOK"), + }, + }, + }, + wantErr: false, + }, + { + name: "send_failure", + card: MsTeam{ + Type: "AdaptiveCard", + Version: "1.2", + Body: []BodyStruct{ + { + Type: "TextBlock", + Text: "Test Alert", + Items: []ItemStruct{ + { + Type: "TextBlock", + Text: "Test Card", + Weight: "Bolder", + Size: "Medium", + }, + { + Type: "TextBlock", + Text: "Error: Test Error", + Weight: "Lighter", + Size: "Small", + }, + }, + }, + }, + Actions: []ActionStruct{ + { + Type: "Action.OpenUrl", + Title: "View Details", + URL: "", + }, + }, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.card.Send(); (err != nil) != tt.wantErr { + t.Errorf("MsTeam.Send() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/ms_teams.go b/ms_teams.go index 4eefb61..59e4bef 100644 --- a/ms_teams.go +++ b/ms_teams.go @@ -12,28 +12,34 @@ import ( "time" ) -// MsTeam is MessageCard for Team notification +// MsTeam is AdaptiveCard for Team notification type MsTeam struct { - Type string `json:"@type"` - Context string `json:"@context"` - Summary string `json:"summary"` - ThemeColor string `json:"themeColor"` - Title string `json:"title"` - Sections []SectionStruct `json:"sections"` + Type string `json:"type"` + Version string `json:"version"` + Body []BodyStruct `json:"body"` + Actions []ActionStruct `json:"actions"` } -// SectionStruct is sub-struct of MsTeam -type SectionStruct struct { - ActivityTitle string `json:"activityTitle"` - ActivitySubtitle string `json:"activitySubtitle"` - ActivityImage string `json:"activityImage"` - Facts []FactStruct `json:"facts"` +// BodyStruct is sub-struct of MsTeam +type BodyStruct struct { + Type string `json:"type"` + Text string `json:"text"` + Items []ItemStruct `json:"items"` } -// FactStruct is sub-struct of SectionStruct -type FactStruct struct { - Name string `json:"name"` - Value string `json:"value"` +// ItemStruct is sub-struct of BodyStruct +type ItemStruct struct { + Type string `json:"type"` + Text string `json:"text"` + Weight string `json:"weight"` + Size string `json:"size"` +} + +// ActionStruct is sub-struct of MsTeam +type ActionStruct struct { + Type string `json:"type"` + Title string `json:"title"` + URL string `json:"url"` } // NewMsTeam is used to create MsTeam @@ -55,28 +61,35 @@ func NewMsTeam(err error, expandos *Expandos) MsTeam { } notificationCard := MsTeam{ - Type: "MessageCard", - Context: "http://schema.org/extensions", - Summary: summary, - ThemeColor: os.Getenv("ALERT_THEME_COLOR"), - Title: title, - Sections: []SectionStruct{ - SectionStruct{ - ActivityTitle: summary, - ActivitySubtitle: fmt.Sprintf("error has occured on %v", os.Getenv("APP_NAME")), - ActivityImage: "", - Facts: []FactStruct{ - FactStruct{ - Name: "Environment:", - Value: os.Getenv("APP_ENV"), + Type: "AdaptiveCard", + Version: "1.2", + Body: []BodyStruct{ + BodyStruct{ + Type: "TextBlock", + Text: title, + Items: []ItemStruct{ + ItemStruct{ + Type: "TextBlock", + Text: summary, + Weight: "Bolder", + Size: "Medium", }, - FactStruct{ - Name: "ERROR", - Value: errMsg, + ItemStruct{ + Type: "TextBlock", + Text: fmt.Sprintf("Error: %s", errMsg), + Weight: "Lighter", + Size: "Small", }, }, }, }, + Actions: []ActionStruct{ + ActionStruct{ + Type: "Action.OpenUrl", + Title: "View Details", + URL: os.Getenv("MS_TEAMS_WEBHOOK"), + }, + }, } return notificationCard }