From de2e2081b70dc9b16454a34903df975e63a5ceca Mon Sep 17 00:00:00 2001 From: Sebastian Mancke Date: Tue, 9 May 2017 11:32:42 +0200 Subject: [PATCH] template modularisation --- login/handler_test.go | 12 +++---- login/login_form.go | 61 ++++++++++++++++++++++----------- login/login_form_test.go | 74 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 26 deletions(-) diff --git a/login/handler_test.go b/login/handler_test.go index a238a87..39881d9 100644 --- a/login/handler_test.go +++ b/login/handler_test.go @@ -29,10 +29,10 @@ func TestHandler_NewFromConfig(t *testing.T) { { &Config{ Backends: Options{ - "simple": map[string]string{"bob": "secret"}, + "simple": {"bob": "secret"}, }, Oauth: Options{ - "github": map[string]string{"client_id": "xxx", "client_secret": "YYY"}, + "github": {"client_id": "xxx", "client_secret": "YYY"}, }, }, 1, @@ -40,7 +40,7 @@ func TestHandler_NewFromConfig(t *testing.T) { false, }, { - &Config{Backends: Options{"simple": map[string]string{"bob": "secret"}}}, + &Config{Backends: Options{"simple": {"bob": "secret"}}}, 1, 0, false, @@ -48,7 +48,7 @@ func TestHandler_NewFromConfig(t *testing.T) { // error cases { // init error because no users are provided - &Config{Backends: Options{"simple": map[string]string{}}}, + &Config{Backends: Options{"simple": {}}}, 1, 0, true, @@ -56,7 +56,7 @@ func TestHandler_NewFromConfig(t *testing.T) { { &Config{ Oauth: Options{ - "FOOO": map[string]string{"client_id": "xxx", "client_secret": "YYY"}, + "FOOO": {"client_id": "xxx", "client_secret": "YYY"}, }, }, 0, @@ -70,7 +70,7 @@ func TestHandler_NewFromConfig(t *testing.T) { true, }, { - &Config{Backends: Options{"simpleFoo": map[string]string{"bob": "secret"}}}, + &Config{Backends: Options{"simpleFoo": {"bob": "secret"}}}, 1, 0, true, diff --git a/login/login_form.go b/login/login_form.go index b0c9849..5b1ce98 100644 --- a/login/login_form.go +++ b/login/login_form.go @@ -9,9 +9,9 @@ import ( "strings" ) -const loginForm = ` - - +const partials = ` + +{{define "styles"}} @@ -47,29 +47,20 @@ const loginForm = ` border-radius: 3px; } - - - -
-
-
+{{end}} - {{ if .Error}} - - {{end}} - - {{ if .Authenticated}} +{{define "userInfo"}} {{with .UserInfo}}

Welcome {{.Sub}}!


{{if .Picture}}{{end}} {{if .Name}}

{{.Name}}

{{end}} {{end}} -
- Logout - {{else}} +
+ Logout +{{end}} + +{{define "login"}} {{ range $providerName, $opts := .Config.Oauth }} Sign in with {{ $providerName | ucfirst }} @@ -106,6 +97,33 @@ const loginForm = `
{{end}} +{{end}}` + +var layout = ` + + + {{ template "styles" . }} + + + +
+
+
+ + {{ if .Error}} + + {{end}} + + {{if .Authenticated}} + + {{template "userInfo" . }} + + {{else}} + + {{template "login" . }} + {{end}}
@@ -126,7 +144,10 @@ func writeLoginForm(w http.ResponseWriter, params loginFormData) { funcMap := template.FuncMap{ "ucfirst": ucfirst, } - t := template.Must(template.New("loginForm").Funcs(funcMap).Parse(loginForm)) + + t := template.New("loginForm").Funcs(funcMap) + t = template.Must(t.Parse(partials)) + t = template.Must(t.Parse(layout)) b := bytes.NewBuffer(nil) err := t.Execute(b, params) if err != nil { diff --git a/login/login_form_test.go b/login/login_form_test.go index a0c9875..fad1025 100644 --- a/login/login_form_test.go +++ b/login/login_form_test.go @@ -2,9 +2,83 @@ package login import ( "github.com/stretchr/testify/assert" + "github.com/tarent/loginsrv/model" + "net/http/httptest" "testing" ) +func Test_form(t *testing.T) { + // show error + recorder := httptest.NewRecorder() + writeLoginForm(recorder, loginFormData{ + Error: true, + Config: &Config{ + LoginPath: "/login", + Backends: Options{"simple": {}}, + }, + }) + assert.Contains(t, recorder.Body.String(), `