Repository navigation
Expand file tree
/
Copy pathadapter_integration_test.go
More file actions
93 lines (80 loc) · 3.16 KB
/
Copy pathadapter_integration_test.go
File metadata and controls
93 lines (80 loc) · 3.16 KB
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
package tests
import (
"context"
"net/http"
"testing"
"time"
httpsms "github.com/NdoleStudio/httpsms-go"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAdapterGatewayOutgoingMessage(t *testing.T) {
ctx := context.Background()
phone := setupAdapterPhone(ctx, t, 60)
contact := randomPhoneNumber()
content := "Adapter outgoing " + randomEncryptionKey()
response, httpResponse, err := newAPIClient().Messages.Send(ctx, &httpsms.MessageSendParams{
From: phone.PhoneNumber,
To: contact,
Content: content,
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, httpResponse.HTTPResponse.StatusCode)
messageID := response.Data.ID.String()
message := pollMessageStatus(ctx, t, messageID, "delivered", 30*time.Second)
assert.Equal(t, phone.PhoneNumber, message.Owner)
assert.Equal(t, contact, message.Contact)
assert.Equal(t, content, message.Content)
records := waitForAdapterMessageRecords(t, phone.GatewayID, messageID, 30*time.Second)
require.Len(t, records, 1)
assert.Equal(t, "message", records[0].Kind)
assert.True(t, records[0].Processed)
assert.Equal(t, messageID, records[0].Data["KEY_MESSAGE_ID"])
assertAdapterNotificationJWT(t, records[0], phone.PhoneID)
}
func TestAdapterGatewayIncomingMessage(t *testing.T) {
ctx := context.Background()
phone := setupAdapterPhone(ctx, t, 60)
contact := randomPhoneNumber()
content := "Adapter incoming " + randomEncryptionKey()
messageID := triggerAdapterIncoming(ctx, t, phone, contact, content)
message := pollMessageStatus(ctx, t, messageID, "received", 15*time.Second)
assert.Equal(t, phone.PhoneNumber, message.Owner)
assert.Equal(t, contact, message.Contact)
assert.Equal(t, content, message.Content)
assert.Equal(t, "received", message.Status)
}
func TestAdapterGatewayHeartbeatWakeUp(t *testing.T) {
ctx := context.Background()
phone := setupAdapterPhone(ctx, t, 60)
monitorID := uuid.NewString()
dispatchInternalEvent(ctx, t, map[string]any{
"specversion": "1.0",
"id": uuid.NewString(),
"source": "/tests/adapter-emulator",
"type": "phone.heartbeat.missed",
"time": time.Now().UTC().Format(time.RFC3339),
"datacontenttype": "application/json",
"data": map[string]any{
"phone_id": phone.PhoneID,
"user_id": "test-user-id",
"last_heartbeat_timestamp": time.Now().UTC().Add(-20 * time.Minute).Format(time.RFC3339),
"timestamp": time.Now().UTC().Format(time.RFC3339),
"monitor_id": monitorID,
"owner": phone.PhoneNumber,
},
})
record := waitForAdapterHeartbeatRecord(t, phone.GatewayID, 30*time.Second)
assert.Equal(t, "heartbeat", record.Kind)
assert.NotEmpty(t, record.Data["KEY_HEARTBEAT_ID"])
assertAdapterNotificationJWT(t, record, phone.PhoneID)
heartbeats, response, err := newAPIClient().Heartbeats.Index(ctx, &httpsms.HeartbeatIndexParams{
Owner: phone.PhoneNumber,
Limit: 1,
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
require.NotEmpty(t, heartbeats.Data)
assert.Equal(t, phone.PhoneNumber, heartbeats.Data[0].Owner)
}