Spaces:
Paused
Paused
File size: 19,039 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 | package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
)
const testGatewayPhoneID = "11111111-1111-1111-1111-111111111111"
func TestRecordNotificationCopiesRecords(t *testing.T) {
t.Parallel()
instance := newEmulator("http://api.example", http.DefaultClient)
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
record := instance.recordNotification(
"gateway-1",
map[string]string{"KEY_MESSAGE_ID": "message-1"},
"message",
"message-1",
"Bearer test-token",
)
instance.markNotificationProcessed(record)
records := instance.listGatewayRecords("gateway-1")
if len(records) != 1 {
t.Fatalf("record count = %d, want 1", len(records))
}
if !records[0].Processed {
t.Fatal("processed state was not retained")
}
records[0].Data["KEY_MESSAGE_ID"] = "mutated"
fresh := instance.listGatewayRecords("gateway-1")
if fresh[0].Data["KEY_MESSAGE_ID"] != "message-1" {
t.Fatalf("record list returned mutable state: %#v", fresh[0])
}
}
func TestNotificationHandlerProcessesMessage(t *testing.T) {
t.Parallel()
var mu sync.Mutex
var outstandingCalls int
var events []string
api := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.Header.Get("x-api-key") != "phone-key" {
t.Errorf("x-api-key = %q, want phone-key", request.Header.Get("x-api-key"))
}
switch {
case request.Method == http.MethodGet && request.URL.Path == "/v1/messages/outstanding":
mu.Lock()
outstandingCalls++
mu.Unlock()
if request.URL.Query().Get("message_id") != "message-1" {
t.Errorf("message_id = %q, want message-1", request.URL.Query().Get("message_id"))
}
writeJSON(writer, http.StatusOK, map[string]any{
"data": map[string]any{"id": "message-1"},
})
case request.Method == http.MethodPost && request.URL.Path == "/v1/messages/message-1/events":
var payload struct {
EventName string `json:"event_name"`
Timestamp string `json:"timestamp"`
}
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil {
t.Errorf("decode event: %v", err)
}
if payload.Timestamp == "" {
t.Error("event timestamp is empty")
}
mu.Lock()
events = append(events, payload.EventName)
mu.Unlock()
writeJSON(writer, http.StatusOK, map[string]any{"data": map[string]any{"id": "message-1"}})
default:
http.NotFound(writer, request)
}
}))
defer api.Close()
instance := newEmulator(api.URL, api.Client())
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
body := callbackBody(t, map[string]string{"KEY_MESSAGE_ID": "message-1"})
request := httptest.NewRequest(
http.MethodPost,
"/notifications/gateway-1",
bytes.NewReader(body),
)
request.Header.Set("Authorization", validNotificationToken(t, testGatewayPhoneID))
response := httptest.NewRecorder()
instance.notificationHandler().ServeHTTP(response, request)
if response.Code != http.StatusNoContent {
t.Fatalf("callback status = %d, want 204: %s", response.Code, response.Body.String())
}
mu.Lock()
defer mu.Unlock()
if outstandingCalls != 1 {
t.Fatalf("outstanding calls = %d, want 1", outstandingCalls)
}
if !reflect.DeepEqual(events, []string{"SENT", "DELIVERED"}) {
t.Fatalf("events = %#v, want SENT then DELIVERED", events)
}
records := instance.listGatewayRecords("gateway-1")
if len(records) != 1 {
t.Fatalf("record count = %d, want 1", len(records))
}
record := records[0]
if record.Kind != "message" || record.MessageID != "message-1" || !record.Processed {
t.Fatalf("unexpected message record: %#v", record)
}
}
func TestNotificationHandlerStoresHeartbeat(t *testing.T) {
t.Parallel()
var heartbeatPayload map[string]any
api := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodPost || request.URL.Path != "/v1/heartbeats" {
http.NotFound(writer, request)
return
}
if request.Header.Get("x-api-key") != "phone-key" {
t.Errorf("x-api-key = %q, want phone-key", request.Header.Get("x-api-key"))
}
if err := json.NewDecoder(request.Body).Decode(&heartbeatPayload); err != nil {
t.Errorf("decode heartbeat: %v", err)
}
writeJSON(writer, http.StatusCreated, map[string]any{"data": []any{}})
}))
defer api.Close()
instance := newEmulator(api.URL, api.Client())
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
request := httptest.NewRequest(
http.MethodPost,
"/notifications/gateway-1",
bytes.NewReader(callbackBody(t, map[string]string{"KEY_HEARTBEAT_ID": "heartbeat-1"})),
)
request.Header.Set("Authorization", validNotificationToken(t, testGatewayPhoneID))
response := httptest.NewRecorder()
instance.notificationHandler().ServeHTTP(response, request)
if response.Code != http.StatusNoContent {
t.Fatalf("callback status = %d, want 204: %s", response.Code, response.Body.String())
}
if !reflect.DeepEqual(heartbeatPayload["phone_numbers"], []any{"+18005550199"}) {
t.Fatalf("phone_numbers = %#v, want gateway phone", heartbeatPayload["phone_numbers"])
}
if heartbeatPayload["charging"] != true {
t.Fatalf("charging = %#v, want true", heartbeatPayload["charging"])
}
records := instance.listGatewayRecords("gateway-1")
if len(records) != 1 || records[0].Kind != "heartbeat" || !records[0].Processed {
t.Fatalf("unexpected heartbeat records: %#v", records)
}
}
func TestNotificationHandlerRetainsProcessingFailure(t *testing.T) {
t.Parallel()
api := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
http.Error(writer, "outstanding unavailable", http.StatusServiceUnavailable)
}))
defer api.Close()
instance := newEmulator(api.URL, api.Client())
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
request := httptest.NewRequest(
http.MethodPost,
"/notifications/gateway-1",
bytes.NewReader(callbackBody(t, map[string]string{"KEY_MESSAGE_ID": "message-1"})),
)
request.Header.Set("Authorization", validNotificationToken(t, testGatewayPhoneID))
response := httptest.NewRecorder()
instance.notificationHandler().ServeHTTP(response, request)
if response.Code != http.StatusInternalServerError {
t.Fatalf("callback status = %d, want 500: %s", response.Code, response.Body.String())
}
records := instance.listGatewayRecords("gateway-1")
if len(records) != 1 {
t.Fatalf("record count = %d, want 1", len(records))
}
if records[0].Processed {
t.Fatal("failed notification was marked processed")
}
if !strings.Contains(records[0].Error, "fetch outstanding message") {
t.Fatalf("record error = %q, want fetch context", records[0].Error)
}
}
func TestNotificationHandlerProcessesRetryAfterFailure(t *testing.T) {
t.Parallel()
var mu sync.Mutex
var outstandingCalls int
var events []string
api := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
switch {
case request.Method == http.MethodGet && request.URL.Path == "/v1/messages/outstanding":
mu.Lock()
outstandingCalls++
firstCall := outstandingCalls == 1
mu.Unlock()
if firstCall {
http.Error(writer, "outstanding unavailable", http.StatusServiceUnavailable)
return
}
writeJSON(writer, http.StatusOK, map[string]any{
"data": map[string]any{"id": "message-1"},
})
case request.Method == http.MethodPost && request.URL.Path == "/v1/messages/message-1/events":
var payload struct {
EventName string `json:"event_name"`
}
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil {
t.Errorf("decode event: %v", err)
}
mu.Lock()
events = append(events, payload.EventName)
mu.Unlock()
writeJSON(writer, http.StatusOK, map[string]any{"data": map[string]any{"id": "message-1"}})
default:
http.NotFound(writer, request)
}
}))
defer api.Close()
instance := newEmulator(api.URL, api.Client())
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
handler := instance.notificationHandler()
body := callbackBody(t, map[string]string{"KEY_MESSAGE_ID": "message-1"})
firstRequest := httptest.NewRequest(http.MethodPost, "/notifications/gateway-1", bytes.NewReader(body))
firstRequest.Header.Set("Authorization", validNotificationToken(t, testGatewayPhoneID))
firstResponse := httptest.NewRecorder()
handler.ServeHTTP(firstResponse, firstRequest)
if firstResponse.Code != http.StatusInternalServerError {
t.Fatalf("first callback status = %d, want 500: %s", firstResponse.Code, firstResponse.Body.String())
}
secondRequest := httptest.NewRequest(http.MethodPost, "/notifications/gateway-1", bytes.NewReader(body))
secondRequest.Header.Set("Authorization", validNotificationToken(t, testGatewayPhoneID))
secondResponse := httptest.NewRecorder()
handler.ServeHTTP(secondResponse, secondRequest)
if secondResponse.Code != http.StatusNoContent {
t.Fatalf("retry callback status = %d, want 204: %s", secondResponse.Code, secondResponse.Body.String())
}
mu.Lock()
defer mu.Unlock()
if outstandingCalls != 2 {
t.Fatalf("outstanding calls = %d, want 2", outstandingCalls)
}
if !reflect.DeepEqual(events, []string{"SENT", "DELIVERED"}) {
t.Fatalf("events = %#v, want SENT then DELIVERED", events)
}
records := instance.listGatewayRecords("gateway-1")
if len(records) != 2 {
t.Fatalf("record count = %d, want 2", len(records))
}
if records[0].Processed || records[0].Error == "" {
t.Fatalf("unexpected failed record: %#v", records[0])
}
if !records[1].Processed || records[1].Error != "" {
t.Fatalf("unexpected successful retry record: %#v", records[1])
}
}
func TestControlHandlerRegistersGatewayAndReceivesIncomingMessage(t *testing.T) {
t.Parallel()
var receivePayload map[string]any
api := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodPost || request.URL.Path != "/v1/messages/receive" {
http.NotFound(writer, request)
return
}
if request.Header.Get("x-api-key") != "phone-key" {
t.Errorf("x-api-key = %q, want phone-key", request.Header.Get("x-api-key"))
}
if err := json.NewDecoder(request.Body).Decode(&receivePayload); err != nil {
t.Errorf("decode receive payload: %v", err)
}
writeJSON(writer, http.StatusOK, map[string]any{
"data": map[string]any{"id": "message-1"},
})
}))
defer api.Close()
instance := newEmulator(api.URL, api.Client())
handler := instance.controlHandler()
registration := performJSONRequest(t, handler, http.MethodPut, "/test/gateways/gateway-1", map[string]any{
"phone_number": "+18005550199",
"phone_api_key": "phone-key",
"phone_id": testGatewayPhoneID,
})
if registration.Code != http.StatusNoContent {
t.Fatalf("registration status = %d, want 204: %s", registration.Code, registration.Body.String())
}
incoming := performJSONRequest(t, handler, http.MethodPost, "/test/gateways/gateway-1/incoming", map[string]any{
"contact": "+18005550100",
"content": "hello",
"encrypted": true,
})
if incoming.Code != http.StatusOK {
t.Fatalf("incoming status = %d, want 200: %s", incoming.Code, incoming.Body.String())
}
if receivePayload["to"] != "+18005550199" ||
receivePayload["from"] != "+18005550100" ||
receivePayload["content"] != "hello" ||
receivePayload["encrypted"] != true ||
receivePayload["sim"] != "SIM1" ||
receivePayload["timestamp"] == "" {
t.Fatalf("unexpected receive payload: %#v", receivePayload)
}
var incomingResponse struct {
Data map[string]any `json:"data"`
}
if err := json.NewDecoder(incoming.Body).Decode(&incomingResponse); err != nil {
t.Fatalf("decode incoming response: %v", err)
}
if incomingResponse.Data["id"] != "message-1" {
t.Fatalf("incoming message id = %#v, want message-1", incomingResponse.Data["id"])
}
health := httptest.NewRecorder()
handler.ServeHTTP(health, httptest.NewRequest(http.MethodGet, "/health", nil))
if health.Code != http.StatusOK {
t.Fatalf("health status = %d, want 200", health.Code)
}
records := httptest.NewRecorder()
handler.ServeHTTP(records, httptest.NewRequest(http.MethodGet, "/test/gateways/gateway-1/notifications", nil))
if records.Code != http.StatusOK {
t.Fatalf("records status = %d, want 200", records.Code)
}
}
func TestControlHandlerFiltersNotificationRecordsByMessageID(t *testing.T) {
t.Parallel()
instance := newEmulator("http://api.example", http.DefaultClient)
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
instance.recordNotification(
"gateway-1",
map[string]string{"KEY_MESSAGE_ID": "message-1"},
"message",
"message-1",
"Bearer test-token",
)
instance.recordNotification(
"gateway-1",
map[string]string{"KEY_MESSAGE_ID": "message-2"},
"message",
"message-2",
"Bearer test-token",
)
response := httptest.NewRecorder()
instance.controlHandler().ServeHTTP(
response,
httptest.NewRequest(
http.MethodGet,
"/test/gateways/gateway-1/notifications?message_id=message-2",
nil,
),
)
if response.Code != http.StatusOK {
t.Fatalf("records status = %d, want 200", response.Code)
}
var payload struct {
Data []notificationRecord `json:"data"`
}
if err := json.NewDecoder(response.Body).Decode(&payload); err != nil {
t.Fatalf("decode records: %v", err)
}
if len(payload.Data) != 1 || payload.Data[0].MessageID != "message-2" {
t.Fatalf("filtered records = %#v, want message-2 only", payload.Data)
}
}
func TestNotificationHandlerRejectsMissingAuthorization(t *testing.T) {
t.Parallel()
instance := newEmulator("http://api.example", http.DefaultClient)
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
request := httptest.NewRequest(
http.MethodPost,
"/notifications/gateway-1",
bytes.NewReader(callbackBody(t, map[string]string{"KEY_MESSAGE_ID": "message-1"})),
)
response := httptest.NewRecorder()
instance.notificationHandler().ServeHTTP(response, request)
if response.Code != http.StatusUnauthorized {
t.Fatalf("callback status = %d, want 401: %s", response.Code, response.Body.String())
}
if records := instance.listGatewayRecords("gateway-1"); len(records) != 0 {
t.Fatalf("record count = %d, want 0 for an unauthenticated request", len(records))
}
}
func TestNotificationHandlerRejectsTokenSignedWithWrongSecret(t *testing.T) {
t.Parallel()
instance := newEmulator("http://api.example", http.DefaultClient)
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
request := httptest.NewRequest(
http.MethodPost,
"/notifications/gateway-1",
bytes.NewReader(callbackBody(t, map[string]string{"KEY_MESSAGE_ID": "message-1"})),
)
request.Header.Set("Authorization", validNotificationToken(t, "some-other-phone-id"))
response := httptest.NewRecorder()
instance.notificationHandler().ServeHTTP(response, request)
if response.Code != http.StatusUnauthorized {
t.Fatalf("callback status = %d, want 401: %s", response.Code, response.Body.String())
}
if records := instance.listGatewayRecords("gateway-1"); len(records) != 0 {
t.Fatalf("record count = %d, want 0 for a request signed with the wrong secret", len(records))
}
}
func TestNotificationHandlerRejectsNonHS256SigningMethod(t *testing.T) {
t.Parallel()
instance := newEmulator("http://api.example", http.DefaultClient)
instance.registerGateway("gateway-1", gatewayRegistration{
PhoneNumber: "+18005550199",
PhoneAPIKey: "phone-key",
PhoneID: testGatewayPhoneID,
})
now := time.Now().UTC()
token := jwt.NewWithClaims(jwt.SigningMethodHS384, jwt.RegisteredClaims{
Audience: []string{"https://adapter-emulator:9091/notifications/gateway-1"},
ExpiresAt: jwt.NewNumericDate(now.Add(10 * time.Minute)),
IssuedAt: jwt.NewNumericDate(now),
Issuer: notificationJWTIssuer,
NotBefore: jwt.NewNumericDate(now.Add(-10 * time.Minute)),
})
signed, err := token.SignedString([]byte(testGatewayPhoneID))
if err != nil {
t.Fatalf("sign notification token: %v", err)
}
request := httptest.NewRequest(
http.MethodPost,
"/notifications/gateway-1",
bytes.NewReader(callbackBody(t, map[string]string{"KEY_MESSAGE_ID": "message-1"})),
)
request.Header.Set("Authorization", "Bearer "+signed)
response := httptest.NewRecorder()
instance.notificationHandler().ServeHTTP(response, request)
if response.Code != http.StatusUnauthorized {
t.Fatalf("callback status = %d, want 401: %s", response.Code, response.Body.String())
}
if records := instance.listGatewayRecords("gateway-1"); len(records) != 0 {
t.Fatalf("record count = %d, want 0 for a request signed with a non-HS256 method", len(records))
}
}
func validNotificationToken(t *testing.T, phoneID string) string {
t.Helper()
now := time.Now().UTC()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.RegisteredClaims{
Audience: []string{"https://adapter-emulator:9091/notifications/gateway-1"},
ExpiresAt: jwt.NewNumericDate(now.Add(10 * time.Minute)),
IssuedAt: jwt.NewNumericDate(now),
Issuer: notificationJWTIssuer,
NotBefore: jwt.NewNumericDate(now.Add(-10 * time.Minute)),
})
signed, err := token.SignedString([]byte(phoneID))
if err != nil {
t.Fatalf("sign notification token: %v", err)
}
return "Bearer " + signed
}
func callbackBody(t *testing.T, data map[string]string) []byte {
t.Helper()
body, err := json.Marshal(map[string]any{
"message": map[string]any{
"token": "https://adapter-emulator:9091/notifications/gateway-1",
"data": data,
},
})
if err != nil {
t.Fatalf("marshal callback: %v", err)
}
return body
}
func performJSONRequest(
t *testing.T,
handler http.Handler,
method string,
target string,
payload map[string]any,
) *httptest.ResponseRecorder {
t.Helper()
body, err := json.Marshal(payload)
if err != nil {
t.Fatalf("marshal request: %v", err)
}
request := httptest.NewRequest(method, target, bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
return response
}
func writeJSON(writer http.ResponseWriter, status int, payload any) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(status)
_ = json.NewEncoder(writer).Encode(payload)
}
|