diff --git a/email/mailgun_test.go b/email/mailgun_test.go index 6c78dd91..3c299b69 100644 --- a/email/mailgun_test.go +++ b/email/mailgun_test.go @@ -16,7 +16,6 @@ func TestNewEmailConfigFromReader(t *testing.T) { { json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","publicAPIKey":"public","domain":"example.com"}`, want: MailgunEmailerConfig{ - ID: "mg", PrivateAPIKey: "private", PublicAPIKey: "public", Domain: "example.com", diff --git a/email/template.go b/email/template.go index 2f3c3e03..d992eded 100644 --- a/email/template.go +++ b/email/template.go @@ -36,6 +36,11 @@ type TemplatizedEmailer struct { textTemplates *template.Template htmlTemplates *htmltemplate.Template emailer Emailer + globalCtx map[string]interface{} +} + +func (t *TemplatizedEmailer) SetGlobalContext(ctx map[string]interface{}) { + t.globalCtx = ctx } // SendMail queues an email to be sent to a recipient. @@ -59,6 +64,10 @@ func (t *TemplatizedEmailer) SendMail(from, subject, tplName string, data map[st data["from"] = from data["subject"] = subject + for k, v := range t.globalCtx { + data[k] = v + } + var textBuffer bytes.Buffer if textTpl != nil { err := textTpl.Execute(&textBuffer, data) diff --git a/email/template_test.go b/email/template_test.go index a4a37cad..59644172 100644 --- a/email/template_test.go +++ b/email/template_test.go @@ -9,9 +9,11 @@ import ( const ( textTemplateString = `{{define "T1.txt"}}{{.gift}} from {{.from}} to {{.to}}.{{end}} {{define "T3.txt"}}Hello there, {{.name}}!{{end}} +{{define "T4.txt"}}Hello there, {{.name}}! Welcome to {{.planet}}!{{end}} ` htmlTemplateString = `{{define "T1.html"}}{{.gift}} from {{.from}} to {{.to}}.{{end}} {{define "T2.html"}}Hello, {{.name}}!{{end}} +{{define "T4.html"}}Hello there, {{.name}}! Welcome to {{.planet}}!{{end}} ` ) @@ -51,6 +53,7 @@ func TestTemplatizedEmailSendMail(t *testing.T) { wantText string wantHtml string wantErr bool + ctx map[string]interface{} }{ { tplName: "T1", @@ -97,11 +100,29 @@ func TestTemplatizedEmailSendMail(t *testing.T) { wantText: "", wantHtml: htmlStart + "Hello, Alice<script>alert('hacked!')</script>!" + htmlEnd, }, + { + tplName: "T4", + from: "bob@example.com", + to: "alice@example.com", + subject: "hello there", + data: map[string]interface{}{ + "name": "Alice", + }, + wantText: "Hello there, Alice! Welcome to Mars!", + ctx: map[string]interface{}{ + "planet": "Mars", + }, + wantHtml: "Hello there, Alice! Welcome to Mars!", + }, } for i, tt := range tests { emailer := &testEmailer{} templatizer := NewTemplatizedEmailerFromTemplates(textTemplates, htmlTemplates, emailer) + if tt.ctx != nil { + templatizer.SetGlobalContext(tt.ctx) + } + err := templatizer.SendMail(tt.from, tt.subject, tt.tplName, tt.data, tt.to) if tt.wantErr { if err == nil { diff --git a/test b/test index 7116d8ef..3d26c7ba 100755 --- a/test +++ b/test @@ -14,7 +14,7 @@ COVER=${COVER:-"-cover"} source ./build -TESTABLE="connector db integration pkg/crypto pkg/flag pkg/http pkg/net pkg/time pkg/html functional/repo server session user user/api" +TESTABLE="connector db integration pkg/crypto pkg/flag pkg/http pkg/net pkg/time pkg/html functional/repo server session user user/api email" FORMATTABLE="$TESTABLE cmd/dexctl cmd/dex-worker cmd/dex-overlord examples/app functional pkg/log" # user has not provided PKG override