2022-10-05 05:26:34 +02:00
|
|
|
package ntfy
|
|
|
|
|
2022-10-09 22:39:51 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/TwiN/gatus/v4/alerting/alert"
|
|
|
|
"github.com/TwiN/gatus/v4/core"
|
|
|
|
)
|
2022-10-05 05:26:34 +02:00
|
|
|
|
|
|
|
func TestAlertDefaultProvider_IsValid(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
provider AlertProvider
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid",
|
|
|
|
provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 1},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2022-10-09 22:34:08 +02:00
|
|
|
name: "no-url-should-use-default-value",
|
|
|
|
provider: AlertProvider{Topic: "example", Priority: 1},
|
|
|
|
expected: true,
|
2022-10-05 05:26:34 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-topic",
|
|
|
|
provider: AlertProvider{URL: "https://ntfy.sh", Topic: "", Priority: 1},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-priority-too-high",
|
|
|
|
provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 6},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-priority-too-low",
|
2022-10-09 22:34:08 +02:00
|
|
|
provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: -1},
|
2022-10-05 05:26:34 +02:00
|
|
|
expected: false,
|
|
|
|
},
|
2022-10-09 22:34:08 +02:00
|
|
|
{
|
|
|
|
name: "no-priority-should-use-default-value",
|
|
|
|
provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example"},
|
|
|
|
expected: true,
|
|
|
|
},
|
2022-10-05 05:26:34 +02:00
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.name, func(t *testing.T) {
|
|
|
|
if scenario.provider.IsValid() != scenario.expected {
|
|
|
|
t.Errorf("expected %t, got %t", scenario.expected, scenario.provider.IsValid())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 22:39:51 +02:00
|
|
|
|
|
|
|
func TestAlertProvider_buildRequestBody(t *testing.T) {
|
|
|
|
firstDescription := "description-1"
|
|
|
|
secondDescription := "description-2"
|
|
|
|
scenarios := []struct {
|
|
|
|
Name string
|
|
|
|
Provider AlertProvider
|
|
|
|
Alert alert.Alert
|
|
|
|
Resolved bool
|
|
|
|
ExpectedBody string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "triggered",
|
|
|
|
Provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 1},
|
|
|
|
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
|
|
|
Resolved: false,
|
2022-10-10 04:58:18 +02:00
|
|
|
ExpectedBody: "{\"topic\":\"example\",\"title\":\"Gatus\",\"message\":\"endpoint-name - description-1\",\"tags\":[\"x\"],\"priority\":1}",
|
2022-10-09 22:39:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "resolved",
|
|
|
|
Provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 2},
|
|
|
|
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
|
|
|
Resolved: true,
|
2022-10-10 04:58:18 +02:00
|
|
|
ExpectedBody: "{\"topic\":\"example\",\"title\":\"Gatus\",\"message\":\"endpoint-name - description-2\",\"tags\":[\"white_check_mark\"],\"priority\":2}",
|
2022-10-09 22:39:51 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.Name, func(t *testing.T) {
|
|
|
|
body := scenario.Provider.buildRequestBody(
|
|
|
|
&core.Endpoint{Name: "endpoint-name"},
|
|
|
|
&scenario.Alert,
|
|
|
|
&core.Result{
|
|
|
|
ConditionResults: []*core.ConditionResult{
|
|
|
|
{Condition: "[CONNECTED] == true", Success: scenario.Resolved},
|
|
|
|
{Condition: "[STATUS] == 200", Success: scenario.Resolved},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scenario.Resolved,
|
|
|
|
)
|
2022-10-10 04:58:18 +02:00
|
|
|
if string(body) != scenario.ExpectedBody {
|
2022-10-09 22:39:51 +02:00
|
|
|
t.Errorf("expected %s, got %s", scenario.ExpectedBody, body)
|
|
|
|
}
|
|
|
|
out := make(map[string]interface{})
|
2022-10-10 04:58:18 +02:00
|
|
|
if err := json.Unmarshal(body, &out); err != nil {
|
2022-10-09 22:39:51 +02:00
|
|
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|