forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_events.go
245 lines (223 loc) · 6.48 KB
/
mock_events.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
package mailgun
import (
"net/http"
"net/url"
"time"
"github.com/go-chi/chi/v5"
"github.com/mailgun/mailgun-go/v4/events"
)
func (ms *mockServer) addEventRoutes(r chi.Router) {
r.Get("/{domain}/events", ms.listEvents)
var (
tags = []string{"tag1", "tag2"}
recipients = []string{"[email protected]", "[email protected]"}
recipientDomain = "mailgun.test"
timeStamp = TimeToFloat(time.Now().UTC())
ipAddress = "192.168.1.1"
message = events.Message{Headers: events.MessageHeaders{MessageID: "1234"}}
clientInfo = events.ClientInfo{
AcceptLanguage: "EN",
ClientName: "Firefox",
ClientOS: "OS X",
ClientType: "browser",
DeviceType: "desktop",
IP: "8.8.8.8",
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0",
Bot: "",
}
geoLocation = events.GeoLocation{
City: "San Antonio",
Country: "US",
Region: "TX",
}
)
// AcceptedNoAuth
accepted := new(events.Accepted)
accepted.ID = randomString(16, "ID-")
accepted.Message.Headers.MessageID = accepted.ID
accepted.Name = events.EventAccepted
accepted.Tags = tags
accepted.Timestamp = timeStamp
accepted.Recipient = recipients[0]
accepted.RecipientDomain = recipientDomain
accepted.Flags = events.Flags{
IsAuthenticated: false,
}
ms.events = append(ms.events, accepted)
// AcceptedAuth
accepted = new(events.Accepted)
accepted.ID = randomString(16, "ID-")
accepted.Message.Headers.MessageID = accepted.ID
accepted.Name = events.EventAccepted
accepted.Tags = tags
accepted.Timestamp = timeStamp
accepted.Recipient = recipients[0]
accepted.RecipientDomain = recipientDomain
accepted.Campaigns = []events.Campaign{
{ID: "test-id", Name: "test"},
}
accepted.Flags = events.Flags{
IsAuthenticated: true,
}
ms.events = append(ms.events, accepted)
// DeliveredSMTP
delivered := new(events.Delivered)
delivered.ID = randomString(16, "ID-")
delivered.Message.Headers.MessageID = delivered.ID
delivered.Name = events.EventDelivered
delivered.Tags = tags
delivered.Timestamp = timeStamp
delivered.Recipient = recipients[0]
delivered.RecipientDomain = recipientDomain
delivered.DeliveryStatus.Message = "We sent an email Yo"
delivered.Envelope = events.Envelope{
Transport: "smtp",
SendingIP: ipAddress,
}
delivered.Flags = events.Flags{
IsAuthenticated: true,
}
ms.events = append(ms.events, delivered)
// DeliveredHTTP
delivered = new(events.Delivered)
delivered.ID = randomString(16, "ID-")
delivered.Message.Headers.MessageID = delivered.ID
delivered.Name = events.EventDelivered
delivered.Tags = tags
delivered.Timestamp = timeStamp
delivered.Recipient = recipients[0]
delivered.RecipientDomain = recipientDomain
delivered.DeliveryStatus.Message = "We sent an email Yo"
delivered.Envelope = events.Envelope{
Transport: "http",
SendingIP: ipAddress,
}
delivered.Flags = events.Flags{
IsAuthenticated: true,
}
ms.events = append(ms.events, delivered)
// Stored
stored := new(events.Stored)
stored.ID = randomString(16, "ID-")
stored.Name = events.EventStored
stored.Tags = tags
stored.Timestamp = timeStamp
stored.Storage.URL = "http://mailgun.text/some/url"
ms.events = append(ms.events, stored)
// Clicked
for _, recipient := range recipients {
clicked := new(events.Clicked)
clicked.ID = randomString(16, "ID-")
clicked.Name = events.EventClicked
clicked.Message = message
clicked.Tags = tags
clicked.Recipient = recipient
clicked.ClientInfo = clientInfo
clicked.GeoLocation = geoLocation
clicked.Timestamp = timeStamp
ms.events = append(ms.events, clicked)
}
clicked := new(events.Clicked)
clicked.ID = randomString(16, "ID-")
clicked.Name = events.EventClicked
clicked.Message = message
clicked.Tags = tags
clicked.Recipient = recipients[0]
clicked.ClientInfo = clientInfo
clicked.GeoLocation = geoLocation
clicked.Timestamp = timeStamp
ms.events = append(ms.events, clicked)
// Opened
for _, recipient := range recipients {
opened := new(events.Opened)
opened.ID = randomString(16, "ID-")
opened.Name = events.EventOpened
opened.Message = message
opened.Tags = tags
opened.Recipient = recipient
opened.ClientInfo = clientInfo
opened.GeoLocation = geoLocation
opened.Timestamp = timeStamp
ms.events = append(ms.events, opened)
}
opened := new(events.Opened)
opened.ID = randomString(16, "ID-")
opened.Name = events.EventOpened
opened.Message = message
opened.Tags = tags
opened.Recipient = recipients[0]
opened.ClientInfo = clientInfo
opened.GeoLocation = geoLocation
opened.Timestamp = timeStamp
ms.events = append(ms.events, opened)
// Unsubscribed
for _, recipient := range recipients {
unsub := new(events.Unsubscribed)
unsub.ID = randomString(16, "ID-")
unsub.Name = events.EventUnsubscribed
unsub.Tags = tags
unsub.Recipient = recipient
unsub.ClientInfo = clientInfo
unsub.GeoLocation = geoLocation
unsub.Timestamp = timeStamp
ms.events = append(ms.events, unsub)
}
// Complained
for _, recipient := range recipients {
complained := new(events.Complained)
complained.ID = randomString(16, "ID-")
complained.Name = events.EventComplained
complained.Tags = tags
complained.Recipient = recipient
complained.Timestamp = timeStamp
ms.events = append(ms.events, complained)
}
}
type eventsResponse struct {
Items []Event `json:"items"`
Paging Paging `json:"paging"`
}
func (ms *mockServer) listEvents(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
var idx []string
for _, e := range ms.events {
idx = append(idx, e.GetID())
}
limit := stringToInt(r.FormValue("limit"))
if limit == 0 {
limit = 100
}
start, end := pageOffsets(idx, r.FormValue("page"), r.FormValue("address"), limit)
var nextAddress, prevAddress string
var results []Event
if start != end {
results = ms.events[start:end]
nextAddress = results[len(results)-1].GetID()
prevAddress = results[0].GetID()
} else {
results = []Event{}
nextAddress = r.FormValue("address")
prevAddress = r.FormValue("address")
}
resp := eventsResponse{
Paging: Paging{
First: getPageURL(r, url.Values{
"page": []string{"first"},
}),
Last: getPageURL(r, url.Values{
"page": []string{"last"},
}),
Next: getPageURL(r, url.Values{
"page": []string{"next"},
"address": []string{nextAddress},
}),
Previous: getPageURL(r, url.Values{
"page": []string{"prev"},
"address": []string{prevAddress},
}),
},
Items: results,
}
toJSON(w, resp)
}