-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsendgrid-driver_test.go
More file actions
125 lines (118 loc) · 3.39 KB
/
sendgrid-driver_test.go
File metadata and controls
125 lines (118 loc) · 3.39 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
package mailing
import (
"errors"
"io/ioutil"
"net/mail"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/uuid"
)
func TestSendGridDriverSend(t *testing.T) {
sDriver := initiateSendGrid(&SendGridConfig{
Host: "https://api.sendgrid.com",
Endpoint: "/v3/mail/send",
ApiKey: "test-api-key",
})
tmpFilePath := filepath.Join(t.TempDir(), uuid.NewString())
sDriver.initiateSend = func(from string, rcpts []string, message []byte, d Driver) error {
file, err := os.Create(tmpFilePath)
if err != nil {
t.Error("faild test send")
}
file.Write(message)
file.Close()
return nil
}
sDriver.SetFrom(mail.Address{
Name: "test from name",
Address: "from@mail.com",
})
sDriver.SetTo([]mail.Address{
{Name: "test from name1", Address: "from1@mail.com"},
{Name: "test from name2", Address: "from2@mail.com"},
})
sDriver.SetCC([]mail.Address{
{Name: "test cc name1", Address: "cc1@mail.com"},
{Name: "test cc name2", Address: "cc2@mail.com"},
})
sDriver.SetBCC([]mail.Address{
{Name: "test bcc name1", Address: "bcc1@mail.com"},
{Name: "test bcc name2", Address: "bcc2@mail.com"},
})
sDriver.SetSubject("this is the subject")
sDriver.SetPlainTextBody("this is plain text body")
sDriver.SetHTMLBody("this is html body")
sDriver.SetAttachments([]Attachment{
{
Name: "attachment name1",
Path: "./testingdata/attachment1.md",
},
{
Name: "attachment name2",
Path: "./testingdata/attachment2.md",
},
})
err := sDriver.Send()
if err != nil {
t.Error("failed testing send")
}
file, _ := os.Open(tmpFilePath)
mBytes, _ := ioutil.ReadAll(file)
file.Close()
os.Truncate(tmpFilePath, 0)
m := string(mBytes)
if !strings.Contains(m, `From: "test from name" <from@mail.com>`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `To: "test from name1" <from1@mail.com>;"test from name2" <from2@mail.com>`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `Cc: "test cc name1" <cc1@mail.com>;"test cc name2" <cc2@mail.com>`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `Subject: this is the subject`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `Content-Type: text/html; charset="UTF-8"`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `this is html body`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `Content-Disposition: attachment; filename="attachment name1"`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `dGhpcyBpcyBhIHRlc3QgZmlsZSBmb3IgZW1haWwgYXR0YWNobWVudCAx`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `Content-Disposition: attachment; filename="attachment name2"`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `dGhpcyBpcyBhIHRlc3QgZmlsZSBmb3IgZW1haWwgYXR0YWNobWVudCAy`) {
t.Error("Failed test send")
}
sDriver.SetHTMLBody("")
sDriver.SetPlainTextBody("this is plain text body")
err = sDriver.Send()
if err != nil {
t.Error("failed testing send")
}
file, _ = os.Open(tmpFilePath)
mBytes, _ = ioutil.ReadAll(file)
m = string(mBytes)
if !strings.Contains(m, `Content-Type: text/plain; charset="UTF-8"`) {
t.Error("Failed test send")
}
if !strings.Contains(m, `this is plain text body`) {
t.Error("Failed test send")
}
sDriver.initiateSend = func(from string, rcpts []string, message []byte, d Driver) error {
return errors.New("this is a test error")
}
err = sDriver.Send()
if err == nil {
t.Error("failed testing send")
}
}