| 1 | package e2e_test |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/onsi/ginkgo/v2" |
| 15 | "github.com/onsi/gomega" |
| 16 | |
| 17 | "github.com/nicholas-fedor/shoutrrr/internal/testutils" |
| 18 | "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix" |
| 19 | ) |
| 20 | |
| 21 | const envValueTrue = "true" |
| 22 | |
| 23 | // sharedService is the authenticated Matrix service shared across tests. |
| 24 | // Initialized once in BeforeSuite to avoid repeated login operations. |
| 25 | var ( |
| 26 | sharedService *matrix.Service |
| 27 | sharedServiceOnce sync.Once |
| 28 | errSharedServiceInit error |
| 29 | ) |
| 30 | |
| 31 | // Test_Matrix_E2E runs the Matrix E2E test suite using Ginkgo. |
| 32 | // These tests connect to a real Matrix server and verify actual behavior. |
| 33 | func Test_Matrix_E2E(t *testing.T) { |
| 34 | t.Parallel() |
| 35 | |
| 36 | // Load .env file if it exists |
| 37 | loadEnvFile(".env") |
| 38 | |
| 39 | gomega.RegisterFailHandler(ginkgo.Fail) |
| 40 | |
| 41 | // Initialize shared service once before all tests |
| 42 | ginkgo.BeforeSuite(func() { |
| 43 | serviceURL := buildServiceURL() |
| 44 | if serviceURL == "" { |
| 45 | return // tests will skip anyway |
| 46 | } |
| 47 | |
| 48 | parsedURL, err := url.Parse(serviceURL) |
| 49 | if err != nil { |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | // Create a local service instance and only assign to sharedService |
| 54 | // after Initialize succeeds to prevent tests from using an |
| 55 | // uninitialized client. |
| 56 | service := &matrix.Service{} |
| 57 | if err := service.Initialize(parsedURL, testutils.TestLogger()); err != nil { |
| 58 | // Log but continue; individual tests will handle failures |
| 59 | fmt.Printf("Warning: shared service init failed: %v\n", err) |
| 60 | |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | sharedService = service |
| 65 | }) |
| 66 | |
| 67 | ginkgo.BeforeEach(func() { |
| 68 | time.Sleep(500 * time.Millisecond) // slightly longer safety delay |
| 69 | }) |
| 70 | |
| 71 | ginkgo.RunSpecs(t, "Matrix E2E Tests") |
| 72 | } |
| 73 | |
| 74 | // matrixServerURL returns the Matrix server URL from environment or default. |
| 75 | // Expected format: matrix://user:password@host:port |
| 76 | func matrixServerURL() string { |
| 77 | // Check for URL in environment variable |
| 78 | envURL := os.Getenv("SHOUTRRR_MATRIX_URL") |
| 79 | if envURL != "" { |
| 80 | return envURL |
| 81 | } |
| 82 | |
| 83 | // Build URL from individual components |
| 84 | host := os.Getenv("SHOUTRRR_MATRIX_HOST") |
| 85 | if host == "" { |
| 86 | host = "localhost:8008" |
| 87 | } |
| 88 | |
| 89 | user := os.Getenv("SHOUTRRR_MATRIX_USER") |
| 90 | password := os.Getenv("SHOUTRRR_MATRIX_PASSWORD") |
| 91 | |
| 92 | if user != "" && password != "" { |
| 93 | return "matrix://" + url.UserPassword(user, password).String() + "@" + host |
| 94 | } |
| 95 | |
| 96 | // Return empty string if no credentials available |
| 97 | return "" |
| 98 | } |
| 99 | |
| 100 | // matrixRoom returns the Matrix room from environment or default test room. |
| 101 | func matrixRoom() string { |
| 102 | room := os.Getenv("SHOUTRRR_MATRIX_ROOM") |
| 103 | if room != "" { |
| 104 | return room |
| 105 | } |
| 106 | |
| 107 | // Default test room alias |
| 108 | return "#test:localhost" |
| 109 | } |
| 110 | |
| 111 | // matrixDisableTLS returns whether TLS should be disabled. |
| 112 | func matrixDisableTLS() bool { |
| 113 | return os.Getenv("SHOUTRRR_MATRIX_DISABLE_TLS") == envValueTrue |
| 114 | } |
| 115 | |
| 116 | // buildServiceURL builds a complete Matrix service URL with all parameters. |
| 117 | func buildServiceURL() string { |
| 118 | baseURL := matrixServerURL() |
| 119 | |
| 120 | // If no URL from environment, return empty |
| 121 | if baseURL == "" { |
| 122 | return "" |
| 123 | } |
| 124 | |
| 125 | // Parse to add query params |
| 126 | parsedURL, err := url.Parse(baseURL) |
| 127 | if err != nil { |
| 128 | return "" |
| 129 | } |
| 130 | |
| 131 | // Add room parameter |
| 132 | room := matrixRoom() |
| 133 | if room != "" { |
| 134 | q := parsedURL.Query() |
| 135 | q.Add("room", room) |
| 136 | parsedURL.RawQuery = q.Encode() |
| 137 | } |
| 138 | |
| 139 | // Add TLS disable if needed |
| 140 | if matrixDisableTLS() { |
| 141 | q := parsedURL.Query() |
| 142 | q.Add("disableTLS", "true") |
| 143 | parsedURL.RawQuery = q.Encode() |
| 144 | } |
| 145 | |
| 146 | return parsedURL.String() |
| 147 | } |
| 148 | |
| 149 | // loadEnvFile loads environment variables from a .env file. |
| 150 | func loadEnvFile(filename string) { |
| 151 | file, err := os.Open(filename) |
| 152 | if err != nil { |
| 153 | // .env file doesn't exist, skip loading |
| 154 | return |
| 155 | } |
| 156 | |
| 157 | defer func() { _ = file.Close() }() |
| 158 | |
| 159 | scanner := bufio.NewScanner(file) |
| 160 | for scanner.Scan() { |
| 161 | line := strings.TrimSpace(scanner.Text()) |
| 162 | if line == "" || strings.HasPrefix(line, "#") { |
| 163 | continue |
| 164 | } |
| 165 | |
| 166 | parts := strings.SplitN(line, "=", 2) |
| 167 | if len(parts) == 2 { |
| 168 | key := strings.TrimSpace(parts[0]) |
| 169 | value := strings.TrimSpace(parts[1]) |
| 170 | // Remove quotes if present |
| 171 | value = strings.Trim(value, `"'`) |
| 172 | // Only set if not already present in the environment |
| 173 | if _, exists := os.LookupEnv(key); !exists { |
| 174 | _ = os.Setenv(key, value) // Ignore error as it's test setup |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if err := scanner.Err(); err != nil { |
| 180 | return |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // getOrInitSharedService initializes the shared Matrix service once in a |
| 185 | // thread-safe manner if it hasn't been initialized yet. If BeforeSuite has |
| 186 | // already initialized sharedService, it returns the existing instance without |
| 187 | // re-initializing. |
| 188 | func getOrInitSharedService() error { |
| 189 | // Check if sharedService was already initialized (e.g., by BeforeSuite) |
| 190 | if sharedService != nil { |
| 191 | return nil |
| 192 | } |
| 193 | |
| 194 | sharedServiceOnce.Do(func() { |
| 195 | serviceURL := buildServiceURL() |
| 196 | if serviceURL == "" { |
| 197 | errSharedServiceInit = errors.New("Matrix server not configured") |
| 198 | |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | parsedURL, err := url.Parse(serviceURL) |
| 203 | if err != nil { |
| 204 | errSharedServiceInit = fmt.Errorf("parsing service URL: %w", err) |
| 205 | |
| 206 | return |
| 207 | } |
| 208 | |
| 209 | // Create a local service instance and only assign to sharedService |
| 210 | // after Initialize succeeds to prevent tests from using an |
| 211 | // uninitialized client. |
| 212 | service := &matrix.Service{} |
| 213 | if err := service.Initialize(parsedURL, testutils.TestLogger()); err != nil { |
| 214 | errSharedServiceInit = fmt.Errorf("initializing service: %w", err) |
| 215 | |
| 216 | return |
| 217 | } |
| 218 | |
| 219 | sharedService = service |
| 220 | }) |
| 221 | |
| 222 | return errSharedServiceInit |
| 223 | } |