2022-10-04 23:26:34 -04:00
package ntfy
2022-10-09 16:39:51 -04:00
import (
"encoding/json"
"testing"
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/core"
2022-10-09 16:39:51 -04:00
)
2022-10-04 23:26:34 -04: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 16:34:08 -04:00
name : "no-url-should-use-default-value" ,
provider : AlertProvider { Topic : "example" , Priority : 1 },
expected : true ,
2022-10-04 23:26:34 -04: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 16:34:08 -04:00
provider : AlertProvider { URL : "https://ntfy.sh" , Topic : "example" , Priority : - 1 },
2022-10-04 23:26:34 -04:00
expected : false ,
},
2022-10-09 16:34:08 -04:00
{
name : "no-priority-should-use-default-value" ,
provider : AlertProvider { URL : "https://ntfy.sh" , Topic : "example" },
expected : true ,
},
2022-10-04 23:26:34 -04: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 16:39:51 -04: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-12-01 20:26:14 -05:00
ExpectedBody : `{"topic":"example","title":"Gatus: endpoint-name","message":"An alert has been triggered due to having failed 3 time(s) in a row with the following description: description-1","tags":["x"],"priority":1}` ,
2022-10-09 16:39:51 -04: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-12-01 20:26:14 -05:00
ExpectedBody : `{"topic":"example","title":"Gatus: endpoint-name","message":"An alert has been resolved after passing successfully 5 time(s) in a row with the following description: description-2","tags":["white_check_mark"],"priority":2}` ,
2022-10-09 16:39:51 -04: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-09 22:58:18 -04:00
if string ( body ) != scenario . ExpectedBody {
2022-10-20 17:09:40 -04:00
t . Errorf ( "expected:\n%s\ngot:\n%s" , scenario . ExpectedBody , body )
2022-10-09 16:39:51 -04:00
}
out := make ( map [ string ] interface {})
2022-10-09 22:58:18 -04:00
if err := json . Unmarshal ( body , & out ); err != nil {
2022-10-09 16:39:51 -04:00
t . Error ( "expected body to be valid JSON, got error:" , err . Error ())
}
})
}
}