Skip to content

Commit

Permalink
Validate retry and expire in Pushover receiver (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akhmetov authored Aug 27, 2024
1 parent 9daa623 commit 70248a7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
7 changes: 7 additions & 0 deletions receivers/pushover/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ func NewConfig(jsonData json.RawMessage, decryptFn receivers.DecryptFunc) (Confi
}

settings.Retry, _ = rawSettings.Retry.Int64()
if settings.Retry < 30 && (settings.AlertingPriority == 2 || settings.OkPriority == 2) {
return settings, errors.New("retry must be at least 30 seconds when priority is set to Emergency")
}

settings.Expire, _ = rawSettings.Expire.Int64()
if settings.Expire > 10800 {
return settings, errors.New("expire must be at most 10800 seconds")
}

settings.Device = rawSettings.Device
settings.AlertingSound = rawSettings.AlertingSound
Expand Down
71 changes: 71 additions & 0 deletions receivers/pushover/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,72 @@ func TestNewConfig(t *testing.T) {
settings: `{ "userKey": "test-user-key" }`,
expectedInitError: `API token not found`,
},
{
name: "Error if Emergency and retry is invalid",
settings: `{
"userKey": "test-user-key",
"apiToken" : "test-api-token",
"priority": "2",
"retry": "29"
}`,
expectedInitError: `retry must be at least 30 seconds when priority is set to Emergency`,
},
{
name: "Valid configuration with Emergency priority and proper retry",
settings: `{
"userKey": "test-user-key",
"apiToken" : "test-api-token",
"priority": "2",
"retry": "30"
}`,
expectedConfig: Config{
UserKey: "test-user-key",
APIToken: "test-api-token",
AlertingPriority: 2,
OkPriority: 0,
Retry: 30,
Expire: 0,
Device: "",
AlertingSound: "",
OkSound: "",
Upload: true,
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
},
},
{
name: "Error if okPriority is Emergency and retry is invalid",
settings: `{
"userKey": "test-user-key",
"apiToken" : "test-api-token",
"okPriority": "2",
"retry": "29"
}`,
expectedInitError: `retry must be at least 30 seconds when priority is set to Emergency`,
},
{
name: "Valid configuration with Emergency okPriority and proper retry",
settings: `{
"userKey": "test-user-key",
"apiToken" : "test-api-token",
"okPriority": "2",
"retry": "30"
}`,
expectedConfig: Config{
UserKey: "test-user-key",
APIToken: "test-api-token",
AlertingPriority: 0,
OkPriority: 2,
Retry: 30,
Expire: 0,
Device: "",
AlertingSound: "",
OkSound: "",
Upload: true,
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
},
},
{
name: "Minimal valid configuration",
settings: `{"userKey": "test-user-key", "apiToken" : "test-api-token" }`,
Expand All @@ -56,6 +122,11 @@ func TestNewConfig(t *testing.T) {
Message: templates.DefaultMessageEmbed,
},
},
{
name: "Error when expire is invalid",
settings: `{"userKey": "test-user-key", "apiToken" : "test-api-token", "expire": "10801"}`,
expectedInitError: `expire must be at most 10800 seconds`,
},
{
name: "Minimal valid configuration from secrets",
settings: `{ }`,
Expand Down
4 changes: 2 additions & 2 deletions receivers/pushover/pushover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestNotify(t *testing.T) {
AlertingPriority: 2,
OkPriority: 0,
Retry: 30,
Expire: 86400,
Expire: 10800,
Device: "device",
AlertingSound: "echo",
OkSound: "magic",
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestNotify(t *testing.T) {
"attachment": "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\b\x04\x00\x00\x00\xb5\x1c\f\x02\x00\x00\x00\vIDATx\xdacd`\x00\x00\x00\x06\x00\x020\x81\xd0/\x00\x00\x00\x00IEND\xaeB`\x82",
"html": "1",
"retry": "30",
"expire": "86400",
"expire": "10800",
"device": "device",
},
expMsgError: nil,
Expand Down

0 comments on commit 70248a7

Please sign in to comment.