-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
1073 lines (1036 loc) · 31.7 KB
/
builder_test.go
File metadata and controls
1073 lines (1036 loc) · 31.7 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
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mailgen_test
import (
"fmt"
"testing"
"time"
"github.com/akfaiz/go-mailgen"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testCase struct {
name string
builderFunc func() *mailgen.Builder
expectError bool
expectFunc func(msg mailgen.Message)
}
func (tc testCase) run(t *testing.T, modifyBuilder ...func(*mailgen.Builder)) {
t.Run(tc.name, func(t *testing.T) {
builder := tc.builderFunc()
for _, cb := range modifyBuilder {
cb(builder)
}
msg, err := builder.Build()
if tc.expectError {
assert.Error(t, err, "Build should return an error")
return
}
require.NoError(t, err, "Build should not return an error")
assert.NotNil(t, msg, "Build should return a non-nil Message")
tc.expectFunc(msg)
})
}
func TestSetDefault(t *testing.T) {
originalDefault := mailgen.New()
defer mailgen.SetDefault(originalDefault) // Restore original default after tests
t.Run("set valid builder as default", func(t *testing.T) {
customBuilder := mailgen.New()
customBuilder.Subject("Custom Default Subject").
Greeting("Custom Greeting").
Salutation("Custom Salutation").
Product(mailgen.Product{
Name: "Custom Product",
Link: "https://custom.com",
Copyright: "© 2023 Custom",
})
mailgen.SetDefault(customBuilder)
// Create a new message and verify it uses the custom defaults
msg, err := mailgen.New().Build()
require.NoError(t, err)
assert.Equal(t, "Custom Default Subject", msg.Subject())
assert.Contains(t, msg.HTML(), "Custom Greeting")
assert.Contains(t, msg.HTML(), "Custom Salutation")
assert.Contains(t, msg.HTML(), "Custom Product")
assert.Contains(t, msg.HTML(), "https://custom.com")
assert.Contains(t, msg.HTML(), "© 2023 Custom")
})
t.Run("set nil builder should not change default", func(t *testing.T) {
// First set a custom default
customBuilder := mailgen.New()
customBuilder.Subject("Before Nil Test")
mailgen.SetDefault(customBuilder)
// Verify the custom default is set
msg1, err := mailgen.New().Build()
require.NoError(t, err)
assert.Equal(t, "Before Nil Test", msg1.Subject())
// Try to set nil
mailgen.SetDefault(nil)
// Verify the default hasn't changed
msg2, err := mailgen.New().Build()
require.NoError(t, err)
assert.Equal(t, "Before Nil Test", msg2.Subject())
})
t.Run("new instances are independent after setting default", func(t *testing.T) {
customBuilder := mailgen.New()
customBuilder.Subject("Base Subject")
mailgen.SetDefault(customBuilder)
// Create two new instances
builder1 := mailgen.New().Subject("Modified Subject 1")
builder2 := mailgen.New().Subject("Modified Subject 2")
msg1, err := builder1.Build()
require.NoError(t, err)
msg2, err := builder2.Build()
require.NoError(t, err)
// Verify they have different subjects
assert.Equal(t, "Modified Subject 1", msg1.Subject())
assert.Equal(t, "Modified Subject 2", msg2.Subject())
// Verify a new unmodified instance still has the default
msg3, err := mailgen.New().Build()
require.NoError(t, err)
assert.Equal(t, "Base Subject", msg3.Subject())
})
}
func TestBuilder_Subject(t *testing.T) {
testCases := []testCase{
{
name: "set subject",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Subject("Test Subject")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Equal(t, "Test Subject", msg.Subject(), "Subject should match the set value")
},
},
{
name: "not set subject",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.Subject())
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_From(t *testing.T) {
testCases := []testCase{
{
name: "set from",
builderFunc: func() *mailgen.Builder {
return mailgen.New().From("sender@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Equal(t, "sender@example.com", msg.From().String(), "From should match the set value")
},
},
{
name: "set from with name",
builderFunc: func() *mailgen.Builder {
return mailgen.New().From("sender@example.com", "Sender Name")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Equal(
t,
"Sender Name <sender@example.com>",
msg.From().String(),
"From should match the set value",
)
},
},
{
name: "not set from",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.FromString())
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_ReplyTo(t *testing.T) {
testCases := []testCase{
{
name: "set reply-to",
builderFunc: func() *mailgen.Builder {
return mailgen.New().ReplyTo("replyto@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Equal(t, "replyto@example.com", msg.ReplyToString(), "Reply-To should match the set value")
},
},
{
name: "set reply-to with name",
builderFunc: func() *mailgen.Builder {
return mailgen.New().ReplyTo("replyto@example.com", "Reply To Name")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Equal(
t,
"Reply To Name <replyto@example.com>",
msg.ReplyToString(),
"Reply-To should match the set value",
)
},
},
{
name: "not set reply-to",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.ReplyToString())
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_To(t *testing.T) {
testCases := []testCase{
{
name: "set single recipient",
builderFunc: func() *mailgen.Builder {
return mailgen.New().To("user1@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Len(t, msg.To(), 1, "To should contain one recipient")
assert.Contains(t, msg.To(), "user1@example.com", "To should contain the added recipient")
},
},
{
name: "set multiple recipients",
builderFunc: func() *mailgen.Builder {
return mailgen.New().To("user2@example.com", "user3@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Len(t, msg.To(), 2, "To should contain two recipients")
assert.Contains(t, msg.To(), "user2@example.com", "To should contain the added recipient")
assert.Contains(t, msg.To(), "user3@example.com", "To should contain the added recipient")
},
},
{
name: "set no recipients",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.To(), "To should be empty when no recipients are set")
},
},
{
name: "set empty recipient",
builderFunc: func() *mailgen.Builder {
return mailgen.New().To("")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.To(), "To should be empty when an empty recipient is set")
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Cc(t *testing.T) {
testCases := []testCase{
{
name: "set single CC",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Cc("cc1@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Len(t, msg.Cc(), 1, "CC should contain one recipient")
assert.Contains(t, msg.Cc(), "cc1@example.com", "CC should contain the added recipient")
},
},
{
name: "set multiple CCs",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Cc("cc2@example.com", "cc3@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Len(t, msg.Cc(), 2, "CC should contain two recipients")
assert.Contains(t, msg.Cc(), "cc2@example.com", "CC should contain the added recipient")
assert.Contains(t, msg.Cc(), "cc3@example.com", "CC should contain the added recipient")
},
},
{
name: "set no CCs",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.Cc(), "CC should be empty when no recipients are set")
},
},
{
name: "set empty CC",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Cc("")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.Cc(), "CC should be empty when an empty recipient is set")
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Bcc(t *testing.T) {
testCases := []testCase{
{
name: "set single BCC",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Bcc("bcc1@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Len(t, msg.Bcc(), 1, "BCC should contain one recipient")
assert.Contains(t, msg.Bcc(), "bcc1@example.com", "BCC should contain the added recipient")
},
},
{
name: "set multiple BCCs",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Bcc("bcc2@example.com", "bcc3@example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Len(t, msg.Bcc(), 2, "BCC should contain two recipients")
assert.Contains(t, msg.Bcc(), "bcc2@example.com", "BCC should contain the added recipient")
assert.Contains(t, msg.Bcc(), "bcc3@example.com", "BCC should contain the added recipient")
},
},
{
name: "set no BCCs",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.Bcc(), "BCC should be empty when no recipients are set")
},
},
{
name: "set empty BCC",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Bcc("")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Empty(t, msg.Bcc(), "BCC should be empty when an empty recipient is set")
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Preheader(t *testing.T) {
testCases := []testCase{
{
name: "set preheader",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Preheader("This is a preheader text")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "This is a preheader text", "HTML should contain the preheader text")
assert.Contains(
t,
msg.PlainText(),
"This is a preheader text",
"PlainText should contain the preheader text",
)
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Greeting(t *testing.T) {
testCases := []testCase{
{
name: "set greeting",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Greeting("Hi there")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Hi there", "HTML should contain the greeting text")
assert.Contains(t, msg.PlainText(), "Hi there", "PlainText should contain the greeting text")
},
},
{
name: "not set greeting should use default",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Hi", "HTML should contain the default greeting text")
assert.Contains(t, msg.PlainText(), "Hi", "PlainText should contain the default greeting text")
},
},
{
name: "set greeting with name",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Greeting("Hello").Name("John Doe")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Hello John Doe", "HTML should contain the greeting with name")
assert.Contains(t, msg.PlainText(), "Hello John Doe", "PlainText should contain the greeting with name")
},
},
{
name: "set name without greeting",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Name("Jane Doe")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Hi Jane Doe", "HTML should contain the default greeting with name")
assert.Contains(
t,
msg.PlainText(),
"Hi Jane Doe",
"PlainText should contain the default greeting with name",
)
},
},
{
name: "set empty greeting",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Greeting("")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Hi", "HTML should contain the default greeting text")
assert.Contains(t, msg.PlainText(), "Hi", "PlainText should contain the default greeting text")
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Salutation(t *testing.T) {
testCases := []testCase{
{
name: "set salutation",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Salutation("Kind regards")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Kind regards", "HTML should contain the salutation text")
assert.Contains(t, msg.PlainText(), "Kind regards", "PlainText should contain the salutation text")
},
},
{
name: "not set salutation should use default",
builderFunc: mailgen.New,
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Best regards", "HTML should contain the default salutation text")
assert.Contains(
t,
msg.PlainText(),
"Best regards",
"PlainText should contain the default salutation text",
)
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Line(t *testing.T) {
testCases := []testCase{
{
name: "add line",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Line("This is a line of text")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "This is a line of text", "HTML should contain the line text")
assert.Contains(t, msg.PlainText(), "This is a line of text", "PlainText should contain the line text")
},
},
{
name: "add multiple lines",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Line("First line").Line("Second line")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "First line", "HTML should contain the first line text")
assert.Contains(t, msg.HTML(), "Second line", "HTML should contain the second line text")
assert.Contains(t, msg.PlainText(), "First line", "PlainText should contain the first line text")
assert.Contains(t, msg.PlainText(), "Second line", "PlainText should contain the second line text")
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Linef(t *testing.T) {
testCases := []testCase{
{
name: "add formatted line",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Linef("Hello %s, your order #%d is ready", "John", 12345)
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
expectedText := "Hello John, your order #12345 is ready"
assert.Contains(t, msg.HTML(), expectedText, "HTML should contain the formatted line text")
assert.Contains(t, msg.PlainText(), expectedText, "PlainText should contain the formatted line text")
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Action(t *testing.T) {
testCases := []testCase{
{
name: "add action with default color",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Action("Click Here", "https://example.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Click Here", "HTML should contain the action text")
assert.Contains(t, msg.HTML(), "https://example.com", "HTML should contain the action URL")
assert.Contains(
t,
msg.HTML(),
"background-color:#3869D4",
"HTML should contain the default action color",
)
assert.Contains(
t,
msg.HTML(),
"If you're having trouble clicking",
"HTML should contain the fallback text",
)
assert.Contains(t, msg.PlainText(), "Click Here", "PlainText should contain the action text")
assert.Contains(t, msg.PlainText(), "https://example.com", "PlainText should contain the action URL")
},
},
{
name: "add action with custom color",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Action("Custom Button", "https://custom.com", mailgen.Action{Color: "#FF0000"})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Custom Button", "HTML should contain the custom action text")
assert.Contains(t, msg.HTML(), "https://custom.com", "HTML should contain the custom action URL")
assert.Contains(
t,
msg.HTML(),
"background-color:#FF0000",
"HTML should contain the custom action color",
)
assert.Contains(
t,
msg.HTML(),
"If you're having trouble clicking",
"HTML should contain the fallback text",
)
assert.Contains(t, msg.PlainText(), "Custom Button", "PlainText should contain the custom action text")
assert.Contains(
t,
msg.PlainText(),
"https://custom.com",
"PlainText should contain the custom action URL",
)
},
},
{
name: "disable action fallback",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Action("No Fallback", "https://nofallback.com", mailgen.Action{NoFallback: true})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "No Fallback", "HTML should contain the action text")
assert.Contains(t, msg.HTML(), "https://nofallback.com", "HTML should contain the action URL")
assert.NotContains(
t,
msg.HTML(),
"If you're having trouble clicking",
"HTML should not contain the fallback text",
)
assert.Contains(t, msg.PlainText(), "No Fallback", "PlainText should contain the action text")
assert.Contains(t, msg.PlainText(), "https://nofallback.com", "PlainText should contain the action URL")
},
},
{
name: "custom fallback format",
builderFunc: func() *mailgen.Builder {
return mailgen.New().
FallbackFormat("If you cannot click the button, visit this link:").
Action("Custom Fallback", "https://customfallback.com")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Custom Fallback", "HTML should contain the action text")
assert.Contains(t, msg.HTML(), "https://customfallback.com", "HTML should contain the action URL")
assert.Contains(
t,
msg.HTML(),
"If you cannot click the button, visit this link:",
"HTML should contain the custom fallback text",
)
assert.Contains(t, msg.PlainText(), "Custom Fallback", "PlainText should contain the action text")
assert.Contains(
t,
msg.PlainText(),
"https://customfallback.com",
"PlainText should contain the action URL",
)
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Product(t *testing.T) {
testCases := []testCase{
{
name: "set product with complete info",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Product(mailgen.Product{
Name: "Test Product",
Link: "https://test.com",
Copyright: "© 2023 Test Product",
})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Test Product", "HTML should contain the product name")
assert.Contains(t, msg.HTML(), "https://test.com", "HTML should contain the product URL")
assert.Contains(t, msg.HTML(), "© 2023 Test Product", "HTML should contain the product copyright")
assert.Contains(t, msg.PlainText(), "Test Product", "PlainText should contain the product name")
assert.Contains(t, msg.PlainText(), "https://test.com", "PlainText should contain the product URL")
assert.Contains(
t,
msg.PlainText(),
"© 2023 Test Product",
"PlainText should contain the product copyright",
)
},
},
{
name: "set product with only name",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Product(mailgen.Product{
Name: "Test Product",
})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
defaultCopyright := fmt.Sprintf("© %d Test Product. All rights reserved.", time.Now().Year())
assert.Contains(t, msg.HTML(), "Test Product", "HTML should contain the product name")
assert.Contains(t, msg.HTML(), defaultCopyright, "HTML should contain the default product copyright")
assert.Contains(t, msg.PlainText(), "Test Product", "PlainText should contain the product name")
assert.Contains(
t,
msg.PlainText(),
defaultCopyright,
"PlainText should contain the default product copyright",
)
},
},
{
name: "set product with only copyright",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Product(mailgen.Product{
Copyright: "© 2023 Test Product. All rights reserved.",
})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
defaultProductName := "Go-Mailgen"
assert.Contains(t, msg.HTML(), defaultProductName, "HTML should contain the default product name")
assert.Contains(
t,
msg.HTML(),
"© 2023 Test Product. All rights reserved.",
"HTML should contain the product copyright",
)
assert.Contains(
t,
msg.PlainText(),
"© 2023 Test Product. All rights reserved.",
"PlainText should contain the product copyright",
)
assert.Contains(
t,
msg.PlainText(),
defaultProductName,
"PlainText should contain the default product name",
)
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Table(t *testing.T) {
testCases := []testCase{
{
name: "simple table with headers and rows",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Table(mailgen.Table{
Data: [][]mailgen.Entry{
{
{Key: "Item", Value: "Widget A"},
{Key: "Price", Value: "$10.00"},
{Key: "Count", Value: "2"},
{Key: "Total", Value: "$20.00"},
},
{
{Key: "Item", Value: "Widget B"},
{Key: "Price", Value: "$150.00"},
{Key: "Count", Value: "1"},
{Key: "Total", Value: "$150.00"},
},
},
Columns: mailgen.Columns{
CustomAlign: map[string]string{
"Item": "left",
"Price": "center",
"Count": "center",
"Total": "right",
},
CustomWidth: map[string]string{
"Item": "40%",
"Price": "20%",
"Count": "20%",
"Total": "20%",
},
},
})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
htmlContains := []string{
">Item</", ">Widget A</", ">$10.00</", ">2</", ">$20.00</", ">Widget B</", ">$150.00</",
">1</",
}
for _, str := range htmlContains {
assert.Contains(t, msg.HTML(), str, "HTML should contain '%s'", str)
}
textContains := []string{"Item", "Widget A", "$10.00", "2", "$20.00", "Widget B", "$150.00", "1"}
for _, str := range textContains {
assert.Contains(t, msg.PlainText(), str, "PlainText should contain '%s'", str)
}
},
},
{
name: "table with no data",
builderFunc: func() *mailgen.Builder {
return mailgen.New().Table(mailgen.Table{
Data: [][]mailgen.Entry{},
Columns: mailgen.Columns{
CustomAlign: map[string]string{
"Item": "left",
"Price": "center",
},
CustomWidth: map[string]string{
"Item": "50%",
"Price": "50%",
},
},
})
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.NotContains(
t,
msg.PlainText(),
"Item",
"PlainText should not contain table headers when no data is provided",
)
},
},
}
for _, tc := range testCases {
tc.run(t)
}
}
func TestBuilder_Build(t *testing.T) {
themes := []string{"default", "plain"}
testCases := []testCase{
{
name: "reset password message",
builderFunc: func() *mailgen.Builder {
return mailgen.New().
Line("Click the link below to reset your password:").
Action("Reset Password", "https://example.com/reset").
Line("If you did not request this, please ignore this email.")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(
t,
msg.HTML(),
"Click the link below to reset your password:",
"HTML should contain the line text",
)
assert.Contains(t, msg.HTML(), "Reset Password", "HTML should contain the action text")
assert.Contains(t, msg.HTML(), "https://example.com/reset", "HTML should contain the action URL")
assert.Contains(
t,
msg.PlainText(),
"Click the link below to reset your password:",
"PlainText should match the set value",
)
assert.Contains(t, msg.PlainText(), "Reset Password", "PlainText should contain the action text")
assert.Contains(
t,
msg.PlainText(),
"https://example.com/reset",
"PlainText should contain the action URL",
)
},
},
{
name: "welcome message",
builderFunc: func() *mailgen.Builder {
return mailgen.New().
Line("Welcome to our service!").
Line("We're glad to have you on board.").
Line("If you have any questions, feel free to reach out to our support team.")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), "Welcome to our service!", "HTML should contain the welcome message")
assert.Contains(
t,
msg.HTML(),
"We're glad to have you on board.",
"HTML should contain the second line",
)
assert.Contains(
t,
msg.HTML(),
"If you have any questions, feel free to reach out to our support team.",
"HTML should contain the third line",
)
assert.Contains(t, msg.PlainText(), "Welcome to our service!", "PlainText should match the set value")
assert.Contains(
t,
msg.PlainText(),
"We're glad to have you on board.",
"PlainText should contain the second line",
)
assert.Contains(
t,
msg.PlainText(),
"If you have any questions, feel free to reach out to our support team.",
"PlainText should contain the third line",
)
},
},
{
name: "invoice message",
builderFunc: func() *mailgen.Builder {
return mailgen.New().
Line("Thank you for your purchase!").
Line("Below are the details of your order:").
Table(mailgen.Table{
Data: [][]mailgen.Entry{
{{Key: "Item", Value: "Widget A"}, {Key: "Price", Value: "$10.00"}},
{{Key: "Item", Value: "Widget B"}, {Key: "Price", Value: "$15.00"}},
{{Key: "Item", Value: "Widget C"}, {Key: "Price", Value: "$20.00"}},
{{Key: "Item", Value: "Total"}, {Key: "Price", Value: "$45.00"}},
},
Columns: mailgen.Columns{
CustomAlign: map[string]string{
"Item": "left",
"Price": "right",
},
CustomWidth: map[string]string{
"Item": "70%",
"Price": "30%",
},
},
}).
Line("Click the button below to track your order.").
Action("Track Order", "https://example.com/track-order").
Line("If you have any questions, please contact our support team.")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(
t,
msg.HTML(),
"Thank you for your purchase!",
"HTML should contain the thank you message",
)
assert.Contains(
t,
msg.HTML(),
"Below are the details of your order:",
"HTML should contain the order details message",
)
assert.Contains(t, msg.HTML(), "<table", "HTML should contain a table")
assert.Contains(t, msg.HTML(), ">Widget A</", "HTML should contain the first row item")
assert.Contains(t, msg.HTML(), ">$10.00</", "HTML should contain the first row price")
assert.Contains(t, msg.HTML(), "Track Order", "HTML should contain the action text")
assert.Contains(t, msg.HTML(), "https://example.com/track-order", "HTML should contain the action URL")
assert.Contains(
t,
msg.PlainText(),
"Thank you for your purchase!",
"PlainText should match the set value",
)
assert.Contains(
t,
msg.PlainText(),
"Below are the details of your order:",
"PlainText should contain the order details message",
)
assert.Contains(t, msg.PlainText(), "Widget A", "PlainText should contain the first row item")
assert.Contains(t, msg.PlainText(), "$10.00", "PlainText should contain the first row price")
assert.Contains(t, msg.PlainText(), "Track Order", "PlainText should contain the action text")
assert.Contains(
t,
msg.PlainText(),
"https://example.com/track-order",
"PlainText should contain the action URL",
)
},
},
}
for _, theme := range themes {
for _, tc := range testCases {
tc.name = theme + " " + tc.name
tc.run(t, func(b *mailgen.Builder) {
b.Theme(theme)
})
}
}
}
func TestBuilder_TextDirection(t *testing.T) {
testCases := []testCase{
{
name: "set ltr text direction",
builderFunc: func() *mailgen.Builder {
return mailgen.New().TextDirection("ltr")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), `dir="ltr"`, "HTML should contain ltr text direction")
},
},
{
name: "set rtl text direction",
builderFunc: func() *mailgen.Builder {
return mailgen.New().TextDirection("rtl")
},
expectError: false,
expectFunc: func(msg mailgen.Message) {
assert.Contains(t, msg.HTML(), `dir="rtl"`, "HTML should contain rtl text direction")
},
},
{
name: "default text direction should be ltr",
builderFunc: mailgen.New,
expectError: false,