email: templatizer takes global context.
This is so we can populate emails with things like the issuer name.
This commit is contained in:
parent
2ef1b4beff
commit
c94e53846e
4 changed files with 31 additions and 2 deletions
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"}}<html><body>{{.gift}} from {{.from}} to {{.to}}.</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) {
|
|||
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: "<html><body>Hello there, Alice! Welcome to Mars!</body></html>",
|
||||
},
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
2
test
2
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
|
||||
|
|
Reference in a new issue