-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailgun_test.go
166 lines (133 loc) · 4.8 KB
/
mailgun_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
package signup
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/operationspark/service-signup/greenlight"
)
func TestSendWelcome(t *testing.T) {
t.Run("sends a 'Welcome Email' with the correct template variables", func(t *testing.T) {
sessionStartDate, _ := time.Parse(time.RFC822, "14 Mar 22 17:00 UTC")
form := Signup{
NameFirst: "Henri",
NameLast: "Testaroni",
Email: "[email protected]",
Cell: "555-123-4567",
Referrer: "instagram",
ReferrerResponse: "",
StartDateTime: sessionStartDate,
SessionID: "this-is-a-session-id",
Cohort: "is-mar-14-22-12pm",
JoinCode: "tlav",
userJoinCode: "user-join-code-here",
}
domain := "test.notarealdomain.org"
apiKey := "test-key"
expectedFormFields := map[string]string{
"to": form.Email,
// mgSrv.defaultTemplate
"template": "info-session-signup",
// mgSrv.defaultSender
"from": "Operation Spark <[email protected]>",
"subject": "Welcome from Operation Spark!",
}
mockMailgunAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(128)
assertNilError(t, err)
// Check template version is not set
assertEqual(t, r.FormValue("t:version"), "")
// Check request has the correct fields
for key, want := range expectedFormFields {
got := r.FormValue(key)
if got != want {
w.WriteHeader(http.StatusExpectationFailed)
t.Fatalf("expected Mailgun POST /messages form field %q:%q\nGot value: %q\n", key, want, got)
}
}
// Check the template variables are correct
jsonVars := r.Form.Get("h:X-Mailgun-Variables")
var gotVars welcomeVariables
err = json.Unmarshal([]byte(jsonVars), &gotVars)
assertNilError(t, err)
assertEqual(t, gotVars.FirstName, form.NameFirst)
assertEqual(t, gotVars.LastName, form.NameLast)
assertEqual(t, gotVars.SessionDate, "Monday, Mar 14")
assertEqual(t, gotVars.SessionTime, "12:00 PM CDT")
assertEqual(t, gotVars.JoinCode, form.JoinCode)
assertEqual(t, gotVars.IsGmail, true)
assertEqual(t, gotVars.GreenlightEnrollURL, "https://greenlight.operationspark.org/sessions/this-is-a-session-id/?subview=overview&userJoinCode=user-join-code-here&joinCode=tlav")
// TODO: ZoomURL
// assertEqual(t, gotVars.ZoomURL, "TODO")
_, err = w.Write([]byte("{}"))
assertNilError(t, err)
}))
defer mockMailgunAPI.Close()
mgSvc := NewMailgunService(domain, apiKey, mockMailgunAPI.URL+"/v4")
err := mgSvc.sendWelcome(context.Background(), form)
if err != nil {
t.Fatalf("send welcome: %v", err)
}
})
t.Run("uses 'dev' info-session-signup template 'APP_ENV' == 'staging' ", func(t *testing.T) {
os.Setenv("APP_ENV", "staging")
mockMailgunAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(128)
assertNilError(t, err)
assertEqual(t, r.FormValue("t:version"), "dev")
_, err = w.Write([]byte("{}"))
assertNilError(t, err)
}))
mgSvc := NewMailgunService(
"mail.example.com",
"api-key",
mockMailgunAPI.URL+"/v4",
)
err := mgSvc.sendWelcome(context.Background(), Signup{})
assertNilError(t, err)
})
t.Run("uses the 'info-session-signup-hybrid' template when 'hybrid' is true", func(t *testing.T) {
signUp := Signup{
LocationType: "HYBRID",
GooglePlace: greenlight.GooglePlace{
PlaceID: "ChIJ7YchCHSmIIYRYsAEPZN_E0o",
Name: "Operation Spark",
Address: "514 Franklin Ave, New Orleans, LA 70117, USA",
Phone: "+1 504-534-8277",
Website: "https://www.operationspark.org/",
Geometry: greenlight.Geometry{
Lat: 29.96325999999999,
Lng: -90.052138,
},
},
StartDateTime: mustMakeTime(t, time.RFC3339, "2022-12-05T18:00:00.000Z"),
}
mockMailgunAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(128)
assertNilError(t, err)
assertEqual(t, r.FormValue("template"), "info-session-signup-hybrid")
// Check the template variables are correct
jsonVars := r.Form.Get("h:X-Mailgun-Variables")
var gotVars welcomeVariables
err = json.Unmarshal([]byte(jsonVars), &gotVars)
assertNilError(t, err)
assertEqual(t, gotVars.LocationLine1, "514 Franklin Ave")
assertEqual(t, gotVars.LocationCityStateZip, "New Orleans, LA 70117")
assertEqual(t, gotVars.LocationMapURL, "https://www.google.com/maps/place/514+Franklin+Ave%2CNew+Orleans%2C+LA+70117")
_, err = w.Write([]byte("{}"))
assertNilError(t, err)
}))
mgSvc := NewMailgunService(
"mail.example.com",
"api-key",
mockMailgunAPI.URL+"/v4",
)
err := mgSvc.sendWelcome(context.Background(), signUp)
if err != nil {
t.Fatal(err)
}
})
}