Commit c94e5384 authored by Bobby Rullo's avatar Bobby Rullo

email: templatizer takes global context.

This is so we can populate emails with things like the issuer name.
parent 2ef1b4be
...@@ -16,7 +16,6 @@ func TestNewEmailConfigFromReader(t *testing.T) { ...@@ -16,7 +16,6 @@ func TestNewEmailConfigFromReader(t *testing.T) {
{ {
json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","publicAPIKey":"public","domain":"example.com"}`, json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","publicAPIKey":"public","domain":"example.com"}`,
want: MailgunEmailerConfig{ want: MailgunEmailerConfig{
ID: "mg",
PrivateAPIKey: "private", PrivateAPIKey: "private",
PublicAPIKey: "public", PublicAPIKey: "public",
Domain: "example.com", Domain: "example.com",
......
...@@ -36,6 +36,11 @@ type TemplatizedEmailer struct { ...@@ -36,6 +36,11 @@ type TemplatizedEmailer struct {
textTemplates *template.Template textTemplates *template.Template
htmlTemplates *htmltemplate.Template htmlTemplates *htmltemplate.Template
emailer Emailer 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. // 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 ...@@ -59,6 +64,10 @@ func (t *TemplatizedEmailer) SendMail(from, subject, tplName string, data map[st
data["from"] = from data["from"] = from
data["subject"] = subject data["subject"] = subject
for k, v := range t.globalCtx {
data[k] = v
}
var textBuffer bytes.Buffer var textBuffer bytes.Buffer
if textTpl != nil { if textTpl != nil {
err := textTpl.Execute(&textBuffer, data) err := textTpl.Execute(&textBuffer, data)
......
...@@ -9,9 +9,11 @@ import ( ...@@ -9,9 +9,11 @@ import (
const ( const (
textTemplateString = `{{define "T1.txt"}}{{.gift}} from {{.from}} to {{.to}}.{{end}} textTemplateString = `{{define "T1.txt"}}{{.gift}} from {{.from}} to {{.to}}.{{end}}
{{define "T3.txt"}}Hello there, {{.name}}!{{end}} {{define "T3.txt"}}Hello there, {{.name}}!{{end}}
{{define "T4.txt"}}Hello there, {{.name}}! Welcome to {{.planet}}!{{end}}
` `
htmlTemplateString = `{{define "T1.html"}}<html><body>{{.gift}} from {{.from}} to {{.to}}.</body></html>{{end}} htmlTemplateString = `{{define "T1.html"}}<html><body>{{.gift}} from {{.from}} to {{.to}}.</body></html>{{end}}
{{define "T2.html"}}<html><body>Hello, {{.name}}!</body></html>{{end}} {{define "T2.html"}}<html><body>Hello, {{.name}}!</body></html>{{end}}
{{define "T4.html"}}<html><body>Hello there, {{.name}}! Welcome to {{.planet}}!</body></html>{{end}}
` `
) )
...@@ -51,6 +53,7 @@ func TestTemplatizedEmailSendMail(t *testing.T) { ...@@ -51,6 +53,7 @@ func TestTemplatizedEmailSendMail(t *testing.T) {
wantText string wantText string
wantHtml string wantHtml string
wantErr bool wantErr bool
ctx map[string]interface{}
}{ }{
{ {
tplName: "T1", tplName: "T1",
...@@ -97,11 +100,29 @@ func TestTemplatizedEmailSendMail(t *testing.T) { ...@@ -97,11 +100,29 @@ func TestTemplatizedEmailSendMail(t *testing.T) {
wantText: "", wantText: "",
wantHtml: htmlStart + "Hello, Alice&lt;script&gt;alert(&#39;hacked!&#39;)&lt;/script&gt;!" + htmlEnd, wantHtml: htmlStart + "Hello, Alice&lt;script&gt;alert(&#39;hacked!&#39;)&lt;/script&gt;!" + 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: "<html><body>Hello there, Alice! Welcome to Mars!</body></html>",
},
} }
for i, tt := range tests { for i, tt := range tests {
emailer := &testEmailer{} emailer := &testEmailer{}
templatizer := NewTemplatizedEmailerFromTemplates(textTemplates, htmlTemplates, emailer) 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) err := templatizer.SendMail(tt.from, tt.subject, tt.tplName, tt.data, tt.to)
if tt.wantErr { if tt.wantErr {
if err == nil { if err == nil {
......
...@@ -14,7 +14,7 @@ COVER=${COVER:-"-cover"} ...@@ -14,7 +14,7 @@ COVER=${COVER:-"-cover"}
source ./build 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" FORMATTABLE="$TESTABLE cmd/dexctl cmd/dex-worker cmd/dex-overlord examples/app functional pkg/log"
# user has not provided PKG override # user has not provided PKG override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment