2022-10-05 05:26:34 +02:00
package ntfy
2022-10-09 22:39:51 +02:00
import (
"encoding/json"
"testing"
2022-12-06 07:41:09 +01:00
"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/core"
2022-10-09 22:39:51 +02:00
)
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
} ,
2023-07-03 05:10:16 +02:00
{
name : "valid-with-token" ,
provider : AlertProvider { Topic : "example" , Priority : 1 , Token : "tk_faketoken" } ,
expected : true ,
} ,
{
name : "invalid-token" ,
provider : AlertProvider { Topic : "example" , Priority : 1 , Token : "xx_faketoken" } ,
expected : false ,
} ,
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-12-02 02:26:14 +01: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 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-12-02 02:26:14 +01: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 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-20 23:09:40 +02:00
t . Errorf ( "expected:\n%s\ngot:\n%s" , scenario . ExpectedBody , body )
2022-10-09 22:39:51 +02:00
}
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 ( ) )
}
} )
}
}