| 1 |
package pushbullet_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"net/url" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/jarcoal/httpmock" |
| 10 |
"github.com/onsi/ginkgo/v2" |
| 11 |
"github.com/onsi/gomega" |
| 12 |
|
| 13 |
"github.com/nicholas-fedor/shoutrrr/internal/testutils" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/push/pushbullet" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
service *pushbullet.Service |
| 19 |
envPushbulletURL *url.URL |
| 20 |
_ = ginkgo.BeforeSuite(func() { |
| 21 |
service = &pushbullet.Service{} |
| 22 |
envPushbulletURL, _ = url.Parse(os.Getenv("SHOUTRRR_PUSHBULLET_URL")) |
| 23 |
}) |
| 24 |
) |
| 25 |
|
| 26 |
var _ = ginkgo.Describe("the pushbullet service", func() { |
| 27 |
ginkgo.When("running integration tests", func() { |
| 28 |
ginkgo.It("should not error out", func() { |
| 29 |
if envPushbulletURL.String() == "" { |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
serviceURL, _ := url.Parse(envPushbulletURL.String()) |
| 34 |
err := service.Initialize(serviceURL, testutils.TestLogger()) |
| 35 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 36 |
err = service.Send("This is an integration test message", nil) |
| 37 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 38 |
}) |
| 39 |
ginkgo.It("returns the correct service identifier", func() { |
| 40 |
gomega.Expect(service.GetID()).To(gomega.Equal("pushbullet")) |
| 41 |
}) |
| 42 |
}) |
| 43 |
|
| 44 |
ginkgo.Describe("the pushbullet config", func() { |
| 45 |
ginkgo.When("generating a config object", func() { |
| 46 |
ginkgo.It("should set token", func() { |
| 47 |
pushbulletURL, _ := url.Parse("pushbullet://tokentokentokentokentokentokentoke") |
| 48 |
config := pushbullet.Config{} |
| 49 |
err := config.SetURL(pushbulletURL) |
| 50 |
|
| 51 |
gomega.Expect(config.Token).To(gomega.Equal("tokentokentokentokentokentokentoke")) |
| 52 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 53 |
}) |
| 54 |
|
| 55 |
ginkgo.It("should set the device from path", func() { |
| 56 |
pushbulletURL, _ := url.Parse( |
| 57 |
"pushbullet://tokentokentokentokentokentokentoke/test", |
| 58 |
) |
| 59 |
config := pushbullet.Config{} |
| 60 |
err := config.SetURL(pushbulletURL) |
| 61 |
|
| 62 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 63 |
gomega.Expect(config.Targets).To(gomega.HaveLen(1)) |
| 64 |
gomega.Expect(config.Targets).To(gomega.ContainElements("test")) |
| 65 |
}) |
| 66 |
|
| 67 |
ginkgo.It("should set the channel from path", func() { |
| 68 |
pushbulletURL, _ := url.Parse( |
| 69 |
"pushbullet://tokentokentokentokentokentokentoke/foo#bar", |
| 70 |
) |
| 71 |
config := pushbullet.Config{} |
| 72 |
err := config.SetURL(pushbulletURL) |
| 73 |
|
| 74 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 75 |
gomega.Expect(config.Targets).To(gomega.HaveLen(2)) |
| 76 |
gomega.Expect(config.Targets).To(gomega.ContainElements("foo", "#bar")) |
| 77 |
}) |
| 78 |
}) |
| 79 |
|
| 80 |
ginkgo.When("parsing the configuration URL", func() { |
| 81 |
ginkgo.It("should be identical after de-/serialization", func() { |
| 82 |
testURL := "pushbullet://tokentokentokentokentokentokentoke/device?title=Great+News" |
| 83 |
|
| 84 |
config := &pushbullet.Config{} |
| 85 |
err := config.SetURL(testutils.URLMust(testURL)) |
| 86 |
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "verifying") |
| 87 |
|
| 88 |
outputURL := config.GetURL() |
| 89 |
gomega.Expect(outputURL.String()).To(gomega.Equal(testURL)) |
| 90 |
}) |
| 91 |
}) |
| 92 |
}) |
| 93 |
|
| 94 |
ginkgo.Describe("building the payload", func() { |
| 95 |
ginkgo.It("Email target should only populate one the correct field", func() { |
| 96 |
push := pushbullet.PushRequest{} |
| 97 |
push.SetTarget("[email protected]") |
| 98 |
gomega.Expect(push.Email).To(gomega.Equal("[email protected]")) |
| 99 |
gomega.Expect(push.DeviceIden).To(gomega.BeEmpty()) |
| 100 |
gomega.Expect(push.ChannelTag).To(gomega.BeEmpty()) |
| 101 |
}) |
| 102 |
|
| 103 |
ginkgo.It("Device target should only populate one the correct field", func() { |
| 104 |
push := pushbullet.PushRequest{} |
| 105 |
push.SetTarget("device") |
| 106 |
gomega.Expect(push.Email).To(gomega.BeEmpty()) |
| 107 |
gomega.Expect(push.DeviceIden).To(gomega.Equal("device")) |
| 108 |
gomega.Expect(push.ChannelTag).To(gomega.BeEmpty()) |
| 109 |
}) |
| 110 |
|
| 111 |
ginkgo.It("Channel target should only populate one the correct field", func() { |
| 112 |
push := pushbullet.PushRequest{} |
| 113 |
push.SetTarget("#channel") |
| 114 |
gomega.Expect(push.Email).To(gomega.BeEmpty()) |
| 115 |
gomega.Expect(push.DeviceIden).To(gomega.BeEmpty()) |
| 116 |
gomega.Expect(push.ChannelTag).To(gomega.Equal("channel")) |
| 117 |
}) |
| 118 |
}) |
| 119 |
|
| 120 |
ginkgo.Describe("sending the payload", func() { |
| 121 |
var err error |
| 122 |
|
| 123 |
targetURL := "https://api.pushbullet.com/v2/pushes" |
| 124 |
|
| 125 |
ginkgo.BeforeEach(func() { |
| 126 |
httpmock.Activate() |
| 127 |
}) |
| 128 |
|
| 129 |
ginkgo.AfterEach(func() { |
| 130 |
httpmock.DeactivateAndReset() |
| 131 |
}) |
| 132 |
|
| 133 |
ginkgo.It("should not report an error if the server accepts the payload", func() { |
| 134 |
err = initService() |
| 135 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 136 |
|
| 137 |
response := pushbullet.PushResponse{ |
| 138 |
Type: "note", |
| 139 |
Body: "Message", |
| 140 |
Title: "Shoutrrr notification", // Matches default |
| 141 |
Active: true, |
| 142 |
} |
| 143 |
responder, _ := httpmock.NewJsonResponder(200, &response) |
| 144 |
httpmock.RegisterResponder("POST", targetURL, responder) |
| 145 |
|
| 146 |
err = service.Send("Message", nil) |
| 147 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 148 |
}) |
| 149 |
|
| 150 |
ginkgo.It("should not panic if an error occurs when sending the payload", func() { |
| 151 |
err = initService() |
| 152 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 153 |
|
| 154 |
httpmock.RegisterResponder( |
| 155 |
"POST", |
| 156 |
targetURL, |
| 157 |
httpmock.NewErrorResponder(errors.New("")), |
| 158 |
) |
| 159 |
|
| 160 |
err = service.Send("Message", nil) |
| 161 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 162 |
}) |
| 163 |
|
| 164 |
ginkgo.It("should return an error if the response type is incorrect", func() { |
| 165 |
err = initService() |
| 166 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 167 |
|
| 168 |
response := pushbullet.PushResponse{ |
| 169 |
Type: "link", // Incorrect type |
| 170 |
Body: "Message", |
| 171 |
Title: "Shoutrrr notification", |
| 172 |
Active: true, |
| 173 |
} |
| 174 |
responder, _ := httpmock.NewJsonResponder(200, &response) |
| 175 |
httpmock.RegisterResponder("POST", targetURL, responder) |
| 176 |
|
| 177 |
err = service.Send("Message", nil) |
| 178 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 179 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("unexpected response type")) |
| 180 |
}) |
| 181 |
|
| 182 |
ginkgo.It("should return an error if the response body does not match", func() { |
| 183 |
err = initService() |
| 184 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 185 |
|
| 186 |
response := pushbullet.PushResponse{ |
| 187 |
Type: "note", |
| 188 |
Body: "Wrong message", |
| 189 |
Title: "Shoutrrr notification", |
| 190 |
Active: true, |
| 191 |
} |
| 192 |
responder, _ := httpmock.NewJsonResponder(200, &response) |
| 193 |
httpmock.RegisterResponder("POST", targetURL, responder) |
| 194 |
|
| 195 |
err = service.Send("Message", nil) |
| 196 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 197 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("response body mismatch")) |
| 198 |
}) |
| 199 |
|
| 200 |
ginkgo.It("should return an error if the response title does not match", func() { |
| 201 |
err = initService() |
| 202 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 203 |
|
| 204 |
response := pushbullet.PushResponse{ |
| 205 |
Type: "note", |
| 206 |
Body: "Message", |
| 207 |
Title: "Wrong Title", |
| 208 |
Active: true, |
| 209 |
} |
| 210 |
responder, _ := httpmock.NewJsonResponder(200, &response) |
| 211 |
httpmock.RegisterResponder("POST", targetURL, responder) |
| 212 |
|
| 213 |
err = service.Send("Message", nil) |
| 214 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 215 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("response title mismatch")) |
| 216 |
}) |
| 217 |
|
| 218 |
ginkgo.It("should return an error if the push is not active", func() { |
| 219 |
err = initService() |
| 220 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 221 |
|
| 222 |
response := pushbullet.PushResponse{ |
| 223 |
Type: "note", |
| 224 |
Body: "Message", |
| 225 |
Title: "Shoutrrr notification", // Matches default |
| 226 |
Active: false, |
| 227 |
} |
| 228 |
responder, _ := httpmock.NewJsonResponder(200, &response) |
| 229 |
httpmock.RegisterResponder("POST", targetURL, responder) |
| 230 |
|
| 231 |
err = service.Send("Message", nil) |
| 232 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 233 |
gomega.Expect(err.Error()). |
| 234 |
To(gomega.ContainSubstring("push notification is not active")) |
| 235 |
}) |
| 236 |
}) |
| 237 |
}) |
| 238 |
|
| 239 |
func TestPushbullet(t *testing.T) { |
| 240 |
t.Parallel() |
| 241 |
gomega.RegisterFailHandler(ginkgo.Fail) |
| 242 |
ginkgo.RunSpecs(t, "Shoutrrr Pushbullet Suite") |
| 243 |
} |
| 244 |
|
| 245 |
// initService initializes the service with a fixed test configuration. |
| 246 |
func initService() error { |
| 247 |
serviceURL, err := url.Parse("pushbullet://tokentokentokentokentokentokentoke/test") |
| 248 |
gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred()) |
| 249 |
|
| 250 |
return service.Initialize(serviceURL, testutils.TestLogger()) |
| 251 |
} |