git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add basic string functions to templates (#514)

* Add basic string functions to templates Co-authored-by: Oliver <oliver006@users.noreply.github.com> Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk> Co-authored-by: Henry Jenkins <henry@henryjenkins.name>

commitbe9eaaeb48132a34748fa474eb2077a8d270dfdc authorn-i-x <440158+n-i-x@users.noreply.github.com> dateSat 9 May 2020, 16:05 (6 years ago) parent9d62626
3 files changed+57−1
CHANGELOG.md+1 templates.go+6−1 templates_test.go+50
CHANGELOG.md+1
@@ -25,6 +25,7 @@
2525
2626## Changes since v5.1.1
2727
28+- [#514](https://github.com/oauth2-proxy/oauth2-proxy/pull/514) Add basic string functions to templates
2829- [#524](https://github.com/oauth2-proxy/oauth2-proxy/pull/524) Sign cookies with SHA256 (@NickMeves)
2930- [#515](https://github.com/oauth2-proxy/oauth2-proxy/pull/515) Drop configure script in favour of native Makefile env and checks (@JoelSpeed)
3031- [#487](https://github.com/oauth2-proxy/oauth2-proxy/pull/487) Switch flags to PFlag to remove StringArray (@JoelSpeed)
templates.go+6−1
@@ -3,6 +3,7 @@ package main
33import (
44 "html/template"
55 "path"
6+ "strings"
67
78 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
89)
@@ -12,7 +13,11 @@ func loadTemplates(dir string) *template.Template {
1213 return getTemplates()
1314 }
1415 logger.Printf("using custom template directory %q", dir)
15− t, err := template.New("").ParseFiles(path.Join(dir, "sign_in.html"), path.Join(dir, "error.html"))
16+ funcMap := template.FuncMap{
17+ "ToUpper": strings.ToUpper,
18+ "ToLower": strings.ToLower,
19+ }
20+ t, err := template.New("").Funcs(funcMap).ParseFiles(path.Join(dir, "sign_in.html"), path.Join(dir, "error.html"))
1621 if err != nil {
1722 logger.Fatalf("failed parsing template %s", err)
1823 }
templates_test.go+50
@@ -1,11 +1,61 @@
11package main
22
33import (
4+ "bytes"
5+ "io/ioutil"
6+ "log"
7+ "os"
8+ "path/filepath"
49 "testing"
510
611 "github.com/stretchr/testify/assert"
712)
813
14+func TestLoadTemplates(t *testing.T) {
15+ data := struct {
16+ TestString string
17+ }{
18+ TestString: "Testing",
19+ }
20+
21+ templates := loadTemplates("")
22+ assert.NotEqual(t, templates, nil)
23+
24+ var defaultSignin bytes.Buffer
25+ templates.ExecuteTemplate(&defaultSignin, "sign_in.html", data)
26+ assert.Equal(t, "\n<!DOCTYPE html>", defaultSignin.String()[0:16])
27+
28+ var defaultError bytes.Buffer
29+ templates.ExecuteTemplate(&defaultError, "error.html", data)
30+ assert.Equal(t, "\n<!DOCTYPE html>", defaultError.String()[0:16])
31+
32+ dir, err := ioutil.TempDir("", "templatetest")
33+ if err != nil {
34+ log.Fatal(err)
35+ }
36+ defer os.RemoveAll(dir)
37+
38+ templateHTML := `{{.TestString}} {{.TestString | ToLower}} {{.TestString | ToUpper}}`
39+ signInFile := filepath.Join(dir, "sign_in.html")
40+ if err := ioutil.WriteFile(signInFile, []byte(templateHTML), 0666); err != nil {
41+ log.Fatal(err)
42+ }
43+ errorFile := filepath.Join(dir, "error.html")
44+ if err := ioutil.WriteFile(errorFile, []byte(templateHTML), 0666); err != nil {
45+ log.Fatal(err)
46+ }
47+ templates = loadTemplates(dir)
48+ assert.NotEqual(t, templates, nil)
49+
50+ var sitpl bytes.Buffer
51+ templates.ExecuteTemplate(&sitpl, "sign_in.html", data)
52+ assert.Equal(t, "Testing testing TESTING", sitpl.String())
53+
54+ var errtpl bytes.Buffer
55+ templates.ExecuteTemplate(&errtpl, "error.html", data)
56+ assert.Equal(t, "Testing testing TESTING", errtpl.String())
57+}
58+
959func TestTemplatesCompile(t *testing.T) {
1060 templates := getTemplates()
1161 assert.NotEqual(t, templates, nil)