template modularisation

This commit is contained in:
Sebastian Mancke
2017-05-09 11:32:42 +02:00
parent 0444d0b571
commit de2e2081b7
3 changed files with 121 additions and 26 deletions
+6 -6
View File
@@ -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,
+41 -20
View File
@@ -9,9 +9,9 @@ import (
"strings"
)
const loginForm = `<!DOCTYPE html>
<html>
<head>
const partials = `
{{define "styles"}}
<link uic-remove rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link uic-remove rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/5.1.1/bootstrap-social.min.css">
<link uic-remove rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
@@ -47,29 +47,20 @@ const loginForm = `<!DOCTYPE html>
border-radius: 3px;
}
</style>
</head>
<body>
<uic-fragment name="content">
<div class="container">
<div class="row vertical-offset-100">
<div class="col-md-4 col-md-offset-4">
{{end}}
{{ if .Error}}
<div class="alert alert-danger" role="alert">
<strong>Internal Error. </strong> Please try again later.
</div>
{{end}}
{{ if .Authenticated}}
{{define "userInfo"}}
{{with .UserInfo}}
<h1>Welcome {{.Sub}}!</h1>
<br/>
{{if .Picture}}<img class="login-picture" src="{{.Picture}}?s=120">{{end}}
{{if .Name}}<h3>{{.Name}}</h3>{{end}}
{{end}}
<br/>
<a class="btn btn-md btn-primary" href="{{ .Config.LoginPath }}?logout=true">Logout</a>
{{else}}
<br/>
<a class="btn btn-md btn-primary" href="{{ .Config.LoginPath }}?logout=true">Logout</a>
{{end}}
{{define "login"}}
{{ range $providerName, $opts := .Config.Oauth }}
<a class="btn btn-block btn-lg btn-social btn-{{ $providerName }}" href="{{ $.Config.LoginPath }}/{{ $providerName }}">
<span class="fa fa-{{ $providerName }}"></span> Sign in with {{ $providerName | ucfirst }}
@@ -106,6 +97,33 @@ const loginForm = `<!DOCTYPE html>
</div>
</div>
{{end}}
{{end}}`
var layout = `<!DOCTYPE html>
<html>
<head>
{{ template "styles" . }}
</head>
<body>
<uic-fragment name="content">
<div class="container">
<div class="row vertical-offset-100">
<div class="col-md-4 col-md-offset-4">
{{ if .Error}}
<div class="alert alert-danger" role="alert">
<strong>Internal Error. </strong> Please try again later.
</div>
{{end}}
{{if .Authenticated}}
{{template "userInfo" . }}
{{else}}
{{template "login" . }}
{{end}}
</div>
</div>
@@ -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 {
+74
View File
@@ -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(), `<form`)
assert.NotContains(t, recorder.Body.String(), `github`)
assert.NotContains(t, recorder.Body.String(), `Welcome`)
assert.Contains(t, recorder.Body.String(), `Error`)
// only form
recorder = httptest.NewRecorder()
writeLoginForm(recorder, loginFormData{
Config: &Config{
LoginPath: "/login",
Backends: Options{"simple": {}},
},
})
assert.Contains(t, recorder.Body.String(), `<form`)
assert.NotContains(t, recorder.Body.String(), `github`)
assert.NotContains(t, recorder.Body.String(), `Welcome`)
assert.NotContains(t, recorder.Body.String(), `Error`)
// only links
recorder = httptest.NewRecorder()
writeLoginForm(recorder, loginFormData{
Config: &Config{
LoginPath: "/login",
Oauth: Options{"github": {}},
},
})
assert.NotContains(t, recorder.Body.String(), `<form`)
assert.Contains(t, recorder.Body.String(), `href="/login/github"`)
assert.NotContains(t, recorder.Body.String(), `Welcome`)
assert.NotContains(t, recorder.Body.String(), `Error`)
// with form and links
recorder = httptest.NewRecorder()
writeLoginForm(recorder, loginFormData{
Config: &Config{
LoginPath: "/login",
Backends: Options{"simple": {}},
Oauth: Options{"github": {}},
},
})
assert.Contains(t, recorder.Body.String(), `<form`)
assert.Contains(t, recorder.Body.String(), `href="/login/github"`)
assert.NotContains(t, recorder.Body.String(), `Welcome`)
assert.NotContains(t, recorder.Body.String(), `Error`)
// show only the user info
recorder = httptest.NewRecorder()
writeLoginForm(recorder, loginFormData{
Authenticated: true,
UserInfo: model.UserInfo{Sub: "smancke", Name: "Sebastian Mancke"},
Config: &Config{
LoginPath: "/login",
Backends: Options{"simple": {}},
Oauth: Options{"github": {}},
},
})
assert.NotContains(t, recorder.Body.String(), `<form`)
assert.NotContains(t, recorder.Body.String(), `href="/login/github"`)
assert.Contains(t, recorder.Body.String(), `Welcome smancke`)
assert.NotContains(t, recorder.Body.String(), `Error`)
}
func Test_ucfirst(t *testing.T) {
assert.Equal(t, "", ucfirst(""))
assert.Equal(t, "A", ucfirst("a"))