mirror of
https://github.com/volatiletech/authboss.git
synced 2025-09-16 09:06:20 +02:00
Merge internal/views into internal/render
- Remove internal/view - Remove internal/flashutil
This commit is contained in:
15
config.go
15
config.go
@@ -8,7 +8,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gopkg.in/authboss.v0/internal/views"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -68,24 +67,14 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
layout, err := views.AssetToTemplate(layoutTpl)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
layoutEmail, err := views.AssetToTemplate(layoutEmailTpl)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
MountPath: "/",
|
MountPath: "/",
|
||||||
ViewsPath: "/",
|
ViewsPath: "/",
|
||||||
HostName: "localhost:8080",
|
HostName: "localhost:8080",
|
||||||
BCryptCost: bcrypt.DefaultCost,
|
BCryptCost: bcrypt.DefaultCost,
|
||||||
|
|
||||||
Layout: layout,
|
Layout: template.Must(template.New("").Parse(`{{template "authboss" .}}`)),
|
||||||
LayoutEmail: layoutEmail,
|
LayoutEmail: template.Must(template.New("").Parse(`{{template "authboss" .}}`)),
|
||||||
|
|
||||||
AuthLogoutRoute: "/",
|
AuthLogoutRoute: "/",
|
||||||
AuthLoginSuccessRoute: "/",
|
AuthLoginSuccessRoute: "/",
|
||||||
|
@@ -1,11 +0,0 @@
|
|||||||
package flashutil
|
|
||||||
|
|
||||||
import "gopkg.in/authboss.v0"
|
|
||||||
|
|
||||||
// Pull is a convenience func to retreive then delete a flash message. Any ok
|
|
||||||
// checks are ignored as they don't alter the intended use.
|
|
||||||
func Pull(storer authboss.ClientStorer, key string) string {
|
|
||||||
value, _ := storer.Get(key)
|
|
||||||
storer.Del(key)
|
|
||||||
return value
|
|
||||||
}
|
|
@@ -1,26 +0,0 @@
|
|||||||
package flashutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gopkg.in/authboss.v0/internal/mocks"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPull(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
storer := mocks.NewMockClientStorer()
|
|
||||||
storer.Values = map[string]string{
|
|
||||||
"a": "1",
|
|
||||||
}
|
|
||||||
|
|
||||||
v := Pull(storer, "a")
|
|
||||||
|
|
||||||
if v != "1" {
|
|
||||||
t.Error(`Expected value "1", got:`, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(storer.Values) != 0 {
|
|
||||||
t.Error("Expected length of zero")
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,4 +1,4 @@
|
|||||||
package views
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@@ -1,17 +1,66 @@
|
|||||||
|
// Package render is responsible for loading and rendering authboss templates.
|
||||||
package render
|
package render
|
||||||
|
|
||||||
|
//go:generate go-bindata -pkg=render -prefix=templates templates
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"gopkg.in/authboss.v0"
|
"gopkg.in/authboss.v0"
|
||||||
"gopkg.in/authboss.v0/internal/views"
|
"gopkg.in/authboss.v0/internal/views"
|
||||||
)
|
)
|
||||||
|
|
||||||
// View renders a view with xsrf and flash attributes.
|
var (
|
||||||
func View(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, t views.Templates, name string, data authboss.HTMLData) error {
|
// ErrTemplateNotFound should be returned from Get when the view is not found
|
||||||
|
ErrTemplateNotFound = errors.New("Template not found")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Templates is a map depicting the forms a template needs wrapped within the specified layout
|
||||||
|
type Templates map[string]*template.Template
|
||||||
|
|
||||||
|
// LoadTemplates parses all specified files located in path. Each template is wrapped
|
||||||
|
// in a unique clone of layout. All templates are expecting {{authboss}} handlebars
|
||||||
|
// for parsing. It will check the override directory specified in the config, replacing any
|
||||||
|
// templates as necessary.
|
||||||
|
func LoadTemplates(layout *template.Template, path string, files ...string) (Templates, error) {
|
||||||
|
m := make(Templates)
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join(path, file))
|
||||||
|
if exists := !os.IsNotExist(err); err != nil && exists {
|
||||||
|
return nil, err
|
||||||
|
} else if !exists {
|
||||||
|
b, err = Asset(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clone, err := layout.Clone()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = clone.New("authboss").Parse(string(b))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
m[file] = clone
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render renders a view with xsrf and flash attributes.
|
||||||
|
func (t Templates) Render(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, name string, data authboss.HTMLData) error {
|
||||||
tpl, ok := t[name]
|
tpl, ok := t[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return authboss.RenderErr{tpl.Name(), data, views.ErrTemplateNotFound}
|
return authboss.RenderErr{tpl.Name(), data, views.ErrTemplateNotFound}
|
||||||
|
@@ -2,18 +2,52 @@ package render
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gopkg.in/authboss.v0"
|
"gopkg.in/authboss.v0"
|
||||||
"gopkg.in/authboss.v0/internal/mocks"
|
"gopkg.in/authboss.v0/internal/mocks"
|
||||||
"gopkg.in/authboss.v0/internal/views"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var testViewTemplate = template.Must(template.New("").Parse(`{{.external}} {{.fun}} {{.flash_success}} {{.flash_error}} {{.xsrfName}} {{.xsrfToken}}`))
|
var testViewTemplate = template.Must(template.New("").Parse(`{{.external}} {{.fun}} {{.flash_success}} {{.flash_error}} {{.xsrfName}} {{.xsrfToken}}`))
|
||||||
|
|
||||||
func TestView(t *testing.T) {
|
func TestLoadTemplates(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
file, err := ioutil.TempFile(os.TempDir(), "authboss")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unexpected error:", err)
|
||||||
|
}
|
||||||
|
if _, err := file.Write([]byte("{{.Val}}")); err != nil {
|
||||||
|
t.Error("Error writing to temp file", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
layout, err := template.New("").Parse(`<strong>{{template "authboss" .}}</strong>`)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unexpected error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := filepath.Base(file.Name())
|
||||||
|
|
||||||
|
tpls, err := LoadTemplates(layout, filepath.Dir(file.Name()), filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unexpected error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tpls) != 1 {
|
||||||
|
t.Error("Expected 1 template:", len(tpls))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := tpls[filename]; !ok {
|
||||||
|
t.Error("Expected tpl with name:", filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTemplates_Render(t *testing.T) {
|
||||||
cookies := mocks.NewMockClientStorer()
|
cookies := mocks.NewMockClientStorer()
|
||||||
authboss.Cfg = &authboss.Config{
|
authboss.Cfg = &authboss.Config{
|
||||||
LayoutDataMaker: func(_ http.ResponseWriter, _ *http.Request) authboss.HTMLData {
|
LayoutDataMaker: func(_ http.ResponseWriter, _ *http.Request) authboss.HTMLData {
|
||||||
@@ -34,11 +68,11 @@ func TestView(t *testing.T) {
|
|||||||
ctx, _ := authboss.ContextFromRequest(r)
|
ctx, _ := authboss.ContextFromRequest(r)
|
||||||
ctx.CookieStorer = cookies
|
ctx.CookieStorer = cookies
|
||||||
|
|
||||||
tpls := views.Templates{
|
tpls := Templates{
|
||||||
"hello": testViewTemplate,
|
"hello": testViewTemplate,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := View(ctx, w, r, tpls, "hello", authboss.HTMLData{"external": "there"})
|
err := tpls.Render(ctx, w, r, "hello", authboss.HTMLData{"external": "there"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@@ -1,65 +0,0 @@
|
|||||||
// Package views is responsible for loading authboss templates. It will check
|
|
||||||
// the override directory specified in the config, replace any tempaltes where
|
|
||||||
// need be.
|
|
||||||
package views
|
|
||||||
|
|
||||||
//go:generate go-bindata -pkg=views -prefix=templates templates
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"html/template"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrTemplateNotFound should be returned from Get when the view is not found
|
|
||||||
ErrTemplateNotFound = errors.New("Template not found")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Templates is a map depicting the forms a template needs wrapped within the specified layout
|
|
||||||
type Templates map[string]*template.Template
|
|
||||||
|
|
||||||
// Get parses all specified files located in path. Each template is wrapped
|
|
||||||
// in a unique clone of layout. All templates are expecting {{authboss}} handlebars
|
|
||||||
// for parsing.
|
|
||||||
func Get(layout *template.Template, path string, files ...string) (Templates, error) {
|
|
||||||
m := make(Templates)
|
|
||||||
|
|
||||||
for _, file := range files {
|
|
||||||
b, err := ioutil.ReadFile(filepath.Join(path, file))
|
|
||||||
if exists := !os.IsNotExist(err); err != nil && exists {
|
|
||||||
return nil, err
|
|
||||||
} else if !exists {
|
|
||||||
b, err = Asset(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
clone, err := layout.Clone()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = clone.New("authboss").Parse(string(b))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
m[file] = clone
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asset parses a specified file from the internal bindata.
|
|
||||||
func AssetToTemplate(file string) (*template.Template, error) {
|
|
||||||
b, err := Asset(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return template.New("").Parse(string(b))
|
|
||||||
}
|
|
@@ -1,78 +0,0 @@
|
|||||||
package views
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTemplates_ExecuteTemplate_ReturnsTemplateWhenFound(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tpl, _ := template.New("").Parse("<strong>{{.Val}}</strong>")
|
|
||||||
tpls := Templates{"a": tpl}
|
|
||||||
|
|
||||||
//b := &bytes.Buffer{}
|
|
||||||
b, err := tpls.ExecuteTemplate("a", struct{ Val string }{"hi"})
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Unexpected error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := "<strong>hi</strong>"
|
|
||||||
actual := b.String()
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf(`Expected "%s", got %s`, expected, actual)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTemplates_ExecuteTemplate_ReturnsErrTempalteNotFound(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tpls := Templates{}
|
|
||||||
_, err := tpls.ExecuteTemplate("shouldnotbefound", nil)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Expected error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err.Error() != "Template not found" {
|
|
||||||
t.Errorf(`Expected err.Error() to be "Template not found", got: "%s"`, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
file, err := ioutil.TempFile(os.TempDir(), "authboss")
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Unexpected error:", err)
|
|
||||||
}
|
|
||||||
if _, err := file.Write([]byte("{{.Val}}")); err != nil {
|
|
||||||
t.Error("Error writing to temp file", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
layout, err := template.New("").Parse(`<strong>{{template "authboss" .}}</strong>`)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Unexpected error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
filename := filepath.Base(file.Name())
|
|
||||||
|
|
||||||
tpls, err := Get(layout, filepath.Dir(file.Name()), filename)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Unexpected error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := tpls.ExecuteTemplate(filename, struct{ Val string }{"hi"})
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Unexpected error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := "<strong>hi</strong>"
|
|
||||||
actual := b.String()
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf(`Expected "%s", got %s`, expected, actual)
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user