forked from mailjet/mailjet-apiv3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailjet_client_test.go
181 lines (156 loc) · 3.95 KB
/
mailjet_client_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
package mailjet
import (
"fmt"
"math/rand"
"os"
"testing"
"time"
"github.com/mailjet/mailjet-apiv3-go/resources"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func TestNewMailjetClient(t *testing.T) {
ak := os.Getenv("MJ_APIKEY_PUBLIC")
sk := os.Getenv("MJ_APIKEY_PRIVATE")
m := NewMailjetClient(ak, sk)
if ak != m.APIKeyPublic() {
t.Fatal("Wrong public key:", m.APIKeyPublic())
}
if sk != m.APIKeyPrivate() {
t.Fatal("Wrong secret key:", m.APIKeyPrivate())
}
}
func TestList(t *testing.T) {
m := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
var data []resources.Sender
count, _, err := m.List("sender", &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if count < 1 {
t.Fatal("At least one sender expected !")
}
}
func TestGet(t *testing.T) {
m := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
var data []resources.User
resource := "user"
count, _, err := m.List(resource, &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if count < 1 {
t.Fatal("At least one user expected !")
}
if data == nil {
t.Fatal("Empty result")
}
mr := &Request{Resource: resource, ID: data[0].ID}
data = make([]resources.User, 0)
err = m.Get(mr, &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
fmt.Printf("Data: %+v\n", data[0])
}
func TestPost(t *testing.T) {
m := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
var data []resources.Contact
rstr := randSeq(10)
fmt.Printf("Create new contact: \"%[email protected]\"\n", rstr)
fmr := &FullRequest{
Info: &Request{Resource: "contact"},
Payload: &resources.Contact{Name: rstr, Email: rstr + "@mailjet.com"},
}
err := m.Post(fmr, &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if data == nil {
t.Fatal("Empty result")
}
fmt.Printf("Data: %+v\n", data[0])
}
func TestPut(t *testing.T) {
m := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
var data []resources.Contactslist
resource := "contactslist"
count, _, err := m.List(resource, &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if count < 1 {
t.Fatal("At least one contact list expected on test account!")
}
if data == nil {
t.Fatal("Empty result")
}
rstr := randSeq(10)
fmt.Printf("Update name of the contact list: %s -> %s\n", data[0].Name, rstr)
data[0].Name = randSeq(10)
fmr := &FullRequest{
Info: &Request{Resource: resource, AltID: data[0].Address},
Payload: data[0],
}
err = m.Put(fmr, []string{"Name"})
if err != nil {
t.Fatal("Unexpected error:", err)
}
}
func TestDelete(t *testing.T) {
m := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
var data []resources.Listrecipient
resource := "listrecipient"
count, _, err := m.List(resource, &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if count < 1 {
return
}
if data == nil {
t.Fatal("Empty result")
}
mr := &Request{
ID: data[0].ID,
Resource: resource,
}
err = m.Delete(mr)
if err != nil {
fmt.Println(err)
}
}
func TestSendMail(t *testing.T) {
m := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
var data []resources.Sender
count, _, err := m.List("sender", &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if count < 1 || data == nil {
t.Fatal("At least one sender expected in the test account!")
}
param := &InfoSendMail{
FromEmail: data[0].Email,
FromName: data[0].Name,
Recipients: []Recipient{
Recipient{
Email: data[0].Email,
},
},
Subject: "Send API testing",
TextPart: "SendMail is working !",
}
res, err := m.SendMail(param)
if err != nil {
t.Fatal("Unexpected error:", err)
}
fmt.Printf("Data: %+v\n", res)
}