-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathmail_v3_test.go
849 lines (636 loc) · 29 KB
/
mail_v3_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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
package mail
import (
"encoding/json"
"fmt"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestV3NewMail will test New mail method
func TestV3NewMail(t *testing.T) {
m := NewV3Mail()
assert.NotNil(t, m, "NewV3Mail() shouldn't return nil")
assert.NotNil(t, m.Personalizations, "Personalizations shouldn't be nil")
assert.NotNil(t, m.Attachments, "Attachments shouldn't be nil")
assert.NotNil(t, m.Content, "Content shouldn't be nil")
}
func TestV3NewMailInit(t *testing.T) {
from := NewEmail("Example User", "[email protected]")
subject := "Hello World from the Twilio SendGrid Go Library"
to := NewEmail("Example User", "[email protected]")
content := NewContent("text/plain", "some text here")
m := NewV3MailInit(from, subject, to, content)
assert.NotNil(t, m, "NewV3MailInit() shouldn't return nil")
assert.NotNil(t, m.From, "From shouldn't be nil")
assert.NotNil(t, m.Personalizations, "Personalizations shouldn't be nil")
assert.NotNil(t, m.Content, "Content shouldn't be nil")
}
func TestV3AddPersonalizations(t *testing.T) {
numOfPersonalizations := rand.New(rand.NewSource(99)).Intn(10)
personalizations := make([]*Personalization, 0)
for i := 0; i < numOfPersonalizations; i++ {
personalizations = append(personalizations, NewPersonalization())
}
m := NewV3Mail()
m.AddPersonalizations(personalizations...)
assert.Equal(t, numOfPersonalizations, len(m.Personalizations), fmt.Sprintf("Mail should have %d personalizations, got %d personalizations", len(personalizations), len(m.Personalizations)))
}
func TestV3AddContent(t *testing.T) {
numOfContent := 2
content := make([]*Content, 0)
for i := 0; i < numOfContent; i++ {
content = append(content, NewContent("type", "value"))
}
m := NewV3Mail()
m.AddContent(content...)
assert.Equal(t, numOfContent, len(m.Content), fmt.Sprintf("Mail should have %d contents, got %d contents", numOfContent, len(m.Content)))
}
func TestV3AddAttachment(t *testing.T) {
numOfAttachments := 2
attachment := make([]*Attachment, 0)
for i := 0; i < numOfAttachments; i++ {
attachment = append(attachment, NewAttachment())
}
m := NewV3Mail()
m.AddAttachment(attachment...)
assert.Equal(t, numOfAttachments, len(m.Attachments), fmt.Sprintf("Mail should have %d attachments, got %d attachments", numOfAttachments, len(m.Attachments)))
}
func TestV3SetFrom(t *testing.T) {
m := NewV3Mail()
address := "[email protected]"
name := "Test User"
e := NewEmail(name, address)
m.SetFrom(e)
assert.Equal(t, name, m.From.Name, fmt.Sprintf("name should be %s, got %s", name, e.Name))
assert.Equal(t, address, m.From.Address, fmt.Sprintf("address should be %s, got %s", address, e.Address))
}
func TestV3SetReplyTo(t *testing.T) {
m := NewV3Mail()
address := "[email protected]"
name := "Test User"
e := NewEmail(name, address)
m.SetReplyTo(e)
assert.Equal(t, name, m.ReplyTo.Name, fmt.Sprintf("name should be %s, got %s", name, e.Name))
assert.Equal(t, address, m.ReplyTo.Address, fmt.Sprintf("address should be %s, got %s", address, e.Address))
}
func TestV3SetReplyToList(t *testing.T) {
m := NewV3Mail()
replyTos := []*Email{
NewEmail("Example User", "[email protected]"),
NewEmail("Example User", "[email protected]"),
}
m.SetReplyToList(replyTos)
assert.Equal(t, len(replyTos), len(m.ReplyToList), fmt.Sprintf("length of To should be %d, got %d", len(replyTos), len(m.ReplyToList)))
}
func TestV3SetTemplateID(t *testing.T) {
m := NewV3Mail()
templateID := "templateabcd12345"
m.SetTemplateID(templateID)
assert.Equal(t, templateID, m.TemplateID, fmt.Sprintf("templateID should be %s, got %s", templateID, m.TemplateID))
}
func TestV3AddSection(t *testing.T) {
m := NewV3Mail()
sectionKey := "key"
sectionValue := "value"
m.AddSection(sectionKey, sectionValue)
v, ok := m.Sections[sectionKey]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in Sections map", sectionKey)) {
assert.Equal(t, v, sectionValue, fmt.Sprintf("value should be %s, got %s", sectionValue, v))
}
}
func TestV3SetHeader(t *testing.T) {
m := NewV3Mail()
headerKey := "key"
headerValue := "value"
m.SetHeader(headerKey, headerValue)
v, ok := m.Headers[headerKey]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in Headers map", headerKey)) {
assert.Equal(t, v, headerValue, fmt.Sprintf("value should be %s, got %s", headerValue, v))
}
}
func TestV3AddCategory(t *testing.T) {
m := NewV3Mail()
categories := []string{"cats", "dogs", "hamburgers", "cheeseburgers"}
m.AddCategories(categories...)
assert.Equal(t, len(categories), len(m.Categories), fmt.Sprintf("Length of Categories should be %d, got %d", len(categories), len(m.Categories)))
}
func TestV3SetCustomArg(t *testing.T) {
m := NewV3Mail()
customArgKey := "key"
customArgValue := "value"
m.SetCustomArg(customArgKey, customArgValue)
v, ok := m.CustomArgs[customArgKey]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in CustomArgs map", customArgKey)) {
assert.Equal(t, v, customArgValue, fmt.Sprintf("value should be %s, got %s", customArgValue, v))
}
}
func TestV3SetSendAt(t *testing.T) {
m := NewV3Mail()
sendAt := time.Now().Second()
m.SetSendAt(sendAt)
assert.Equal(t, sendAt, m.SendAt, fmt.Sprintf("SendAt should be %d, got %d", sendAt, m.SendAt))
}
func TestV3SetBatchID(t *testing.T) {
m := NewV3Mail()
batchID := "batchID123455"
m.SetBatchID(batchID)
assert.Equal(t, batchID, m.BatchID, fmt.Sprintf("BatchID should be %s, got %s", batchID, m.BatchID))
}
func TestV3SetIPPoolID(t *testing.T) {
m := NewV3Mail()
ipPoolID := "42"
m.SetIPPoolID(ipPoolID)
assert.Equal(t, ipPoolID, m.IPPoolID, fmt.Sprintf("IP Pool ID should be %s, got %s", ipPoolID, m.IPPoolID))
}
func TestV3SetASM(t *testing.T) {
m := NewV3Mail()
asm := NewASM()
groupID := 1
groupsToDisplay := []int{1, 2, 3, 4}
asm.SetGroupID(groupID)
asm.AddGroupsToDisplay(groupsToDisplay...)
m.SetASM(asm)
assert.Equal(t, groupID, m.Asm.GroupID, fmt.Sprintf("GroupID should be %d, got %d", groupID, m.Asm.GroupID))
assert.Equal(t, groupsToDisplay, m.Asm.GroupsToDisplay, fmt.Sprintf("Length of GroupsToDisplay should be %d, got %d", len(groupsToDisplay), len(m.Asm.GroupsToDisplay)))
}
func TestV3SetMailSettings(t *testing.T) {
m := NewV3Mail()
ms := NewMailSettings()
ms.SetBCC(NewBCCSetting().SetEnable(true))
m.SetMailSettings(ms)
assert.NotNil(t, m.MailSettings, "Mail Settings should not be nil")
assert.True(t, *m.MailSettings.BCC.Enable, "BCC should be enabled in Mail Settings")
}
func TestV3SetTrackingSettings(t *testing.T) {
m := NewV3Mail()
ts := NewTrackingSettings()
n := NewClickTrackingSetting()
n.SetEnable(true)
n.SetEnableText(true)
ts.SetClickTracking(n)
m.SetTrackingSettings(ts)
assert.NotNil(t, m.TrackingSettings, "Tracking Settings should not be nil")
assert.True(t, *m.TrackingSettings.ClickTracking.Enable, "Click Tracking should be enabled")
}
func TestV3NewPersonalization(t *testing.T) {
p := NewPersonalization()
assert.NotNil(t, p, "NewPersonalization() shouldn't return nil")
assert.NotNil(t, p.To, "To should't be nil")
assert.Equal(t, 0, len(p.To), "Length of p.To should be 0")
assert.NotNil(t, p.CC, "CC should't be nil")
assert.Equal(t, 0, len(p.CC), "Length of p.CCs should be 0")
assert.NotNil(t, p.BCC, "BCC should't be nil")
assert.Equal(t, 0, len(p.BCC), "Length of p.BCC should be 0")
assert.NotNil(t, p.Headers, "Headers should't be nil")
assert.Equal(t, 0, len(p.Headers), "Length of p.Headers should be 0")
assert.NotNil(t, p.Substitutions, "Substitutions should't be nil")
assert.Equal(t, 0, len(p.Substitutions), "Length of p.Substitutions should be 0")
assert.NotNil(t, p.CustomArgs, "CustomArgs should't be nil")
assert.Equal(t, 0, len(p.CustomArgs), "Length of p.CustomArgs should be 0")
assert.NotNil(t, p.Categories, "Categories should't be nil")
assert.Equal(t, 0, len(p.Categories), "Length of p.Categories should be 0")
}
func TestV3PersonalizationAddTos(t *testing.T) {
tos := []*Email{
NewEmail("Example User", "[email protected]"),
NewEmail("Example User", "[email protected]"),
}
p := NewPersonalization()
p.AddTos(tos...)
assert.Equal(t, len(tos), len(p.To), fmt.Sprintf("length of To should be %d, got %d", len(tos), len(p.To)))
}
func TestV3PersonalizationAddFrom(t *testing.T) {
address := "[email protected]"
name := "Test User"
from := NewEmail(name, address)
p := NewPersonalization()
p.AddFrom(from)
assert.Equal(t, name, p.From.Name, fmt.Sprintf("name should be %s got %s", name, p.From.Name))
assert.Equal(t, address, p.From.Address, fmt.Sprintf("address should be %s got %s", address, p.From.Address))
}
func TestV3PersonalizationAddCCs(t *testing.T) {
ccs := []*Email{
NewEmail("Example User", "[email protected]"),
NewEmail("Example User", "[email protected]"),
}
p := NewPersonalization()
p.AddCCs(ccs...)
assert.Equal(t, len(ccs), len(p.CC), fmt.Sprintf("length of CC should be %d, got %d", len(ccs), len(p.CC)))
}
func TestV3PersonalizationAddBCCs(t *testing.T) {
bccs := []*Email{
NewEmail("Example User", "[email protected]"),
NewEmail("Example User", "[email protected]"),
}
p := NewPersonalization()
p.AddBCCs(bccs...)
assert.Equal(t, len(bccs), len(p.BCC), fmt.Sprintf("length of BCC should be %d, got %d", len(bccs), len(p.BCC)))
}
func TestV3PersonalizationSetHeader(t *testing.T) {
p := NewPersonalization()
headerKey := "key"
headerValue := "value"
p.SetHeader(headerKey, headerValue)
v, ok := p.Headers[headerKey]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in Headers map", headerKey)) {
assert.Equal(t, headerValue, v, fmt.Sprintf("value should be %s, got %s", headerValue, v))
}
}
func TestV3PersonalizationSetSubstitution(t *testing.T) {
p := NewPersonalization()
substitutionKey := "key"
substitutionValue := "value"
p.SetSubstitution(substitutionKey, substitutionValue)
v, ok := p.Substitutions[substitutionKey]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in Substitutions map", substitutionKey)) {
assert.Equal(t, substitutionValue, v, fmt.Sprintf("value should be %s, got %s", substitutionValue, v))
}
}
func TestV3PersonalizationSetCustomArg(t *testing.T) {
p := NewPersonalization()
customArgKey := "key"
customArgValue := "value"
p.SetCustomArg(customArgKey, customArgValue)
v, ok := p.CustomArgs[customArgKey]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in CustomArgs map", customArgKey)) {
assert.Equal(t, customArgValue, v, fmt.Sprintf("value should be %s, got %s", customArgValue, v))
}
}
func TestV3PersonalizationSetDynamicTemplateData(t *testing.T) {
p := NewPersonalization()
dynamicTemplateDataKey0 := "simpleString"
dynamicTemplateDataValue0 := "value"
p.SetDynamicTemplateData(dynamicTemplateDataKey0, dynamicTemplateDataValue0)
v, ok := p.DynamicTemplateData[dynamicTemplateDataKey0]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in DynamictemplateData map", dynamicTemplateDataKey0)) {
assert.Equal(t, dynamicTemplateDataValue0, v, fmt.Sprintf("value should be %s, got %s", dynamicTemplateDataValue0, v))
}
dynamicTemplateDataKey1 := "arr"
dynamicTemplateDataValue1 := "[true, false, true]"
p.SetDynamicTemplateData(dynamicTemplateDataKey1, dynamicTemplateDataValue1)
v, ok = p.DynamicTemplateData[dynamicTemplateDataKey1]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in DynamictemplateData map", dynamicTemplateDataKey1)) {
assert.Equal(t, dynamicTemplateDataValue1, v, fmt.Sprintf("value should be %s, got %s", dynamicTemplateDataValue1, v))
}
dynamicTemplateDataKey2 := "obj"
dynamicTemplateDataValue2 := map[string]string{
"dynamic": "templates",
"dynamicArr": "[]int{0, 1, 2}",
"bool": "false",
"int": "10",
}
p.SetDynamicTemplateData(dynamicTemplateDataKey2, dynamicTemplateDataValue2)
v, ok = p.DynamicTemplateData[dynamicTemplateDataKey2]
if !assert.True(t, ok, fmt.Sprintf("key %s not found in DynamictemplateData map", dynamicTemplateDataKey2)) {
assert.Equal(t, dynamicTemplateDataValue2, v, fmt.Sprintf("value should be %s, got %s", dynamicTemplateDataValue2, v))
}
}
func TestV3PersonalizationSetSendAt(t *testing.T) {
p := NewPersonalization()
sendAt := time.Now().Second()
p.SetSendAt(sendAt)
assert.Equal(t, sendAt, p.SendAt, fmt.Sprintf("sendat should be %d, got %d", sendAt, p.SendAt))
}
func TestV3NewAttachment(t *testing.T) {
assert.NotNil(t, NewAttachment(), "NewAttachment() shouldn't return nil")
}
func TestV3AttachmentSetContent(t *testing.T) {
content := "somebase64encodedcontent"
a := NewAttachment().SetContent(content)
assert.Equal(t, content, a.Content, fmt.Sprintf("Content should be %s, got %s", content, a.Content))
}
func TestV3AttachmentSetType(t *testing.T) {
contentType := "pdf"
a := NewAttachment().SetType(contentType)
assert.Equal(t, contentType, a.Type, fmt.Sprintf("ContentType should be %s, got %s", contentType, a.Type))
}
func TestV3AttachmentSetContentID(t *testing.T) {
contentID := "contentID"
a := NewAttachment().SetContentID(contentID)
assert.Equal(t, contentID, a.ContentID, fmt.Sprintf("ContentID should be %s, got %s", contentID, a.ContentID))
}
func TestV3AttachmentSetDisposition(t *testing.T) {
disposition := "inline"
a := NewAttachment().SetDisposition(disposition)
assert.Equal(t, disposition, a.Disposition, fmt.Sprintf("Disposition should be %s, got %s", disposition, a.Disposition))
}
func TestV3AttachmentSetFilename(t *testing.T) {
filename := "mydoc.pdf"
a := NewAttachment().SetFilename(filename)
assert.Equal(t, filename, a.Filename, fmt.Sprintf("Filename should be %s, got %s", filename, a.Filename))
}
func TestV3NewASM(t *testing.T) {
assert.NotNil(t, NewASM(), "NewASM() should not return nil")
}
func TestV3ASMSetGroupID(t *testing.T) {
groupID := 1
a := NewASM().SetGroupID(groupID)
assert.Equal(t, groupID, a.GroupID, fmt.Sprintf("GroupID should be %v, got %v", groupID, a.GroupID))
}
func TestV3ASMSetGroupstoDisplay(t *testing.T) {
groupsToDisplay := []int{1, 2, 3, 4}
a := NewASM().AddGroupsToDisplay(groupsToDisplay...)
assert.Equal(t, len(groupsToDisplay), len(a.GroupsToDisplay), fmt.Sprintf("Length of GroupsToDisplay should be %d, got %d", groupsToDisplay, a.GroupsToDisplay))
}
func TestV3NewMailSettings(t *testing.T) {
assert.NotNil(t, NewMailSettings(), "NewMailSettings() shouldn't return nil")
}
func TestV3MailSettingsSetBCC(t *testing.T) {
m := NewMailSettings().SetBCC(NewBCCSetting().SetEnable(true))
assert.NotNil(t, m.BCC, "BCC should not be nil")
assert.True(t, *m.BCC.Enable, "BCC should be enabled")
}
func TestV3MailSettingsSetBypassListManagement(t *testing.T) {
m := NewMailSettings().SetBypassListManagement(NewSetting(true))
assert.NotNil(t, m.BypassListManagement, "BypassListManagement should not be nil")
assert.True(t, *m.BypassListManagement.Enable, "BypassListManagement should be enabled")
}
func TestV3MailSettingsSetBypassSpamManagement(t *testing.T) {
m := NewMailSettings().SetBypassSpamManagement(NewSetting(true))
assert.NotNil(t, m.BypassSpamManagement, "BypassSpamManagement should not be nil")
assert.True(t, *m.BypassSpamManagement.Enable, "BypassSpamManagement should be enabled")
}
func TestV3MailSettingsSetBypassBounceManagement(t *testing.T) {
m := NewMailSettings().SetBypassBounceManagement(NewSetting(true))
assert.NotNil(t, m.BypassBounceManagement, "BypassBounceManagement should not be nil")
assert.True(t, *m.BypassBounceManagement.Enable, "BypassBounceManagement should be enabled")
}
func TestV3MailSettingsSetBypassUnsubscribeManagement(t *testing.T) {
m := NewMailSettings().SetBypassUnsubscribeManagement(NewSetting(true))
assert.NotNil(t, m.BypassUnsubscribeManagement, "BypassUnsubscribeManagement should not be nil")
assert.True(t, *m.BypassUnsubscribeManagement.Enable, "BypassUnsubscribeManagement should be enabled")
}
func TestV3MailSettingsSetSandboxMode(t *testing.T) {
m := NewMailSettings().SetSandboxMode(NewSetting(true))
assert.NotNil(t, m.SandboxMode, "SandboxMode should not be nil")
assert.True(t, *m.SandboxMode.Enable, "SandboxMode should be enabled")
}
func TestV3MailSettingsSpamCheckSettings(t *testing.T) {
m := NewMailSettings()
s := NewSpamCheckSetting()
s.SetEnable(true)
s.SetPostToURL("http://test.com")
s.SetSpamThreshold(1)
m.SetSpamCheckSettings(s)
assert.True(t, *m.SpamCheckSetting.Enable, "SpamCheckSetting should be enabled")
assert.NotNil(t, m.SpamCheckSetting.PostToURL, "PostToURL should not be nil")
assert.Equal(t, 1, m.SpamCheckSetting.SpamThreshold, "Spam threshold should be 1")
}
func TestV3MailSettingsSetFooter(t *testing.T) {
m := NewMailSettings().SetFooter(NewFooterSetting().SetEnable(true))
assert.NotNil(t, m.Footer, "Footer should not be nil")
assert.True(t, *m.Footer.Enable, "Footer should be enabled")
}
func TestV3NewTrackingSettings(t *testing.T) {
assert.NotNil(t, NewTrackingSettings(), "NewTrackingSettings() shouldn't return nil")
}
func TestV3TrackingSettingsSetClickTracking(t *testing.T) {
n := NewClickTrackingSetting()
n.SetEnable(true)
n.SetEnableText(true)
ts := NewTrackingSettings().SetClickTracking(n)
assert.NotNil(t, ts.ClickTracking, "Click Tracking shouldn't return nil")
assert.True(t, *ts.ClickTracking.Enable, "ClickTracking should be enabled")
}
func TestV3TrackingSettingsSetOpenTracking(t *testing.T) {
substitutionTag := "subTag"
ts := NewTrackingSettings().SetOpenTracking(NewOpenTrackingSetting().SetEnable(true).SetSubstitutionTag(substitutionTag))
assert.NotNil(t, ts.OpenTracking, "Open Tracking should not be nil")
assert.True(t, *ts.OpenTracking.Enable, "OpenTracking should be enabled")
assert.Equal(t, substitutionTag, ts.OpenTracking.SubstitutionTag, fmt.Sprintf("Substitution Tag should be %s, got %s", substitutionTag, ts.OpenTracking.SubstitutionTag))
}
func TestV3TrackingSettingsSetSubscriptionTracking(t *testing.T) {
ts := NewTrackingSettings().SetSubscriptionTracking(NewSubscriptionTrackingSetting())
assert.NotNil(t, ts.SubscriptionTracking, "SubscriptionTracking should not be nil")
}
func TestV3TrackingSettingsSetGoogleAnalytics(t *testing.T) {
campaignName := "campaign1"
campaignTerm := "campaign1_term"
campaignSource := "campaign1_source"
campaignContent := "campaign1_content"
campaignMedium := "campaign1_medium"
ts := NewTrackingSettings().SetGoogleAnalytics(NewGaSetting().SetCampaignName(campaignName).SetCampaignTerm(campaignTerm).SetCampaignSource(campaignSource).SetCampaignContent(campaignContent).SetCampaignMedium(campaignMedium).SetEnable(true))
assert.NotNil(t, ts.GoogleAnalytics, "GoogleAnalytics should not be nil")
assert.Equal(t, campaignName, ts.GoogleAnalytics.CampaignName, fmt.Sprintf("CampaignName should be %s, got %s", campaignName, ts.GoogleAnalytics.CampaignName))
assert.Equal(t, campaignTerm, ts.GoogleAnalytics.CampaignTerm, fmt.Sprintf("CampaignTerm should be %s, got %s", campaignTerm, ts.GoogleAnalytics.CampaignTerm))
assert.Equal(t, campaignSource, ts.GoogleAnalytics.CampaignSource, fmt.Sprintf("CampaignSource should be %s, got %s", campaignSource, ts.GoogleAnalytics.CampaignSource))
assert.Equal(t, campaignContent, ts.GoogleAnalytics.CampaignContent, fmt.Sprintf("CampaignContent should be %s, got %s", campaignContent, ts.GoogleAnalytics.CampaignContent))
assert.Equal(t, campaignMedium, ts.GoogleAnalytics.CampaignMedium, fmt.Sprintf("CampaignMedium should be %s, got %s", campaignMedium, ts.GoogleAnalytics.CampaignMedium))
}
func TestV3NewBCCSetting(t *testing.T) {
assert.NotNil(t, NewBCCSetting(), "NewBCCSetting() shouldn't return nil")
}
func TestV3BCCSettingSetEnable(t *testing.T) {
b := NewBCCSetting().SetEnable(true)
assert.True(t, *b.Enable, "BCCSetting should be enabled")
}
func TestV3BCCSettingSetEmail(t *testing.T) {
address := "[email protected]"
b := NewBCCSetting().SetEmail(address)
assert.NotNil(t, b.Email, "Email should not be empty")
}
func TestV3NewFooterSetting(t *testing.T) {
assert.NotNil(t, NewFooterSetting(), "NewFooterSetting() shouldn't return nil")
}
func TestV3FooterSettingSetEnable(t *testing.T) {
f := NewFooterSetting().SetEnable(true)
assert.True(t, *f.Enable, "FooterSetting should be enabled")
}
func TestV3FooterSettingSetText(t *testing.T) {
text := "some test here"
f := NewFooterSetting().SetText(text)
assert.Equal(t, text, f.Text, fmt.Sprintf("Text should be %s, got %s", text, f.Text))
}
func TestV3FooterSettingSetHtml(t *testing.T) {
html := "<h1>some html</h1>"
f := NewFooterSetting().SetHTML(html)
assert.Equal(t, html, f.Html, fmt.Sprintf("Html should be %s, got %s", html, f.Html))
}
func TestV3NewOpenTrackingSetting(t *testing.T) {
assert.NotNil(t, NewOpenTrackingSetting(), "NewOpenTrackingSetting() shouldn't return nil")
}
func TestV3OpenTrackingSettingSetEnable(t *testing.T) {
f := NewOpenTrackingSetting().SetEnable(true)
assert.True(t, *f.Enable, "OpenTrackingSetting should be enabled")
}
func TestV3OpenTrackingSettingSetSubstitutionTag(t *testing.T) {
substitutionTag := "tag"
f := NewOpenTrackingSetting().SetSubstitutionTag(substitutionTag)
assert.Equal(t, substitutionTag, f.SubstitutionTag, fmt.Sprintf("SubstitutionTag should be %s, got %s", substitutionTag, f.SubstitutionTag))
}
func TestV3NewSubscriptionTrackingSetting(t *testing.T) {
assert.NotNil(t, NewSubscriptionTrackingSetting(), "NewSubscriptionTrackingSetting() shouldn't return nil")
}
func TestV3NewSubscriptionTrackingSetEnable(t *testing.T) {
s := NewSubscriptionTrackingSetting().SetEnable(true)
assert.True(t, *s.Enable, "SubscriptionTracking should be enabled")
}
func TestV3NewSubscriptionTrackingSetSubstitutionTag(t *testing.T) {
substitutionTag := "subTag"
s := NewSubscriptionTrackingSetting().SetSubstitutionTag(substitutionTag)
assert.Equal(t, substitutionTag, s.SubstitutionTag, fmt.Sprintf("SubstitutionTag should be %s, got %s", substitutionTag, s.SubstitutionTag))
}
func TestV3NewSubscriptionTrackingSetText(t *testing.T) {
text := "text"
s := NewSubscriptionTrackingSetting().SetText(text)
assert.Equal(t, text, s.Text, fmt.Sprintf("Text should be %s, got %s", text, s.Text))
}
func TestV3NewSubscriptionTrackingSetHtml(t *testing.T) {
html := "<h1>hello</h1>"
s := NewSubscriptionTrackingSetting().SetHTML(html)
assert.Equal(t, html, s.Html, fmt.Sprintf("Html should be %s, got %s", html, s.Html))
}
func TestV3NewGaSetting(t *testing.T) {
assert.NotNil(t, NewGaSetting(), "NewGaSetting() shouldn't return nil")
}
func TestV3GaSettingSetCampaignName(t *testing.T) {
campaignName := "campaign1"
g := NewGaSetting().SetCampaignName(campaignName)
assert.Equal(t, campaignName, g.CampaignName, fmt.Sprintf("CampaignName should be %s, got %s", campaignName, g.CampaignName))
}
func TestV3GaSettingSetCampaignTerm(t *testing.T) {
campaignTerm := "campaign1_term"
g := NewGaSetting().SetCampaignTerm(campaignTerm)
assert.Equal(t, campaignTerm, g.CampaignTerm, fmt.Sprintf("CampaignTerm should be %s, got %s", campaignTerm, g.CampaignTerm))
}
func TestV3GaSettingSetCampaignSource(t *testing.T) {
campaignSource := "campaign1_source"
g := NewGaSetting().SetCampaignSource(campaignSource)
assert.Equal(t, campaignSource, g.CampaignSource, fmt.Sprintf("CampaignSource should be %s, got %s", campaignSource, g.CampaignSource))
}
func TestV3GaSettingSetCampaignContent(t *testing.T) {
campaignContent := "campaign1_content"
g := NewGaSetting().SetCampaignContent(campaignContent)
assert.Equal(t, campaignContent, g.CampaignContent, fmt.Sprintf("CampaignContent should be %s, got %s", campaignContent, g.CampaignContent))
}
func TestV3NewSetting(t *testing.T) {
s := NewSetting(true)
assert.NotNil(t, s, "NewSetting() shouldn't return nil")
assert.True(t, *s.Enable, "NewSetting(true) should return a setting with Enabled = true")
}
func TestV3NewEmail(t *testing.T) {
name := "Johnny"
address := "[email protected]"
e := NewEmail(name, address)
assert.Equal(t, name, e.Name, fmt.Sprintf("Name should be %s, got %s", name, e.Name))
assert.Equal(t, address, e.Address, fmt.Sprintf("Address should be %s, got %s", address, e.Address))
}
func TestV3NewSingleEmail(t *testing.T) {
from := NewEmail("Example User", "[email protected]")
subject := "Sending with Twilio SendGrid is Fun"
to := NewEmail("Example User", "[email protected]")
plainTextContent := "and easy to do anywhere, even with Go"
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
message := NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
assert.NotNil(t, message, "NewV3MailInit() shouldn't return nil")
assert.NotNil(t, message.From, "From shouldn't return nil")
assert.Equal(t, subject, message.Subject, fmt.Sprintf("Subject should be %s, got %s", subject, message.Subject))
assert.NotNil(t, message.Content, "Content shouldn't be nil")
}
func TestV3NewSingleEmailWithEmptyHTMLContent(t *testing.T) {
from := NewEmail("Example User", "[email protected]")
subject := "Sending with Twilio SendGrid is Fun"
to := NewEmail("Example User", "[email protected]")
plainTextContent := "and easy to do anywhere, even with Go"
message := NewSingleEmail(from, subject, to, plainTextContent, "")
m, _ := json.Marshal(message)
fmt.Println(string(m))
if message == nil {
t.Errorf("NewV3MailInit() shouldn't return nil")
}
if message.From == nil {
t.Errorf("From shouldn't be nil")
}
if message.Subject != subject {
t.Errorf("Subject should be %s, got %s", subject, message.Subject)
}
if message.Content == nil {
t.Errorf("Content shouldn't be nil")
}
if len(message.Content) != 1 {
t.Errorf("Content length should be 1, got %d", len(message.Content))
}
if len(message.Content) == 1 && message.Content[0].Type != "text/plain" {
t.Errorf("Content type should be 'text/plain', got %s", message.Content[0].Type)
}
}
func TestV3NewSingleEmailPlainText(t *testing.T) {
from := NewEmail("Example User", "[email protected]")
subject := "Sending with SendGrid is Fun"
to := NewEmail("Example User", "[email protected]")
plainTextContent := "and easy to do anywhere, even with Go"
message := NewSingleEmailPlainText(from, subject, to, plainTextContent)
m, _ := json.Marshal(message)
fmt.Println(string(m))
assert.NotNil(t, message, "NewV3MailInit() shouldn't return nil")
assert.NotNil(t, message.From, "From shouldn't return nil")
assert.Equal(t, subject, message.Subject, fmt.Sprintf("Subject should be %s, got %s", subject, message.Subject))
assert.NotNil(t, message.Content, "Content shouldn't be nil")
}
func TestV3NewClickTrackingSetting(t *testing.T) {
c := NewClickTrackingSetting()
c.SetEnable(true)
c.SetEnableText(false)
assert.True(t, *c.Enable, "Click Tracking should be enabled")
assert.False(t, *c.EnableText, "Enable Text should not be enabled")
}
func TestV3NewSpamCheckSetting(t *testing.T) {
spamThreshold := 8
postToURL := "http://myurl.com"
s := NewSpamCheckSetting()
s.SetEnable(true)
s.SetSpamThreshold(spamThreshold)
s.SetPostToURL(postToURL)
assert.True(t, *s.Enable, "SpamCheck should be enabled")
assert.Equal(t, spamThreshold, s.SpamThreshold, fmt.Sprintf("SpamThreshold should be %d, got %d", spamThreshold, s.SpamThreshold))
assert.Equal(t, postToURL, s.PostToURL, fmt.Sprintf("PostToURL should be %s, got %s", postToURL, s.PostToURL))
}
func TestV3NewSandboxModeSetting(t *testing.T) {
spamCheck := NewSpamCheckSetting()
spamCheck.SetEnable(true)
spamCheck.SetSpamThreshold(1)
spamCheck.SetPostToURL("http://wwww.google.com")
s := NewSandboxModeSetting(true, true, spamCheck)
assert.True(t, *s.Enable, "Sandbox Mode should be enabled")
assert.True(t, *s.ForwardSpam, "ForwardSpam should be enabled")
assert.NotNil(t, s.SpamCheck, "SpamCheck should not be nil")
}
func TestParseEmail(t *testing.T) {
e, err := ParseEmail("example example <[email protected]>")
if err != nil {
t.Error("Email should have been parsed successfully")
}
expectedName := "example example"
if e.Name != expectedName {
t.Errorf("Expect email with name %s but got %s", expectedName, e.Name)
}
expectedAddress := "[email protected]"
if e.Address != expectedAddress {
t.Errorf("Expect email with address %s but got %s", expectedAddress, e.Address)
}
}
func TestParseInvalidEmail(t *testing.T) {
_, err := ParseEmail("example example <example/example.com>")
if err == nil {
t.Error("Expected an error to be thrown from ParseEmail")
}
}
func TestParseInvalidEmailLength(t *testing.T) {
_, err := ParseEmail("example example <[email protected]>")
if err != nil {
t.Error("ParseEmail should have been parsed successfully")
}
_, err = ParseEmail("example example <exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee@example.com>")
if err == nil {
t.Error("Expected an error to be thrown from ParseEmail")
}
_, err = ParseEmail("example example <example@exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.com>")
if err == nil {
t.Error("Expected an error to be thrown from ParseEmail")
}
_, err = ParseEmail("example example <exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee@exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.com>")
if err == nil {
t.Error("Expected an error to be thrown from ParseEmail")
}
}