| 1 |
package matrix |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
|
| 11 |
"github.com/onsi/gomega" |
| 12 |
"github.com/stretchr/testify/mock" |
| 13 |
|
| 14 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 15 |
|
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix/mocks" |
| 17 |
) |
| 18 |
|
| 19 |
// testLogger is a simple logger for testing purposes. |
| 20 |
type testLogger struct { |
| 21 |
formatted string |
| 22 |
} |
| 23 |
|
| 24 |
// errorReadCloser is a ReadCloser that always returns an error on read. |
| 25 |
type errorReadCloser struct{} |
| 26 |
|
| 27 |
// errorBodyClose is a ReadCloser that returns error on close. |
| 28 |
type errorBodyClose struct{} |
| 29 |
|
| 30 |
var _ = ginkgo.Describe("client", func() { |
| 31 |
ginkgo.Describe("newClient", func() { |
| 32 |
ginkgo.It("should create a client with https scheme by default", func() { |
| 33 |
c := newClient("matrix.example.com", false, nil) |
| 34 |
gomega.Expect(c).ToNot(gomega.BeNil()) |
| 35 |
gomega.Expect(c.apiURL.Scheme).To(gomega.Equal("https")) |
| 36 |
gomega.Expect(c.apiURL.Host).To(gomega.Equal("matrix.example.com")) |
| 37 |
}) |
| 38 |
|
| 39 |
ginkgo.It("should create a client with http scheme when TLS is disabled", func() { |
| 40 |
c := newClient("matrix.example.com", true, nil) |
| 41 |
gomega.Expect(c).ToNot(gomega.BeNil()) |
| 42 |
gomega.Expect(c.apiURL.Scheme).To(gomega.Equal("http")) |
| 43 |
}) |
| 44 |
|
| 45 |
ginkgo.It("should use discard logger when nil logger is provided", func() { |
| 46 |
c := newClient("matrix.example.com", false, nil) |
| 47 |
gomega.Expect(c.logger).ToNot(gomega.BeNil()) |
| 48 |
}) |
| 49 |
}) |
| 50 |
|
| 51 |
ginkgo.Describe("apiGet", func() { |
| 52 |
ginkgo.It("should handle HTTP errors from the client", func() { |
| 53 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 54 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 55 |
|
| 56 |
c := &client{ |
| 57 |
apiURL: url.URL{ |
| 58 |
Scheme: "https", |
| 59 |
Host: "matrix.example.com", |
| 60 |
}, |
| 61 |
httpClient: mockHTTPClient, |
| 62 |
logger: &testLogger{}, |
| 63 |
} |
| 64 |
|
| 65 |
resp := &apiResLogin{} |
| 66 |
err := c.apiGet(context.Background(), "/_matrix/client/v3/login", resp) |
| 67 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 68 |
}) |
| 69 |
|
| 70 |
ginkgo.It("should handle HTTP error status codes", func() { |
| 71 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 72 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 73 |
StatusCode: http.StatusBadRequest, |
| 74 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"error":"invalid request","errcode":"M_INVALID_PARAM"}`))), |
| 75 |
}, nil) |
| 76 |
|
| 77 |
c := &client{ |
| 78 |
apiURL: url.URL{ |
| 79 |
Scheme: "https", |
| 80 |
Host: "matrix.example.com", |
| 81 |
}, |
| 82 |
httpClient: mockHTTPClient, |
| 83 |
logger: &testLogger{}, |
| 84 |
} |
| 85 |
|
| 86 |
resp := &apiResLogin{} |
| 87 |
err := c.apiGet(context.Background(), "/_matrix/client/v3/login", resp) |
| 88 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 89 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("invalid request")) |
| 90 |
}) |
| 91 |
|
| 92 |
ginkgo.It("should handle HTTP error status without JSON response", func() { |
| 93 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 94 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 95 |
StatusCode: http.StatusInternalServerError, |
| 96 |
Body: io.NopCloser(bytes.NewReader([]byte("internal server error"))), |
| 97 |
}, nil) |
| 98 |
|
| 99 |
c := &client{ |
| 100 |
apiURL: url.URL{ |
| 101 |
Scheme: "https", |
| 102 |
Host: "matrix.example.com", |
| 103 |
}, |
| 104 |
httpClient: mockHTTPClient, |
| 105 |
logger: &testLogger{}, |
| 106 |
} |
| 107 |
|
| 108 |
resp := &apiResLogin{} |
| 109 |
err := c.apiGet(context.Background(), "/_matrix/client/v3/login", resp) |
| 110 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 111 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("HTTP")) |
| 112 |
}) |
| 113 |
|
| 114 |
ginkgo.It("should handle JSON unmarshal error on response", func() { |
| 115 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 116 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 117 |
StatusCode: http.StatusOK, |
| 118 |
Body: io.NopCloser(bytes.NewReader([]byte("not valid json"))), |
| 119 |
}, nil) |
| 120 |
|
| 121 |
c := &client{ |
| 122 |
apiURL: url.URL{ |
| 123 |
Scheme: "https", |
| 124 |
Host: "matrix.example.com", |
| 125 |
}, |
| 126 |
httpClient: mockHTTPClient, |
| 127 |
logger: &testLogger{}, |
| 128 |
} |
| 129 |
|
| 130 |
resp := &apiResLogin{} |
| 131 |
err := c.apiGet(context.Background(), "/_matrix/client/v3/login", resp) |
| 132 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 133 |
}) |
| 134 |
|
| 135 |
ginkgo.It("should handle body read error", func() { |
| 136 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 137 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 138 |
StatusCode: http.StatusOK, |
| 139 |
Body: &errorReadCloser{}, |
| 140 |
}, nil) |
| 141 |
|
| 142 |
c := &client{ |
| 143 |
apiURL: url.URL{ |
| 144 |
Scheme: "https", |
| 145 |
Host: "matrix.example.com", |
| 146 |
}, |
| 147 |
httpClient: mockHTTPClient, |
| 148 |
logger: &testLogger{}, |
| 149 |
} |
| 150 |
|
| 151 |
resp := &apiResLogin{} |
| 152 |
err := c.apiGet(context.Background(), "/_matrix/client/v3/login", resp) |
| 153 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 154 |
}) |
| 155 |
}) |
| 156 |
|
| 157 |
ginkgo.Describe("apiPost", func() { |
| 158 |
ginkgo.It("should handle HTTP errors from the client", func() { |
| 159 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 160 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 161 |
|
| 162 |
c := &client{ |
| 163 |
apiURL: url.URL{ |
| 164 |
Scheme: "https", |
| 165 |
Host: "matrix.example.com", |
| 166 |
}, |
| 167 |
httpClient: mockHTTPClient, |
| 168 |
logger: &testLogger{}, |
| 169 |
} |
| 170 |
|
| 171 |
resp := &apiResLogin{} |
| 172 |
err := c.apiPost(context.Background(), "/_matrix/client/v3/login", nil, resp) |
| 173 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 174 |
}) |
| 175 |
|
| 176 |
ginkgo.It("should handle HTTP error status codes", func() { |
| 177 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 178 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 179 |
StatusCode: http.StatusBadRequest, |
| 180 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"error":"invalid request","errcode":"M_INVALID_PARAM"}`))), |
| 181 |
}, nil) |
| 182 |
|
| 183 |
c := &client{ |
| 184 |
apiURL: url.URL{ |
| 185 |
Scheme: "https", |
| 186 |
Host: "matrix.example.com", |
| 187 |
}, |
| 188 |
httpClient: mockHTTPClient, |
| 189 |
logger: &testLogger{}, |
| 190 |
} |
| 191 |
|
| 192 |
resp := &apiResLogin{} |
| 193 |
err := c.apiPost(context.Background(), "/_matrix/client/v3/login", nil, resp) |
| 194 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 195 |
}) |
| 196 |
|
| 197 |
ginkgo.It("should handle JSON unmarshal error on response", func() { |
| 198 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 199 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 200 |
StatusCode: http.StatusOK, |
| 201 |
Body: io.NopCloser(bytes.NewReader([]byte("not valid json"))), |
| 202 |
}, nil) |
| 203 |
|
| 204 |
c := &client{ |
| 205 |
apiURL: url.URL{ |
| 206 |
Scheme: "https", |
| 207 |
Host: "matrix.example.com", |
| 208 |
}, |
| 209 |
httpClient: mockHTTPClient, |
| 210 |
logger: &testLogger{}, |
| 211 |
} |
| 212 |
|
| 213 |
resp := &apiResLogin{} |
| 214 |
err := c.apiPost(context.Background(), "/_matrix/client/v3/login", nil, resp) |
| 215 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 216 |
}) |
| 217 |
}) |
| 218 |
|
| 219 |
ginkgo.Describe("getJoinedRooms", func() { |
| 220 |
ginkgo.It("should return empty slice on API error", func() { |
| 221 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 222 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 223 |
|
| 224 |
c := &client{ |
| 225 |
apiURL: url.URL{ |
| 226 |
Scheme: "https", |
| 227 |
Host: "matrix.example.com", |
| 228 |
}, |
| 229 |
httpClient: mockHTTPClient, |
| 230 |
logger: &testLogger{}, |
| 231 |
} |
| 232 |
|
| 233 |
rooms, err := c.getJoinedRooms(context.Background()) |
| 234 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 235 |
gomega.Expect(rooms).To(gomega.BeEmpty()) |
| 236 |
}) |
| 237 |
|
| 238 |
ginkgo.It("should return rooms on success", func() { |
| 239 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 240 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 241 |
StatusCode: http.StatusOK, |
| 242 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"joined_rooms":["!room1:matrix.example.com","!room2:matrix.example.com"]}`))), |
| 243 |
}, nil) |
| 244 |
|
| 245 |
c := &client{ |
| 246 |
apiURL: url.URL{ |
| 247 |
Scheme: "https", |
| 248 |
Host: "matrix.example.com", |
| 249 |
}, |
| 250 |
httpClient: mockHTTPClient, |
| 251 |
logger: &testLogger{}, |
| 252 |
} |
| 253 |
|
| 254 |
rooms, err := c.getJoinedRooms(context.Background()) |
| 255 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 256 |
gomega.Expect(rooms).To(gomega.HaveLen(2)) |
| 257 |
}) |
| 258 |
}) |
| 259 |
|
| 260 |
ginkgo.Describe("joinRoom", func() { |
| 261 |
ginkgo.It("should return error on API failure", func() { |
| 262 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 263 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 264 |
|
| 265 |
c := &client{ |
| 266 |
apiURL: url.URL{ |
| 267 |
Scheme: "https", |
| 268 |
Host: "matrix.example.com", |
| 269 |
}, |
| 270 |
httpClient: mockHTTPClient, |
| 271 |
logger: &testLogger{}, |
| 272 |
} |
| 273 |
|
| 274 |
roomID, err := c.joinRoom(context.Background(), "#test:matrix.example.com") |
| 275 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 276 |
gomega.Expect(roomID).To(gomega.BeEmpty()) |
| 277 |
}) |
| 278 |
|
| 279 |
ginkgo.It("should return room ID on success", func() { |
| 280 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 281 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 282 |
StatusCode: http.StatusOK, |
| 283 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!joinedroom:matrix.example.com"}`))), |
| 284 |
}, nil) |
| 285 |
|
| 286 |
c := &client{ |
| 287 |
apiURL: url.URL{ |
| 288 |
Scheme: "https", |
| 289 |
Host: "matrix.example.com", |
| 290 |
}, |
| 291 |
httpClient: mockHTTPClient, |
| 292 |
logger: &testLogger{}, |
| 293 |
} |
| 294 |
|
| 295 |
roomID, err := c.joinRoom(context.Background(), "#test:matrix.example.com") |
| 296 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 297 |
gomega.Expect(roomID).To(gomega.Equal("!joinedroom:matrix.example.com")) |
| 298 |
}) |
| 299 |
|
| 300 |
ginkgo.It("should send a valid JSON object body, not null", func() { |
| 301 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 302 |
mockHTTPClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { |
| 303 |
body, err := io.ReadAll(req.Body) |
| 304 |
if err != nil { |
| 305 |
return false |
| 306 |
} |
| 307 |
|
| 308 |
return string(body) == "{}" |
| 309 |
})).Return(&http.Response{ |
| 310 |
StatusCode: http.StatusOK, |
| 311 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!joinedroom:matrix.example.com"}`))), |
| 312 |
}, nil) |
| 313 |
|
| 314 |
c := &client{ |
| 315 |
apiURL: url.URL{ |
| 316 |
Scheme: "https", |
| 317 |
Host: "matrix.example.com", |
| 318 |
}, |
| 319 |
httpClient: mockHTTPClient, |
| 320 |
logger: &testLogger{}, |
| 321 |
} |
| 322 |
|
| 323 |
roomID, err := c.joinRoom(context.Background(), "#test:matrix.example.com") |
| 324 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 325 |
gomega.Expect(roomID).To(gomega.Equal("!joinedroom:matrix.example.com")) |
| 326 |
}) |
| 327 |
}) |
| 328 |
|
| 329 |
ginkgo.Describe("logf", func() { |
| 330 |
ginkgo.It("should call logger Printf", func() { |
| 331 |
mockLogger := &testLogger{} |
| 332 |
c := &client{ |
| 333 |
logger: mockLogger, |
| 334 |
} |
| 335 |
|
| 336 |
c.logf("test %s", "message") |
| 337 |
gomega.Expect(mockLogger.formatted).To(gomega.ContainSubstring("test")) |
| 338 |
}) |
| 339 |
}) |
| 340 |
|
| 341 |
ginkgo.Describe("login", func() { |
| 342 |
ginkgo.It("should return error when API call fails", func() { |
| 343 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 344 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 345 |
|
| 346 |
c := &client{ |
| 347 |
apiURL: url.URL{ |
| 348 |
Scheme: "https", |
| 349 |
Host: "matrix.example.com", |
| 350 |
}, |
| 351 |
httpClient: mockHTTPClient, |
| 352 |
logger: &testLogger{}, |
| 353 |
} |
| 354 |
|
| 355 |
err := c.login(context.Background(), "user", "password") |
| 356 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 357 |
}) |
| 358 |
|
| 359 |
ginkgo.It("should return error when no supported login flows", func() { |
| 360 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 361 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 362 |
StatusCode: http.StatusOK, |
| 363 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"flows":[{"type":"m.login.unknown"}]}`))), |
| 364 |
}, nil) |
| 365 |
|
| 366 |
c := &client{ |
| 367 |
apiURL: url.URL{ |
| 368 |
Scheme: "https", |
| 369 |
Host: "matrix.example.com", |
| 370 |
}, |
| 371 |
httpClient: mockHTTPClient, |
| 372 |
logger: &testLogger{}, |
| 373 |
} |
| 374 |
|
| 375 |
err := c.login(context.Background(), "user", "password") |
| 376 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 377 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("login flows")) |
| 378 |
}) |
| 379 |
|
| 380 |
ginkgo.It("should use token login when user is empty", func() { |
| 381 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 382 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 383 |
StatusCode: http.StatusOK, |
| 384 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"flows":[{"type":"m.login.token"}]}`))), |
| 385 |
}, nil).Once() |
| 386 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 387 |
StatusCode: http.StatusOK, |
| 388 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"access_token":"testtoken123","home_server":"matrix.example.com","user_id":"@user:matrix.example.com"}`))), |
| 389 |
}, nil).Once() |
| 390 |
|
| 391 |
c := &client{ |
| 392 |
apiURL: url.URL{ |
| 393 |
Scheme: "https", |
| 394 |
Host: "matrix.example.com", |
| 395 |
}, |
| 396 |
httpClient: mockHTTPClient, |
| 397 |
logger: &testLogger{}, |
| 398 |
} |
| 399 |
|
| 400 |
err := c.login(context.Background(), "", "testtoken123") |
| 401 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 402 |
gomega.Expect(c.accessToken).To(gomega.Equal("testtoken123")) |
| 403 |
}) |
| 404 |
}) |
| 405 |
|
| 406 |
ginkgo.Describe("loginPassword", func() { |
| 407 |
ginkgo.It("should return error when API call fails", func() { |
| 408 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 409 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 410 |
|
| 411 |
c := &client{ |
| 412 |
apiURL: url.URL{ |
| 413 |
Scheme: "https", |
| 414 |
Host: "matrix.example.com", |
| 415 |
}, |
| 416 |
httpClient: mockHTTPClient, |
| 417 |
logger: &testLogger{}, |
| 418 |
} |
| 419 |
|
| 420 |
err := c.loginPassword(context.Background(), "user", "password") |
| 421 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 422 |
}) |
| 423 |
|
| 424 |
ginkgo.It("should successfully login with password", func() { |
| 425 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 426 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 427 |
StatusCode: http.StatusOK, |
| 428 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"access_token":"mytoken123","home_server":"matrix.example.com","user_id":"@user:matrix.example.com","device_id":"TESTDEVICE"}`))), |
| 429 |
}, nil) |
| 430 |
|
| 431 |
c := &client{ |
| 432 |
apiURL: url.URL{ |
| 433 |
Scheme: "https", |
| 434 |
Host: "matrix.example.com", |
| 435 |
}, |
| 436 |
httpClient: mockHTTPClient, |
| 437 |
logger: &testLogger{}, |
| 438 |
} |
| 439 |
|
| 440 |
err := c.loginPassword(context.Background(), "user", "password") |
| 441 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 442 |
gomega.Expect(c.accessToken).To(gomega.Equal("mytoken123")) |
| 443 |
}) |
| 444 |
}) |
| 445 |
|
| 446 |
ginkgo.Describe("loginToken", func() { |
| 447 |
ginkgo.It("should return error when API call fails", func() { |
| 448 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 449 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 450 |
|
| 451 |
c := &client{ |
| 452 |
apiURL: url.URL{ |
| 453 |
Scheme: "https", |
| 454 |
Host: "matrix.example.com", |
| 455 |
}, |
| 456 |
httpClient: mockHTTPClient, |
| 457 |
logger: &testLogger{}, |
| 458 |
} |
| 459 |
|
| 460 |
err := c.loginToken(context.Background(), "token") |
| 461 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 462 |
}) |
| 463 |
|
| 464 |
ginkgo.It("should successfully login with token", func() { |
| 465 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 466 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 467 |
StatusCode: http.StatusOK, |
| 468 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"access_token":"token123","home_server":"matrix.example.com","user_id":"@user:matrix.example.com"}`))), |
| 469 |
}, nil) |
| 470 |
|
| 471 |
c := &client{ |
| 472 |
apiURL: url.URL{ |
| 473 |
Scheme: "https", |
| 474 |
Host: "matrix.example.com", |
| 475 |
}, |
| 476 |
httpClient: mockHTTPClient, |
| 477 |
logger: &testLogger{}, |
| 478 |
} |
| 479 |
|
| 480 |
err := c.loginToken(context.Background(), "token123") |
| 481 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 482 |
gomega.Expect(c.accessToken).To(gomega.Equal("token123")) |
| 483 |
}) |
| 484 |
}) |
| 485 |
|
| 486 |
ginkgo.Describe("sendMessage", func() { |
| 487 |
ginkgo.It("should call sendToJoinedRooms when no rooms specified", func() { |
| 488 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 489 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 490 |
|
| 491 |
c := &client{ |
| 492 |
apiURL: url.URL{ |
| 493 |
Scheme: "https", |
| 494 |
Host: "matrix.example.com", |
| 495 |
}, |
| 496 |
httpClient: mockHTTPClient, |
| 497 |
logger: &testLogger{}, |
| 498 |
} |
| 499 |
|
| 500 |
errs := c.sendMessage(context.Background(), "test message", nil) |
| 501 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 502 |
}) |
| 503 |
|
| 504 |
ginkgo.It("should call sendToExplicitRooms when rooms are specified", func() { |
| 505 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 506 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 507 |
|
| 508 |
c := &client{ |
| 509 |
apiURL: url.URL{ |
| 510 |
Scheme: "https", |
| 511 |
Host: "matrix.example.com", |
| 512 |
}, |
| 513 |
httpClient: mockHTTPClient, |
| 514 |
logger: &testLogger{}, |
| 515 |
} |
| 516 |
|
| 517 |
errs := c.sendMessage(context.Background(), "test message", []string{"#room:matrix.example.com"}) |
| 518 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 519 |
}) |
| 520 |
}) |
| 521 |
|
| 522 |
ginkgo.Describe("sendMessageToRoom", func() { |
| 523 |
ginkgo.It("should return error when API call fails", func() { |
| 524 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 525 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 526 |
|
| 527 |
c := &client{ |
| 528 |
apiURL: url.URL{ |
| 529 |
Scheme: "https", |
| 530 |
Host: "matrix.example.com", |
| 531 |
}, |
| 532 |
httpClient: mockHTTPClient, |
| 533 |
logger: &testLogger{}, |
| 534 |
} |
| 535 |
|
| 536 |
err := c.sendMessageToRoom(context.Background(), "test message", "!room:matrix.example.com") |
| 537 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 538 |
}) |
| 539 |
}) |
| 540 |
|
| 541 |
ginkgo.Describe("sendToExplicitRooms", func() { |
| 542 |
ginkgo.It("should return errors when joining rooms fails", func() { |
| 543 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 544 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 545 |
|
| 546 |
c := &client{ |
| 547 |
apiURL: url.URL{ |
| 548 |
Scheme: "https", |
| 549 |
Host: "matrix.example.com", |
| 550 |
}, |
| 551 |
httpClient: mockHTTPClient, |
| 552 |
logger: &testLogger{}, |
| 553 |
} |
| 554 |
|
| 555 |
errs := c.sendToExplicitRooms(context.Background(), []string{"#room:matrix.example.com"}, "test message") |
| 556 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 557 |
}) |
| 558 |
|
| 559 |
ginkgo.It("should send message successfully when joining succeeds", func() { |
| 560 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 561 |
// First call: joinRoom |
| 562 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 563 |
StatusCode: http.StatusOK, |
| 564 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!roomid:matrix.example.com"}`))), |
| 565 |
}, nil).Once() |
| 566 |
// Second call: sendMessageToRoom |
| 567 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 568 |
StatusCode: http.StatusOK, |
| 569 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid"}`))), |
| 570 |
}, nil).Once() |
| 571 |
|
| 572 |
c := &client{ |
| 573 |
apiURL: url.URL{ |
| 574 |
Scheme: "https", |
| 575 |
Host: "matrix.example.com", |
| 576 |
}, |
| 577 |
httpClient: mockHTTPClient, |
| 578 |
logger: &testLogger{}, |
| 579 |
} |
| 580 |
|
| 581 |
errs := c.sendToExplicitRooms(context.Background(), []string{"#room:matrix.example.com"}, "test message") |
| 582 |
gomega.Expect(errs).To(gomega.BeEmpty()) |
| 583 |
}) |
| 584 |
|
| 585 |
ginkgo.It("should return error when sending message fails after joining", func() { |
| 586 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 587 |
// First call: joinRoom - success |
| 588 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 589 |
StatusCode: http.StatusOK, |
| 590 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!roomid:matrix.example.com"}`))), |
| 591 |
}, nil).Once() |
| 592 |
// Second call: sendMessageToRoom - failure |
| 593 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("send error")).Once() |
| 594 |
|
| 595 |
c := &client{ |
| 596 |
apiURL: url.URL{ |
| 597 |
Scheme: "https", |
| 598 |
Host: "matrix.example.com", |
| 599 |
}, |
| 600 |
httpClient: mockHTTPClient, |
| 601 |
logger: &testLogger{}, |
| 602 |
} |
| 603 |
|
| 604 |
errs := c.sendToExplicitRooms(context.Background(), []string{"#room:matrix.example.com"}, "test message") |
| 605 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 606 |
}) |
| 607 |
|
| 608 |
ginkgo.It("should skip joinRoom when room ID starts with !", func() { |
| 609 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 610 |
// Only one call expected: sendMessageToRoom (no joinRoom) |
| 611 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 612 |
StatusCode: http.StatusOK, |
| 613 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid"}`))), |
| 614 |
}, nil).Once() |
| 615 |
|
| 616 |
c := &client{ |
| 617 |
apiURL: url.URL{ |
| 618 |
Scheme: "https", |
| 619 |
Host: "matrix.example.com", |
| 620 |
}, |
| 621 |
httpClient: mockHTTPClient, |
| 622 |
logger: &testLogger{}, |
| 623 |
} |
| 624 |
|
| 625 |
errs := c.sendToExplicitRooms(context.Background(), []string{"!roomid:matrix.example.com"}, "test message") |
| 626 |
gomega.Expect(errs).To(gomega.BeEmpty()) |
| 627 |
mockHTTPClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 628 |
}) |
| 629 |
|
| 630 |
ginkgo.It("should handle mixed room IDs and aliases", func() { |
| 631 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 632 |
// First call: joinRoom for alias |
| 633 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 634 |
StatusCode: http.StatusOK, |
| 635 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!roomid:matrix.example.com"}`))), |
| 636 |
}, nil).Once() |
| 637 |
// Second call: sendMessageToRoom for alias-resolved room |
| 638 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 639 |
StatusCode: http.StatusOK, |
| 640 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid1"}`))), |
| 641 |
}, nil).Once() |
| 642 |
// Third call: sendMessageToRoom for room ID (no joinRoom needed) |
| 643 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 644 |
StatusCode: http.StatusOK, |
| 645 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid2"}`))), |
| 646 |
}, nil).Once() |
| 647 |
|
| 648 |
c := &client{ |
| 649 |
apiURL: url.URL{ |
| 650 |
Scheme: "https", |
| 651 |
Host: "matrix.example.com", |
| 652 |
}, |
| 653 |
httpClient: mockHTTPClient, |
| 654 |
logger: &testLogger{}, |
| 655 |
} |
| 656 |
|
| 657 |
errs := c.sendToExplicitRooms(context.Background(), []string{"#room:matrix.example.com", "!otherroom:matrix.example.com"}, "test message") |
| 658 |
gomega.Expect(errs).To(gomega.BeEmpty()) |
| 659 |
mockHTTPClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 3) |
| 660 |
}) |
| 661 |
|
| 662 |
ginkgo.It("should return error for empty room value", func() { |
| 663 |
c := &client{ |
| 664 |
apiURL: url.URL{ |
| 665 |
Scheme: "https", |
| 666 |
Host: "matrix.example.com", |
| 667 |
}, |
| 668 |
logger: &testLogger{}, |
| 669 |
} |
| 670 |
|
| 671 |
errs := c.sendToExplicitRooms(context.Background(), []string{""}, "test message") |
| 672 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 673 |
gomega.Expect(errors.Is(errs[0], ErrEmptyRoom)).To(gomega.BeTrue()) |
| 674 |
}) |
| 675 |
|
| 676 |
ginkgo.It("should return error for whitespace-only room value", func() { |
| 677 |
c := &client{ |
| 678 |
apiURL: url.URL{ |
| 679 |
Scheme: "https", |
| 680 |
Host: "matrix.example.com", |
| 681 |
}, |
| 682 |
logger: &testLogger{}, |
| 683 |
} |
| 684 |
|
| 685 |
errs := c.sendToExplicitRooms(context.Background(), []string{" "}, "test message") |
| 686 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 687 |
gomega.Expect(errors.Is(errs[0], ErrEmptyRoom)).To(gomega.BeTrue()) |
| 688 |
}) |
| 689 |
|
| 690 |
ginkgo.It("should trim whitespace from room value before processing", func() { |
| 691 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 692 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 693 |
StatusCode: http.StatusOK, |
| 694 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid"}`))), |
| 695 |
}, nil).Once() |
| 696 |
|
| 697 |
c := &client{ |
| 698 |
apiURL: url.URL{ |
| 699 |
Scheme: "https", |
| 700 |
Host: "matrix.example.com", |
| 701 |
}, |
| 702 |
httpClient: mockHTTPClient, |
| 703 |
logger: &testLogger{}, |
| 704 |
} |
| 705 |
|
| 706 |
errs := c.sendToExplicitRooms(context.Background(), []string{" !roomid:matrix.example.com "}, "test message") |
| 707 |
gomega.Expect(errs).To(gomega.BeEmpty()) |
| 708 |
}) |
| 709 |
}) |
| 710 |
|
| 711 |
ginkgo.Describe("sendToJoinedRooms", func() { |
| 712 |
ginkgo.It("should return error when getJoinedRooms fails", func() { |
| 713 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 714 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("mock error")) |
| 715 |
|
| 716 |
c := &client{ |
| 717 |
apiURL: url.URL{ |
| 718 |
Scheme: "https", |
| 719 |
Host: "matrix.example.com", |
| 720 |
}, |
| 721 |
httpClient: mockHTTPClient, |
| 722 |
logger: &testLogger{}, |
| 723 |
} |
| 724 |
|
| 725 |
errs := c.sendToJoinedRooms(context.Background(), "test message") |
| 726 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 727 |
}) |
| 728 |
|
| 729 |
ginkgo.It("should send message to all joined rooms", func() { |
| 730 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 731 |
// First call: getJoinedRooms |
| 732 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 733 |
StatusCode: http.StatusOK, |
| 734 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"joined_rooms":["!room1:matrix.example.com"]}`))), |
| 735 |
}, nil).Once() |
| 736 |
// Second call: sendMessageToRoom |
| 737 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 738 |
StatusCode: http.StatusOK, |
| 739 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid"}`))), |
| 740 |
}, nil).Once() |
| 741 |
|
| 742 |
c := &client{ |
| 743 |
apiURL: url.URL{ |
| 744 |
Scheme: "https", |
| 745 |
Host: "matrix.example.com", |
| 746 |
}, |
| 747 |
httpClient: mockHTTPClient, |
| 748 |
logger: &testLogger{}, |
| 749 |
} |
| 750 |
|
| 751 |
errs := c.sendToJoinedRooms(context.Background(), "test message") |
| 752 |
gomega.Expect(errs).To(gomega.BeEmpty()) |
| 753 |
}) |
| 754 |
|
| 755 |
ginkgo.It("should return errors when sending to joined rooms fails", func() { |
| 756 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 757 |
// First call: getJoinedRooms - success |
| 758 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 759 |
StatusCode: http.StatusOK, |
| 760 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"joined_rooms":["!room1:matrix.example.com"]}`))), |
| 761 |
}, nil).Once() |
| 762 |
// Second call: sendMessageToRoom - failure |
| 763 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("send error")).Once() |
| 764 |
|
| 765 |
c := &client{ |
| 766 |
apiURL: url.URL{ |
| 767 |
Scheme: "https", |
| 768 |
Host: "matrix.example.com", |
| 769 |
}, |
| 770 |
httpClient: mockHTTPClient, |
| 771 |
logger: &testLogger{}, |
| 772 |
} |
| 773 |
|
| 774 |
errs := c.sendToJoinedRooms(context.Background(), "test message") |
| 775 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 776 |
}) |
| 777 |
}) |
| 778 |
|
| 779 |
ginkgo.Describe("useToken", func() { |
| 780 |
ginkgo.It("should set the access token", func() { |
| 781 |
c := &client{ |
| 782 |
apiURL: url.URL{ |
| 783 |
Scheme: "https", |
| 784 |
Host: "matrix.example.com", |
| 785 |
}, |
| 786 |
logger: &testLogger{}, |
| 787 |
} |
| 788 |
|
| 789 |
c.useToken("my-token") |
| 790 |
gomega.Expect(c.accessToken).To(gomega.Equal("my-token")) |
| 791 |
}) |
| 792 |
}) |
| 793 |
|
| 794 |
ginkgo.Describe("generateTransactionID", func() { |
| 795 |
ginkgo.It("should generate unique transaction IDs", func() { |
| 796 |
txn1 := generateTransactionID() |
| 797 |
txn2 := generateTransactionID() |
| 798 |
gomega.Expect(txn1).ToNot(gomega.Equal(txn2)) |
| 799 |
gomega.Expect(txn1).To(gomega.MatchRegexp(`^[0-9]+-[a-f0-9]+$`)) |
| 800 |
}) |
| 801 |
}) |
| 802 |
|
| 803 |
ginkgo.Describe("generateDeviceID", func() { |
| 804 |
ginkgo.It("should generate a deterministic device ID for the same user and host", func() { |
| 805 |
id1 := generateDeviceID("user", "matrix.example.com") |
| 806 |
id2 := generateDeviceID("user", "matrix.example.com") |
| 807 |
gomega.Expect(id1).To(gomega.Equal(id2)) |
| 808 |
}) |
| 809 |
|
| 810 |
ginkgo.It("should generate different device IDs for different users on the same host", func() { |
| 811 |
id1 := generateDeviceID("user1", "matrix.example.com") |
| 812 |
id2 := generateDeviceID("user2", "matrix.example.com") |
| 813 |
gomega.Expect(id1).ToNot(gomega.Equal(id2)) |
| 814 |
}) |
| 815 |
|
| 816 |
ginkgo.It("should generate different device IDs for the same user on different hosts", func() { |
| 817 |
id1 := generateDeviceID("user", "matrix.example.com") |
| 818 |
id2 := generateDeviceID("user", "matrix2.example.com") |
| 819 |
gomega.Expect(id1).ToNot(gomega.Equal(id2)) |
| 820 |
}) |
| 821 |
|
| 822 |
ginkgo.It("should generate a device ID with exactly 10 characters", func() { |
| 823 |
id := generateDeviceID("user", "matrix.example.com") |
| 824 |
gomega.Expect(id).To(gomega.HaveLen(10)) |
| 825 |
}) |
| 826 |
|
| 827 |
ginkgo.It("should generate a device ID starting with SH prefix", func() { |
| 828 |
id := generateDeviceID("user", "matrix.example.com") |
| 829 |
gomega.Expect(id).To(gomega.MatchRegexp(`^SH.{8}$`)) |
| 830 |
}) |
| 831 |
|
| 832 |
ginkgo.It("should generate a device ID with only valid Opaque Identifier Grammar characters", func() { |
| 833 |
id := generateDeviceID("user", "matrix.example.com") |
| 834 |
// Implementation uses alphanumeric characters only (A-Z, a-z, 0-9) |
| 835 |
gomega.Expect(id).To(gomega.MatchRegexp(`^[A-Za-z0-9]+$`)) |
| 836 |
}) |
| 837 |
|
| 838 |
ginkgo.It("should handle empty user string for token-based login", func() { |
| 839 |
id := generateDeviceID("", "matrix.example.com") |
| 840 |
gomega.Expect(id).To(gomega.HaveLen(10)) |
| 841 |
gomega.Expect(id).To(gomega.MatchRegexp(`^SH.{8}$`)) |
| 842 |
// Verify determinism |
| 843 |
id2 := generateDeviceID("", "matrix.example.com") |
| 844 |
gomega.Expect(id).To(gomega.Equal(id2)) |
| 845 |
}) |
| 846 |
|
| 847 |
ginkgo.It("should handle special characters in user string", func() { |
| 848 |
id := generateDeviceID("@user:matrix.example.com", "matrix.example.com") |
| 849 |
gomega.Expect(id).To(gomega.HaveLen(10)) |
| 850 |
gomega.Expect(id).To(gomega.MatchRegexp(`^[A-Za-z0-9]+$`)) |
| 851 |
}) |
| 852 |
|
| 853 |
ginkgo.It("should normalize host by removing default HTTPS port 443", func() { |
| 854 |
idWithoutPort := generateDeviceID("user", "matrix.example.com") |
| 855 |
idWithPort := generateDeviceID("user", "matrix.example.com:443") |
| 856 |
gomega.Expect(idWithoutPort).To(gomega.Equal(idWithPort)) |
| 857 |
}) |
| 858 |
|
| 859 |
ginkgo.It("should normalize host by removing default HTTP port 80", func() { |
| 860 |
idWithoutPort := generateDeviceID("user", "matrix.example.com") |
| 861 |
idWithPort := generateDeviceID("user", "matrix.example.com:80") |
| 862 |
gomega.Expect(idWithoutPort).To(gomega.Equal(idWithPort)) |
| 863 |
}) |
| 864 |
|
| 865 |
ginkgo.It("should keep non-default ports for uniqueness", func() { |
| 866 |
idWithoutPort := generateDeviceID("user", "matrix.example.com") |
| 867 |
idWithPort := generateDeviceID("user", "matrix.example.com:8008") |
| 868 |
gomega.Expect(idWithoutPort).ToNot(gomega.Equal(idWithPort)) |
| 869 |
}) |
| 870 |
}) |
| 871 |
|
| 872 |
ginkgo.Describe("createMessage", func() { |
| 873 |
ginkgo.It("should return original message when title is empty", func() { |
| 874 |
result := createMessage("Hello world", "") |
| 875 |
gomega.Expect(result).To(gomega.Equal("Hello world")) |
| 876 |
}) |
| 877 |
|
| 878 |
ginkgo.It("should prepend title to message", func() { |
| 879 |
result := createMessage("Hello world", "Alert") |
| 880 |
gomega.Expect(result).To(gomega.Equal("Alert\n\nHello world")) |
| 881 |
}) |
| 882 |
|
| 883 |
ginkgo.It("should trim title whitespace", func() { |
| 884 |
result := createMessage("Hello world", " Alert ") |
| 885 |
gomega.Expect(result).To(gomega.Equal("Alert\n\nHello world")) |
| 886 |
}) |
| 887 |
|
| 888 |
ginkgo.It("should handle empty message with title", func() { |
| 889 |
result := createMessage("", "Alert") |
| 890 |
gomega.Expect(result).To(gomega.Equal("Alert\n\n")) |
| 891 |
}) |
| 892 |
|
| 893 |
ginkgo.It("should handle empty title and empty message", func() { |
| 894 |
result := createMessage("", "") |
| 895 |
gomega.Expect(result).To(gomega.Equal("")) |
| 896 |
}) |
| 897 |
|
| 898 |
ginkgo.It("should handle title with only whitespace", func() { |
| 899 |
result := createMessage("Hello world", " ") |
| 900 |
gomega.Expect(result).To(gomega.Equal("Hello world")) |
| 901 |
}) |
| 902 |
|
| 903 |
ginkgo.It("should preserve message with existing newlines", func() { |
| 904 |
result := createMessage("Line1\nLine2\nLine3", "Alert") |
| 905 |
gomega.Expect(result).To(gomega.Equal("Alert\n\nLine1\nLine2\nLine3")) |
| 906 |
}) |
| 907 |
|
| 908 |
ginkgo.It("should handle message with special characters", func() { |
| 909 |
result := createMessage("Message with <html> & \"quotes\"", "Alert") |
| 910 |
gomega.Expect(result).To(gomega.Equal("Alert\n\nMessage with <html> & \"quotes\"")) |
| 911 |
}) |
| 912 |
}) |
| 913 |
|
| 914 |
// Tests for setAuthorizationHeader |
| 915 |
ginkgo.Describe("setAuthorizationHeader", func() { |
| 916 |
ginkgo.It("should set Authorization header when accessToken is non-empty", func() { |
| 917 |
c := &client{ |
| 918 |
accessToken: "test-token-123", |
| 919 |
} |
| 920 |
req, _ := http.NewRequest(http.MethodGet, "https://matrix.example.com/_matrix", http.NoBody) |
| 921 |
c.setAuthorizationHeader(req) |
| 922 |
gomega.Expect(req.Header.Get("Authorization")).To(gomega.Equal("Bearer test-token-123")) |
| 923 |
}) |
| 924 |
|
| 925 |
ginkgo.It("should set header with correct Bearer prefix format", func() { |
| 926 |
c := &client{ |
| 927 |
accessToken: "mytoken", |
| 928 |
} |
| 929 |
req, _ := http.NewRequest(http.MethodGet, "https://matrix.example.com/_matrix", http.NoBody) |
| 930 |
c.setAuthorizationHeader(req) |
| 931 |
headerValue := req.Header.Get("Authorization") |
| 932 |
gomega.Expect(headerValue).To(gomega.MatchRegexp("^Bearer .+$")) |
| 933 |
gomega.Expect(headerValue).To(gomega.Equal("Bearer mytoken")) |
| 934 |
}) |
| 935 |
|
| 936 |
ginkgo.It("should NOT set header when accessToken is empty", func() { |
| 937 |
c := &client{ |
| 938 |
accessToken: "", |
| 939 |
} |
| 940 |
req, _ := http.NewRequest(http.MethodGet, "https://matrix.example.com/_matrix", http.NoBody) |
| 941 |
c.setAuthorizationHeader(req) |
| 942 |
gomega.Expect(req.Header.Get("Authorization")).To(gomega.BeEmpty()) |
| 943 |
}) |
| 944 |
|
| 945 |
ginkgo.It("should overwrite existing Authorization header", func() { |
| 946 |
c := &client{ |
| 947 |
accessToken: "new-token", |
| 948 |
} |
| 949 |
req, _ := http.NewRequest(http.MethodGet, "https://matrix.example.com/_matrix", http.NoBody) |
| 950 |
req.Header.Set("Authorization", "Existing-Token") |
| 951 |
c.setAuthorizationHeader(req) |
| 952 |
gomega.Expect(req.Header.Get("Authorization")).To(gomega.Equal("Bearer new-token")) |
| 953 |
}) |
| 954 |
}) |
| 955 |
|
| 956 |
// Tests for apiDo |
| 957 |
ginkgo.Describe("apiDo", func() { |
| 958 |
ginkgo.It("should return error on JSON marshal error with request body", func() { |
| 959 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 960 |
|
| 961 |
c := &client{ |
| 962 |
apiURL: url.URL{ |
| 963 |
Scheme: "https", |
| 964 |
Host: "matrix.example.com", |
| 965 |
}, |
| 966 |
httpClient: mockHTTPClient, |
| 967 |
logger: &testLogger{}, |
| 968 |
} |
| 969 |
|
| 970 |
// Use a channel that cannot be marshaled to JSON |
| 971 |
ch := make(chan string) |
| 972 |
resp := &apiResLogin{} |
| 973 |
err := c.apiDo(context.Background(), "POST", "/_matrix/client/v3/login", ch, resp) |
| 974 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 975 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("marshaling")) |
| 976 |
}) |
| 977 |
|
| 978 |
ginkgo.It("should return error on HTTP request creation failure", func() { |
| 979 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 980 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("request failed")).Maybe() |
| 981 |
|
| 982 |
c := &client{ |
| 983 |
apiURL: url.URL{ |
| 984 |
Scheme: "https", |
| 985 |
Host: "matrix.example.com", |
| 986 |
}, |
| 987 |
httpClient: mockHTTPClient, |
| 988 |
logger: &testLogger{}, |
| 989 |
} |
| 990 |
|
| 991 |
// Use an invalid URL to trigger request creation failure - empty scheme |
| 992 |
c.apiURL = url.URL{ |
| 993 |
Scheme: "", |
| 994 |
Host: "", |
| 995 |
} |
| 996 |
|
| 997 |
resp := &apiResLogin{} |
| 998 |
err := c.apiDo(context.Background(), "POST", "/_matrix/client/v3/login", nil, resp) |
| 999 |
// The actual behavior may vary - request creation might succeed with invalid URL |
| 1000 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 1001 |
}) |
| 1002 |
|
| 1003 |
ginkgo.It("should handle response body close error", func() { |
| 1004 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1005 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1006 |
StatusCode: http.StatusOK, |
| 1007 |
Body: &errorBodyClose{}, |
| 1008 |
}, nil) |
| 1009 |
|
| 1010 |
c := &client{ |
| 1011 |
apiURL: url.URL{ |
| 1012 |
Scheme: "https", |
| 1013 |
Host: "matrix.example.com", |
| 1014 |
}, |
| 1015 |
httpClient: mockHTTPClient, |
| 1016 |
logger: &testLogger{}, |
| 1017 |
} |
| 1018 |
|
| 1019 |
resp := &apiResLogin{} |
| 1020 |
err := c.apiDo(context.Background(), "POST", "/_matrix/client/v3/login", nil, resp) |
| 1021 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 1022 |
}) |
| 1023 |
|
| 1024 |
ginkgo.It("should set Content-Type header to application/json", func() { |
| 1025 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1026 |
|
| 1027 |
var capturedReq *http.Request |
| 1028 |
|
| 1029 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1030 |
StatusCode: http.StatusOK, |
| 1031 |
Body: io.NopCloser(bytes.NewReader([]byte(`{}`))), |
| 1032 |
}, nil).Run(func(args mock.Arguments) { |
| 1033 |
capturedReq = args.Get(0).(*http.Request) |
| 1034 |
}) |
| 1035 |
|
| 1036 |
c := &client{ |
| 1037 |
apiURL: url.URL{ |
| 1038 |
Scheme: "https", |
| 1039 |
Host: "matrix.example.com", |
| 1040 |
}, |
| 1041 |
httpClient: mockHTTPClient, |
| 1042 |
logger: &testLogger{}, |
| 1043 |
} |
| 1044 |
|
| 1045 |
resp := &apiResLogin{} |
| 1046 |
_ = c.apiDo(context.Background(), "POST", "/_matrix/client/v3/login", nil, resp) |
| 1047 |
|
| 1048 |
gomega.Expect(capturedReq.Header.Get("Content-Type")).To(gomega.Equal("application/json")) |
| 1049 |
}) |
| 1050 |
|
| 1051 |
ginkgo.It("should use correct HTTP method", func() { |
| 1052 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1053 |
|
| 1054 |
var capturedReq *http.Request |
| 1055 |
|
| 1056 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1057 |
StatusCode: http.StatusOK, |
| 1058 |
Body: io.NopCloser(bytes.NewReader([]byte(`{}`))), |
| 1059 |
}, nil).Run(func(args mock.Arguments) { |
| 1060 |
capturedReq = args.Get(0).(*http.Request) |
| 1061 |
}) |
| 1062 |
|
| 1063 |
c := &client{ |
| 1064 |
apiURL: url.URL{ |
| 1065 |
Scheme: "https", |
| 1066 |
Host: "matrix.example.com", |
| 1067 |
}, |
| 1068 |
httpClient: mockHTTPClient, |
| 1069 |
logger: &testLogger{}, |
| 1070 |
} |
| 1071 |
|
| 1072 |
resp := &apiResLogin{} |
| 1073 |
_ = c.apiDo(context.Background(), "PUT", "/_matrix/client/v3/rooms/!room/send/m.room.message/txn", nil, resp) |
| 1074 |
|
| 1075 |
gomega.Expect(capturedReq.Method).To(gomega.Equal("PUT")) |
| 1076 |
}) |
| 1077 |
}) |
| 1078 |
|
| 1079 |
// Tests for apiPut |
| 1080 |
ginkgo.Describe("apiPut", func() { |
| 1081 |
ginkgo.It("should successfully make PUT request", func() { |
| 1082 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1083 |
|
| 1084 |
var capturedReq *http.Request |
| 1085 |
|
| 1086 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1087 |
StatusCode: http.StatusOK, |
| 1088 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$eventid"}`))), |
| 1089 |
}, nil).Run(func(args mock.Arguments) { |
| 1090 |
capturedReq = args.Get(0).(*http.Request) |
| 1091 |
}) |
| 1092 |
|
| 1093 |
c := &client{ |
| 1094 |
apiURL: url.URL{ |
| 1095 |
Scheme: "https", |
| 1096 |
Host: "matrix.example.com", |
| 1097 |
}, |
| 1098 |
httpClient: mockHTTPClient, |
| 1099 |
logger: &testLogger{}, |
| 1100 |
} |
| 1101 |
|
| 1102 |
resp := &apiResEvent{} |
| 1103 |
err := c.apiPut(context.Background(), "/_matrix/client/v3/rooms/!room/send/m.room.message/txn1", |
| 1104 |
apiReqSend{MsgType: msgTypeText, Body: "test message"}, resp) |
| 1105 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1106 |
gomega.Expect(capturedReq.Method).To(gomega.Equal("PUT")) |
| 1107 |
}) |
| 1108 |
|
| 1109 |
ginkgo.It("should return error on PUT request failure", func() { |
| 1110 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1111 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("PUT request failed")) |
| 1112 |
|
| 1113 |
c := &client{ |
| 1114 |
apiURL: url.URL{ |
| 1115 |
Scheme: "https", |
| 1116 |
Host: "matrix.example.com", |
| 1117 |
}, |
| 1118 |
httpClient: mockHTTPClient, |
| 1119 |
logger: &testLogger{}, |
| 1120 |
} |
| 1121 |
|
| 1122 |
resp := &apiResEvent{} |
| 1123 |
err := c.apiPut(context.Background(), "/_matrix/client/v3/rooms/!room/send/m.room.message/txn1", |
| 1124 |
apiReqSend{MsgType: msgTypeText, Body: "test message"}, resp) |
| 1125 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 1126 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("executing PUT")) |
| 1127 |
}) |
| 1128 |
}) |
| 1129 |
|
| 1130 |
// Tests for context cancellation handling |
| 1131 |
ginkgo.Describe("Context Cancellation Handling", func() { |
| 1132 |
ginkgo.It("should return error when context is canceled before API call", func() { |
| 1133 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1134 |
// The HTTP client may or may not be called depending on timing |
| 1135 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, context.Canceled).Maybe() |
| 1136 |
|
| 1137 |
c := &client{ |
| 1138 |
apiURL: url.URL{ |
| 1139 |
Scheme: "https", |
| 1140 |
Host: "matrix.example.com", |
| 1141 |
}, |
| 1142 |
httpClient: mockHTTPClient, |
| 1143 |
logger: &testLogger{}, |
| 1144 |
} |
| 1145 |
|
| 1146 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1147 |
cancel() // Cancel immediately |
| 1148 |
|
| 1149 |
resp := &apiResLogin{} |
| 1150 |
err := c.apiDo(ctx, "POST", "/_matrix/client/v3/login", nil, resp) |
| 1151 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 1152 |
}) |
| 1153 |
|
| 1154 |
ginkgo.It("should handle context cancellation during API call via HTTP client", func() { |
| 1155 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1156 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, context.Canceled) |
| 1157 |
|
| 1158 |
c := &client{ |
| 1159 |
apiURL: url.URL{ |
| 1160 |
Scheme: "https", |
| 1161 |
Host: "matrix.example.com", |
| 1162 |
}, |
| 1163 |
httpClient: mockHTTPClient, |
| 1164 |
logger: &testLogger{}, |
| 1165 |
} |
| 1166 |
|
| 1167 |
resp := &apiResLogin{} |
| 1168 |
err := c.apiDo(context.Background(), "POST", "/_matrix/client/v3/login", nil, resp) |
| 1169 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 1170 |
}) |
| 1171 |
|
| 1172 |
ginkgo.It("should handle context timeout", func() { |
| 1173 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1174 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, context.DeadlineExceeded) |
| 1175 |
|
| 1176 |
c := &client{ |
| 1177 |
apiURL: url.URL{ |
| 1178 |
Scheme: "https", |
| 1179 |
Host: "matrix.example.com", |
| 1180 |
}, |
| 1181 |
httpClient: mockHTTPClient, |
| 1182 |
logger: &testLogger{}, |
| 1183 |
} |
| 1184 |
|
| 1185 |
resp := &apiResLogin{} |
| 1186 |
err := c.apiDo(context.Background(), "POST", "/_matrix/client/v3/login", nil, resp) |
| 1187 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 1188 |
}) |
| 1189 |
}) |
| 1190 |
|
| 1191 |
// Tests for auth method gaps |
| 1192 |
ginkgo.Describe("Auth Method Testing Gaps", func() { |
| 1193 |
ginkgo.It("should handle user with @domain format in password login", func() { |
| 1194 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1195 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1196 |
StatusCode: http.StatusOK, |
| 1197 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"access_token":"token123","home_server":"matrix.example.com","user_id":"@user:matrix.example.com"}`))), |
| 1198 |
}, nil) |
| 1199 |
|
| 1200 |
c := &client{ |
| 1201 |
apiURL: url.URL{ |
| 1202 |
Scheme: "https", |
| 1203 |
Host: "matrix.example.com", |
| 1204 |
}, |
| 1205 |
httpClient: mockHTTPClient, |
| 1206 |
logger: &testLogger{}, |
| 1207 |
} |
| 1208 |
|
| 1209 |
// Test with @domain format user |
| 1210 |
err := c.loginPassword(context.Background(), "@user:matrix.example.com", "password") |
| 1211 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1212 |
gomega.Expect(c.accessToken).To(gomega.Equal("token123")) |
| 1213 |
}) |
| 1214 |
}) |
| 1215 |
|
| 1216 |
// Tests for room handling edge cases |
| 1217 |
ginkgo.Describe("Room Handling Edge Cases", func() { |
| 1218 |
ginkgo.It("should return empty slice when getJoinedRooms returns empty list", func() { |
| 1219 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1220 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1221 |
StatusCode: http.StatusOK, |
| 1222 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"joined_rooms":[]}`))), |
| 1223 |
}, nil) |
| 1224 |
|
| 1225 |
c := &client{ |
| 1226 |
apiURL: url.URL{ |
| 1227 |
Scheme: "https", |
| 1228 |
Host: "matrix.example.com", |
| 1229 |
}, |
| 1230 |
httpClient: mockHTTPClient, |
| 1231 |
logger: &testLogger{}, |
| 1232 |
} |
| 1233 |
|
| 1234 |
rooms, err := c.getJoinedRooms(context.Background()) |
| 1235 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1236 |
gomega.Expect(rooms).To(gomega.BeEmpty()) |
| 1237 |
}) |
| 1238 |
|
| 1239 |
ginkgo.It("should handle multiple rooms with partial failures", func() { |
| 1240 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1241 |
// First call: getJoinedRooms - returns multiple rooms |
| 1242 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1243 |
StatusCode: http.StatusOK, |
| 1244 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"joined_rooms":["!room1:matrix.example.com","!room2:matrix.example.com"]}`))), |
| 1245 |
}, nil).Once() |
| 1246 |
// Second call: sendMessageToRoom - success for room1 |
| 1247 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1248 |
StatusCode: http.StatusOK, |
| 1249 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$event1"}`))), |
| 1250 |
}, nil).Once() |
| 1251 |
// Third call: sendMessageToRoom - failure for room2 |
| 1252 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("send error")).Once() |
| 1253 |
|
| 1254 |
c := &client{ |
| 1255 |
apiURL: url.URL{ |
| 1256 |
Scheme: "https", |
| 1257 |
Host: "matrix.example.com", |
| 1258 |
}, |
| 1259 |
httpClient: mockHTTPClient, |
| 1260 |
logger: &testLogger{}, |
| 1261 |
} |
| 1262 |
|
| 1263 |
errs := c.sendToJoinedRooms(context.Background(), "test message") |
| 1264 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 1265 |
}) |
| 1266 |
|
| 1267 |
ginkgo.It("should handle sendToExplicitRooms with multiple rooms and partial failures", func() { |
| 1268 |
mockHTTPClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 1269 |
// First room: join succeeds, send succeeds |
| 1270 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1271 |
StatusCode: http.StatusOK, |
| 1272 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!room1:matrix.example.com"}`))), |
| 1273 |
}, nil).Once() |
| 1274 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1275 |
StatusCode: http.StatusOK, |
| 1276 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"event_id":"$event1"}`))), |
| 1277 |
}, nil).Once() |
| 1278 |
// Second room: join succeeds, send fails |
| 1279 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 1280 |
StatusCode: http.StatusOK, |
| 1281 |
Body: io.NopCloser(bytes.NewReader([]byte(`{"room_id":"!room2:matrix.example.com"}`))), |
| 1282 |
}, nil).Once() |
| 1283 |
mockHTTPClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("send error")).Once() |
| 1284 |
|
| 1285 |
c := &client{ |
| 1286 |
apiURL: url.URL{ |
| 1287 |
Scheme: "https", |
| 1288 |
Host: "matrix.example.com", |
| 1289 |
}, |
| 1290 |
httpClient: mockHTTPClient, |
| 1291 |
logger: &testLogger{}, |
| 1292 |
} |
| 1293 |
|
| 1294 |
errs := c.sendToExplicitRooms(context.Background(), |
| 1295 |
[]string{"#room1:matrix.example.com", "#room2:matrix.example.com"}, "test message") |
| 1296 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 1297 |
}) |
| 1298 |
}) |
| 1299 |
}) |
| 1300 |
|
| 1301 |
func (m *testLogger) Print(args ...any) { |
| 1302 |
// no-op |
| 1303 |
} |
| 1304 |
|
| 1305 |
func (m *testLogger) Printf(format string, args ...any) { |
| 1306 |
m.formatted = format |
| 1307 |
} |
| 1308 |
|
| 1309 |
func (m *testLogger) Println(args ...any) { |
| 1310 |
// no-op |
| 1311 |
} |
| 1312 |
|
| 1313 |
func (e *errorReadCloser) Close() error { |
| 1314 |
return nil |
| 1315 |
} |
| 1316 |
|
| 1317 |
func (e *errorReadCloser) Read(p []byte) (int, error) { |
| 1318 |
return 0, errors.New("read error") |
| 1319 |
} |
| 1320 |
|
| 1321 |
func (e *errorBodyClose) Close() error { |
| 1322 |
return errors.New("close error") |
| 1323 |
} |
| 1324 |
|
| 1325 |
func (e *errorBodyClose) Read(p []byte) (int, error) { |
| 1326 |
return 0, io.EOF |
| 1327 |
} |