| 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/gorilla/websocket" |
| 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/push/homeassistant" |
| 19 |
) |
| 20 |
|
| 21 |
type haNotification struct { |
| 22 |
Message string `json:"message"` |
| 23 |
NotificationID string `json:"notification_id"` |
| 24 |
Title string `json:"title"` |
| 25 |
} |
| 26 |
|
| 27 |
const defaultHTTPTimeout = 10 * time.Second |
| 28 |
|
| 29 |
func Test_HomeAssistant_E2E(t *testing.T) { //nolint:paralleltest // mutates process-wide env via loadEnvFile |
| 30 |
loadEnvFile(".env") |
| 31 |
|
| 32 |
gomega.RegisterFailHandler(ginkgo.Fail) |
| 33 |
ginkgo.RunSpecs(t, "Home Assistant E2E Tests") |
| 34 |
} |
| 35 |
|
| 36 |
func serviceURL() string { |
| 37 |
return os.Getenv("SHOUTRRR_HOMEASSISTANT_URL") |
| 38 |
} |
| 39 |
|
| 40 |
func initializeService(urlStr string) *homeassistant.Service { |
| 41 |
parsed, err := url.Parse(urlStr) |
| 42 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 43 |
|
| 44 |
service := &homeassistant.Service{} |
| 45 |
err = service.Initialize(parsed, testutils.TestLogger()) |
| 46 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 47 |
|
| 48 |
return service |
| 49 |
} |
| 50 |
|
| 51 |
func withNid(rawURL, nid string) string { |
| 52 |
parsed, err := url.Parse(rawURL) |
| 53 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 54 |
|
| 55 |
query := parsed.Query() |
| 56 |
query.Set("nid", nid) |
| 57 |
parsed.RawQuery = query.Encode() |
| 58 |
|
| 59 |
return parsed.String() |
| 60 |
} |
| 61 |
|
| 62 |
func isHomeAssistantAvailable() bool { |
| 63 |
baseURL := serviceURL() |
| 64 |
if baseURL == "" { |
| 65 |
return false |
| 66 |
} |
| 67 |
|
| 68 |
parsed, err := url.Parse(baseURL) |
| 69 |
if err != nil || parsed.User == nil { |
| 70 |
return false |
| 71 |
} |
| 72 |
|
| 73 |
token := parsed.User.Username() |
| 74 |
apiURL := "http://localhost:8123/api/" |
| 75 |
|
| 76 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 77 |
defer cancel() |
| 78 |
|
| 79 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, http.NoBody) |
| 80 |
if err != nil { |
| 81 |
return false |
| 82 |
} |
| 83 |
|
| 84 |
req.Header.Set("Authorization", "Bearer "+token) |
| 85 |
|
| 86 |
client := &http.Client{Timeout: 2 * time.Second} |
| 87 |
|
| 88 |
resp, err := client.Do(req) |
| 89 |
if err != nil { |
| 90 |
return false |
| 91 |
} |
| 92 |
|
| 93 |
defer func() { _ = resp.Body.Close() }() |
| 94 |
|
| 95 |
return resp.StatusCode == http.StatusOK |
| 96 |
} |
| 97 |
|
| 98 |
func fetchPersistentNotification(token, nid string) haNotification { |
| 99 |
dialer := websocket.Dialer{HandshakeTimeout: defaultHTTPTimeout} |
| 100 |
conn, _, err := dialer.Dial("ws://localhost:8123/api/websocket", nil) |
| 101 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 102 |
|
| 103 |
defer func() { _ = conn.Close() }() |
| 104 |
|
| 105 |
gomega.Expect(conn.SetReadDeadline(time.Now().Add(defaultHTTPTimeout))).To(gomega.Succeed()) |
| 106 |
gomega.Expect(conn.SetWriteDeadline(time.Now().Add(defaultHTTPTimeout))).To(gomega.Succeed()) |
| 107 |
|
| 108 |
var hello map[string]any |
| 109 |
gomega.Expect(conn.ReadJSON(&hello)).To(gomega.Succeed()) |
| 110 |
gomega.Expect(hello["type"]).To(gomega.Equal("auth_required")) |
| 111 |
|
| 112 |
gomega.Expect(conn.WriteJSON(map[string]string{ |
| 113 |
"type": "auth", |
| 114 |
"access_token": token, |
| 115 |
})).To(gomega.Succeed()) |
| 116 |
|
| 117 |
var auth map[string]any |
| 118 |
gomega.Expect(conn.ReadJSON(&auth)).To(gomega.Succeed()) |
| 119 |
gomega.Expect(auth["type"]).To(gomega.Equal("auth_ok")) |
| 120 |
|
| 121 |
gomega.Expect(conn.WriteJSON(map[string]any{ |
| 122 |
"id": 1, |
| 123 |
"type": "persistent_notification/get", |
| 124 |
})).To(gomega.Succeed()) |
| 125 |
|
| 126 |
var result struct { |
| 127 |
Success bool `json:"success"` |
| 128 |
Result []haNotification `json:"result"` |
| 129 |
} |
| 130 |
gomega.Expect(conn.ReadJSON(&result)).To(gomega.Succeed()) |
| 131 |
gomega.Expect(result.Success).To(gomega.BeTrue()) |
| 132 |
|
| 133 |
for _, notification := range result.Result { |
| 134 |
if notification.NotificationID == nid { |
| 135 |
return notification |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
gomega.Expect(result.Result).To(gomega.ContainElement(gomega.HaveField("NotificationID", nid))) |
| 140 |
|
| 141 |
return haNotification{} |
| 142 |
} |
| 143 |
|
| 144 |
func accessToken(rawURL string) string { |
| 145 |
parsed, err := url.Parse(rawURL) |
| 146 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 147 |
gomega.Expect(parsed.User).NotTo(gomega.BeNil()) |
| 148 |
|
| 149 |
return parsed.User.Username() |
| 150 |
} |
| 151 |
|
| 152 |
func loadEnvFile(filename string) { |
| 153 |
file, err := os.Open(filename) |
| 154 |
if err != nil { |
| 155 |
return |
| 156 |
} |
| 157 |
|
| 158 |
defer func() { _ = file.Close() }() |
| 159 |
|
| 160 |
scanner := bufio.NewScanner(file) |
| 161 |
for scanner.Scan() { |
| 162 |
line := strings.TrimSpace(scanner.Text()) |
| 163 |
if line == "" || strings.HasPrefix(line, "#") { |
| 164 |
continue |
| 165 |
} |
| 166 |
|
| 167 |
parts := strings.SplitN(line, "=", 2) |
| 168 |
if len(parts) != 2 { |
| 169 |
continue |
| 170 |
} |
| 171 |
|
| 172 |
key := strings.TrimSpace(parts[0]) |
| 173 |
|
| 174 |
value := strings.Trim(strings.TrimSpace(parts[1]), `"'`) |
| 175 |
if _, exists := os.LookupEnv(key); !exists { |
| 176 |
_ = os.Setenv(key, value) |
| 177 |
} |
| 178 |
} |
| 179 |
} |