Skip to content

Commit

Permalink
fix(pushover): send all devices in single request (#115)
Browse files Browse the repository at this point in the history
* fix(pushover): send all devices in single request (and dont skip if empty)
* test(pushover): add blackbox send test
  • Loading branch information
piksel committed Jan 17, 2021
1 parent 4034690 commit 94fef61
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
13 changes: 3 additions & 10 deletions pkg/services/pushover/pushover.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,9 @@ func (service *Service) Send(message string, params *types.Params) error {
return err
}

errors := make([]error, 0)

for _, device := range config.Devices {
if err := service.sendToDevice(device, message, config); err != nil {
errors = append(errors, err)
}
}

if len(errors) > 0 {
return fmt.Errorf("failed to send notifications to pushover devices: %v", errors)
device := strings.Join(config.Devices, ",")
if err := service.sendToDevice(device, message, config); err != nil {
return fmt.Errorf("failed to send notifications to pushover devices: %v", err)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/services/pushover/pushover_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
type Config struct {
Token string
User string
Devices []string `key:"devices"`
Priority int8 `key:"priority"`
Title string `key:"title" role:"title"`
Devices []string `key:"devices" optional:""`
Priority int8 `key:"priority" default:"0"`
Title string `key:"title" optional:""`
}

// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values
Expand Down
40 changes: 40 additions & 0 deletions pkg/services/pushover/pushover_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package pushover_test

import (
"errors"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/pushover"
"github.com/containrrr/shoutrrr/pkg/util"
"github.com/jarcoal/httpmock"
"log"
"net/url"
"os"
"testing"
Expand All @@ -12,6 +15,8 @@ import (
. "github.com/onsi/gomega"
)

const hookURL = "https://api.pushover.net/1/messages.json"

func TestPushover(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Pushover Suite")
Expand All @@ -22,10 +27,12 @@ var (
config *pushover.Config
keyResolver format.PropKeyResolver
envPushoverURL *url.URL
logger *log.Logger
)
var _ = Describe("the pushover service", func() {
BeforeSuite(func() {
service = &pushover.Service{}
logger = log.New(GinkgoWriter, "Test", log.LstdFlags)
envPushoverURL, _ = url.Parse(os.Getenv("SHOUTRRR_PUSHOVER_URL"))
})
When("running integration tests", func() {
Expand Down Expand Up @@ -125,6 +132,39 @@ var _ = Describe("the pushover config", func() {
Expect(fields).To(Equal([]string{"devices", "priority", "title"}))
})
})

Describe("sending the payload", func() {
BeforeEach(func() {
httpmock.Activate()
})
AfterEach(func() {
httpmock.DeactivateAndReset()
})
It("should not report an error if the server accepts the payload", func() {
serviceURL, err := url.Parse("pushover://:apptoken@usertoken")
Expect(err).NotTo(HaveOccurred())

err = service.Initialize(serviceURL, logger)
Expect(err).NotTo(HaveOccurred())

httpmock.RegisterResponder("POST", hookURL, httpmock.NewStringResponder(200, ""))

err = service.Send("Message", nil)
Expect(err).NotTo(HaveOccurred())
})
It("should not panic if an error occurs when sending the payload", func() {
serviceURL, err := url.Parse("pushover://:apptoken@usertoken")
Expect(err).NotTo(HaveOccurred())

err = service.Initialize(serviceURL, logger)
Expect(err).NotTo(HaveOccurred())

httpmock.RegisterResponder("POST", hookURL, httpmock.NewErrorResponder(errors.New("dummy error")))

err = service.Send("Message", nil)
Expect(err).To(HaveOccurred())
})
})
})

func createURL(username string, token string) *url.URL {
Expand Down

0 comments on commit 94fef61

Please sign in to comment.