forked from mrz1836/postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_streams_test.go
326 lines (287 loc) · 9.61 KB
/
message_streams_test.go
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
package postmark
import (
"context"
"encoding/json"
"net/http"
"testing"
"goji.io/pat"
)
const (
transactionalDev = "transactional-dev"
)
func TestListMessageStreams(t *testing.T) {
responseJSON := `{
"MessageStreams": [ {
"ID": "outbound",
"ServerID": 123457,
"Name": "Transactional Stream",
"Description": "This is my stream to send transactional messages",
"MessageStreamType": "Transactional",
"CreatedAt": "2020-07-01T00:00:00-04:00",
"UpdatedAt": "2020-07-05T00:00:00-04:00",
"ArchivedAt": null,
"ExpectedPurgeDate": null,
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "none"
}
},
{
"ID": "inbound",
"ServerID": 123457,
"Name": "Inbound Stream",
"Description": "Stream used for receiving inbound messages",
"MessageStreamType": "Inbound",
"CreatedAt": "2020-07-01T00:00:00-04:00",
"UpdatedAt": null,
"ArchivedAt": null,
"ExpectedPurgeDate": null,
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "none"
}
},
{
"ID": "transactional-dev",
"ServerID": 123457,
"Name": "My Dev Transactional Stream",
"Description": "This is my second transactional stream",
"MessageStreamType": "Transactional",
"CreatedAt": "2020-07-02T00:00:00-04:00",
"UpdatedAt": "2020-07-04T00:00:00-04:00",
"ArchivedAt": null,
"ExpectedPurgeDate": null,
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "none"
}
}
],
"TotalCount": 3
}`
tMux.HandleFunc(pat.Get("/message-streams"), func(w http.ResponseWriter, req *http.Request) {
if req.URL.Query().Get("IncludeArchivedStreams") != "false" {
t.Fatalf("MessageStreams: wrong IncludeArchivedStreams value (%s)", req.URL.Query().Get("IncludeArchivedStreams"))
}
if req.URL.Query().Get("MessageStreamType") != "All" {
t.Fatalf("MessageStreams: wrong messageStreamType value (%s)", req.URL.Query().Get("MessageStreamType"))
}
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.ListMessageStreams(context.Background(), "All", false)
if err != nil {
t.Fatalf("MessageStreams: %s", err.Error())
}
if len(res) != 3 {
t.Fatalf("MessageStreams: wrong number of message streams (%d)", len(res))
}
// For each message stream, check the ServerID
for _, ms := range res {
if ms.ServerID != 123457 {
t.Fatalf("MessageStreams: wrong ServerID (%d)", ms.ServerID)
}
if ms.ArchivedAt != nil {
t.Fatalf("MessageStreams: wrong ArchivedAt (%s)", *ms.ArchivedAt)
}
}
if res[0].ID != "outbound" {
t.Fatalf("MessageStreams: wrong ID (%s)", res[0].ID)
}
if res[1].ID != "inbound" {
t.Fatalf("MessageStreams: wrong ID (%s)", res[1].ID)
}
if res[2].ID != transactionalDev {
t.Fatalf("MessageStreams: wrong ID (%s)", res[2].ID)
}
}
func TestGetUnknownMessageStream(t *testing.T) {
responseJSON := `{"ErrorCode":1226,"Message":"The message stream for the provided 'ID' was not found."}`
tMux.HandleFunc(pat.Get("/message-streams/unknown"), func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity)
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.GetMessageStream(context.Background(), "unknown")
if err == nil {
t.Fatalf("MessageStream: expected error")
}
if err.Error() != "The message stream for the provided 'ID' was not found." {
t.Fatalf("MessageStream: wrong error message (%s)", err.Error())
}
var zero MessageStream
if res != zero {
t.Fatalf("MessageStream: expected empty response")
}
}
func TestGetMessageStream(t *testing.T) {
responseJSON := `{
"ID": "broadcasts",
"ServerID": 123456,
"Name": "Broadcast Stream",
"Description": "This is my stream to send broadcast messages",
"MessageStreamType": "Broadcasts",
"CreatedAt": "2020-07-01T00:00:00-04:00",
"UpdatedAt": "2020-07-01T00:00:00-04:00",
"ArchivedAt": null,
"ExpectedPurgeDate": null,
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "Postmark"
}
}`
tMux.HandleFunc(pat.Get("/message-streams/broadcasts"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.GetMessageStream(context.Background(), "broadcasts")
if err != nil {
t.Fatalf("MessageStream: %s", err.Error())
}
if res.ID != "broadcasts" {
t.Fatalf("MessageStream: wrong ID (%s)", res.ID)
}
if res.Name != "Broadcast Stream" {
t.Fatalf("MessageStream: wrong Name (%s)", res.Name)
}
if *res.Description != "This is my stream to send broadcast messages" {
t.Fatalf("MessageStream: wrong Description (%s)", *res.Description)
}
}
func TestEditMessageStream(t *testing.T) {
responseJSON := `{
"ID": "transactional-dev",
"ServerID": 123457,
"Name": "Updated Dev Stream",
"Description": "Updating my dev transactional stream",
"MessageStreamType": "Transactional",
"CreatedAt": "2020-07-02T00:00:00-04:00",
"UpdatedAt": "2020-07-03T00:00:00-04:00",
"ArchivedAt": null,
"ExpectedPurgeDate": null,
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "none"
}
}`
editReq := EditMessageStreamRequest{
Name: "Updated Dev Stream",
SubscriptionManagementConfiguration: MessageStreamSubscriptionManagementConfiguration{
UnsubscribeHandlingType: "none",
},
}
tMux.HandleFunc(pat.Patch("/message-streams/transactional-dev"), func(w http.ResponseWriter, req *http.Request) {
var body EditMessageStreamRequest
err := json.NewDecoder(req.Body).Decode(&body)
if err != nil {
t.Fatalf("Failed to read request body: %s", err.Error())
}
if body.Description != nil {
t.Fatalf("EditMessageStream: wrong Description (%v)", body.Description)
}
if editReq.Name != body.Name {
t.Fatalf("EditMessageStream: wrong Name (%s)", body.Name)
}
if editReq.SubscriptionManagementConfiguration.UnsubscribeHandlingType != body.SubscriptionManagementConfiguration.UnsubscribeHandlingType {
t.Fatalf("EditMessageStream: wrong UnsubscribeHandlingType (%s)", body.SubscriptionManagementConfiguration.UnsubscribeHandlingType)
}
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.EditMessageStream(context.Background(), transactionalDev, editReq)
if err != nil {
t.Fatalf("MessageStream: %s", err.Error())
}
if res.ID != transactionalDev {
t.Fatalf("MessageStream: wrong ID (%s)", res.ID)
}
if res.ServerID != 123457 {
t.Fatalf("MessageStream: wrong ServerID (%d)", res.ServerID)
}
if *res.Description != "Updating my dev transactional stream" {
t.Fatalf("MessageStream: wrong Description (%s)", *res.Description)
}
}
func TestCreateMessageStream(t *testing.T) {
responseJSON := `{
"ID": "transactional-dev",
"ServerID": 123457,
"Name": "My Dev Transactional Stream",
"Description": "This is my second transactional stream",
"MessageStreamType": "Transactional",
"CreatedAt": "2020-07-02T00:00:00-04:00",
"UpdatedAt": "2020-07-02T00:00:00-04:00",
"ArchivedAt": "2020-07-02T00:00:00-04:00",
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "None"
}
}`
desc := "This is my second transactional stream"
createReq := CreateMessageStreamRequest{
ID: transactionalDev,
Name: "My Dev Transactional Stream",
Description: &desc,
MessageStreamType: "Transactional",
SubscriptionManagementConfiguration: MessageStreamSubscriptionManagementConfiguration{
UnsubscribeHandlingType: "None",
},
}
tMux.HandleFunc(pat.Post("/message-streams"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.CreateMessageStream(context.Background(), createReq)
if err != nil {
t.Fatalf("MessageStream: %s", err.Error())
}
if res.ID != transactionalDev {
t.Fatalf("MessageStream: wrong ID (%s)", res.ID)
}
if res.ServerID != 123457 {
t.Fatalf("MessageStream: wrong ServerID (%d)", res.ServerID)
}
if res.MessageStreamType != "Transactional" {
t.Fatalf("MessageStream: wrong MessageStreamType (%s)", res.MessageStreamType)
}
}
func TestArchiveMessageStream(t *testing.T) {
responseJSON := `{
"ID": "transactional-dev",
"ServerID": 123457,
"ExpectedPurgeDate": "2020-08-30T12:30:00.00-04:00"
}`
tMux.HandleFunc(pat.Post("/message-streams/transactional-dev/archive"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.ArchiveMessageStream(context.Background(), transactionalDev)
if err != nil {
t.Fatalf("MessageStream: %s", err.Error())
}
if res.ID != transactionalDev {
t.Fatalf("MessageStream: wrong ID (%s)", res.ID)
}
if res.ServerID != 123457 {
t.Fatalf("MessageStream: wrong ServerID (%d)", res.ServerID)
}
if res.ExpectedPurgeDate != "2020-08-30T12:30:00.00-04:00" {
t.Fatalf("MessageStream: wrong ExpectedPurgeDate (%s)", res.ExpectedPurgeDate)
}
}
func TestUnarchiveMessageStream(t *testing.T) {
responseJSON := `{
"ID": "transactional-dev",
"ServerID": 123457,
"Name": "Updated Dev Stream",
"Description": "Updating my dev transactional stream",
"MessageStreamType": "Transactional",
"CreatedAt": "2020-07-02T00:00:00-04:00",
"UpdatedAt": "2020-07-04T00:00:00-04:00",
"ArchivedAt": null,
"SubscriptionManagementConfiguration": {
"UnsubscribeHandlingType": "none"
}
}`
tMux.HandleFunc(pat.Post("/message-streams/transactional-dev/unarchive"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.UnarchiveMessageStream(context.Background(), transactionalDev)
if err != nil {
t.Fatalf("MessageStream: %s", err.Error())
}
if res.ID != transactionalDev {
t.Fatalf("MessageStream: wrong ID (%s)", res.ID)
}
if res.ServerID != 123457 {
t.Fatalf("MessageStream: wrong ServerID (%d)", res.ServerID)
}
}