gatus/alerting/provider/mattermost/mattermost_test.go

66 lines
2.5 KiB
Go
Raw Normal View History

2020-11-14 15:55:37 +01:00
package mattermost
import (
2021-02-20 01:06:20 +01:00
"encoding/json"
"net/http"
2020-11-14 15:55:37 +01:00
"strings"
"testing"
2020-12-11 02:00:42 +01:00
"github.com/TwinProduction/gatus/core"
2020-11-14 15:55:37 +01:00
)
func TestAlertProvider_IsValid(t *testing.T) {
invalidProvider := AlertProvider{WebhookURL: ""}
if invalidProvider.IsValid() {
t.Error("provider shouldn't have been valid")
}
validProvider := AlertProvider{WebhookURL: "http://example.com"}
if !validProvider.IsValid() {
t.Error("provider should've been valid")
}
}
func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
2021-02-20 18:52:21 +01:00
provider := AlertProvider{WebhookURL: "http://example.org"}
2020-11-14 15:55:37 +01:00
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true)
if customAlertProvider == nil {
2021-02-20 01:06:20 +01:00
t.Fatal("customAlertProvider shouldn't have been nil")
2020-11-14 15:55:37 +01:00
}
if !strings.Contains(customAlertProvider.Body, "resolved") {
t.Error("customAlertProvider.Body should've contained the substring resolved")
}
2021-02-20 18:52:21 +01:00
if customAlertProvider.URL != "http://example.org" {
t.Errorf("expected URL to be %s, got %s", "http://example.org", customAlertProvider.URL)
2021-02-20 01:06:20 +01:00
}
if customAlertProvider.Method != http.MethodPost {
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
}
body := make(map[string]interface{})
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
if err != nil {
t.Error("expected body to be valid JSON, got error:", err.Error())
}
2020-11-14 15:55:37 +01:00
}
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
2021-02-20 18:52:21 +01:00
provider := AlertProvider{WebhookURL: "http://example.org"}
2020-11-14 15:55:37 +01:00
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false)
if customAlertProvider == nil {
2021-02-20 01:06:20 +01:00
t.Fatal("customAlertProvider shouldn't have been nil")
2020-11-14 15:55:37 +01:00
}
if !strings.Contains(customAlertProvider.Body, "triggered") {
t.Error("customAlertProvider.Body should've contained the substring triggered")
}
2021-02-20 18:52:21 +01:00
if customAlertProvider.URL != "http://example.org" {
t.Errorf("expected URL to be %s, got %s", "http://example.org", customAlertProvider.URL)
2021-02-20 01:06:20 +01:00
}
if customAlertProvider.Method != http.MethodPost {
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
}
body := make(map[string]interface{})
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
if err != nil {
t.Error("expected body to be valid JSON, got error:", err.Error())
}
2020-11-14 15:55:37 +01:00
}