-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_messages_test.go
More file actions
316 lines (298 loc) · 9.23 KB
/
email_messages_test.go
File metadata and controls
316 lines (298 loc) · 9.23 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
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
package loops
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestGetEmailMessage(t *testing.T) {
body := `{
"success": true,
"emailMessageId": "em_abc123",
"campaignId": "cmp_xyz789",
"subject": "Hello",
"previewText": "Preview",
"fromName": "Acme",
"fromEmail": "hello",
"replyToEmail": "support@acme.com",
"lmx": "<Paragraph>Hi</Paragraph><Paragraph>Body text.</Paragraph>",
"contentRevisionId": "rev_1",
"updatedAt": "2026-04-20T10:00:00Z"
}`
tests := []struct {
name string
id string
statusCode int
body string
wantAPIErr *APIError
wantErrMsg string
wantID string
}{
{
name: "success",
id: "em_abc123",
statusCode: http.StatusOK,
body: body,
wantID: "em_abc123",
},
{
name: "not found",
id: "em_missing",
statusCode: http.StatusNotFound,
body: `{"success":false,"message":"Email message not found"}`,
wantAPIErr: &APIError{StatusCode: http.StatusNotFound, Message: "Email message not found"},
},
{
name: "mjml conflict",
id: "em_mjml",
statusCode: http.StatusConflict,
body: `{"success":false,"message":"Email message uses MJML format"}`,
wantAPIErr: &APIError{StatusCode: http.StatusConflict, Message: "Email message uses MJML format"},
},
{
name: "invalid json",
id: "em_abc123",
statusCode: http.StatusOK,
body: `not json`,
wantErrMsg: "failed to decode response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPath string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
result, err := client.GetEmailMessage(tt.id)
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
if apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if tt.wantErrMsg != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErrMsg)
}
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErrMsg)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want := "/email-messages/" + tt.id; gotPath != want {
t.Errorf("path = %q, want %q", gotPath, want)
}
if result.EmailMessageID != tt.wantID {
t.Errorf("EmailMessageID = %q, want %q", result.EmailMessageID, tt.wantID)
}
if result.CampaignID == nil || *result.CampaignID != "cmp_xyz789" {
t.Errorf("CampaignID = %v, want cmp_xyz789", result.CampaignID)
}
if result.Subject != "Hello" {
t.Errorf("Subject = %q, want Hello", result.Subject)
}
if result.LMX != "<Paragraph>Hi</Paragraph><Paragraph>Body text.</Paragraph>" {
t.Errorf("LMX = %q", result.LMX)
}
if result.ContentRevisionID == nil || *result.ContentRevisionID != "rev_1" {
t.Errorf("ContentRevisionID = %v, want rev_1", result.ContentRevisionID)
}
})
}
}
const updateEmailMessageResponse = `{
"success": true,
"emailMessageId": "em_abc123",
"campaignId": "cmp_xyz789",
"subject": "Updated",
"previewText": "new preview",
"fromName": "Acme",
"fromEmail": "hello",
"replyToEmail": "support@acme.com",
"lmx": "<Paragraph>Hi</Paragraph>",
"contentRevisionId": "rev_2",
"updatedAt": "2026-04-20T11:00:00Z",
"warnings": [
{"rule":"unknown_attr","severity":"warning","message":"unknown","path":"body.0"}
]
}`
func TestUpdateEmailMessage(t *testing.T) {
tests := []struct {
name string
statusCode int
body string
wantAPIErr *APIError
}{
{
name: "success",
statusCode: http.StatusOK,
body: updateEmailMessageResponse,
},
{
name: "revision conflict",
statusCode: http.StatusConflict,
body: `{"success":false,"message":"Revision mismatch"}`,
wantAPIErr: &APIError{StatusCode: http.StatusConflict, Message: "Revision mismatch"},
},
{
name: "not found",
statusCode: http.StatusNotFound,
body: `{"success":false,"message":"Email message not found"}`,
wantAPIErr: &APIError{StatusCode: http.StatusNotFound, Message: "Email message not found"},
},
{
name: "lmx compile failure",
statusCode: http.StatusUnprocessableEntity,
body: `{"success":false,"message":"LMX failed to compile"}`,
wantAPIErr: &APIError{StatusCode: http.StatusUnprocessableEntity, Message: "LMX failed to compile"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPath, gotMethod string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotMethod = r.Method
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
req := UpdateEmailMessageRequest{
EmailMessageFields: EmailMessageFields{Subject: "Updated"},
Set: map[string]bool{"subject": true},
}
result, err := client.UpdateEmailMessage("em_abc123", req)
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
if apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotMethod != http.MethodPost {
t.Errorf("method = %q, want POST", gotMethod)
}
if want := "/email-messages/em_abc123"; gotPath != want {
t.Errorf("path = %q, want %q", gotPath, want)
}
if result.EmailMessageID != "em_abc123" {
t.Errorf("EmailMessageID = %q, want em_abc123", result.EmailMessageID)
}
if result.ContentRevisionID == nil || *result.ContentRevisionID != "rev_2" {
t.Errorf("ContentRevisionID = %v, want rev_2", result.ContentRevisionID)
}
if len(result.Warnings) != 1 || result.Warnings[0].Rule != "unknown_attr" {
t.Errorf("Warnings = %v, want [unknown_attr]", result.Warnings)
}
})
}
}
func TestUpdateEmailMessage_RequestBody(t *testing.T) {
tests := []struct {
name string
req UpdateEmailMessageRequest
wantFields map[string]any
absent []string
}{
{
name: "only subject set",
req: UpdateEmailMessageRequest{
EmailMessageFields: EmailMessageFields{
Subject: "Hello",
PreviewText: "ignored-not-in-set",
},
Set: map[string]bool{"subject": true},
},
wantFields: map[string]any{"subject": "Hello"},
absent: []string{"previewText", "fromName", "fromEmail", "replyToEmail", "lmx", "expectedRevisionId"},
},
{
name: "blank string is sent when set",
req: UpdateEmailMessageRequest{
EmailMessageFields: EmailMessageFields{PreviewText: ""},
Set: map[string]bool{"previewText": true},
},
wantFields: map[string]any{"previewText": ""},
absent: []string{"subject"},
},
{
name: "expected revision id included when non-empty",
req: UpdateEmailMessageRequest{
EmailMessageFields: EmailMessageFields{Subject: "Hi"},
Set: map[string]bool{"subject": true},
ExpectedRevisionID: "rev_1",
},
wantFields: map[string]any{"subject": "Hi", "expectedRevisionId": "rev_1"},
},
{
name: "all six content fields",
req: UpdateEmailMessageRequest{
EmailMessageFields: EmailMessageFields{
Subject: "s", PreviewText: "p", FromName: "n",
FromEmail: "u", ReplyToEmail: "r@x.com", LMX: "<p/>",
},
Set: map[string]bool{
"subject": true, "previewText": true, "fromName": true,
"fromEmail": true, "replyToEmail": true, "lmx": true,
},
},
wantFields: map[string]any{
"subject": "s", "previewText": "p", "fromName": "n",
"fromEmail": "u", "replyToEmail": "r@x.com", "lmx": "<p/>",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body map[string]any
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
json.Unmarshal(b, &body)
w.WriteHeader(http.StatusOK)
w.Write([]byte(updateEmailMessageResponse))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
if _, err := client.UpdateEmailMessage("em_abc123", tt.req); err != nil {
t.Fatalf("unexpected error: %v", err)
}
for k, want := range tt.wantFields {
if body[k] != want {
t.Errorf("body[%q] = %v, want %v", k, body[k], want)
}
}
for _, k := range tt.absent {
if _, present := body[k]; present {
t.Errorf("body[%q] should not be present, got %v", k, body[k])
}
}
})
}
}