| 1 |
package signal |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/onsi/ginkgo/v2" |
| 5 |
"github.com/onsi/gomega" |
| 6 |
) |
| 7 |
|
| 8 |
var _ = ginkgo.Describe("payload", func() { |
| 9 |
ginkgo.Describe("composeMessage", func() { |
| 10 |
ginkgo.It("should return the body when title is empty", func() { |
| 11 |
gomega.Expect(composeMessage("", "body", false)).To(gomega.Equal("body")) |
| 12 |
}) |
| 13 |
|
| 14 |
ginkgo.It("should prepend a plain title", func() { |
| 15 |
gomega.Expect(composeMessage("Alert", "body", false)).To(gomega.Equal("Alert\nbody")) |
| 16 |
}) |
| 17 |
|
| 18 |
ginkgo.It("should bold the title when styled", func() { |
| 19 |
gomega.Expect(composeMessage("Alert", "body", true)).To(gomega.Equal("**Alert**\nbody")) |
| 20 |
}) |
| 21 |
|
| 22 |
ginkgo.It("should send title only when the body is empty", func() { |
| 23 |
gomega.Expect(composeMessage("Alert", "", false)).To(gomega.Equal("Alert")) |
| 24 |
}) |
| 25 |
}) |
| 26 |
|
| 27 |
ginkgo.Describe("parseTimestamp", func() { |
| 28 |
ginkgo.It("should accept an unquoted JSON number", func() { |
| 29 |
n, err := parseTimestamp([]byte(`{"timestamp": 1234567890}`)) |
| 30 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 31 |
gomega.Expect(n).To(gomega.Equal(int64(1234567890))) |
| 32 |
}) |
| 33 |
|
| 34 |
ginkgo.It("should accept a quoted JSON number", func() { |
| 35 |
n, err := parseTimestamp([]byte(`{"timestamp": "1234567890"}`)) |
| 36 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 37 |
gomega.Expect(n).To(gomega.Equal(int64(1234567890))) |
| 38 |
}) |
| 39 |
|
| 40 |
ginkgo.It("should accept a numeric zero", func() { |
| 41 |
n, err := parseTimestamp([]byte(`{"timestamp": 0}`)) |
| 42 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 43 |
gomega.Expect(n).To(gomega.Equal(int64(0))) |
| 44 |
}) |
| 45 |
|
| 46 |
ginkgo.It("should reject a missing timestamp", func() { |
| 47 |
_, err := parseTimestamp([]byte(`{}`)) |
| 48 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 49 |
}) |
| 50 |
|
| 51 |
ginkgo.It("should reject a null timestamp", func() { |
| 52 |
_, err := parseTimestamp([]byte(`{"timestamp": null}`)) |
| 53 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 54 |
}) |
| 55 |
}) |
| 56 |
|
| 57 |
ginkgo.Describe("apiRecipients", func() { |
| 58 |
ginkgo.It("should strip the u: prefix from usernames", func() { |
| 59 |
gomega.Expect(apiRecipients([]string{"u:someuser.123", "+1234567890"})). |
| 60 |
To(gomega.Equal([]string{"someuser.123", "+1234567890"})) |
| 61 |
}) |
| 62 |
}) |
| 63 |
|
| 64 |
ginkgo.Describe("parseAttachments", func() { |
| 65 |
ginkgo.It("should return nil for empty input", func() { |
| 66 |
gomega.Expect(parseAttachments("")).To(gomega.BeNil()) |
| 67 |
gomega.Expect(parseAttachments(" ")).To(gomega.BeNil()) |
| 68 |
}) |
| 69 |
|
| 70 |
ginkgo.It("should split comma-separated raw base64", func() { |
| 71 |
gomega.Expect(parseAttachments("a, b")).To(gomega.Equal([]string{"a", "b"})) |
| 72 |
}) |
| 73 |
|
| 74 |
ginkgo.It("should keep a data URI as a single attachment", func() { |
| 75 |
gomega.Expect(parseAttachments("data:image/png;base64,iVBOR")). |
| 76 |
To(gomega.Equal([]string{"data:image/png;base64,iVBOR"})) |
| 77 |
}) |
| 78 |
}) |
| 79 |
}) |