| 1 | package e2e_test |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/onsi/ginkgo/v2" |
| 14 | "github.com/onsi/gomega" |
| 15 | |
| 16 | "github.com/nicholas-fedor/shoutrrr/internal/testutils" |
| 17 | "github.com/nicholas-fedor/shoutrrr/pkg/services/push/ntfy" |
| 18 | ) |
| 19 | |
| 20 | // envValueTrue is the string value for boolean true in environment variables. |
| 21 | const envValueTrue = "true" |
| 22 | |
| 23 | // Test_Ntfy_E2E runs the ntfy E2E test suite using Ginkgo. |
| 24 | // These tests connect to a real ntfy server and verify actual behavior. |
| 25 | // |
| 26 | // Environment variables: |
| 27 | // - SHOUTRRR_NTFY_URL: ntfy server URL (default: ntfy://localhost:8080/shoutrrr-e2e-test) |
| 28 | // - SHOUTRRR_NTFY_USERNAME: Basic auth username (optional) |
| 29 | // - SHOUTRRR_NTFY_PASSWORD: Basic auth password (optional) |
| 30 | // - SHOUTRRR_NTFY_DISABLE_TLS: Set to "true" to use HTTP instead of HTTPS (optional) |
| 31 | func Test_Ntfy_E2E(t *testing.T) { //nolint:paralleltest // mutates process-wide env via loadEnvFile |
| 32 | loadEnvFile(".env") |
| 33 | |
| 34 | gomega.RegisterFailHandler(ginkgo.Fail) |
| 35 | |
| 36 | // Add delay between tests to respect ntfy rate limits |
| 37 | ginkgo.BeforeEach(func() { |
| 38 | time.Sleep(500 * time.Millisecond) |
| 39 | }) |
| 40 | |
| 41 | ginkgo.RunSpecs(t, "ntfy E2E Tests") |
| 42 | } |
| 43 | |
| 44 | // buildServiceURL constructs the ntfy service URL from environment variables. |
| 45 | func buildServiceURL() string { |
| 46 | baseURL := os.Getenv("SHOUTRRR_NTFY_URL") |
| 47 | if baseURL == "" { |
| 48 | baseURL = "ntfy://localhost:8080/shoutrrr-e2e-test" |
| 49 | } |
| 50 | |
| 51 | serviceURL, err := url.Parse(baseURL) |
| 52 | if err != nil { |
| 53 | return baseURL |
| 54 | } |
| 55 | |
| 56 | query := serviceURL.Query() |
| 57 | if os.Getenv("SHOUTRRR_NTFY_DISABLE_TLS") == envValueTrue { |
| 58 | query.Set("disabletls", "yes") |
| 59 | } |
| 60 | |
| 61 | username := os.Getenv("SHOUTRRR_NTFY_USERNAME") |
| 62 | password := os.Getenv("SHOUTRRR_NTFY_PASSWORD") |
| 63 | |
| 64 | if username != "" || password != "" { |
| 65 | if password != "" { |
| 66 | serviceURL.User = url.UserPassword(username, password) |
| 67 | } else { |
| 68 | serviceURL.User = url.User(username) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | serviceURL.RawQuery = query.Encode() |
| 73 | |
| 74 | result := serviceURL.String() |
| 75 | if !strings.Contains(result, "?") { |
| 76 | result += "?" |
| 77 | } |
| 78 | |
| 79 | return result |
| 80 | } |
| 81 | |
| 82 | // addQueryParam adds a query parameter to a URL string. |
| 83 | func addQueryParam(rawURL, key, value string) string { |
| 84 | serviceURL, err := url.Parse(rawURL) |
| 85 | if err != nil { |
| 86 | return rawURL |
| 87 | } |
| 88 | |
| 89 | query := serviceURL.Query() |
| 90 | query.Set(key, value) |
| 91 | serviceURL.RawQuery = query.Encode() |
| 92 | |
| 93 | return serviceURL.String() |
| 94 | } |
| 95 | |
| 96 | // initializeService parses a service URL string, creates an ntfy.Service, and initializes it. |
| 97 | func initializeService(urlStr string) *ntfy.Service { |
| 98 | serviceURL, err := url.Parse(urlStr) |
| 99 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 100 | |
| 101 | service := &ntfy.Service{} |
| 102 | err = service.Initialize(serviceURL, testutils.TestLogger()) |
| 103 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 104 | |
| 105 | return service |
| 106 | } |
| 107 | |
| 108 | // loadEnvFile loads environment variables from a .env file. |
| 109 | func loadEnvFile(filename string) { |
| 110 | file, err := os.Open(filename) |
| 111 | if err != nil { |
| 112 | // .env file doesn't exist, skip loading |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | defer func() { _ = file.Close() }() |
| 117 | |
| 118 | scanner := bufio.NewScanner(file) |
| 119 | for scanner.Scan() { |
| 120 | line := strings.TrimSpace(scanner.Text()) |
| 121 | if line == "" || strings.HasPrefix(line, "#") { |
| 122 | continue |
| 123 | } |
| 124 | |
| 125 | parts := strings.SplitN(line, "=", 2) |
| 126 | if len(parts) == 2 { |
| 127 | key := strings.TrimSpace(parts[0]) |
| 128 | value := strings.TrimSpace(parts[1]) |
| 129 | // Remove quotes if present |
| 130 | value = strings.Trim(value, `"'`) |
| 131 | if _, exists := os.LookupEnv(key); !exists { |
| 132 | _ = os.Setenv(key, value) |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if err := scanner.Err(); err != nil { |
| 138 | return |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // getNtfyBaseURL returns the base URL for the ntfy server using the SHOUTRRR_NTFY_URL environment variable. |
| 143 | func getNtfyBaseURL() string { |
| 144 | baseURL := os.Getenv("SHOUTRRR_NTFY_URL") |
| 145 | if baseURL == "" { |
| 146 | baseURL = "ntfy://localhost:8080/shoutrrr-e2e-test" |
| 147 | } |
| 148 | |
| 149 | parsed, err := url.Parse(baseURL) |
| 150 | if err != nil { |
| 151 | return "http://localhost:8080" |
| 152 | } |
| 153 | |
| 154 | scheme := "https" |
| 155 | if os.Getenv("SHOUTRRR_NTFY_DISABLE_TLS") == envValueTrue { |
| 156 | scheme = "http" |
| 157 | } |
| 158 | |
| 159 | return scheme + "://" + parsed.Host |
| 160 | } |
| 161 | |
| 162 | // isNtfyServerAvailable checks if the ntfy server is reachable. |
| 163 | func isNtfyServerAvailable() bool { |
| 164 | baseURL := getNtfyBaseURL() |
| 165 | |
| 166 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 167 | defer cancel() |
| 168 | |
| 169 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/v1/health", http.NoBody) |
| 170 | if err != nil { |
| 171 | return false |
| 172 | } |
| 173 | |
| 174 | client := &http.Client{Timeout: 2 * time.Second} |
| 175 | |
| 176 | resp, err := client.Do(req) |
| 177 | if err != nil { |
| 178 | return false |
| 179 | } |
| 180 | |
| 181 | defer func() { _ = resp.Body.Close() }() |
| 182 | |
| 183 | return resp.StatusCode == http.StatusOK |
| 184 | } |