mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-02-03 13:21:51 +02:00
127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
package pagewriter
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Writer", func() {
|
|
Context("NewWriter", func() {
|
|
var writer Writer
|
|
var opts Opts
|
|
|
|
BeforeEach(func() {
|
|
opts = Opts{
|
|
TemplatesPath: "",
|
|
ProxyPrefix: "/prefix",
|
|
Footer: "<Footer>",
|
|
Version: "<Version>",
|
|
Debug: false,
|
|
DisplayLoginForm: false,
|
|
ProviderName: "<ProviderName>",
|
|
SignInMessage: "<SignInMessage>",
|
|
}
|
|
})
|
|
|
|
Context("With no custom templates", func() {
|
|
BeforeEach(func() {
|
|
var err error
|
|
writer, err = NewWriter(opts)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("Writes the default error template", func() {
|
|
recorder := httptest.NewRecorder()
|
|
writer.WriteErrorPage(recorder, 500, "/redirect", "Some debug error")
|
|
|
|
body, err := ioutil.ReadAll(recorder.Result().Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(string(body)).To(HavePrefix("\n<!DOCTYPE html>"))
|
|
})
|
|
|
|
It("Writes the default sign in template", func() {
|
|
recorder := httptest.NewRecorder()
|
|
writer.WriteSignInPage(recorder, "/redirect")
|
|
|
|
body, err := ioutil.ReadAll(recorder.Result().Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(string(body)).To(HavePrefix("\n<!DOCTYPE html>"))
|
|
})
|
|
})
|
|
|
|
Context("With custom templates", func() {
|
|
var customDir string
|
|
|
|
BeforeEach(func() {
|
|
var err error
|
|
customDir, err = ioutil.TempDir("", "oauth2-proxy-pagewriter-test")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
templateHTML := `Custom Template`
|
|
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())
|
|
|
|
opts.TemplatesPath = customDir
|
|
|
|
writer, err = NewWriter(opts)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
Expect(os.RemoveAll(customDir)).To(Succeed())
|
|
})
|
|
|
|
It("Writes the custom error template", func() {
|
|
recorder := httptest.NewRecorder()
|
|
writer.WriteErrorPage(recorder, 500, "/redirect", "Some debug error")
|
|
|
|
body, err := ioutil.ReadAll(recorder.Result().Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(string(body)).To(Equal("Custom Template"))
|
|
})
|
|
|
|
It("Writes the custom sign in template", func() {
|
|
recorder := httptest.NewRecorder()
|
|
writer.WriteSignInPage(recorder, "/redirect")
|
|
|
|
body, err := ioutil.ReadAll(recorder.Result().Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(string(body)).To(Equal("Custom Template"))
|
|
})
|
|
})
|
|
|
|
Context("With an invalid custom template", func() {
|
|
var customDir string
|
|
|
|
BeforeEach(func() {
|
|
var err error
|
|
customDir, err = ioutil.TempDir("", "oauth2-proxy-pagewriter-test")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
templateHTML := `{{ Custom Broken Template`
|
|
signInFile := filepath.Join(customDir, signInTemplateName)
|
|
Expect(ioutil.WriteFile(signInFile, []byte(templateHTML), 0600)).To(Succeed())
|
|
|
|
opts.TemplatesPath = customDir
|
|
})
|
|
|
|
AfterEach(func() {
|
|
Expect(os.RemoveAll(customDir)).To(Succeed())
|
|
})
|
|
|
|
It("Should return an error", func() {
|
|
writer, err := NewWriter(opts)
|
|
Expect(err).To(MatchError(ContainSubstring("template: sign_in.html:1: function \"Custom\" not defined")))
|
|
Expect(writer).To(BeNil())
|
|
})
|
|
})
|
|
})
|
|
})
|