mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-28 10:53:20 +01:00
71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
|
package email
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"github.com/TwiN/gatus/v3/alerting/alert"
|
||
|
"github.com/TwiN/gatus/v3/core"
|
||
|
)
|
||
|
|
||
|
func TestAlertProvider_IsValid(t *testing.T) {
|
||
|
invalidProvider := AlertProvider{}
|
||
|
if invalidProvider.IsValid() {
|
||
|
t.Error("provider shouldn't have been valid")
|
||
|
}
|
||
|
validProvider := AlertProvider{From: "from@example.com", Password: "password", Host: "smtp.gmail.com", Port: 587, To: "to@example.com"}
|
||
|
if !validProvider.IsValid() {
|
||
|
t.Error("provider should've been valid")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestAlertProvider_buildRequestBody(t *testing.T) {
|
||
|
firstDescription := "description-1"
|
||
|
secondDescription := "description-2"
|
||
|
scenarios := []struct {
|
||
|
Name string
|
||
|
Provider AlertProvider
|
||
|
Alert alert.Alert
|
||
|
Resolved bool
|
||
|
ExpectedSubject string
|
||
|
ExpectedBody string
|
||
|
}{
|
||
|
{
|
||
|
Name: "triggered",
|
||
|
Provider: AlertProvider{},
|
||
|
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||
|
Resolved: false,
|
||
|
ExpectedSubject: "[endpoint-name] Alert triggered",
|
||
|
ExpectedBody: "An alert for endpoint-name has been triggered due to having failed 3 time(s) in a row\n\nAlert description: description-1\n\nCondition results:\n❌ [CONNECTED] == true\n❌ [STATUS] == 200\n",
|
||
|
},
|
||
|
{
|
||
|
Name: "resolved",
|
||
|
Provider: AlertProvider{},
|
||
|
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||
|
Resolved: true,
|
||
|
ExpectedSubject: "[endpoint-name] Alert resolved",
|
||
|
ExpectedBody: "An alert for endpoint-name has been resolved after passing successfully 5 time(s) in a row\n\nAlert description: description-2\n\nCondition results:\n✅ [CONNECTED] == true\n✅ [STATUS] == 200\n",
|
||
|
},
|
||
|
}
|
||
|
for _, scenario := range scenarios {
|
||
|
t.Run(scenario.Name, func(t *testing.T) {
|
||
|
subject, body := scenario.Provider.buildMessageSubjectAndBody(
|
||
|
&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,
|
||
|
)
|
||
|
if subject != scenario.ExpectedSubject {
|
||
|
t.Errorf("expected subject to be %s, got %s", scenario.ExpectedSubject, subject)
|
||
|
}
|
||
|
if body != scenario.ExpectedBody {
|
||
|
t.Errorf("expected body to be %s, got %s", scenario.ExpectedBody, body)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|