Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dex
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
dex
Commits
c94e5384
Commit
c94e5384
authored
Oct 30, 2015
by
Bobby Rullo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
email: templatizer takes global context.
This is so we can populate emails with things like the issuer name.
parent
2ef1b4be
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
2 deletions
+31
-2
mailgun_test.go
email/mailgun_test.go
+0
-1
template.go
email/template.go
+9
-0
template_test.go
email/template_test.go
+21
-0
test
test
+1
-1
No files found.
email/mailgun_test.go
View file @
c94e5384
...
@@ -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"
,
...
...
email/template.go
View file @
c94e5384
...
@@ -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
)
...
...
email/template_test.go
View file @
c94e5384
...
@@ -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<script>alert('hacked!')</script>!"
+
htmlEnd
,
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
{
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
{
...
...
test
View file @
c94e5384
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment