| 1 |
package discord |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/url" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/onsi/ginkgo/v2" |
| 8 |
"github.com/onsi/gomega" |
| 9 |
|
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
testWebhookURL = "https://discord.com/api/webhooks/123456789/test-token" |
| 16 |
testWebhookURLWithThread = "https://discord.com/api/webhooks/123456789/test-token?thread_id=987654321" |
| 17 |
) |
| 18 |
|
| 19 |
var _ = ginkgo.Describe("Discord Config Unit Tests", func() { |
| 20 |
ginkgo.Describe("LevelColors method", func() { |
| 21 |
ginkgo.It("should return correct color mapping for default colors", func() { |
| 22 |
config := &Config{ |
| 23 |
Color: 0x50d9ff, |
| 24 |
ColorError: 0xd60510, |
| 25 |
ColorWarn: 0xffc441, |
| 26 |
ColorInfo: 0x2488ff, |
| 27 |
ColorDebug: 0x7b00ab, |
| 28 |
} |
| 29 |
|
| 30 |
colors := config.LevelColors() |
| 31 |
|
| 32 |
gomega.Expect(colors[types.Unknown]).To(gomega.Equal(uint(0x50d9ff))) |
| 33 |
gomega.Expect(colors[types.Error]).To(gomega.Equal(uint(0xd60510))) |
| 34 |
gomega.Expect(colors[types.Warning]).To(gomega.Equal(uint(0xffc441))) |
| 35 |
gomega.Expect(colors[types.Info]).To(gomega.Equal(uint(0x2488ff))) |
| 36 |
gomega.Expect(colors[types.Debug]).To(gomega.Equal(uint(0x7b00ab))) |
| 37 |
}) |
| 38 |
|
| 39 |
ginkgo.It("should return correct color mapping for custom colors", func() { |
| 40 |
config := &Config{ |
| 41 |
Color: 0x123456, |
| 42 |
ColorError: 0x654321, |
| 43 |
ColorWarn: 0xabcdef, |
| 44 |
ColorInfo: 0xfedcba, |
| 45 |
ColorDebug: 0x987654, |
| 46 |
} |
| 47 |
|
| 48 |
colors := config.LevelColors() |
| 49 |
|
| 50 |
gomega.Expect(colors[types.Unknown]).To(gomega.Equal(uint(0x123456))) |
| 51 |
gomega.Expect(colors[types.Error]).To(gomega.Equal(uint(0x654321))) |
| 52 |
gomega.Expect(colors[types.Warning]).To(gomega.Equal(uint(0xabcdef))) |
| 53 |
gomega.Expect(colors[types.Info]).To(gomega.Equal(uint(0xfedcba))) |
| 54 |
gomega.Expect(colors[types.Debug]).To(gomega.Equal(uint(0x987654))) |
| 55 |
}) |
| 56 |
|
| 57 |
ginkgo.It("should handle zero values", func() { |
| 58 |
config := &Config{} |
| 59 |
|
| 60 |
colors := config.LevelColors() |
| 61 |
|
| 62 |
gomega.Expect(colors[types.Unknown]).To(gomega.Equal(uint(0))) |
| 63 |
gomega.Expect(colors[types.Error]).To(gomega.Equal(uint(0))) |
| 64 |
gomega.Expect(colors[types.Warning]).To(gomega.Equal(uint(0))) |
| 65 |
gomega.Expect(colors[types.Info]).To(gomega.Equal(uint(0))) |
| 66 |
gomega.Expect(colors[types.Debug]).To(gomega.Equal(uint(0))) |
| 67 |
}) |
| 68 |
}) |
| 69 |
|
| 70 |
ginkgo.Describe("GetURL and SetURL methods", func() { |
| 71 |
ginkgo.It("should serialize and deserialize basic config correctly", func() { |
| 72 |
originalConfig := &Config{ |
| 73 |
WebhookID: "123456789", |
| 74 |
Token: "test-token", |
| 75 |
} |
| 76 |
|
| 77 |
url := originalConfig.GetURL() |
| 78 |
gomega.Expect(url.Scheme).To(gomega.Equal("discord")) |
| 79 |
gomega.Expect(url.Host).To(gomega.Equal("123456789")) |
| 80 |
gomega.Expect(url.User.Username()).To(gomega.Equal("test-token")) |
| 81 |
|
| 82 |
newConfig := &Config{} |
| 83 |
err := newConfig.SetURL(url) |
| 84 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 85 |
gomega.Expect(newConfig.WebhookID).To(gomega.Equal("123456789")) |
| 86 |
gomega.Expect(newConfig.Token).To(gomega.Equal("test-token")) |
| 87 |
}) |
| 88 |
|
| 89 |
ginkgo.It("should handle all query parameters correctly", func() { |
| 90 |
originalConfig := &Config{ |
| 91 |
WebhookID: "123456789", |
| 92 |
Token: "test-token", |
| 93 |
Title: "Test Title", |
| 94 |
Username: "TestBot", |
| 95 |
Avatar: "https://example.com/avatar.png", |
| 96 |
Color: 0x50d9ff, |
| 97 |
ColorError: 0xd60510, |
| 98 |
ColorWarn: 0xffc441, |
| 99 |
ColorInfo: 0x2488ff, |
| 100 |
ColorDebug: 0x7b00ab, |
| 101 |
SplitLines: false, |
| 102 |
JSON: false, |
| 103 |
ThreadID: "987654321", |
| 104 |
} |
| 105 |
|
| 106 |
url := originalConfig.GetURL() |
| 107 |
gomega.Expect(url.Scheme).To(gomega.Equal("discord")) |
| 108 |
gomega.Expect(url.Host).To(gomega.Equal("123456789")) |
| 109 |
gomega.Expect(url.User.Username()).To(gomega.Equal("test-token")) |
| 110 |
|
| 111 |
newConfig := &Config{} |
| 112 |
resolver := format.NewPropKeyResolver(newConfig) |
| 113 |
err := resolver.SetDefaultProps(newConfig) |
| 114 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 115 |
err = newConfig.SetURL(url) |
| 116 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 117 |
|
| 118 |
gomega.Expect(newConfig.WebhookID).To(gomega.Equal(originalConfig.WebhookID)) |
| 119 |
gomega.Expect(newConfig.Token).To(gomega.Equal(originalConfig.Token)) |
| 120 |
gomega.Expect(newConfig.Title).To(gomega.Equal(originalConfig.Title)) |
| 121 |
gomega.Expect(newConfig.Username).To(gomega.Equal(originalConfig.Username)) |
| 122 |
gomega.Expect(newConfig.Avatar).To(gomega.Equal(originalConfig.Avatar)) |
| 123 |
gomega.Expect(newConfig.Color).To(gomega.Equal(originalConfig.Color)) |
| 124 |
gomega.Expect(newConfig.ColorError).To(gomega.Equal(originalConfig.ColorError)) |
| 125 |
gomega.Expect(newConfig.ColorWarn).To(gomega.Equal(originalConfig.ColorWarn)) |
| 126 |
gomega.Expect(newConfig.ColorInfo).To(gomega.Equal(originalConfig.ColorInfo)) |
| 127 |
gomega.Expect(newConfig.ColorDebug).To(gomega.Equal(originalConfig.ColorDebug)) |
| 128 |
gomega.Expect(newConfig.SplitLines).To(gomega.Equal(originalConfig.SplitLines)) |
| 129 |
gomega.Expect(newConfig.JSON).To(gomega.Equal(originalConfig.JSON)) |
| 130 |
gomega.Expect(newConfig.ThreadID).To(gomega.Equal(originalConfig.ThreadID)) |
| 131 |
}) |
| 132 |
|
| 133 |
ginkgo.It("should handle JSON mode correctly", func() { |
| 134 |
originalConfig := &Config{ |
| 135 |
WebhookID: "123456789", |
| 136 |
Token: "test-token", |
| 137 |
JSON: true, |
| 138 |
} |
| 139 |
|
| 140 |
url := originalConfig.GetURL() |
| 141 |
gomega.Expect(url.Path).To(gomega.Equal("/raw")) |
| 142 |
|
| 143 |
newConfig := &Config{} |
| 144 |
err := newConfig.SetURL(url) |
| 145 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 146 |
gomega.Expect(newConfig.JSON).To(gomega.BeTrue()) |
| 147 |
}) |
| 148 |
|
| 149 |
ginkgo.It("should handle thread_id with whitespace correctly", func() { |
| 150 |
testURL := "discord://token@channel?thread_id=%20%20123456789%20%20" |
| 151 |
url, err := url.Parse(testURL) |
| 152 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 153 |
|
| 154 |
config := &Config{} |
| 155 |
resolver := format.NewPropKeyResolver(config) |
| 156 |
err = resolver.SetDefaultProps(config) |
| 157 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 158 |
err = config.SetURL(url) |
| 159 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 160 |
gomega.Expect(config.ThreadID).To(gomega.Equal("123456789")) |
| 161 |
|
| 162 |
outputURL := config.GetURL() |
| 163 |
gomega.Expect(outputURL.Query().Get("thread_id")).To(gomega.Equal("123456789")) |
| 164 |
}) |
| 165 |
|
| 166 |
ginkgo.It("should not include thread_id in URL when empty", func() { |
| 167 |
config := &Config{ |
| 168 |
WebhookID: "123456789", |
| 169 |
Token: "test-token", |
| 170 |
ThreadID: "", |
| 171 |
} |
| 172 |
|
| 173 |
url := config.GetURL() |
| 174 |
gomega.Expect(url.Query().Get("thread_id")).To(gomega.BeEmpty()) |
| 175 |
}) |
| 176 |
|
| 177 |
ginkgo.It("should handle empty values gracefully", func() { |
| 178 |
config := &Config{} |
| 179 |
url := config.GetURL() |
| 180 |
gomega.Expect(url.Host).To(gomega.BeEmpty()) |
| 181 |
gomega.Expect(url.User.Username()).To(gomega.BeEmpty()) |
| 182 |
}) |
| 183 |
|
| 184 |
ginkgo.It("should return error for missing webhook ID", func() { |
| 185 |
url, _ := url.Parse("discord://token@") |
| 186 |
config := &Config{} |
| 187 |
err := config.SetURL(url) |
| 188 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 189 |
gomega.Expect(err).To(gomega.MatchError(ErrMissingWebhookID)) |
| 190 |
}) |
| 191 |
|
| 192 |
ginkgo.It("should return error for missing token", func() { |
| 193 |
url, _ := url.Parse("discord://@webhook") |
| 194 |
config := &Config{} |
| 195 |
err := config.SetURL(url) |
| 196 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 197 |
gomega.Expect(err).To(gomega.MatchError(ErrMissingToken)) |
| 198 |
}) |
| 199 |
|
| 200 |
ginkgo.It("should return error for invalid path", func() { |
| 201 |
url, _ := url.Parse("discord://token@webhook/invalid") |
| 202 |
config := &Config{} |
| 203 |
err := config.SetURL(url) |
| 204 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 205 |
gomega.Expect(err).To(gomega.MatchError(ErrIllegalURLArgument)) |
| 206 |
}) |
| 207 |
|
| 208 |
ginkgo.It("should return error for invalid query parameters", func() { |
| 209 |
url, _ := url.Parse("discord://token@webhook?invalid_key=value") |
| 210 |
config := &Config{} |
| 211 |
resolver := format.NewPropKeyResolver(config) |
| 212 |
err := resolver.SetDefaultProps(config) |
| 213 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 214 |
err = config.SetURL(url) |
| 215 |
gomega.Expect(err).To(gomega.HaveOccurred()) // Should error on unknown keys |
| 216 |
}) |
| 217 |
}) |
| 218 |
|
| 219 |
ginkgo.Describe("CreatePostURLFromConfig function", func() { |
| 220 |
ginkgo.It("should create correct API URL without thread ID", func() { |
| 221 |
config := &Config{ |
| 222 |
WebhookID: "123456789", |
| 223 |
Token: "test-token", |
| 224 |
} |
| 225 |
apiURL := CreatePostURLFromConfig(config) |
| 226 |
expected := testWebhookURL |
| 227 |
gomega.Expect(apiURL).To(gomega.Equal(expected)) |
| 228 |
}) |
| 229 |
|
| 230 |
ginkgo.It("should create correct API URL with thread ID", func() { |
| 231 |
config := &Config{ |
| 232 |
WebhookID: "123456789", |
| 233 |
Token: "test-token", |
| 234 |
ThreadID: "987654321", |
| 235 |
} |
| 236 |
apiURL := CreatePostURLFromConfig(config) |
| 237 |
expected := testWebhookURLWithThread |
| 238 |
gomega.Expect(apiURL).To(gomega.Equal(expected)) |
| 239 |
}) |
| 240 |
|
| 241 |
ginkgo.It("should trim whitespace from webhook ID and token", func() { |
| 242 |
config := &Config{ |
| 243 |
WebhookID: " 123456789 ", |
| 244 |
Token: " test-token ", |
| 245 |
} |
| 246 |
apiURL := CreatePostURLFromConfig(config) |
| 247 |
expected := testWebhookURL |
| 248 |
gomega.Expect(apiURL).To(gomega.Equal(expected)) |
| 249 |
}) |
| 250 |
|
| 251 |
ginkgo.It("should trim whitespace from thread ID", func() { |
| 252 |
config := &Config{ |
| 253 |
WebhookID: "123456789", |
| 254 |
Token: "test-token", |
| 255 |
ThreadID: " 987654321 ", |
| 256 |
} |
| 257 |
apiURL := CreatePostURLFromConfig(config) |
| 258 |
expected := testWebhookURLWithThread |
| 259 |
gomega.Expect(apiURL).To(gomega.Equal(expected)) |
| 260 |
}) |
| 261 |
|
| 262 |
ginkgo.It("should return empty string for invalid config", func() { |
| 263 |
config := &Config{ |
| 264 |
WebhookID: "", |
| 265 |
Token: "", |
| 266 |
} |
| 267 |
apiURL := CreatePostURLFromConfig(config) |
| 268 |
gomega.Expect(apiURL).To(gomega.BeEmpty()) |
| 269 |
}) |
| 270 |
|
| 271 |
ginkgo.It("should handle empty thread ID", func() { |
| 272 |
config := &Config{ |
| 273 |
WebhookID: "123456789", |
| 274 |
Token: "test-token", |
| 275 |
ThreadID: "", |
| 276 |
} |
| 277 |
apiURL := CreatePostURLFromConfig(config) |
| 278 |
expected := testWebhookURL |
| 279 |
gomega.Expect(apiURL).To(gomega.Equal(expected)) |
| 280 |
}) |
| 281 |
|
| 282 |
ginkgo.It("should handle very long webhook ID and token", func() { |
| 283 |
longID := strings.Repeat("1", 100) |
| 284 |
longToken := strings.Repeat("a", 100) |
| 285 |
config := &Config{ |
| 286 |
WebhookID: longID, |
| 287 |
Token: longToken, |
| 288 |
} |
| 289 |
apiURL := CreatePostURLFromConfig(config) |
| 290 |
expected := "https://discord.com/api/webhooks/" + longID + "/" + longToken |
| 291 |
gomega.Expect(apiURL).To(gomega.Equal(expected)) |
| 292 |
}) |
| 293 |
}) |
| 294 |
}) |