-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtemplates_test.go
203 lines (168 loc) · 5.58 KB
/
templates_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package pagewriter
import (
"bytes"
"html/template"
"io/ioutil"
"os"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Templates", func() {
var customDir string
BeforeEach(func() {
var err error
customDir, err = ioutil.TempDir("", "oauth2-proxy-templates-test")
Expect(err).ToNot(HaveOccurred())
templateHTML := `{{.TestString}} {{.TestString | ToLower}} {{.TestString | ToUpper}}`
signInFile := filepath.Join(customDir, signInTemplateName)
Expect(ioutil.WriteFile(signInFile, []byte(templateHTML), 0600)).To(Succeed())
errorFile := filepath.Join(customDir, errorTemplateName)
Expect(ioutil.WriteFile(errorFile, []byte(templateHTML), 0600)).To(Succeed())
})
AfterEach(func() {
Expect(os.RemoveAll(customDir)).To(Succeed())
})
Context("loadTemplates", func() {
var data interface{}
var t *template.Template
BeforeEach(func() {
data = struct {
// For default templates
ProxyPrefix string
Redirect string
Footer string
// For default sign_in template
SignInMessage string
ProviderName string
CustomLogin bool
LogoData string
// For default error template
StatusCode int
Title string
Message string
RequestID string
// For custom templates
TestString string
}{
ProxyPrefix: "<proxy-prefix>",
Redirect: "<redirect>",
Footer: "<footer>",
SignInMessage: "<sign-in-message>",
ProviderName: "<provider-name>",
CustomLogin: false,
LogoData: "<logo>",
StatusCode: 404,
Title: "<title>",
Message: "<message>",
RequestID: "<request-id>",
TestString: "Testing",
}
})
Context("With no custom directory", func() {
BeforeEach(func() {
var err error
t, err = loadTemplates("")
Expect(err).ToNot(HaveOccurred())
})
It("Use the default sign_in page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, signInTemplateName, data)).To(Succeed())
Expect(buf.String()).To(HavePrefix("\n<!DOCTYPE html>"))
})
It("Use the default error page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, errorTemplateName, data)).To(Succeed())
Expect(buf.String()).To(HavePrefix("\n<!DOCTYPE html>"))
})
})
Context("With a custom directory", func() {
Context("With both templates", func() {
BeforeEach(func() {
var err error
t, err = loadTemplates(customDir)
Expect(err).ToNot(HaveOccurred())
})
It("Use the custom sign_in page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, signInTemplateName, data)).To(Succeed())
Expect(buf.String()).To(Equal("Testing testing TESTING"))
})
It("Use the custom error page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, errorTemplateName, data)).To(Succeed())
Expect(buf.String()).To(Equal("Testing testing TESTING"))
})
})
Context("With no error template", func() {
BeforeEach(func() {
Expect(os.Remove(filepath.Join(customDir, errorTemplateName))).To(Succeed())
var err error
t, err = loadTemplates(customDir)
Expect(err).ToNot(HaveOccurred())
})
It("Use the custom sign_in page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, signInTemplateName, data)).To(Succeed())
Expect(buf.String()).To(Equal("Testing testing TESTING"))
})
It("Use the default error page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, errorTemplateName, data)).To(Succeed())
Expect(buf.String()).To(HavePrefix("\n<!DOCTYPE html>"))
})
})
Context("With no sign_in template", func() {
BeforeEach(func() {
Expect(os.Remove(filepath.Join(customDir, signInTemplateName))).To(Succeed())
var err error
t, err = loadTemplates(customDir)
Expect(err).ToNot(HaveOccurred())
})
It("Use the default sign_in page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, signInTemplateName, data)).To(Succeed())
Expect(buf.String()).To(HavePrefix("\n<!DOCTYPE html>"))
})
It("Use the custom error page", func() {
buf := bytes.NewBuffer([]byte{})
Expect(t.ExecuteTemplate(buf, errorTemplateName, data)).To(Succeed())
Expect(buf.String()).To(Equal("Testing testing TESTING"))
})
})
Context("With an invalid sign_in template", func() {
BeforeEach(func() {
signInFile := filepath.Join(customDir, signInTemplateName)
Expect(ioutil.WriteFile(signInFile, []byte("{{"), 0600))
})
It("Should return an error when loading templates", func() {
t, err := loadTemplates(customDir)
Expect(err).To(MatchError(HavePrefix("could not add Sign In template:")))
Expect(t).To(BeNil())
})
})
Context("With an invalid error template", func() {
BeforeEach(func() {
errorFile := filepath.Join(customDir, errorTemplateName)
Expect(ioutil.WriteFile(errorFile, []byte("{{"), 0600))
})
It("Should return an error when loading templates", func() {
t, err := loadTemplates(customDir)
Expect(err).To(MatchError(HavePrefix("could not add Error template:")))
Expect(t).To(BeNil())
})
})
})
})
Context("isFile", func() {
It("with a valid file", func() {
Expect(isFile(filepath.Join(customDir, signInTemplateName))).To(BeTrue())
})
It("with a directory", func() {
Expect(isFile(customDir)).To(BeFalse())
})
It("with an invalid file", func() {
Expect(isFile(filepath.Join(customDir, "does_not_exist.html"))).To(BeFalse())
})
})
})