Repository navigation
Expand file tree
/
Copy pathread_receipts_test.go
More file actions
206 lines (181 loc) · 5.25 KB
/
Copy pathread_receipts_test.go
File metadata and controls
206 lines (181 loc) · 5.25 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
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
package tests
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"testing"
"time"
httpsms "github.com/NdoleStudio/httpsms-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type integrationMessageThread struct {
ID string `json:"id"`
Contact string `json:"contact"`
UnreadCount uint `json:"unread_count"`
LastMessageContent *string `json:"last_message_content"`
}
func requestJSON(
ctx context.Context,
t *testing.T,
method string,
path string,
apiKey string,
payload any,
expectedStatus int,
output any,
) {
t.Helper()
var encoded []byte
if payload != nil {
var err error
encoded, err = json.Marshal(payload)
require.NoError(t, err)
}
deadline := time.Now().Add(20 * time.Second)
for {
request, err := http.NewRequestWithContext(ctx, method, apiBaseURL+path, bytes.NewReader(encoded))
require.NoError(t, err)
request.Header.Set("x-api-key", apiKey)
request.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)
responseBody, err := io.ReadAll(response.Body)
response.Body.Close()
require.NoError(t, err)
if response.StatusCode == http.StatusUnauthorized &&
apiKey != userAPIKey &&
time.Now().Before(deadline) {
time.Sleep(500 * time.Millisecond)
continue
}
require.Equal(t, expectedStatus, response.StatusCode, "response: %s", string(responseBody))
if output != nil {
require.NoError(t, json.Unmarshal(responseBody, output))
}
return
}
}
func fetchMessageThreads(ctx context.Context, t *testing.T, owner string) []integrationMessageThread {
t.Helper()
var response struct {
Data []integrationMessageThread `json:"data"`
}
path := fmt.Sprintf(
"/v1/message-threads?owner=%s&skip=0&limit=20&is_archived=false",
url.QueryEscape(owner),
)
requestJSON(ctx, t, http.MethodGet, path, userAPIKey, nil, http.StatusOK, &response)
return response.Data
}
func waitForMessageThread(
ctx context.Context,
t *testing.T,
owner string,
contact string,
timeout time.Duration,
matches func(integrationMessageThread) bool,
) integrationMessageThread {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
for _, thread := range fetchMessageThreads(ctx, t, owner) {
if thread.Contact == contact && matches(thread) {
return thread
}
}
time.Sleep(500 * time.Millisecond)
}
t.Fatalf("thread %s -> %s did not reach the expected state within %v", owner, contact, timeout)
return integrationMessageThread{}
}
func clearMessageThreadUnreadCount(ctx context.Context, t *testing.T, threadID string) integrationMessageThread {
t.Helper()
var response struct {
Data integrationMessageThread `json:"data"`
}
requestJSON(
ctx,
t,
http.MethodPut,
"/v1/message-threads/"+threadID,
userAPIKey,
map[string]any{"unread_count": 0},
http.StatusOK,
&response,
)
return response.Data
}
func TestMessageThreadUnreadCount(t *testing.T) {
ctx := context.Background()
phone := setupPhone(ctx, t, 60)
contact := randomPhoneNumber()
requestJSON(
ctx,
t,
http.MethodPost,
"/v1/messages/receive",
phone.PhoneAPIKey,
map[string]any{
"from": contact,
"to": phone.PhoneNumber,
"content": "Unread inbound message",
"encrypted": false,
"sim": "SIM1",
"timestamp": time.Now().UTC().Format(time.RFC3339Nano),
},
http.StatusOK,
nil,
)
thread := waitForMessageThread(ctx, t, phone.PhoneNumber, contact, 20*time.Second, func(thread integrationMessageThread) bool {
return thread.UnreadCount == 1
})
assert.Equal(t, uint(1), thread.UnreadCount)
updated := clearMessageThreadUnreadCount(ctx, t, thread.ID)
assert.Zero(t, updated.UnreadCount)
assert.Equal(t, contact, updated.Contact)
require.NotNil(t, updated.LastMessageContent)
assert.Equal(t, "Unread inbound message", *updated.LastMessageContent)
waitForMessageThread(ctx, t, phone.PhoneNumber, contact, 10*time.Second, func(thread integrationMessageThread) bool {
return thread.UnreadCount == 0
})
requestJSON(
ctx,
t,
http.MethodPost,
"/v1/messages/calls/missed",
phone.PhoneAPIKey,
map[string]any{
"from": contact,
"to": phone.PhoneNumber,
"sim": "SIM1",
"timestamp": time.Now().UTC().Format(time.RFC3339Nano),
},
http.StatusOK,
nil,
)
thread = waitForMessageThread(ctx, t, phone.PhoneNumber, contact, 20*time.Second, func(thread integrationMessageThread) bool {
return thread.UnreadCount == 1 &&
thread.LastMessageContent != nil &&
*thread.LastMessageContent == "Missed phone call"
})
assert.Equal(t, uint(1), thread.UnreadCount)
outboundContent := "Outbound activity preserves unread"
client := newAPIClient()
_, response, err := client.Messages.Send(ctx, &httpsms.MessageSendParams{
From: phone.PhoneNumber,
To: contact,
Content: outboundContent,
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
thread = waitForMessageThread(ctx, t, phone.PhoneNumber, contact, 20*time.Second, func(thread integrationMessageThread) bool {
return thread.LastMessageContent != nil &&
*thread.LastMessageContent == outboundContent
})
assert.Equal(t, uint(1), thread.UnreadCount, "outbound activity must not clear unread count")
}