2020-09-19 16:29:08 -04:00
package twilio
2020-09-19 16:22:12 -04:00
2020-10-21 21:18:06 -04:00
import (
2024-12-16 20:32:13 -05:00
"net/http"
2020-10-21 21:18:06 -04:00
"testing"
2020-12-10 20:00:42 -05:00
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/alerting/alert"
2024-12-16 20:32:13 -05:00
"github.com/TwiN/gatus/v5/client"
2024-05-09 22:56:16 -04:00
"github.com/TwiN/gatus/v5/config/endpoint"
2024-12-16 20:32:13 -05:00
"github.com/TwiN/gatus/v5/test"
2020-10-21 21:18:06 -04:00
)
2020-09-19 16:22:12 -04:00
func TestTwilioAlertProvider_IsValid ( t * testing . T ) {
2020-09-19 16:29:08 -04:00
invalidProvider := AlertProvider {}
2024-12-16 20:32:13 -05:00
if err := invalidProvider . Validate (); err == nil {
2020-09-19 16:22:12 -04:00
t . Error ( "provider shouldn't have been valid" )
}
2020-09-19 16:29:08 -04:00
validProvider := AlertProvider {
2024-12-16 20:32:13 -05:00
DefaultConfig : Config {
SID : "1" ,
Token : "1" ,
From : "1" ,
To : "1" ,
},
2020-09-19 16:22:12 -04:00
}
2024-12-16 20:32:13 -05:00
if err := validProvider . Validate (); err != nil {
2020-09-19 16:22:12 -04:00
t . Error ( "provider should've been valid" )
}
}
2020-10-21 21:18:06 -04:00
2024-12-16 20:32:13 -05:00
func TestAlertProvider_Send ( t * testing . T ) {
defer client . InjectHTTPClient ( nil )
firstDescription := "description-1"
secondDescription := "description-2"
scenarios := [] struct {
Name string
Provider AlertProvider
Alert alert . Alert
Resolved bool
MockRoundTripper test . MockRoundTripper
ExpectedError bool
}{
{
Name : "triggered" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" }},
Alert : alert . Alert { Description : & firstDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : false ,
MockRoundTripper : test . MockRoundTripper ( func ( r * http . Request ) * http . Response {
return & http . Response { StatusCode : http . StatusOK , Body : http . NoBody }
}),
ExpectedError : false ,
},
{
Name : "triggered-error" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" }},
Alert : alert . Alert { Description : & firstDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : false ,
MockRoundTripper : test . MockRoundTripper ( func ( r * http . Request ) * http . Response {
return & http . Response { StatusCode : http . StatusInternalServerError , Body : http . NoBody }
}),
ExpectedError : true ,
},
{
Name : "resolved" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" }},
Alert : alert . Alert { Description : & secondDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : true ,
MockRoundTripper : test . MockRoundTripper ( func ( r * http . Request ) * http . Response {
return & http . Response { StatusCode : http . StatusOK , Body : http . NoBody }
}),
ExpectedError : false ,
},
{
Name : "resolved-error" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" }},
Alert : alert . Alert { Description : & secondDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : true ,
MockRoundTripper : test . MockRoundTripper ( func ( r * http . Request ) * http . Response {
return & http . Response { StatusCode : http . StatusInternalServerError , Body : http . NoBody }
}),
ExpectedError : true ,
},
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
client . InjectHTTPClient ( & http . Client { Transport : scenario . MockRoundTripper })
err := scenario . Provider . Send (
& endpoint . Endpoint { Name : "endpoint-name" },
& scenario . Alert ,
& endpoint . Result {
ConditionResults : [] * endpoint . ConditionResult {
{ Condition : "[CONNECTED] == true" , Success : scenario . Resolved },
{ Condition : "[STATUS] == 200" , Success : scenario . Resolved },
},
},
scenario . Resolved ,
)
if scenario . ExpectedError && err == nil {
t . Error ( "expected error, got none" )
}
if ! scenario . ExpectedError && err != nil {
t . Error ( "expected no error, got" , err . Error ())
}
})
}
}
2021-12-02 21:05:17 -05: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" ,
2024-12-16 20:32:13 -05:00
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" }},
2021-12-02 21:05:17 -05:00
Alert : alert . Alert { Description : & firstDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : false ,
ExpectedBody : "Body=TRIGGERED%3A+endpoint-name+-+description-1&From=3&To=4" ,
},
{
Name : "resolved" ,
2024-12-16 20:32:13 -05:00
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" }},
2021-12-02 21:05:17 -05:00
Alert : alert . Alert { Description : & secondDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : true ,
ExpectedBody : "Body=RESOLVED%3A+endpoint-name+-+description-2&From=3&To=4" ,
},
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
{
Name : "triggered-with-old-placeholders" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" , TextTwilioTriggered : "Alert: {endpoint} - {description}" }},
Alert : alert . Alert { Description : & firstDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : false ,
ExpectedBody : "Body=Alert%3A+endpoint-name+-+description-1&From=3&To=4" ,
},
{
Name : "triggered-with-new-placeholders" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" , TextTwilioTriggered : "Alert: [ENDPOINT] - [ALERT_DESCRIPTION]" }},
Alert : alert . Alert { Description : & firstDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : false ,
ExpectedBody : "Body=Alert%3A+endpoint-name+-+description-1&From=3&To=4" ,
},
{
Name : "resolved-with-mixed-placeholders" ,
Provider : AlertProvider { DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" , TextTwilioResolved : "Resolved: {endpoint} and [ENDPOINT] - {description} and [ALERT_DESCRIPTION]" }},
Alert : alert . Alert { Description : & secondDescription , SuccessThreshold : 5 , FailureThreshold : 3 },
Resolved : true ,
ExpectedBody : "Body=Resolved%3A+endpoint-name+and+endpoint-name+-+description-2+and+description-2&From=3&To=4" ,
},
2021-02-19 19:06:20 -05:00
}
2021-12-02 21:05:17 -05:00
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
body := scenario . Provider . buildRequestBody (
2024-12-16 20:32:13 -05:00
& scenario . Provider . DefaultConfig ,
2024-05-09 22:56:16 -04:00
& endpoint . Endpoint { Name : "endpoint-name" },
2021-12-02 21:05:17 -05:00
& scenario . Alert ,
2024-05-09 22:56:16 -04:00
& endpoint . Result {
ConditionResults : [] * endpoint . ConditionResult {
2021-12-02 21:05:17 -05:00
{ Condition : "[CONNECTED] == true" , Success : scenario . Resolved },
{ Condition : "[STATUS] == 200" , Success : scenario . Resolved },
},
},
scenario . Resolved ,
)
if body != scenario . ExpectedBody {
t . Errorf ( "expected %s, got %s" , scenario . ExpectedBody , body )
}
})
2021-02-19 19:06:20 -05:00
}
2020-10-21 21:18:06 -04:00
}
2021-12-02 22:15:51 -05:00
func TestAlertProvider_GetDefaultAlert ( t * testing . T ) {
2024-02-07 20:09:45 -05:00
if ( & AlertProvider { DefaultAlert : & alert . Alert {}}). GetDefaultAlert () == nil {
2021-12-02 22:15:51 -05:00
t . Error ( "expected default alert to be not nil" )
}
2024-02-07 20:09:45 -05:00
if ( & AlertProvider { DefaultAlert : nil }). GetDefaultAlert () != nil {
2021-12-02 22:15:51 -05:00
t . Error ( "expected default alert to be nil" )
}
}
2024-12-16 20:32:13 -05:00
func TestAlertProvider_GetConfig ( t * testing . T ) {
scenarios := [] struct {
Name string
Provider AlertProvider
InputAlert alert . Alert
ExpectedOutput Config
}{
{
Name : "provider-no-override-should-default" ,
Provider : AlertProvider {
DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" },
},
InputAlert : alert . Alert {},
ExpectedOutput : Config { SID : "1" , Token : "2" , From : "3" , To : "4" },
},
{
Name : "provider-with-alert-override" ,
Provider : AlertProvider {
DefaultConfig : Config { SID : "1" , Token : "2" , From : "3" , To : "4" },
},
InputAlert : alert . Alert { ProviderOverride : map [ string ] any { "sid" : "5" , "token" : "6" , "from" : "7" , "to" : "8" }},
ExpectedOutput : Config { SID : "5" , Token : "6" , From : "7" , To : "8" },
},
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
got , err := scenario . Provider . GetConfig ( "" , & scenario . InputAlert )
if err != nil {
t . Error ( "expected no error, got:" , err . Error ())
}
if got . SID != scenario . ExpectedOutput . SID {
t . Errorf ( "expected SID to be %s, got %s" , scenario . ExpectedOutput . SID , got . SID )
}
if got . Token != scenario . ExpectedOutput . Token {
t . Errorf ( "expected token to be %s, got %s" , scenario . ExpectedOutput . Token , got . Token )
}
if got . From != scenario . ExpectedOutput . From {
t . Errorf ( "expected from to be %s, got %s" , scenario . ExpectedOutput . From , got . From )
}
if got . To != scenario . ExpectedOutput . To {
t . Errorf ( "expected to to be %s, got %s" , scenario . ExpectedOutput . To , got . To )
}
// Test ValidateOverrides as well, since it really just calls GetConfig
if err = scenario . Provider . ValidateOverrides ( "" , & scenario . InputAlert ); err != nil {
t . Errorf ( "unexpected error: %s" , err )
}
})
}
}