Spaces:
Paused
Paused
File size: 23,399 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | package tests
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"math/big"
"mime/multipart"
"net/http"
"net/url"
"strings"
"testing"
"time"
httpsms "github.com/NdoleStudio/httpsms-go"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/wiremock/go-wiremock"
wmJournal "github.com/wiremock/go-wiremock/journal"
)
const (
apiBaseURL = "http://localhost:8000"
wiremockURL = "http://localhost:8080"
wiremockWebhookURL = "http://wiremock.local:8080" // reachable from API container, passes URL validation (needs a dot)
adapterControlURL = "http://localhost:9092"
userAPIKey = "test-user-api-key"
systemAPIKey = "system-user-api-key"
)
type testPhone struct {
PhoneNumber string
PhoneAPIKey string
FcmToken string
}
type adapterTestPhone struct {
testPhone
PhoneID string
GatewayID string
}
type notificationRecord struct {
GatewayID string `json:"gateway_id"`
Data map[string]string `json:"data"`
MessageID string `json:"message_id,omitempty"`
Kind string `json:"kind"`
Processed bool `json:"processed"`
Error string `json:"error,omitempty"`
Authorization string `json:"authorization,omitempty"`
}
func newAPIClient() *httpsms.Client {
return httpsms.New(
httpsms.WithBaseURL(apiBaseURL),
httpsms.WithAPIKey(userAPIKey),
)
}
func newPhoneClient(phoneAPIKey string) *httpsms.Client {
return httpsms.New(
httpsms.WithBaseURL(apiBaseURL),
httpsms.WithAPIKey(phoneAPIKey),
)
}
func newWireMockClient() *wiremock.Client {
return wiremock.NewClient(wiremockURL)
}
func randomPhoneNumber() string {
n, err := rand.Int(rand.Reader, big.NewInt(10000))
if err != nil {
return "+18005550000"
}
return fmt.Sprintf("+1800555%04d", n.Int64())
}
func randomEncryptionKey() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return uuid.New().String()
}
return fmt.Sprintf("%x", b)
}
func setupPhone(ctx context.Context, t *testing.T, messagesPerMinute uint) testPhone {
t.Helper()
phoneNumber := randomPhoneNumber()
fcmToken := "fcm-" + uuid.New().String()
client := newAPIClient()
// Create the phone API key first so that a few seconds pass (during phone upsert)
// before we use it, giving the cache time to clear.
apiKeyResp, resp, err := client.PhoneAPIKeys.Store(ctx, &httpsms.PhoneAPIKeyStoreParams{
Name: "test-key-" + uuid.New().String(),
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.HTTPResponse.StatusCode, "phone api key store failed")
phoneAPIKeyValue := apiKeyResp.Data.APIKey
require.NotEmpty(t, phoneAPIKeyValue)
_, resp, err = client.Phones.Upsert(ctx, &httpsms.PhoneUpsertParams{
PhoneNumber: phoneNumber,
FcmToken: fcmToken,
MessagesPerMinute: messagesPerMinute,
MaxSendAttempts: 2,
MessageExpirationSeconds: 600,
SIM: "SIM1",
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.HTTPResponse.StatusCode, "phone upsert failed")
phoneClient := newPhoneClient(phoneAPIKeyValue)
_, resp, err = phoneClient.Phones.UpsertFCMToken(ctx, &httpsms.PhoneFCMTokenParams{
PhoneNumber: phoneNumber,
FcmToken: fcmToken,
SIM: "SIM1",
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.HTTPResponse.StatusCode, "fcm token bind failed")
waitForPhoneAuthorization(ctx, t, phoneAPIKeyValue, phoneNumber, 20*time.Second)
return testPhone{
PhoneNumber: phoneNumber,
PhoneAPIKey: phoneAPIKeyValue,
FcmToken: fcmToken,
}
}
func setupAdapterPhone(ctx context.Context, t *testing.T, messagesPerMinute uint) adapterTestPhone {
t.Helper()
gatewayID := uuid.NewString()
phoneNumber := randomPhoneNumber()
client := newAPIClient()
apiKeyResponse, response, err := client.PhoneAPIKeys.Store(ctx, &httpsms.PhoneAPIKeyStoreParams{
Name: "adapter-test-key-" + uuid.NewString(),
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode, "phone api key store failed")
phoneAPIKey := apiKeyResponse.Data.APIKey
require.NotEmpty(t, phoneAPIKey)
// Upsert the phone first so its ID is known before the adapter emulator is registered:
// the API signs notification requests with a JWT keyed by the phone ID, and the emulator
// needs that ID up front to validate the JWT on every notification it receives.
callbackURL := fmt.Sprintf("https://adapter-emulator:9091/notifications/%s", gatewayID)
phoneResponse, response, err := client.Phones.Upsert(ctx, &httpsms.PhoneUpsertParams{
PhoneNumber: phoneNumber,
FcmToken: callbackURL,
MessagesPerMinute: messagesPerMinute,
MaxSendAttempts: 2,
MessageExpirationSeconds: 600,
SIM: "SIM1",
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode, "phone upsert failed")
require.NotEmpty(t, phoneResponse.Data.ID)
registrationBody, err := json.Marshal(map[string]any{
"phone_number": phoneNumber,
"phone_api_key": phoneAPIKey,
"phone_id": phoneResponse.Data.ID,
})
require.NoError(t, err)
registrationRequest, err := http.NewRequestWithContext(
ctx,
http.MethodPut,
fmt.Sprintf("%s/test/gateways/%s", adapterControlURL, gatewayID),
bytes.NewReader(registrationBody),
)
require.NoError(t, err)
registrationRequest.Header.Set("Content-Type", "application/json")
registrationResponse, err := http.DefaultClient.Do(registrationRequest)
require.NoError(t, err)
registrationResponseBody, err := io.ReadAll(registrationResponse.Body)
registrationResponse.Body.Close()
require.NoError(t, err)
require.Equal(
t,
http.StatusNoContent,
registrationResponse.StatusCode,
"adapter gateway registration failed: %s",
string(registrationResponseBody),
)
phoneClient := newPhoneClient(phoneAPIKey)
_, response, err = phoneClient.Phones.UpsertFCMToken(ctx, &httpsms.PhoneFCMTokenParams{
PhoneNumber: phoneNumber,
FcmToken: callbackURL,
SIM: "SIM1",
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode, "adapter callback bind failed")
waitForPhoneAuthorization(ctx, t, phoneAPIKey, phoneNumber, 20*time.Second)
return adapterTestPhone{
testPhone: testPhone{
PhoneNumber: phoneNumber,
PhoneAPIKey: phoneAPIKey,
FcmToken: callbackURL,
},
PhoneID: phoneResponse.Data.ID,
GatewayID: gatewayID,
}
}
func dispatchInternalEvent(ctx context.Context, t *testing.T, event map[string]any) {
t.Helper()
body, err := json.Marshal(event)
require.NoError(t, err)
request, err := http.NewRequestWithContext(ctx, http.MethodPost, apiBaseURL+"/v1/events", bytes.NewReader(body))
require.NoError(t, err)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("x-api-key", systemAPIKey)
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)
defer response.Body.Close()
responseBody, err := io.ReadAll(response.Body)
require.NoError(t, err)
require.Equal(t, http.StatusNoContent, response.StatusCode, "event dispatch failed: %s", string(responseBody))
}
func waitForAdapterMessageRecords(
t *testing.T,
gatewayID string,
messageID string,
timeout time.Duration,
) []notificationRecord {
t.Helper()
deadline := time.Now().Add(timeout)
var records []notificationRecord
var lastErr error
for time.Now().Before(deadline) {
records, lastErr = fetchAdapterNotificationRecords(gatewayID, messageID)
if lastErr == nil && len(records) > 0 && adapterRecordsProcessed(records) {
return records
}
time.Sleep(500 * time.Millisecond)
}
require.NoError(t, lastErr)
require.NotEmpty(t, records, "adapter message record for %s was not available within %v", messageID, timeout)
require.True(t, adapterRecordsProcessed(records), "adapter message records were not processed: %#v", records)
return records
}
func waitForAdapterHeartbeatRecord(
t *testing.T,
gatewayID string,
timeout time.Duration,
) notificationRecord {
t.Helper()
deadline := time.Now().Add(timeout)
var lastErr error
for time.Now().Before(deadline) {
records, err := fetchAdapterNotificationRecords(gatewayID, "")
lastErr = err
if err == nil {
for _, record := range records {
if record.Kind == "heartbeat" && record.Processed {
return record
}
}
}
time.Sleep(500 * time.Millisecond)
}
require.NoError(t, lastErr)
t.Fatalf("processed adapter heartbeat record was not available within %v", timeout)
return notificationRecord{}
}
func triggerAdapterIncoming(
ctx context.Context,
t *testing.T,
phone adapterTestPhone,
contact string,
content string,
) string {
t.Helper()
body, err := json.Marshal(map[string]any{
"contact": contact,
"content": content,
"encrypted": false,
})
require.NoError(t, err)
request, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/test/gateways/%s/incoming", adapterControlURL, phone.GatewayID),
bytes.NewReader(body),
)
require.NoError(t, err)
request.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)
defer response.Body.Close()
responseBody, err := io.ReadAll(response.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode, "adapter incoming trigger failed: %s", string(responseBody))
var result struct {
Data struct {
ID string `json:"id"`
} `json:"data"`
}
require.NoError(t, json.Unmarshal(responseBody, &result))
require.NotEmpty(t, result.Data.ID)
return result.Data.ID
}
func fetchAdapterNotificationRecords(gatewayID string, messageID string) ([]notificationRecord, error) {
endpoint := fmt.Sprintf("%s/test/gateways/%s/notifications", adapterControlURL, gatewayID)
if messageID != "" {
endpoint += "?message_id=" + url.QueryEscape(messageID)
}
response, err := (&http.Client{Timeout: 5 * time.Second}).Get(endpoint)
if err != nil {
return nil, fmt.Errorf("fetch adapter notification records: %w", err)
}
defer response.Body.Close()
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("read adapter notification records: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetch adapter notification records: status %d: %s", response.StatusCode, string(responseBody))
}
var result struct {
Data []notificationRecord `json:"data"`
}
if err := json.Unmarshal(responseBody, &result); err != nil {
return nil, fmt.Errorf("decode adapter notification records: %w", err)
}
return result.Data, nil
}
func adapterRecordsProcessed(records []notificationRecord) bool {
for _, record := range records {
if !record.Processed {
return false
}
}
return true
}
func waitForPhoneAuthorization(
ctx context.Context,
t *testing.T,
phoneAPIKey string,
phoneNumber string,
timeout time.Duration,
) {
t.Helper()
body, err := json.Marshal(map[string]interface{}{
"phone_numbers": []string{phoneNumber},
"charging": true,
})
require.NoError(t, err)
var responseBody []byte
var statusCode int
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
request, requestErr := http.NewRequestWithContext(
ctx,
http.MethodPost,
apiBaseURL+"/v1/heartbeats",
bytes.NewReader(body),
)
require.NoError(t, requestErr)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("x-api-key", phoneAPIKey)
response, requestErr := http.DefaultClient.Do(request)
require.NoError(t, requestErr)
responseBody, requestErr = io.ReadAll(response.Body)
response.Body.Close()
require.NoError(t, requestErr)
statusCode = response.StatusCode
if statusCode == http.StatusCreated {
return
}
if statusCode != http.StatusUnauthorized {
require.Equal(t, http.StatusCreated, statusCode, "phone authorization check failed: %s", string(responseBody))
}
time.Sleep(500 * time.Millisecond)
}
require.Equal(t, http.StatusCreated, statusCode, "phone authorization was not ready within %v: %s", timeout, string(responseBody))
}
func setupWebhook(ctx context.Context, t *testing.T, phoneNumber string, events []string) (signingKey string, webhookPath string) {
t.Helper()
signingKey = randomEncryptionKey()
webhookPath = "/webhooks/" + uuid.New().String()
webhookURL := wiremockWebhookURL + webhookPath
client := newAPIClient()
_, resp, err := client.Webhooks.Store(ctx, &httpsms.WebhookStoreParams{
SigningKey: signingKey,
URL: webhookURL,
PhoneNumbers: []string{phoneNumber},
Events: events,
})
require.NoError(t, err)
require.Equal(t, http.StatusCreated, resp.HTTPResponse.StatusCode, "webhook store failed")
return signingKey, webhookPath
}
func fireEvent(ctx context.Context, t *testing.T, phoneAPIKey string, messageID string, eventName string) {
t.Helper()
url := fmt.Sprintf("%s/v1/messages/%s/events", apiBaseURL, messageID)
payload := map[string]interface{}{
"event_name": eventName,
"timestamp": time.Now().UTC().Format(time.RFC3339),
}
body, err := json.Marshal(payload)
require.NoError(t, err)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", phoneAPIKey)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "fire event %s failed: %s", eventName, string(respBody))
}
func pollMessageStatus(ctx context.Context, t *testing.T, messageID string, targetStatus string, timeout time.Duration) httpsms.Message {
t.Helper()
deadline := time.Now().Add(timeout)
lastStatus := "not observed"
for time.Now().Before(deadline) {
url := fmt.Sprintf("%s/v1/messages/%s", apiBaseURL, messageID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err == nil {
req.Header.Set("x-api-key", userAPIKey)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err == nil {
body, readErr := io.ReadAll(resp.Body)
resp.Body.Close()
if readErr == nil && resp.StatusCode == http.StatusOK {
var result httpsms.MessageResponse
if json.Unmarshal(body, &result) == nil {
lastStatus = result.Data.Status
if result.Data.Status == targetStatus {
return result.Data
}
}
}
}
}
time.Sleep(500 * time.Millisecond)
}
t.Fatalf("message %s did not reach status %q within %v (last observed status: %q)", messageID, targetStatus, timeout, lastStatus)
return httpsms.Message{}
}
func fetchOutstandingMessage(ctx context.Context, t *testing.T, phoneAPIKey string, messageID string) map[string]interface{} {
t.Helper()
url := fmt.Sprintf("%s/v1/messages/outstanding?message_id=%s", apiBaseURL, messageID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoError(t, err)
req.Header.Set("x-api-key", phoneAPIKey)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "outstanding: %s", string(body))
var result map[string]interface{}
require.NoError(t, json.Unmarshal(body, &result))
data, ok := result["data"].(map[string]interface{})
require.True(t, ok, "no data in outstanding response")
return data
}
func findFCMRequests(t *testing.T, messageID string) []wmJournal.GetRequestResponse {
t.Helper()
wm := newWireMockClient()
allReqs, err := wm.GetAllRequests()
require.NoError(t, err)
var matched []wmJournal.GetRequestResponse
for _, req := range allReqs.Requests {
if strings.Contains(req.Request.URL, "/messages:send") || strings.Contains(req.Request.AbsoluteURL, "/messages:send") {
if strings.Contains(req.Request.Body, messageID) {
matched = append(matched, req)
}
}
}
return matched
}
func findWebhookRequests(t *testing.T, webhookPath string) []wmJournal.GetRequestResponse {
t.Helper()
wm := newWireMockClient()
allReqs, err := wm.GetAllRequests()
require.NoError(t, err)
var matched []wmJournal.GetRequestResponse
for _, req := range allReqs.Requests {
if strings.Contains(req.Request.URL, webhookPath) || strings.Contains(req.Request.AbsoluteURL, webhookPath) {
matched = append(matched, req)
}
}
return matched
}
func assertWebhookJWT(t *testing.T, request wmJournal.Request, signingKey string) {
t.Helper()
authHeader := request.Headers["Authorization"]
if authHeader == "" {
authHeader = request.Headers["authorization"]
}
require.NotEmpty(t, authHeader, "webhook request missing Authorization header")
require.True(t, strings.HasPrefix(authHeader, "Bearer "), "Authorization header must start with Bearer")
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
require.Equal(t, jwt.SigningMethodHS256, token.Method, "unexpected signing method")
return []byte(signingKey), nil
})
require.NoError(t, err, "JWT validation failed")
require.True(t, token.Valid, "JWT token is not valid")
claims, ok := token.Claims.(jwt.MapClaims)
require.True(t, ok, "cannot parse claims")
require.Equal(t, "api.httpsms.com", claims["iss"], "issuer mismatch")
require.NotEmpty(t, claims["sub"], "subject mismatch")
exp, err := claims.GetExpirationTime()
require.NoError(t, err)
require.True(t, exp.After(time.Now()), "token is expired")
nbf, err := claims.GetNotBefore()
require.NoError(t, err)
require.True(t, nbf.Before(time.Now()), "token not yet valid")
}
// assertAdapterNotificationJWT validates the JWT the API signs adapter notification requests
// with, using the receiving phone's ID as the HMAC-SHA256 secret (see
// api/pkg/services/http_notification_sender.go getAuthToken). The phone ID is only used as the
// secret and is never embedded in a claim, so this only checks the signature and issuer, not a
// subject; the adapter emulator itself rejects notifications with an invalid signature (401).
func assertAdapterNotificationJWT(t *testing.T, record notificationRecord, phoneID string) {
t.Helper()
require.NotEmpty(t, record.Authorization, "adapter notification record missing Authorization header")
require.True(t, strings.HasPrefix(record.Authorization, "Bearer "), "Authorization header must start with Bearer")
tokenString := strings.TrimPrefix(record.Authorization, "Bearer ")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
require.Equal(t, jwt.SigningMethodHS256, token.Method, "unexpected signing method")
return []byte(phoneID), nil
})
require.NoError(t, err, "JWT validation failed")
require.True(t, token.Valid, "JWT token is not valid")
claims, ok := token.Claims.(jwt.MapClaims)
require.True(t, ok, "cannot parse claims")
require.Equal(t, "api.httpsms.com", claims["iss"], "issuer mismatch")
require.Empty(t, claims["sub"], "phone ID must not be embedded in a claim since it is also the signing secret")
exp, err := claims.GetExpirationTime()
require.NoError(t, err)
require.True(t, exp.After(time.Now()), "token is expired")
nbf, err := claims.GetNotBefore()
require.NoError(t, err)
require.True(t, nbf.Before(time.Now()), "token not yet valid")
}
func waitForWebhookEvents(t *testing.T, webhookPath string, expectedCount int, timeout time.Duration) []wmJournal.GetRequestResponse {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
requests := findWebhookRequests(t, webhookPath)
if len(requests) >= expectedCount {
return requests
}
time.Sleep(500 * time.Millisecond)
}
requests := findWebhookRequests(t, webhookPath)
require.GreaterOrEqual(t, len(requests), expectedCount, "expected at least %d webhook events on %s, got %d", expectedCount, webhookPath, len(requests))
return requests
}
func waitForFCMPush(t *testing.T, messageID string, timeout time.Duration) []wmJournal.GetRequestResponse {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
requests := findFCMRequests(t, messageID)
if len(requests) >= 1 {
return requests
}
time.Sleep(500 * time.Millisecond)
}
t.Fatalf("FCM push for message %s not found within %v", messageID, timeout)
return nil
}
type BulkMessageEntry struct {
RequestID string `json:"request_id"`
Total int `json:"total"`
ScheduledCount int `json:"scheduled_count"`
PendingCount int `json:"pending_count"`
FailedCount int `json:"failed_count"`
ExpiredCount int `json:"expired_count"`
SentCount int `json:"sent_count"`
DeliveredCount int `json:"delivered_count"`
CreatedAt string `json:"created_at"`
}
func uploadBulkFile(ctx context.Context, t *testing.T, filename string, fileBytes []byte) (int, []byte) {
t.Helper()
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
part, err := writer.CreateFormFile("document", filename)
require.NoError(t, err)
_, err = part.Write(fileBytes)
require.NoError(t, err)
require.NoError(t, writer.Close())
url := apiBaseURL + "/v1/bulk-messages"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &buf)
require.NoError(t, err)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("x-api-key", userAPIKey)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return resp.StatusCode, body
}
func fetchBulkMessages(ctx context.Context, t *testing.T) []BulkMessageEntry {
t.Helper()
url := apiBaseURL + "/v1/bulk-messages"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoError(t, err)
req.Header.Set("x-api-key", userAPIKey)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "fetch bulk messages failed: %s", string(body))
var result struct {
Data []BulkMessageEntry `json:"data"`
}
require.NoError(t, json.Unmarshal(body, &result))
return result.Data
}
func searchMessages(ctx context.Context, t *testing.T, contact string, owner string) []httpsms.Message {
t.Helper()
url := fmt.Sprintf("%s/v1/messages?contact=%s&owner=%s&limit=20&skip=0", apiBaseURL, contact, owner)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoError(t, err)
req.Header.Set("x-api-key", userAPIKey)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "search messages failed: %s", string(body))
var result struct {
Data []httpsms.Message `json:"data"`
}
require.NoError(t, json.Unmarshal(body, &result))
return result.Data
}
func findBulkEntry(entries []BulkMessageEntry, requestID string) *BulkMessageEntry {
for i := range entries {
if entries[i].RequestID == requestID {
return &entries[i]
}
}
return nil
}
|