mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-05-21 22:33:38 +02:00
127 lines
3.5 KiB
Go
127 lines
3.5 KiB
Go
|
package app
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net/http/httptest"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
. "github.com/onsi/ginkgo"
|
||
|
. "github.com/onsi/gomega"
|
||
|
)
|
||
|
|
||
|
var _ = Describe("PageWriter", func() {
|
||
|
Context("NewPageWriter", func() {
|
||
|
var writer PageWriter
|
||
|
var opts PageWriterOpts
|
||
|
|
||
|
BeforeEach(func() {
|
||
|
opts = PageWriterOpts{
|
||
|
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 = NewPageWriter(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 = NewPageWriter(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 := NewPageWriter(opts)
|
||
|
Expect(err).To(MatchError(ContainSubstring("template: sign_in.html:1: function \"Custom\" not defined")))
|
||
|
Expect(writer).To(BeNil())
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
})
|