Debian Codegraph @

testing/e2e/discord/thread_test.go

raw
1 package e2e_test
2
3 import (
4 "net/url"
5 "os"
6 "strings"
7
8 "github.com/onsi/ginkgo/v2"
9 "github.com/onsi/gomega"
10
11 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
12 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/discord"
13 )
14
15 var _ = ginkgo.Describe("Discord E2E Thread Test", func() {
16 ginkgo.When("running e2e tests", func() {
17 ginkgo.It("should post message to existing thread using thread_id", func() {
18 envURL := os.Getenv("SHOUTRRR_DISCORD_URL")
19 envThreadID := os.Getenv("SHOUTRRR_DISCORD_THREAD_ID")
20
21 if envURL == "" {
22 ginkgo.Skip("SHOUTRRR_DISCORD_URL not set, skipping thread posting test")
23
24 return
25 }
26
27 if envThreadID == "" {
28 ginkgo.Skip("SHOUTRRR_DISCORD_THREAD_ID not set, skipping thread posting test")
29
30 return
31 }
32
33 demoURLStr := envURL + "?thread_id=" + envThreadID
34 demoURL, err := url.Parse(demoURLStr)
35 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to parse Discord URL")
36
37 service := &discord.Service{}
38 err = service.Initialize(demoURL, testutils.TestLogger())
39 gomega.Expect(err).NotTo(gomega.HaveOccurred())
40
41 // Debug: Print configuration
42 ginkgo.GinkgoWriter.Printf(
43 "DEBUG: Discord config - WebhookID: %s, Token: %s, ThreadID: %s\n",
44 redact(service.Config.WebhookID),
45 redact(service.Config.Token),
46 redact(service.Config.ThreadID),
47 )
48
49 err = service.Send("E2E Test: Posting to existing thread", nil)
50 // Debug: Print the error if any
51 if err != nil {
52 ginkgo.GinkgoWriter.Printf("DEBUG: Send error: %v\n", err)
53 }
54
55 // Posting to thread may fail due to permissions or thread state
56 // Accept both success and graceful failure (400 Bad Request due to permissions)
57 if err != nil {
58 // Check if it's a 400 error (permission/thread issue) - this is acceptable
59 errStr := err.Error()
60 if strings.Contains(errStr, "400") || strings.Contains(errStr, "Bad Request") {
61 ginkgo.GinkgoWriter.Printf("DEBUG: Skipping due to 400 error: %v\n", err)
62 ginkgo.Skip(
63 "Thread posting failed due to permissions/thread state (400), skipping test",
64 )
65
66 return
67 }
68 // For other errors, still fail the test
69 gomega.Expect(err).NotTo(gomega.HaveOccurred())
70 } else {
71 ginkgo.GinkgoWriter.Printf(
72 "DEBUG: Send succeeded, message should be posted to thread\n",
73 )
74 }
75 })
76 })
77 })
78
79 // redact masks sensitive string values for logging.
80 func redact(value string) string {
81 if len(value) <= 4 {
82 return "****"
83 }
84
85 return "****" + value[len(value)-4:]
86 }