mirror of
https://github.com/volatiletech/authboss.git
synced 2025-09-16 09:06:20 +02:00
Add RenderEmail to internal/render
This commit is contained in:
@@ -203,7 +203,7 @@ func (m *MockClientStorer) GetErr(key string) (string, error) {
|
||||
if !ok {
|
||||
return v, authboss.ClientDataErr{key}
|
||||
}
|
||||
return v, ok
|
||||
return v, nil
|
||||
}
|
||||
func (m *MockClientStorer) Put(key, val string) { m.Values[key] = val }
|
||||
func (m *MockClientStorer) Del(key string) { delete(m.Values, key) }
|
||||
|
@@ -181,7 +181,7 @@ func recover_text_email() (*asset, error) {
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _recover_tpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xd4\x94\x4f\x8f\x9b\x30\x10\xc5\xef\xfb\x29\x46\xd6\x5e\x09\xf7\x0a\xb8\x44\xed\xad\x7f\xb4\xbb\x91\x7a\x9d\xc0\x10\xac\x18\x1b\x0d\x26\xea\xca\xe2\xbb\xd7\xc6\x24\x9b\x58\x89\xd4\x1e\x7a\x28\x12\x92\x19\xc6\xbf\xe1\xbd\x67\x51\xb4\x86\x7b\xc0\xda\x4a\xa3\x4b\x91\x33\xd5\xe6\x44\x2c\xa0\x27\xdb\x99\xa6\x14\x3f\xbe\xbf\xbe\x89\xea\x09\xfc\xe5\xdc\xf3\x34\x12\x6b\xec\xe9\x33\xf3\x08\x9f\x4a\xd8\xf8\xc5\x57\x1c\x36\xe7\xfa\x3c\x2f\x9d\x45\x23\x4f\x50\x2b\x1c\xc7\x52\x04\x7e\x76\x60\x33\x0d\xce\xc9\x16\x6e\x10\xf3\x0c\x1d\x8e\x19\x31\x1b\x76\x8e\x74\x33\xcf\xeb\xac\x94\x22\xf5\x30\xd9\x88\xb9\xea\x58\xba\xc6\x01\xf5\x9d\xb6\x0c\x9b\xc6\x68\x51\x15\xf2\xf2\x25\x08\x2d\x66\x61\xbe\xaf\xe6\xd2\xdf\x61\x6b\x42\x5b\x08\x37\xdf\x5e\x1b\x6d\xd9\x28\x01\xf6\x7d\xa0\x52\x58\xfa\x65\x05\x04\x05\xa5\x38\x6b\x11\x30\x28\xac\xa9\x33\xaa\x21\x2e\xc5\xee\x52\x3e\xa1\x9a\x7c\x9f\x73\x9b\xdd\xc5\x21\x01\xf9\x95\xc6\xdc\x8b\xfc\x78\x74\x8e\x51\x1f\x08\x9e\xbd\x25\xc1\xdf\xc4\xad\xc7\xc2\x3b\x52\x43\xb6\x57\xa6\x3e\x8a\xca\xb9\x81\xa5\xb6\x0b\x64\x9e\x53\x95\xab\xcf\x4f\x57\xd3\xcf\xe9\x7a\xa5\xad\xe4\x7e\xf7\x20\xe4\xe4\xf5\x1f\x64\x7d\x07\xf8\xbf\x47\x9e\x48\x4a\x92\xdf\xc6\xb7\x70\xef\x04\x6c\x53\xfb\xfe\xea\x20\xdc\xb5\xf2\x1f\x9d\x87\xb0\x8c\xcf\xfb\xc9\x5a\x73\x81\xee\xad\x06\x7f\x67\x1e\xd7\x23\xbf\x2f\xeb\x38\x63\x35\x6a\x9c\xf6\xbd\xb4\xa2\x7a\x89\x7f\x91\x22\x8f\xfb\x23\xb1\xc0\x94\xa3\xa4\x3e\x3e\x84\x40\xc7\xd4\xfa\x3f\x92\x32\x07\xe9\x33\xdd\xa2\xae\x49\x15\x39\xae\xb0\x18\x5a\xdc\xd1\xc9\xa6\x21\x7d\x4e\xc8\x5b\xfd\xf3\xf5\xe5\xcb\xb7\xd5\xe3\x8f\x00\x42\xf5\xcd\x1c\x49\xaf\xd6\x17\x79\x08\xbb\xfa\x1d\x00\x00\xff\xff\xfd\xfd\xd2\x06\x02\x05\x00\x00")
|
||||
var _recover_tpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xd4\x54\x4d\x8f\x9b\x30\x10\xbd\xf3\x2b\xa6\xd6\x5e\xbd\xdc\x2b\xe0\x12\xb5\xb7\x7e\x68\x3f\xa4\x5e\x0d\x98\x60\xc5\xd8\x68\x30\x51\x57\x16\xff\xbd\x43\x0c\x1b\xc2\x92\x36\x6d\xd5\x43\x23\x11\x0d\x66\xfc\x9e\xe7\xbd\xf1\x24\x95\xc5\x06\x44\xe1\x94\x35\x29\x8b\x51\x16\xf6\x28\x91\x41\x23\x5d\x6d\xcb\x94\x7d\xfd\xf2\xf8\xc4\xb2\x08\xe8\x97\x28\xd3\xf6\x0e\xdc\x4b\x2b\x53\xe6\xe4\x77\xc7\xc0\x88\x86\xe2\xbe\x93\x38\x46\x0c\x5a\x2d\x0a\x59\x5b\x5d\x4a\x4c\xd9\xf3\xeb\xf2\x51\xe8\x9e\xf2\xbc\xbf\x9f\x53\x87\x81\x41\x9c\x25\x39\xd2\xff\xcf\xc1\x0b\x6b\x2a\x85\xcd\xf3\x36\xc7\x2e\x7c\x85\x2d\xae\xd5\xce\xb7\x94\x79\xef\x9c\x35\x13\x67\xd7\xe7\x8d\x72\x2c\x7b\x08\x0a\x24\x71\xf8\x3a\xa5\x0a\xa8\x51\x56\x24\x90\xb6\x7b\x65\x58\xb6\x13\xa6\x90\x3a\x89\x45\x16\x25\xf1\x28\x61\x16\x45\x51\xf2\x8e\x73\xf8\x0d\x41\xbd\xbf\x9b\xf5\xf8\x80\xd8\xc1\xfb\x14\xee\x29\xf8\x24\xda\x85\x4e\x81\xbf\x54\x47\x28\xb4\xe8\xba\x94\x8d\xf8\x7c\x8f\xb6\x6f\xbd\x57\x15\x5c\x40\x0c\x03\xd4\xa2\xe3\x12\xd1\xa2\xf7\xd2\x94\x54\x74\xe0\x5a\xa3\x9c\xe4\x0e\x30\x8b\x8c\x53\x56\xd7\x0a\xb3\x91\xc6\x45\x59\x5a\x2a\x3d\x51\xaf\x27\x11\x50\x09\x3e\xf2\xd3\x6a\xac\xe8\x19\xb7\xae\xd0\x82\xaf\xcb\xb3\x93\x2f\x0e\xad\x66\x7f\xd7\x49\x97\xb6\x9e\x6b\x8c\xa9\xc8\xf3\xab\xf7\x28\xcc\x5e\xc2\x1d\x49\x32\xea\xbb\x52\xeb\x7a\xe1\xb5\xd4\x2d\xcf\xb5\x2d\x0e\x2c\xf3\xbe\x45\x65\xdc\x09\x64\x18\xd6\x55\x4e\x3a\x47\x0b\xf6\xd9\xdd\x55\x07\xae\x4d\x7e\xd3\xa0\xbf\xf4\x7a\x03\xf0\x7f\xb7\xfc\xcf\xef\xf7\x6e\xeb\x7e\xdf\xdc\x08\x9b\x52\xfe\xa3\x7e\x18\xc3\x8b\x91\x33\x81\xe6\xce\x00\x3d\x9c\xe0\x1a\x81\x2f\xa7\x38\x70\xdc\x38\x94\x56\x38\x5a\x99\xc3\x55\x90\xeb\x13\x6c\x61\x5a\xd8\x51\xab\xb2\x94\x66\x76\x88\xa4\xfe\xf6\xf8\xf0\xf1\xf3\xa4\xf1\xd9\x80\x71\xf5\xc9\x1e\xa4\x99\xa4\x9f\x26\x21\x70\x9e\xfd\x08\x00\x00\xff\xff\x94\x24\x92\xf9\x57\x06\x00\x00")
|
||||
|
||||
func recover_tpl_bytes() ([]byte, error) {
|
||||
return bindata_read(
|
||||
@@ -196,7 +196,7 @@ func recover_tpl() (*asset, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindata_file_info{name: "recover.tpl", size: 1282, mode: os.FileMode(438), modTime: time.Unix(1424471280, 0)}
|
||||
info := bindata_file_info{name: "recover.tpl", size: 1623, mode: os.FileMode(438), modTime: time.Unix(1424541421, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
@@ -94,6 +94,37 @@ func (t Templates) Render(ctx *authboss.Context, w http.ResponseWriter, r *http.
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenderEmail renders the html and plaintext views for an email and sends it
|
||||
func (t Templates) RenderEmail(email authboss.Email, nameHTML, namePlain string, data interface{}) error {
|
||||
tplHTML, ok := t[nameHTML]
|
||||
if !ok {
|
||||
return authboss.RenderErr{tplHTML.Name(), data, ErrTemplateNotFound}
|
||||
}
|
||||
|
||||
tplPlain, ok := t[namePlain]
|
||||
if !ok {
|
||||
return authboss.RenderErr{tplPlain.Name(), data, ErrTemplateNotFound}
|
||||
}
|
||||
|
||||
htmlBuffer := &bytes.Buffer{}
|
||||
if err := tplHTML.ExecuteTemplate(htmlBuffer, tplHTML.Name(), data); err != nil {
|
||||
return authboss.RenderErr{tplHTML.Name(), data, err}
|
||||
}
|
||||
email.HTMLBody = htmlBuffer.String()
|
||||
|
||||
plainBuffer := &bytes.Buffer{}
|
||||
if err := tplPlain.ExecuteTemplate(plainBuffer, tplPlain.Name(), data); err != nil {
|
||||
return authboss.RenderErr{tplPlain.Name(), data, err}
|
||||
}
|
||||
email.TextBody = plainBuffer.String()
|
||||
|
||||
if err := authboss.Cfg.Mailer.Send(email); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Redirect sets any flash messages given and redirects the user.
|
||||
func Redirect(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, path, flashSuccess, flashError string) {
|
||||
if len(flashSuccess) > 0 {
|
||||
|
@@ -14,6 +14,8 @@ import (
|
||||
)
|
||||
|
||||
var testViewTemplate = template.Must(template.New("").Parse(`{{.external}} {{.fun}} {{.flash_success}} {{.flash_error}} {{.xsrfName}} {{.xsrfToken}}`))
|
||||
var testEmailHTMLTempalte = template.Must(template.New("").Parse(`<h2>{{.}}</h2>`))
|
||||
var testEmailPlainTempalte = template.Must(template.New("").Parse(`i am a {{.}}`))
|
||||
|
||||
func TestLoadTemplates(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -82,6 +84,40 @@ func TestTemplates_Render(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplates_RenderEmail(t *testing.T) {
|
||||
mockMailer := &mocks.MockMailer{}
|
||||
authboss.Cfg.Mailer = mockMailer
|
||||
|
||||
tpls := Templates{
|
||||
"html": testEmailHTMLTempalte,
|
||||
"plain": testEmailPlainTempalte,
|
||||
}
|
||||
|
||||
email := authboss.Email{
|
||||
To: []string{"a@b.c"},
|
||||
}
|
||||
|
||||
err := tpls.RenderEmail(email, "html", "plain", "spoon")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(mockMailer.Last.To) != 1 {
|
||||
t.Error("Expected 1 to addr")
|
||||
}
|
||||
if mockMailer.Last.To[0] != "a@b.c" {
|
||||
t.Error("Unexpected to addr @ 0:", mockMailer.Last.To[0])
|
||||
}
|
||||
|
||||
if mockMailer.Last.HTMLBody != "<h2>spoon</h2>" {
|
||||
t.Error("Unexpected HTMLBody:", mockMailer.Last.HTMLBody)
|
||||
}
|
||||
|
||||
if mockMailer.Last.TextBody != "i am a spoon" {
|
||||
t.Error("Unexpected TextBody:", mockMailer.Last.TextBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirect(t *testing.T) {
|
||||
cookies := mocks.NewMockClientStorer()
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
<form action="/login" method="POST">
|
||||
{{if .error}}{{.error}}<br />{{end}}
|
||||
<input type="text" class="form-control" name="username" placeholder="Username" value="{{.username}}">
|
||||
<input type="password" class="form-control" name="password" placeholder="Password">
|
||||
<input type="text" class="form-control" name="username" placeholder="Username" value="{{.username}}"><br />
|
||||
<input type="password" class="form-control" name="password" placeholder="Password"><br />
|
||||
<input type="hidden" name="{{.xsrfName}}" value="{{.xsrfToken}}" />
|
||||
{{if .showRemember}}<input type="checkbox" name="rm" value="true"> Remember Me{{end}}
|
||||
<br />
|
||||
<button type="submit">Login</button>
|
||||
<button type="submit">Login</button><br />
|
||||
{{if .showRecover}}<a href="/recover">Recover Account</a>{{end}}
|
||||
</form>
|
@@ -1,4 +1,12 @@
|
||||
<form action="/recover" method="POST">
|
||||
<input type="text" name="username" placeholder="Username" value="{{.username}}" /><br />
|
||||
<input type="text" name="confirmUsername" placeholder="Confirm Username" value="{{.confirmUsername}}" /><br />
|
||||
<button type="submit">Recover</button>
|
||||
<a href="/login">Cancel</a>
|
||||
</form>
|
||||
|
||||
|
||||
<!-- <form action="/recover" method="POST">
|
||||
{{$usernameErrs := .ErrMap.username}}
|
||||
<div class="form-group{{if $usernameErrs}} has-error{{end}}">
|
||||
<div class="input-group">
|
||||
@@ -24,4 +32,4 @@
|
||||
<button class="btn btn-primary btn-block" type="submit">Recover</button>
|
||||
<a class="btn btn-link btn-block" type="submit" href="/login">Cancel</a>
|
||||
<input type="hidden" name="{{.XSRFName}}" value="{{.XSRFToken}}" />
|
||||
</form>
|
||||
</form> -->
|
Reference in New Issue
Block a user